summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/functional/functional.tex
blob: b3eb028adc1d0cf175e20529173ca34079779308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
% -*- coding: utf-8 -*-
% !TEX program = lualatex
\documentclass[oneside]{book}
\usepackage[a4paper,margin=2.5cm]{geometry}

\newcommand*{\myversion}{2022B}
\newcommand*{\mydate}{Version \myversion\ (\the\year-\mylpad\month-\mylpad\day)}
\newcommand*{\mylpad}[1]{\ifnum#1<10 0\the#1\else\the#1\fi}

\setlength{\parindent}{0pt}
\setlength{\parskip}{4pt plus 1pt minus 1pt}

\usepackage{codehigh}

\colorlet{highback}{blue9}
%\CodeHigh{lite}
\CodeHigh{language=latex/latex2,style/main=highback,style/code=highback}
\NewCodeHighEnv{code}{style/main=gray9,style/code=gray9}
\NewCodeHighEnv{demo}{style/main=gray9,style/code=gray9,demo}

\usepackage{enumitem}

\NewDocumentCommand\MySubScript{m}{$_{#1}$}

\ExplSyntaxOn
\NewDocumentCommand\PrintVarList{m}{
  \clist_set:Nn \l_tmpa_clist {#1}
  \clist_map_inline:Nn \l_tmpa_clist
    {
      \token_to_str:N ##1 ~
    }
}
\NewDocumentCommand\RelaceChacters{m}{
  \tl_set:Nn \lTmpaTl {#1}
  \regex_replace_once:nnN { \_ } { \c{MySubScript} } \lTmpaTl
}
\ExplSyntaxOff

\NewDocumentEnvironment{variable}{om}{
  \vspace{5pt}
  \begin{minipage}{\linewidth}
  \hrule\vspace{4pt}\obeylines%
  \begingroup
  \ttfamily\bfseries\color{azure3}
  \PrintVarList{#2}
  \endgroup
  \par\vspace{4pt}\hrule
  \end{minipage}\par\nopagebreak\vspace{4pt}
}{%
  \vspace{5pt}%
}

\NewDocumentEnvironment{function}{om}{
  \vspace{5pt}%
}{\vspace{5pt}}

\NewDocumentEnvironment{syntax}{}{%
  \begin{minipage}{\linewidth}
  \hrule\vspace{4pt}\obeylines%
}{%
  \par\vspace{4pt}\hrule
  \end{minipage}\par\nopagebreak\vspace{4pt}
}

\NewDocumentEnvironment{texnote}{}{}{}

\NewDocumentCommand\cs{m}{%
  \texttt{\bfseries\color{purple3}\expandafter\string\csname#1\endcsname}%
}
\NewDocumentCommand\meta{m}{%
  \RelaceChacters{#1}%
  \textsl{$\langle$\ignorespaces\lTmpaTl\unskip$\rangle$}%
}
\NewDocumentCommand\Arg{m}{%
  \RelaceChacters{#1}%
  \texttt{\{}\textsl{$\langle$\ignorespaces\lTmpaTl\unskip$\rangle$}\texttt{\}}%
}

\NewDocumentCommand\nan{}{\texttt{NaN}}
\NewDocumentCommand\enquote{m}{``#1''}

\let\tn=\cs

\RenewDocumentCommand\emph{m}{%
  \underline{\textsl{#1}}%
}

\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  urlcolor=blue3,
  linkcolor=blue3,
}

\usepackage{functional}
%\Functional{scoping=false,tracing=true}

\begin{document}

\title{\sffamily LaTeX2 \textcolor{green3}{Functional} Interfaces for LaTeX3 Programming Layer}
\author{Jianrui Lyu (tolvjr@163.com)\\\url{https://github.com/lvjr/functional}}
\date{\mydate\vspace{1cm}\\\myabstract\vspace{10cm}}

\newcommand\myabstract{\parbox{\linewidth}{\hrule\vspace{0.8em}\large
LaTeX3 programming layer (\textsf{expl3}) is very powerful for advanced users,
but it is a little complicated for normal users.
This \textcolor{green3}{\sffamily functional} package aims to provide
intuitive LaTeX2 functional interfaces for it.
\par\vspace{0.5em}
Although there are functions in LaTeX3, the evaluation of them is from outside to inside.
With this package, the evaluation of functions is from inside to outside,
which is the same as other programming languages such as \texttt{JavaScript} or \texttt{Lua}.
In this way, it is rather easy to debug code too.
\par\vspace{0.5em}
Note that many paragraphs in this manual are copied from the documentation of \textsf{expl3}.
\par\vspace{0.8em}\hrule}}

{\let\newpage\relax\vspace{-4cm}\maketitle}

\tableofcontents

\chapter{Overview of Features}

\section{Evaluation from Inside to Outside}

We will compare our first example with a similar \verb!Lua! example:

\begin{minipage}{0.55\textwidth}
\begin{codehigh}
%% function code
\IgnoreSpacesOn
\PrgNewFunction \MathSquare { m } {
  \IntSet \lTmpaInt { \IntEval {#1 * #1} }
  \Result { \Value \lTmpaInt }
}
\IgnoreSpacesOff
\MathSquare{5}
\MathSquare{\MathSquare{5}}
\end{codehigh}
\end{minipage}%
\begin{minipage}{0.45\textwidth}
\begin{code}
-- define a function --
function MathSquare (arg)
  local lTmpaInt = arg * arg
  return lTmpaInt
end
-- use the function --
print(MathSquare(5))
print(MathSquare(MathSquare(5)))
\end{code}
\end{minipage}

%\IgnoreSpacesOn
%\PrgNewFunction \MathSquare { m }{
%  \IntSet \lTmpaInt { \IntEval { #1 * #1 } }
%  \Result { \Value \lTmpaInt }
%}
%\IgnoreSpacesOff
%\MathSquare{5}
%\MathSquare{\MathSquare{5}}

%\IgnoreSpacesOn
%\PrgNewFunction \MathCubic { m }
%  {
%    \IntSet \lTmpaInt { \IntEval { #1 * #1 * #1 } }
%    \Result { \Value \lTmpaInt }
%  }
%\IgnoreSpacesOff
%\MathCubic{2}
%\MathCubic{\MathCubic{2}}

Both examples calculate first the square of $5$ and produce $25$,
then calculate the square of $25$ and produce $625$.
In contrast to \verb!expl3!, this \verb!functional! package
does evaluation of functions from inside to outside,
which means composition of functions works like othe programming languages
such as \verb!Lua! or \verb!JavsScript!.

You can define new functions with \cs{PrgNewFunction} command.
To make composition of functions work as expected,
every function \emph{must not} insert directly any token to the input stream.
Instead, a function \emph{must} pass the result (if any) to \verb!functional! package
with \cs{Result} command. And \verb!functional! package is responsible for
inserting result tokens to the input stream at the appropriate time.

To remove space tokens inside function code in defining functions,
you'd better put function definitions inside \verb!\IgnoreSpacesOn! and
\verb!\IgnoreSpacesOff! block. Within this block, \verb!~! is used to input a space.

At the end of this section,
we will compare our factorial example with a similar \verb!Lua! example:

\begin{minipage}{0.69\textwidth}
\begin{codehigh}
\IgnoreSpacesOn
\PrgNewFunction \Fact { m } {
  \IntCompareTF {#1} = {0} {
    \Result {1}
  }{
    \Result {\IntMathMult{#1}{\Fact{\IntMathSub{#1}{1}}}}
  }
}
\IgnoreSpacesOff
\Fact{4}
\end{codehigh}
\end{minipage}%
\begin{minipage}{0.31\textwidth}
\begin{code}
-- define a function --
function Fact (n)
  if n == 0 then
    return 1
  else
    return n * Fact(n-1)
  end
end
-- use the function --
print(Fact(4))
\end{code}
\end{minipage}

%\IgnoreSpacesOn
%\PrgNewFunction \Factorial { m } {
%  \IntCompareTF {#1} = {0} {
%    \Result {1}
%  }{
%    \Result { \IntMathMult {#1} { \Factorial { \IntMathSub{#1}{1} } } }
%  }
%}
%\IgnoreSpacesOff
%\Factorial{0}
%\Factorial{4}

\section{Group Scoping of Functions}

In \verb!Lua! language, a function or a condition expression makes a block,
and the values of local variables will be reset after a block.
In \verb!functional! package, a condition expression is in fact a function,
and you can make every function become a group by setting
\verb!\Functional{scoping=true}!. For example

\begin{minipage}{0.58\textwidth}
\begin{codehigh}
\Functional{scoping=true}
\IgnoreSpacesOn
\IntSet \lTmpaInt {1}
\IntLogVar \lTmpaInt            % ---- 1
\PrgNewFunction \SomeFun { } {
  \IntSet \lTmpaInt {2}
  \IntLogVar \lTmpaInt          % ---- 2
  \IntCompareTF {1} > {0} {
    \IntSet \lTmpaInt {3}
    \IntLogVar \lTmpaInt        % ---- 3
  }{ }
  \IntLogVar \lTmpaInt          % ---- 2
}
\SomeFun
\IntLogVar \lTmpaInt            % ---- 1
\IgnoreSpacesOff
\end{codehigh}
\end{minipage}%
\begin{minipage}{0.42\textwidth}
\begin{code}
-- lua code --
-- begin example --
local a = 1
print(a)            ---- 1
function SomeFun()
  local a = 2
  print(a)          ---- 2
  if 1 > 0 then
    local a = 3
    print(a)        ---- 3
  end
  print(a)          ---- 2
end
SomeFun()
print(a)            ---- 1
-- end example --
\end{code}
\end{minipage}

Same as \verb!expl3!, the names of local variables \emph{must} start with \verb!l!,
while names of global variables \emph{must} start with \verb!g!.
The difference is that \verb!functional! package provides only one function for setting
both local and global varianbles of the same type,
by checking leading letters of their names. So for integer variables, you can write
\verb!\IntSet\lTmpaInt{1}! and \verb!\IntSet\gTmpbInt{2}!.

The previous example will produce different result
if we change variable from \verb!\lTmpaInt! to \verb!\gTmpaInt!.

\begin{minipage}{0.58\textwidth}
\begin{codehigh}
\Functional{scoping=true}
\IgnoreSpacesOn
\IntSet \gTmpaInt {1}
\IntLogVar \gTmpaInt            % ---- 1
\PrgNewFunction \SomeFun { } {
  \IntSet \gTmpaInt {2}
  \IntLogVar \gTmpaInt          % ---- 2
  \IntCompareTF {1} > {0} {
    \IntSet \gTmpaInt {3}
    \IntLogVar \gTmpaInt        % ---- 3
  }{ }
  \IntLogVar \gTmpaInt          % ---- 3
}
\SomeFun
\IntLogVar \gTmpaInt            % ---- 3
\IgnoreSpacesOff
\end{codehigh}
\end{minipage}%
\begin{minipage}{0.42\textwidth}
\begin{code}
-- lua code --
-- begin example --
a = 1
print(a)            ---- 1
function SomeFun()
  a = 2
  print(a)          ---- 2
  if 1 > 0 then
    a = 3
    print(a)        ---- 3
  end
  print(a)          ---- 3
end
SomeFun()
print(a)            ---- 3
-- end example --
\end{code}
\end{minipage}

As you can see, the values of global variables will never be reset after a group.

\section{Tracing Evaluation of Functions}

Since every function in \verb!functional! package will pass its return value to
the package, it is quite easy to debug your code.
You can turn on the tracing by setting \verb!\Functional{tracing=true}!.
For example, the tracing log of the first example in this chapter will be the following:

% FIXME: spaces at the first line will be removed
%\begin{codehigh}[]
%    [I] \MathSquare{5}
%            [I] \IntEval{5*5}
%                    [I] \Expand{\int_eval:n {5*5}}
%                    [O] 25
%                [I] \Result{25}
%                [O] 25
%            [O] 25
%        [I] \IntSet\lTmpaInt {25}
%        [O]
%            [I] \Value\lTmpaInt
%            [O] 25
%        [I] \Result{25}
%        [O] 25
%    [O] 25
%\end{codehigh}
\begin{codehigh}[]
[I] \MathSquare{5}
        [I] \IntEval{5*5}
                [I] \Expand{\int_eval:n {5*5}}
                [O] 25
            [I] \Result{25}
            [O] 25
        [O] 25
    [I] \IntSet{\lTmpaInt }{25}
    [O] 
        [I] \Value{\lTmpaInt }
        [O] 25
    [I] \Result{25}
    [O] 25
[O] 25
[I] \MathSquare{25}
        [I] \IntEval{25*25}
                [I] \Expand{\int_eval:n {25*25}}
                [O] 625
            [I] \Result{625}
            [O] 625
        [O] 625
    [I] \IntSet{\lTmpaInt }{625}
    [O] 
        [I] \Value{\lTmpaInt }
        [O] 625
    [I] \Result{625}
    [O] 625
[O] 625
\end{codehigh}

\section{Definitions of Functions}

Within \verb!expl3!, there are eight commands for defining new functions,
which is good for power users.

\begin{minipage}{0.5\textwidth}
\begin{code}[language=latex/latex3]
\cs_new:Npn
\cs_new_nopar:Npn
\cs_new_protected:Npn
\cs_new_protected_nopar:Npn
\end{code}
\end{minipage}%
\begin{minipage}{0.5\textwidth}
\begin{code}[language=latex/latex3]
\cs_new:Nn
\cs_new_nopar:Nn
\cs_new_protected:Nn
\cs_new_protected_nopar:Nn
\end{code}
\end{minipage}

Within \verb!functional! package, there is only one command (\cs{PrgNewFunction})
for defining new functions, which is good for normal users.
The created functions are always protected and accept \verb!\par! in their arguments.

Since \verb!functional! package gets the results of functions by evaluation
(including expansion and execution by \TeX), it is natural to protect all functions.

\section{Variants of Arguments}

Within \verb!expl3!, there are several expansion variants for arguments,
and many expansion functions for expanding them, which are necessary for power users.

\begin{minipage}{0.5\textwidth}
\begin{code}[language=latex/latex3]
\module_foo:c
\module_bar:e
\module_bar:x
\module_bar:f
\module_bar:o
\module_bar:V
\module_bar:v
\end{code}
\end{minipage}%
\begin{minipage}{0.5\textwidth}
\begin{code}[language=latex/latex3]
\exp_args:Nc
\exp_args:Ne
\exp_args:Nx
\exp_args:Nf
\exp_args:No
\exp_args:NV
\exp_args:Nv
\end{code}
\end{minipage}

Within \verb!functional! package, there are only three variants
(\verb!c!, \verb!e!, \verb!V!) are provided, and these variants are defined
as functions (\cs{Name}, \cs{Expand}, \cs{Value}, respetively),
which are easier to use for normal users.

\begin{demohigh}
\newcommand\test{uvw}
\Name{test}
\end{demohigh}

\begin{demohigh}
\newcommand\test{uvw}
\Expand{111\test222}
\end{demohigh}

\begin{demohigh}
\IntSet\lTmpaInt{123}
\Value\lTmpaInt
\end{demohigh}

The most interesting feature is that you can compose these functions.
For example, you can easily get the \verb!v! variant of \verb!expl3! by
simply composing \cs{Name} and \cs{Value} functions:

\begin{demohigh}
\IntSet\lTmpaInt{123}
\Value{\Name{lTmpaInt}}
\end{demohigh}

\chapter{Functional Progarmming (\texttt{Prg})}

\section{Defining Functions and Conditionals}

\begin{function}{\PrgNewFunction}
\begin{syntax}
\cs{PrgNewFunction} \meta{function} \Arg{argument specification} \Arg{code}
\end{syntax}
Creates protected \meta{function} for evaluating the \meta{code}.
Within the \meta{code}, the parameters (\verb|#1|, \verb|#2|,
\emph{etc.}) will be replaced by those absorbed by the function.
The returned value \emph{must} be passed with \cs{Result} function.
The definition is global and an error results if the
\meta{function} is already defined.\par
The \Arg{argument specification} in a list of letters,
where each letter is one of the following argument specifiers
(nearly all of them are \texttt{M} or \texttt{m} for functions provided by this package):\par
{\centering\begin{tabular}{ll}
%\hline
  \texttt{M} & single-token argument, which will be manipulated first \\
  \texttt{m} & multi-token argument, which will be manipulated first \\
  \texttt{N} & single-token argument, which will not be manipulated first \\
  \texttt{n} & multi-token argument, which will not be manipulated first \\
%\hline
\end{tabular}\par}
The argument manipulation for argument type \texttt{M} or \texttt{m}
is: if the argument starts with a function defined with \cs{PrgNewFunction},
the argument will be evaluated and replaced with the returned value.
\end{function}

\begin{function}{\PrgNewConditional}
\begin{syntax}
\cs{PrgNewConditional} \meta{function} \Arg{argument specification} \Arg{code}
\end{syntax}
Creates protected conditional \meta{function} for evaluating the \meta{code}.
The returned value of the \meta{function} \emph{must} be either \verb!\cTrueBool!
or \verb!\cFalseBool! and be passed with \cs{Result} function..
The definition is global and an error results if the \meta{function} is already defined.
\par
Assume the \meta{function} is \verb!\FooIfBar!, then another function \verb!\FooIfBarTF!
will be created at the same time. \verb!\FooIfBarTF! function has two extra arguments
which are \Arg{true code} and \Arg{false code}.\par
\end{function}

\section{Collecting Returned Values}

\begin{function}{\Result}
\begin{syntax}
\cs{Result} \Arg{tokens}
\end{syntax}
Appends \meta{tokens} to \verb!\gResultTl!, which holds the returned value of current function.
This function is normally used in the \meta{code} of \cs{PrgNewFunction} and \cs{PrgNewConditional}.
\end{function}

\chapter{Argument Using (\texttt{Use})}

\section{Expanding Tokens}

\begin{function}{\Name}
\begin{syntax}
\cs{Name} \Arg{control sequence name}
\end{syntax}
Expands the \meta{control sequence name} until only characters
remain, then converts this into a control sequence and returns it.
The \meta{control sequence name} must consist of character tokens %,
%typically a mixture of category code $10$ (space), $11$ (letter) and $12$ (other).
when exhaustively expanded.%
%\begin{texnote}
%Protected macros that appear in a \texttt{c}-type argument are
%expanded despite being protected; \cs{exp_not:n} also has no
%effect.  An internal error occurs if non-characters or active
%characters remain after full expansion, as the conversion to a
%control sequence is not possible.
%\end{texnote}
\end{function}

\begin{function}{\Value}
\begin{syntax}
\cs{Value} \meta{variable}
\end{syntax}
Recovers the content of a \meta{variable} and returns the value.
An error is raised if the variable does not exist or if it is invalid.
Note that it is the same as \cs{TlUse} for \meta{tl var}, or \cs{IntUse} for \meta{int var}.
\end{function}

\begin{function}{\Expand}
\begin{syntax}
\cs{Expand} \Arg{tokens}
\end{syntax}
Expands the \meta{tokens} exhaustively and returns the result.
\end{function}

\begin{function}{\ExpNot}
\begin{syntax}
\cs{ExpNot} \Arg{tokens}
\end{syntax}
Prevents expansion of the \meta{tokens} inside the argument of \cs{Expand} function.
The argument of \cs{ExpNot} \emph{must} be surrounded by braces.
%\begin{texnote}
%This is the \eTeX{} \tn{unexpanded} primitive.  In an
%|x|-expanding definition (\cs{cs_new:Npx}), \cs{exp_not:n}~|{#1}|
%is equivalent to |##1| rather than to~|#1|, namely it inserts the
%two characters |#| and~|1|.  In an |e|-type argument
%\cs{exp_not:n}~|{#}| is equivalent to |#|, namely it inserts the
%character~|#|.
%\end{texnote}
\end{function}

\begin{function}{\ExpValue}
\begin{syntax}
\cs{ExpValue} \meta{variable}
\end{syntax}
Recovers the content of the \meta{variable}, then prevents expansion
of this material inside the argument of \cs{Expand} function.
\end{function}

\section{Using Tokens}

\begin{function}{\UseOne,\GobbleOne}
\begin{syntax}
\cs{UseOne} \Arg{argument}
\cs{GobbleOne} \Arg{argument}
\end{syntax}
The function \cs{UseOne} absorbs one argument and returns it.
%\begin{texnote}
%The \cs{UseOne} function is equivalent to \LaTeXe{}'s \tn{@firstofone}.
%\end{texnote}
\cs{GobbleOne} absorbs one argument and returns nothing.
%\begin{texnote}
%These are equivalent to \LaTeXe{}'s \tn{@gobble}, \tn{@gobbbletwo},
%\emph{etc.}
%\end{texnote}
For example
\begin{demohigh}
\UseOne{abc}\GobbleOne{ijk}\UseOne{xyz}
\end{demohigh}
\end{function}

\begin{function}{\UseGobble,\GobbleUse}
\begin{syntax}
\cs{UseGobble} \Arg{arg_1} \Arg{arg_2}
\cs{GobbleUse} \Arg{arg_1} \Arg{arg_2}
\end{syntax}
These functions absorb two arguments.
The function \cs{UseGobble} discards the second argument,
and returns the content of the first argument.
\cs{GobbleUse} discards the first argument,
and returns the content of the second argument.
%\begin{texnote}
%These are equivalent to \LaTeXe{}'s \tn{@firstoftwo} and
%\tn{@secondoftwo}.
%\end{texnote}
For example
\begin{demohigh}
\UseGobble{abc}{uvw}\GobbleUse{abc}{uvw}
\end{demohigh}
\end{function}

\chapter{Control Structures (\texttt{Bool})}

\section{Constant and Scratch Booleans}

\begin{variable}{\cTrueBool,\cFalseBool}
Constants that represent \verb|true| and \verb|false|, respectively. Used to
implement predicates. For example
\begin{demohigh}
\BoolVarIfTF \cTrueBool {\Result{True!}} {\Result{False!}}
\BoolVarIfTF \cFalseBool {\Result{True!}} {\Result{False!}}
\end{demohigh}
\end{variable}

\begin{variable}{\lTmpaBool,\lTmpbBool,\lTmpcBool,\lTmpiBool,\lTmpjBool,\lTmpkBool}
Scratch booleans for local assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\begin{variable}{\gTmpaBool,\gTmpbBool,\gTmpcBool,\gTmpiBool,\gTmpjBool,\gTmpkBool}
Scratch booleans for global assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\section{Creating and Setting Booleans}

\begin{function}{\BoolNew}
\begin{syntax}
\cs{BoolNew} \meta{boolean}
\end{syntax}
Creates a new \meta{boolean} or raises an error if the
name is already taken. The declaration is global. The
\meta{boolean} is initially \texttt{false}.
\end{function}

\begin{function}{\BoolConst}
\begin{syntax}
\cs{BoolConst} \meta{boolean} \Arg{boolexpr}
\end{syntax}
Creates a new constant \meta{boolean} or raises an error if the name
is already taken. The value of the \meta{boolean} is set globally to
the result of evaluating the \meta{boolexpr}.
For example
\begin{codehigh}
\BoolConst \cFooSomeBool {\IntCompare{3}>{2}}
\BoolVarLog \cFooSomeBool
\end{codehigh}
\end{function}

\begin{function}{\BoolSet}
\begin{syntax}
\cs{BoolSet} \meta{boolean} \Arg{boolexpr}
\end{syntax}
Evaluates the \meta{boolean expression} and sets the \meta{boolean} variable to
the logical truth of this evaluation.
For example
\begin{codehigh}
\BoolSet \lTmpaBool {\IntCompare{3}<{2}}
\BoolVarLog \lTmpaBool
\end{codehigh}
\end{function}

\begin{function}{\BoolSetTrue}
\begin{syntax}
\cs{BoolSetTrue} \meta{boolean}
\end{syntax}
Sets \meta{boolean} logically \texttt{true}.
\end{function}

\begin{function}{\BoolSetFalse}
\begin{syntax}
\cs{BoolSetFalse} \meta{boolean}
\end{syntax}
Sets \meta{boolean} logically \texttt{false}.
\end{function}

\begin{function}{\BoolSetEq}
\begin{syntax}
\cs{BoolSetEq} \meta{boolean_1} \meta{boolean_2}
\end{syntax}
Sets \meta{boolean_1} to the current value of \meta{boolean_2}.
For example
\begin{codehigh}
\BoolSetTrue \lTmpaBool
\BoolSetEq \lTmpbBool \lTmpaBool
\BoolVarLog \lTmpbBool
\end{codehigh}
\end{function}

\section{Viewing Booleans}

\begin{function}{\BoolLog}
\begin{syntax}
\cs{BoolLog} \Arg{boolean expression}
\end{syntax}
Writes the logical truth of the \meta{boolean expression} in the log file.
\end{function}

\begin{function}{\BoolVarLog}
\begin{syntax}
\cs{BoolVarLog} \meta{boolean}
\end{syntax}
Writes the logical truth of the \meta{boolean} in the log file.
\end{function}

\begin{function}{\BoolShow}
\begin{syntax}
\cs{BoolShow} \Arg{boolean expression}
\end{syntax}
Displays the logical truth of the \meta{boolean expression} on the terminal.
\end{function}

\begin{function}{\BoolVarShow}
\begin{syntax}
\cs{BoolVarShow} \meta{boolean}
\end{syntax}
Displays the logical truth of the \meta{boolean} on the terminal.
\end{function}

\section{Booleans and Conditionals}

\begin{function}{\BoolIfExist,\BoolIfExistTF}
\begin{syntax}
\cs{BoolIfExist} \meta{boolean}
\cs{BoolIfExistTF} \meta{boolean} \Arg{true code} \Arg{false code}
\end{syntax}
Tests whether the \meta{boolean} is currently defined.  This does not
check that the \meta{boolean} really is a boolean variable.
For example
\begin{demohigh}
\BoolIfExistTF \lTmpaBool {\Result{Yes}} {\Result{No}}
\BoolIfExistTF \lFooUndefinedBool {\Result{Yes}} {\Result{No}}
\end{demohigh}
\end{function}

\begin{function}{\BoolVarIf,\BoolVarIfTF}
\begin{syntax}
\cs{BoolVarIf} \meta{boolean}
\cs{BoolVarIfTF} \meta{boolean} \Arg{true code} \Arg{false code}
\end{syntax}
Tests the current truth of \meta{boolean}, and continues evaluation
based on this result. For example
\begin{demohigh}
\BoolSetTrue \lTmpaBool
\BoolVarIfTF \lTmpaBool {\Result{True!}} {\Result{False!}}
\BoolSetFalse \lTmpaBool
\BoolVarIfTF \lTmpaBool {\Result{True!}} {\Result{False!}}
\end{demohigh}
\end{function}

\begin{function}{\BoolVarNot,\BoolVarNotTF}
\begin{syntax}
\cs{BoolVarNot} \meta{boolean}
\cs{BoolVarNotTF} \meta{boolean} \Arg{true code} \Arg{false code}
\end{syntax}
Evaluates \meta{true code} if \meta{boolean} is \verb!false!,
and \meta{false code} If \meta{boolean} is \verb!true!.
For example
\begin{demohigh}
\BoolVarNotTF {\IntCompare{3}>{2}} {\Result{Yes}} {\Result{No}}
\end{demohigh}
\end{function}

\begin{function}{\BoolVarAnd,\BoolVarAndTF}
\begin{syntax}
\cs{BoolVarAnd} \meta{boolean_1} \meta{boolean_2}
\cs{BoolVarAndTF} \meta{boolean_1} \meta{boolean_2} \Arg{true code} \Arg{false code}
\end{syntax}
Implements the \enquote{And} operation between two booleans,
hence is \texttt{true} if both are \texttt{true}.
%Contrarily to the infix operator \verb|&&|,
The \meta{boolean_2} is only evaluated if it is needed to determine the result of
\cs{BoolVarAnd}.
For example
\begin{demohigh}
\BoolVarAndTF {\IntCompare{3}>{2}} {\IntCompare{3}>{4}} {\Result{Yes}} {\Result{No}}
\end{demohigh}
\end{function}

\begin{function}{\BoolVarOr,\BoolVarOrTF}
\begin{syntax}
\cs{BoolVarOr} \meta{boolean_1} \meta{boolean_2}
\cs{BoolVarOrTF} \meta{boolean_1} \meta{boolean_2} \Arg{true code} \Arg{false code}
\end{syntax}
Implements the \enquote{Or} operation between two booleans,
hence is \texttt{true} if either one is \texttt{true}.
%Contrarily to the infix operator \verb"||",
The \meta{boolean_2} is only evaluated if it is needed to determine the result of
\cs{BoolVarOr}.
For example
\begin{demohigh}
\BoolVarOrTF {\IntCompare{3}>{2}} {\IntCompare{3}>{4}} {\Result{Yes}} {\Result{No}}
\end{demohigh}
\end{function}

\begin{function}{\BoolVarXor,\BoolVarXorTF}
\begin{syntax}
\cs{BoolVarXor} \meta{boolean_1} \meta{boolean_2}
\cs{BoolVarXorTF} \meta{boolean_1} \meta{boolean_2} \Arg{true code} \Arg{false code}
\end{syntax}
Implements an \enquote{exclusive or} operation between two booleans.
For example
\begin{demohigh}
\BoolVarXorTF {\IntCompare{3}>{2}} {\IntCompare{3}>{4}} {\Result{Yes}} {\Result{No}}
\end{demohigh}
\end{function}

\chapter{Token Lists (\texttt{Tl})}

\section{Constant and Scratch Token Lists}

\begin{variable}{\cSpaceTl}
An explicit space character contained in a token list%
%(compare this with \cs{c_space_token})
. For use where an explicit space is required.
\end{variable}

\begin{variable}{\cEmptyTl}
Constant that is always empty.
\end{variable}

%\begin{variable}{\cNoValueTl}
%A marker for the absence of an argument. This constant \verb|tl| can safely
%be typeset (\emph{cf.}~\cs{qNil}), with the result being \verb|-NoValue-|.
%It is important to note that \cs{cNoValueTl} is constructed such that it
%will \emph{not} match the simple text input \verb|-NoValue-|, \emph{i.e.}
%that
%\begin{demohigh}
%\TlIfEqTF {\TlUse\cNoValueTl} {-NoValue-} {Result{Yes}} {\Result{No}}
%\end{demohigh}
%The \cs{cNoValueTl} marker is intended for
%use in creating document-level interfaces, where it serves as an indicator
%that an (optional) argument was omitted. In particular, it is distinct
%from a simple empty \verb|tl|.
%\end{variable}

\begin{variable}{\lTmpaTl,\lTmpbTl,\lTmpcTl,\lTmpiTl,\lTmpjTl,\lTmpkTl}
Scratch token lists for local assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\begin{variable}{\gTmpaTl,\gTmpbTl,\gTmpcTl,\gTmpiTl,\gTmpjTl,\gTmpkTl}
Scratch token lists for global assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\section{Creating and Using Token Lists}

\begin{function}{\TlNew}
\begin{syntax}
\cs{TlNew} \meta{tl var}
\end{syntax}
Creates a new \meta{tl var} or raises an error if the
name is already taken. The declaration is global. The
\meta{tl~var} is initially empty.
\end{function}

\begin{function}{\TlConst}
\begin{syntax}
\cs{TlConst} \meta{tl var} \Arg{token list}
\end{syntax}
Creates a new constant \meta{tl var} or raises an error
if the name is already taken. The value of the
\meta{tl var} is set globally to the \meta{token list}.
\end{function}

\begin{function}{\TlUse}
\begin{syntax}
\cs{TlUse} \meta{tl~var}
\end{syntax}
Recovers the content of a \meta{tl~var} and returns the value.
An error is raised if the variable
does not exist or if it is invalid. Note that it is possible to use
a \meta{tl~var} directly without an accessor function.
\end{function}

\begin{function}{\TlToStr}
\begin{syntax}
\cs{TlToStr} \Arg{token list}
\end{syntax}
Converts the \meta{token list} to a \meta{string}, returning the
resulting character tokens. A \meta{string}
is a series of tokens with category code $12$ (other) with the exception
of spaces, which retain category code $10$ (space).
%This function requires only a single expansion.
%Its argument \emph{must} be braced.
%\begin{texnote}
%This is the \eTeX{} primitive \tn{detokenize}.
%Converting a \meta{token list} to a \meta{string} yields a
%concatenation of the string representations of every token in the
%\meta{token list}.
%The string representation of a control sequence is
%\begin{itemize}
%\item an escape character, whose character code is given by the
%internal parameter \tn{escapechar}, absent if the
%\tn{escapechar} is negative or greater than the largest
%character code;
%\item the control sequence name, as defined by \cs{cs_to_str:N};
%\item a space, unless the control sequence name is a single
%character whose category at the time of expansion of
%\cs{tl_to_str:n} is not \enquote{letter}.
%\end{itemize}
%The string representation of an explicit character token is that
%character, doubled in the case of (explicit) macro parameter
%characters (normally \verb|#|).
%In particular, the string representation of a token list may
%depend on the category codes in effect when it is evaluated, and
%the value of the \tn{escapechar}: for instance |\tl_to_str:n {\a}|
%normally produces the three character \enquote{backslash},
%\enquote{lower-case a}, \enquote{space}, but it may also produce a
%single \enquote{lower-case a} if the escape character is negative
%and \texttt{a} is currently not a letter.
%\end{texnote}
\end{function}

\begin{function}{\TlVarToStr}
\begin{syntax}
\cs{TlVarToStr} \meta{tl var}
\end{syntax}
Converts the content of the \meta{tl var} to a string, returning the
resulting character tokens. A \meta{string}
is a series of tokens with category code $12$ (other) with the exception
of spaces, which retain category code $10$ (space).
\end{function}

\section{Viewing Token Lists}

\begin{function}{\TlLog}
\begin{syntax}
\cs{TlLog} \Arg{token list}
\end{syntax}
Writes the \meta{token list} in the log file. See also
\cs{TlShow} which displays the result in the terminal.
\end{function}

\begin{function}{\TlVarLog}
\begin{syntax}
\cs{TlVarLog} \meta{tl var}
\end{syntax}
Writes the content of the \meta{tl var} in the log file. See also
\cs{TlVarShow} which displays the result in the terminal.
\end{function}

\begin{function}{\TlShow}
\begin{syntax}
\cs{TlShow} \Arg{token list}
\end{syntax}
Displays the \meta{token list} on the terminal.
%\begin{texnote}
%This is similar to the \eTeX{} primitive \tn{showtokens}, wrapped
%to a fixed number of characters per line.
%\end{texnote}
\end{function}

\begin{function}{\TlVarShow}
\begin{syntax}
\cs{TlVarShow} \meta{tl var}
\end{syntax}
Displays the content of the \meta{tl var} on the terminal.
\begin{texnote}
This is similar to the \TeX{} primitive \tn{show}, wrapped to a
fixed number of characters per line.
\end{texnote}
\end{function}

\section{Setting Token List Variables}

\begin{function}{\TlSet}
\begin{syntax}
\cs{TlSet} \meta{tl~var} \Arg{tokens}
\end{syntax}
Sets \meta{tl~var} to contain \meta{tokens},
removing any previous content from the variable. For example
\begin{demohigh}
\TlSet \lTmpiTl {\IntMathMult{4}{5}}
\TlUse \lTmpiTl
\end{demohigh}
\end{function}

\begin{function}{\TlSetEq}
\begin{syntax}
\cs{TlSetEq} \meta{tl var_1} \meta{tl var_2}
\end{syntax}
Sets the content of \meta{tl var_1} equal to that of
\meta{tl var_2}.
\end{function}

\begin{function}{\TlClear}
\begin{syntax}
\cs{TlClear} \meta{tl~var}
\end{syntax}
Clears all entries from the \meta{tl~var}. For example
\begin{demohigh}
\TlSet \lTmpjTl {One}
\TlClear \lTmpjTl
\TlSet \lTmpjTl {Two}
\TlUse \lTmpjTl
\end{demohigh}
\end{function}

\begin{function}{\TlClearNew}
\begin{syntax}
\cs{TlClearNew} \meta{tl var}
\end{syntax}
Ensures that the \meta{tl var} exists globally by applying
\cs{TlNew} if necessary, then applies \cs{TlClear} to leave
the \meta{tl var} empty.
\end{function}

\begin{function}{\TlConcat}
\begin{syntax}
\cs{TlConcat} \meta{tl var_1} \meta{tl var_2} \meta{tl var_3}
\end{syntax}
Concatenates the content of \meta{tl var_2} and \meta{tl var_3}
together and saves the result in \meta{tl var_1}. The \meta{tl var_2}
is placed at the left side of the new token list.
\end{function}

\begin{function}{\TlPutLeft}
\begin{syntax}
\cs{TlPutLeft} \meta{tl~var} \Arg{tokens}
\end{syntax}
Appends \meta{tokens} to the left side of the current content of
\meta{tl~var}. For example
\begin{demohigh}
\TlSet \lTmpkTl {Functional}
\TlPutLeft \lTmpkTl {Hello}
\TlUse \lTmpkTl
\end{demohigh}
\end{function}

\begin{function}{\TlPutRight}
\begin{syntax}
\cs{TlPutRight} \meta{tl~var} \Arg{tokens}
\end{syntax}
Appends \meta{tokens} to the right side of the current content of
\meta{tl~var}. For example
\begin{demohigh}
\TlSet \lTmpkTl {Functional}
\TlPutRight \lTmpkTl {World}
\TlUse \lTmpkTl
\end{demohigh}
\end{function}

\section{Replacing Tokens}

Within token lists, replacement takes place at the top level: there is
no recursion into brace groups (more precisely, within a group defined by
a categroy code $1$/$2$ pair).

\begin{function}{\TlReplaceOnce}
\begin{syntax}
\cs{TlReplaceOnce} \meta{tl var} \Arg{old tokens} \Arg{new tokens}
\end{syntax}
Replaces the first (leftmost) occurrence of \meta{old tokens} in the
\meta{tl var} with \meta{new tokens}. \meta{Old tokens}
cannot contain \verb|{|, \verb|}| or \verb|#|
(more precisely, explicit character tokens with category code $1$
(begin-group) or $2$ (end-group), and tokens with category code $6$).
\end{function}

\begin{function}{\TlReplaceAll}
\begin{syntax}
\cs{TlReplaceAll} \meta{tl var} \Arg{old tokens} \Arg{new tokens}
\end{syntax}
Replaces all occurrences of \meta{old tokens} in the
\meta{tl var} with \meta{new tokens}. \meta{Old tokens}
cannot contain \verb|{|, \verb|}| or \verb|#|
(more precisely, explicit character tokens with category code $1$
(begin-group) or $2$ (end-group), and tokens with category code $6$).
As this function
operates from left to right, the pattern \meta{old tokens}
may remain after the replacement (see \cs{TlRemoveAll}
for an example).
\end{function}

\begin{function}{\TlRemoveOnce}
\begin{syntax}
\cs{TlRemoveOnce} \meta{tl var} \Arg{tokens}
\end{syntax}
Removes the first (leftmost) occurrence of \meta{tokens} from the
\meta{tl var}. \meta{Tokens} cannot contain \verb|{|, \verb|}| or \verb|#|
(more precisely, explicit character tokens with category code $1$
(begin-group) or $2$ (end-group), and tokens with category code $6$).
\end{function}

\begin{function}{\TlRemoveAll}
\begin{syntax}
\cs{TlRemoveAll} \meta{tl var} \Arg{tokens}
\end{syntax}
Removes all occurrences of \meta{tokens} from the
\meta{tl var}. \meta{Tokens} cannot contain \verb|{|, \verb|}| or \verb|#|
(more precisely, explicit character tokens with category code $1$
(begin-group) or $2$ (end-group), and tokens with category code $6$).
As this function
operates from left to right, the pattern \meta{tokens}
may remain after the removal, for instance,
\begin{demohigh}
\TlSet \lTmpaTl {abbccd}
\TlRemoveAll \lTmpaTl {bc}
\TlUse \lTmpaTl
\end{demohigh}
\end{function}

\begin{function}{\TlTrimSpaces}
\begin{syntax}
\cs{TlTrimSpaces} \Arg{token list}
\end{syntax}
Removes any leading and trailing explicit space characters
(explicit tokens with character code $32$ and category code $10$)
from the \meta{token list} and returns the result.
%\begin{texnote}
%The result is returned within \tn{unexpanded}, which means that the token
%list does not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}

\begin{function}{\TlVarTrimSpaces}
\begin{syntax}
\cs{TlVarTrimSpaces} \meta{tl var}
\end{syntax}
Sets the \meta{tl var} to contain the result of removing any leading
and trailing explicit space characters (explicit tokens with
character code $32$ and category code $10$) from its contents.
\end{function}

\section{Working with the Content of Token Lists}

\begin{function}{\TlCount}
\begin{syntax}
\cs{TlCount} \Arg{tokens}
\end{syntax}
Counts the number of \meta{items} in \meta{tokens} and returns this information.
Unbraced tokens count as one element as do each token group (\verb|{|$\cdots$\verb|}|).
This process ignores any unprotected spaces within \meta{tokens}. %See also \cs{TlVarCount}.
%This function requires three expansions, giving an \meta{integer denotation}.
\end{function}

\begin{function}{\TlVarCount}
\begin{syntax}
\cs{TlVarCount} \meta{tl var}
\end{syntax}
Counts the number of \meta{items} in the \meta{tl var} and returns this information.
Unbraced tokens count as one element as do each token group (\verb|{|$\cdots$\verb|}|).
This process ignores any unprotected spaces within the \meta{tl var}. %See also \cs{TlCount}.
%This function requires three expansions, giving an \meta{integer denotation}.
\end{function}

\begin{function}{\TlHead}
\begin{syntax}
\cs{TlHead} \Arg{token list}
\end{syntax}
Returns the first \meta{item} in the \meta{token list},
discarding the rest of the \meta{token list}.
All leading explicit space characters
(explicit tokens with character code $32$ and category code $10$)
are discarded; for example
\begin{demohigh}
\fbox {1\TlHead{ abc }2}
\fbox {1\TlHead{  abc }2}
\end{demohigh}
If the \enquote{head} is a brace group, rather than a single token,
the braces are removed, and so
\begin{codehigh}
\TlHead { { ab} c }
\end{codehigh}
yields \verb*| ab|.
A blank \meta{token list} (see \cs{TlIfBlank}) results in
\cs{TlHead} returning nothing.
%\begin{texnote}
%The result is returned within \cs{exp_not:n}, which means that the token
%list does not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}

\begin{function}{\TlVarHead}
\begin{syntax}
\cs{TlVarHead} \meta{tl var}
\end{syntax}
Returns the first \meta{item} in the \meta{tl var},
discarding the rest of the \meta{tl var}.
All leading explicit space characters
(explicit tokens with character code $32$ and category code $10$)
are discarded.
\end{function}

\begin{function}{\TlTail}
\begin{syntax}
\cs{TlTail} \Arg{token list}
\end{syntax}
Discards all leading explicit space characters
(explicit tokens with character code $32$ and category code $10$)
and the first \meta{item} in the \meta{token list}, and returns the
remaining tokens. Thus for example
\begin{codehigh}
\TlTail { a {bc} d }
\end{codehigh}
and
\begin{codehigh}
\TlTail {  a {bc} d }
\end{codehigh}
both return \verb*| {bc} d |.  A blank \meta{token list} (see \cs{TlIfBlank})
results in \cs{TlTail} returning nothing.
%\begin{texnote}
%The result is returned within \cs{exp_not:n}, which means that the
%token list does not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}

\begin{function}{\TlVarTail}
\begin{syntax}
\cs{TlVarTail} \meta{tl var}
\end{syntax}
Discards all leading explicit space characters
(explicit tokens with character code $32$ and category code $10$)
and the first \meta{item} in the \meta{tl var}, and returns the
remaining tokens.
\end{function}

\begin{function}{\TlItem,\TlVarItem}
\begin{syntax}
\cs{TlItem} \Arg{token list} \Arg{integer expression}
\cs{TlVarItem} \meta{tl var} \Arg{integer expression}
\end{syntax}
Indexing items in the \meta{token list} from $1$ on the left, this
function evaluates the \meta{integer expression} and returns the
appropriate item from the \meta{token list}.
If the \meta{integer expression} is negative, indexing occurs from
the right of the token list, starting at $-1$ for the right-most item.
If the index is out of bounds, then the function returns nothing.
%\begin{texnote}
%The result is returned within the \tn{unexpanded}
%primitive (\cs{exp_not:n}), which means that the \meta{item}
%does not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}

\begin{function}{\TlRandItem,\TlVarRandItem}
\begin{syntax}
\cs{TlRandItem} \Arg{token list}
\cs{TlVarRandItem} \meta{tl var}
\end{syntax}
Selects and returns a pseudo-random item of the \meta{token list}.
If the \meta{token list} is blank, the result is empty.
%This is not available in older versions of \XeTeX{}.
%\begin{texnote}
%The result is returned within the \tn{unexpanded}
%primitive (\cs{exp_not:n}), which means that the \meta{item}
%does not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}

\section{Mapping over Token Lists}

All mappings are done at the current group level, \emph{i.e.} any
local assignments made by the \meta{function} or \meta{code} discussed
below remain in effect after the loop.

%\begin{function}{\TlMapFunction}
%\begin{syntax}
%\cs{TlMapFunction} \Arg{token list} \meta{function}
%\end{syntax}
%Applies \meta{function} to every \meta{item} in the \meta{token list},
%The \meta{function} receives one argument for each iteration.
%This may be a number of tokens if the \meta{item} was stored within
%braces. Hence the \meta{function} should anticipate receiving
%\texttt{n}-type arguments.
%\end{function}
%
%\begin{function}{\TlVarMapFunction}
%\begin{syntax}
%\cs{TlVarMapFunction} \meta{tl var} \meta{function}
%\end{syntax}
%Applies \meta{function} to every \meta{item} in the \meta{tl var}.
%The \meta{function} receives one argument for each iteration.
%This may be a number of tokens if the \meta{item} was stored within
%braces. Hence the \meta{function} should anticipate receiving
%\texttt{n}-type arguments.
%\end{function}

\begin{function}{\TlMapInline}
\begin{syntax}
\cs{TlMapInline} \Arg{token list} \Arg{inline function}
\end{syntax}
Applies the \meta{inline function} to every \meta{item} stored within the
\meta{token list}. The \meta{inline function}  should consist of code which
receives the \meta{item} as \verb|#1|.
\end{function}

\begin{function}{\TlVarMapInline}
\begin{syntax}
\cs{TlVarMapInline} \meta{tl var} \Arg{inline function}
\end{syntax}
Applies the \meta{inline function} to every \meta{item} stored within the
\meta{tl var}. The \meta{inline function} should consist of code which
receives the \meta{item} as \verb|#1|.
\end{function}

%\begin{function}{\TlMapTokens,\TlVarMapTokens}
%\begin{syntax}
%\cs{TlMapTokens} \Arg{tokens} \Arg{code}
%\cs{TlVarMapTokens} \meta{tl var} \Arg{code}
%\end{syntax}
%Analogue of \cs{tl_map_function:NN} which maps several tokens
%instead of a single function.  The \meta{code} receives each \meta{item} in
%the \meta{tl var} or in \meta{tokens} as a trailing brace group. For
%instance,
%\begin{verbatim}
%\tl_map_tokens:Nn \l_my_tl { \prg_replicate:nn { 2 } }
%\end{verbatim}
%expands to twice each \meta{item} in the \meta{tl var}: for each \meta{item} in
%\cs{l_my_tl} the function \cs{prg_replicate:nn} receives |2| and
%\meta{item} as its two arguments.  The function
%\cs{tl_map_inline:Nn} is typically faster but is not expandable.
%\end{function}

\begin{function}{\TlMapVariable}
\begin{syntax}
\cs{TlMapVariable} \Arg{token list} \meta{variable} \Arg{code}
\end{syntax}
Stores each \meta{item} of the \meta{token list} in turn in the
(token list) \meta{variable} and applies the \meta{code}.  The
\meta{code} will usually make use of the \meta{variable}, but this
is not enforced.  The assignments to the \meta{variable} are local.
Its value after the loop is the last \meta{item} in the
\meta{tl var}, or its original value if the \meta{tl var} is blank.
\end{function}

\begin{function}{\TlVarMapVariable}
\begin{syntax}
\cs{TlVarMapVariable} \meta{tl var} \meta{variable} \Arg{code}
\end{syntax}
Stores each \meta{item} of the \meta{tl var} in turn in the (token
list) \meta{variable} and applies the \meta{code}.  The \meta{code}
will usually make use of the \meta{variable}, but this is not
enforced.  The assignments to the \meta{variable} are local.  Its
value after the loop is the last \meta{item} in the \meta{tl var},
or its original value if the \meta{tl var} is blank.
\end{function}

%\begin{function}{\TlMapBreak}
%\begin{syntax}
%\cs{TlMapBreak}
%\end{syntax}
%Used to terminate a tl map function before all
%entries in the \meta{token list variable} have been processed. This
%normally takes place within a conditional statement, for example
%\begin{verbatim}
%\tl_map_inline:Nn \l_my_tl
%{
%\str_if_eq:nnT { #1 } { bingo } { \tl_map_break: }
%Do something useful
%}
%\end{verbatim}
%See also \cs{tl_map_break:n}.
%Use outside of a tl map scenario leads to low
%level \TeX{} errors.
%\begin{texnote}
%When the mapping is broken, additional tokens may be inserted
%before the \meta{tokens} are
%inserted into the input stream.
%This depends on the design of the mapping function.
%\end{texnote}
%\end{function}
%
%\begin{function}{\TlMapBreakDo}
%\begin{syntax}
%\cs{TlMapBreakDo} \Arg{code}
%\end{syntax}
%Used to terminate a tl map function before all
%entries in the \meta{token list variable} have been processed, inserting
%the \meta{code} after the mapping has ended. This
%normally takes place within a conditional statement, for example
%\begin{verbatim}
%\tl_map_inline:Nn \l_my_tl
%{
%\str_if_eq:nnT { #1 } { bingo }
%{ \tl_map_break:n { <code> } }
%Do something useful
%}
%\end{verbatim}
%Use outside of a tl map scenario leads to low
%level \TeX{} errors.
%\begin{texnote}
%When the mapping is broken, additional tokens may be inserted
%before the \meta{code} is
%inserted into the input stream.
%This depends on the design of the mapping function.
%\end{texnote}
%\end{function}

\section{Token List Conditionals}

\begin{function}{\TlIfExist,\TlIfExistTF}
\begin{syntax}
\cs{TlIfExist} \meta{tl var}
\cs{TlIfExistTF} \meta{tl var} \Arg{true code} \Arg{false code}
\end{syntax}
Tests whether the \meta{tl var} is currently defined.  This does not
check that the \meta{tl var} really is a token list variable.
\end{function}

\begin{function}{\TlIfEmpty,\TlIfEmptyTF}
\begin{syntax}
\cs{TlIfEmpty} \Arg{token list}
\cs{TlIfEmptyTF} \Arg{token list} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the \meta{token list} is entirely empty
(\emph{i.e.}~contains no tokens at all). For example
\begin{demohigh}
\TlIfEmptyTF {abc} {\Result{Empty}} {\Result{NonEmpty}}
\TlIfEmptyTF {} {\Result{Empty}} {\Result{NonEmpty}}
\end{demohigh}
\end{function}

\begin{function}{\TlVarIfEmpty,\TlVarIfEmptyTF}
\begin{syntax}
\cs{TlVarIfEmpty} \meta{tl~var}
\cs{TlVarIfEmptyTF} \meta{tl~var} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the \meta{token list variable} is entirely empty
(\emph{i.e.}~contains no tokens at all). For example
\begin{demohigh}
\TlSet \lTmpaTl {abc}
\TlVarIfEmptyTF \lTmpaTl {\Result{Empty}} {\Result{NonEmpty}}
\TlClear \lTmpaTl
\TlVarIfEmptyTF \lTmpaTl {\Result{Empty}} {\Result{NonEmpty}}
\end{demohigh}
\end{function}

\begin{function}{\TlIfBlank}
\begin{syntax}
\cs{TlIfBlank} \Arg{token list}
\cs{TlIfBlankTF} \Arg{token list} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the \meta{token list} consists only of blank spaces
(\emph{i.e.} contains no item). The test is \texttt{true} if
\meta{token list} is zero or more explicit space characters
(explicit tokens with character code $32$ and category code $10$),
and is \texttt{false} otherwise.
\end{function}

\begin{function}{\TlIfEq,\TlIfEqTF}
\begin{syntax}
\cs{TlIfEq} \Arg{token list_1} \Arg{token list_2}
\cs{TlIfEqTF} \Arg{token list_1} \Arg{token list_2} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if \meta{token list_1} and \meta{token list_2} contain the
same list of tokens, both in respect of character codes and category
codes. See \cs{StrIfEq} if category codes are not important.
For example
\begin{demohigh}
\TlIfEqTF {abc} {abc} {\Result{Yes}} {\Result{No}}
\TlIfEqTF {abc} {xyz} {\Result{Yes}} {\Result{No}}
\end{demohigh}
%\begin{demohigh}
%\TlSet\lTmpaTl{abc}
%\TlSet\lTmpbTl{abc}
%\TlSet\lTmpcTl{xyz}
%\TlIfEqTF{\TlUse\lTmpaTl}{\TlUse\lTmpbTl}{\Result{Yes}}{\Result{No}}
%\TlIfEqTF{\TlUse\lTmpaTl}{\TlUse\lTmpcTl}{\Result{Yes}}{\Result{No}}
%\end{demohigh}
\end{function}

\begin{function}{\TlVarIfEq,\TlVarIfEqTF}
\begin{syntax}
\cs{TlVarIfEq} \meta{tl var_1} \meta{tl var_2}
\cs{TlVarIfEqTF} \meta{tl var_1} \meta{tl var_2} \Arg{true code} \Arg{false code}
\end{syntax}
Compares the content of two \meta{token list variables} and
is logically \texttt{true} if the two contain the same list of
tokens (\emph{i.e.}~identical in both the list of characters they
contain and the category codes of those characters). For example
\begin{demohigh}
\TlSet \lTmpaTl {abc}
\TlSet \lTmpbTl {abc}
\TlSet \lTmpcTl {xyz}
\TlVarIfEqTF \lTmpaTl \lTmpbTl {\Result{Yes}} {\Result{No}}
\TlVarIfEqTF \lTmpaTl \lTmpcTl {\Result{Yes}} {\Result{No}}
\end{demohigh}
See also \cs{StrVarIfEq} for a comparison that ignores category codes.
\end{function}

\begin{function}{\TlIfIn,\TlIfInTF}
\begin{syntax}
\cs{TlIfIn} \Arg{token list_1} \Arg{token list_2}
\cs{TlIfInTF} \Arg{token list_1} \Arg{token list_2} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if \meta{token list_2} is found inside \meta{token list_1}.
The \meta{token list_2} cannot contain the tokens \verb|{|, \verb|}| or \verb|#|
(more precisely, explicit character tokens with category code $1$
(begin-group) or $2$ (end-group), and tokens with category code $6$).
The search does \emph{not} enter brace (category code $1$/$2$) groups.
\end{function}

\begin{function}{\TlVarIfIn,\TlVarIfInTF}
\begin{syntax}
\cs{TlVarIfIn} \meta{tl var} \Arg{token list}
\cs{TlVarIfInTF} \meta{tl var} \Arg{token list} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the \meta{token list} is found in the content of the
\meta{tl var}. The \meta{token list} cannot contain
the tokens \verb|{|, \verb|}| or \verb|#|
(more precisely, explicit character tokens with category code $1$
(begin-group) or $2$ (end-group), and tokens with category code $6$).
\end{function}

\begin{function}{\TlIfSingle,\TlIfSingleTF}
\begin{syntax}
\cs{TlIfSingle} \Arg{token list}
\cs{TlIfSingleTF} \Arg{token list} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the \meta{token list} has exactly one \meta{item}, \emph{i.e.} is
a single normal token (neither an explicit space character nor a
begin-group character) or a single brace group, surrounded by
optional spaces on both sides. In other words, such a token list has
token count $1$ according to \cs{TlCount}.
\end{function}

\begin{function}{\TlVarIfSingle,\TlVarIfSingleTF}
\begin{syntax}
\cs{TlVarIfSingle} \meta{tl var}
\cs{TlVarIfSingleTF} \meta{tl var} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the content of the \meta{tl var} consists of a single \meta{item},
\emph{i.e.} is a single normal token (neither an explicit space
character nor a begin-group character) or a single brace group,
surrounded by optional spaces on both sides. In other words, such a
token list has token count $1$ according to \cs{TlVarCount}.
\end{function}

\section{Token List Case Functions}

\begin{function}{\TlVarCase}
\begin{syntax}
\cs{TlVarCase} \meta{test token list variable}
~ ~ \verb"{"
~ ~ ~ ~ \meta{token list variable case_1} \Arg{code case_1}
~ ~ ~ ~ \meta{token list variable case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \meta{token list variable case_n} \Arg{code case_n}
~ ~ \verb"}"
\end{syntax}
This function compares the \meta{test token list variable} in turn
with each of the \meta{token list variable cases}. If the two
are equal (as described for \cs{TlVarIfEq})
then the associated \meta{code} is left in the input
stream and other cases are discarded. The function
does nothing if there is no match.
\end{function}

\begin{function}{\TlVarCaseT}
\begin{syntax}
\cs{TlVarCaseT} \meta{test token list variable}
~ ~ \verb"{"
~ ~ ~ ~ \meta{token list variable case_1} \Arg{code case_1}
~ ~ ~ ~ \meta{token list variable case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \meta{token list variable case_n} \Arg{code case_n}
~ ~ \verb"}"
~ ~ \Arg{true code}
\end{syntax}
This function compares the \meta{test token list variable} in turn
with each of the \meta{token list variable cases}. If the two
are equal (as described for \cs{TlVarIfEq})
then the associated \meta{code} is left in the input
stream and other cases are discarded. If any of the
cases are matched, the \meta{true code} is also inserted into the
input stream (after the code for the appropriate case).
\end{function}

\begin{function}{\TlVarCaseF}
\begin{syntax}
\cs{TlVarCaseF} \meta{test token list variable}
~ ~ \verb"{"
~ ~ ~ ~ \meta{token list variable case_1} \Arg{code case_1}
~ ~ ~ ~ \meta{token list variable case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \meta{token list variable case_n} \Arg{code case_n}
~ ~ \verb"}"
~ ~ \Arg{false code}
\end{syntax}
This function compares the \meta{test token list variable} in turn
with each of the \meta{token list variable cases}. If the two
are equal (as described for \cs{TlVarIfEq})
then the associated \meta{code} is left in the input
stream and other cases are discarded. If none
match then the \meta{false code} is inserted into the input stream
 (after the code for the appropriate case).
\end{function}

\begin{function}{\TlVarCaseTF}
\begin{syntax}
\cs{TlVarCaseTF} \meta{test token list variable}
~ ~ \verb"{"
~ ~ ~ ~ \meta{token list variable case_1} \Arg{code case_1}
~ ~ ~ ~ \meta{token list variable case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \meta{token list variable case_n} \Arg{code case_n}
~ ~ \verb"}"
~ ~ \Arg{true code}
~ ~ \Arg{false code}
\end{syntax}
This function compares the \meta{test token list variable} in turn
with each of the \meta{token list variable cases}. If the two
are equal (as described for \cs{TlVarIfEq})
then the associated \meta{code} is left in the input
stream and other cases are discarded. If any of the
cases are matched, the \meta{true code} is also inserted into the
input stream (after the code for the appropriate case), while if none
match then the \meta{false code} is inserted. The function
\cs{TlVarCase}, which does nothing if there is no match, is also
available.
\end{function}

\chapter{Strings (\texttt{Str})}

\section{Constant and Scratch Strings}

\begin{variable}{\cAmpersandStr,\cAtsignStr,\cBackslashStr,\cLeftBraceStr,\cRightBraceStr,
\cCircumflexStr,\cColonStr,\cDollarStr,\cHashStr,\cPercentStr,\cTildeStr,\cUnderscoreStr,\cZeroStr}
Constant strings, containing a single character token, with category code $12$.
\end{variable}

\begin{variable}{\lTmpaStr,\lTmpbStr,\lTmpcStr,\lTmpiStr,\lTmpjStr,\lTmpkStr}
Scratch strings for local assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\begin{variable}{\gTmpaStr,\gTmpbStr,\gTmpcStr,\gTmpiStr,\gTmpjStr,\gTmpkStr}
Scratch strings for global assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\section{Creating and Using Strings}

\begin{function}{\StrNew}
\begin{syntax}
\cs{StrNew} \meta{str var}
\end{syntax}
Creates a new \meta{str var} or raises an error if the name is
already taken. The declaration is global. The \meta{str var} is
initially empty.
\end{function}

\begin{function}{\StrConst}
\begin{syntax}
\cs{StrConst} \meta{str var} \Arg{token list}
\end{syntax}
Creates a new constant \meta{str var} or raises an error if the name
is already taken.  The value of the \meta{str var} is set
globally to the \meta{token list}, converted to a string.
\end{function}

\begin{function}{\StrUse}
\begin{syntax}
\cs{StrUse} \meta{str var}
\end{syntax}
Recovers the content of a \meta{str var} and returns the value.
An error is raised if the variable
does not exist or if it is invalid. Note that it is possible to use
a \meta{str} directly without an accessor function.
\end{function}

\section{Viewing Strings}

\begin{function}{\StrLog}
\begin{syntax}
\cs{StrLog} \Arg{token list}
\end{syntax}
Writes \meta{token list} in the log file.
\end{function}

\begin{function}{\StrVarLog}
\begin{syntax}
\cs{StrVarLog} \meta{str var}
\end{syntax}
Writes the content of the \meta{str var} in the log file.
For example
\begin{codehigh}
\StrSet \lTmpiStr {1234\abcd5678}
\StrVarLog \lTmpiStr
\end{codehigh}
\end{function}

\begin{function}{\StrShow}
\begin{syntax}
\cs{StrShow} \Arg{token list}
\end{syntax}
Displays \meta{token list} on the terminal.
\end{function}

\begin{function}{\StrVarShow}
\begin{syntax}
\cs{StrVarShow} \meta{str var}
\end{syntax}
Displays the content of the \meta{str var} on the terminal.
\end{function}

\section{Setting String Variables}

\begin{function}{\StrSet}
\begin{syntax}
\cs{StrSet} \meta{str var} \Arg{token list}
\end{syntax}
Converts the \meta{token list} to a \meta{string}, and stores the
result in \meta{str var}. For example
\begin{demohigh}
\StrSet \lTmpiStr {\IntMathMult{4}{5}}
\StrUse \lTmpiStr
\end{demohigh}
\end{function}

\begin{function}{\StrSetEq}
\begin{syntax}
\cs{StrSetEq} \meta{str var_1} \meta{str var_2}
\end{syntax}
Sets the content of \meta{str var_1} equal to that of
\meta{str var_2}.
\end{function}

\begin{function}{\StrClear}
\begin{syntax}
\cs{StrClear} \meta{str var}
\end{syntax}
Clears the content of the \meta{str var}. For example
\begin{demohigh}
\StrSet \lTmpjStr {One}
\StrClear \lTmpjStr
\StrSet \lTmpjStr {Two}
\StrUse \lTmpjStr
\end{demohigh}
\end{function}

\begin{function}{\StrClearNew}
\begin{syntax}
\cs{StrClearNew} \meta{str var}
\end{syntax}
Ensures that the \meta{str var} exists globally by applying
\cs{StrNew} if necessary, then applies \cs{StrClear} to leave
the \meta{str var} empty.
\end{function}

\begin{function}{\StrConcat}
\begin{syntax}
\cs{StrConcat} \meta{str var_1} \meta{str var_2} \meta{str var_3}
\end{syntax}
Concatenates the content of \meta{str var_2} and \meta{str var_3}
together and saves the result in \meta{str var_1}. The \meta{str var_2}
is placed at the left side of the new string variable.
The \meta{str var_2} and \meta{str var_3} must indeed be strings, as
this function does not convert their contents to a string.
\end{function}

\begin{function}{\StrPutLeft}
\begin{syntax}
\cs{StrPutLeft} \meta{str var} \Arg{token list}
\end{syntax}
Converts the \meta{token list} to a \meta{string}, and prepends the
result to \meta{str var}.  The current contents of the \meta{str
var} are not automatically converted to a string. For example
\begin{demohigh}
\StrSet \lTmpkStr {Functional}
\StrPutLeft \lTmpkStr {Hello}
\StrUse \lTmpkStr
\end{demohigh}
\end{function}

\begin{function}{\StrPutRight}
\begin{syntax}
\cs{StrPutRight} \meta{str var} \Arg{token list}
\end{syntax}
Converts the \meta{token list} to a \meta{string}, and appends the
result to \meta{str var}.  The current contents of the \meta{str
var} are not automatically converted to a string. For example
\begin{demohigh}
\StrSet \lTmpkStr {Functional}
\StrPutRight \lTmpkStr {World}
\StrUse \lTmpkStr
\end{demohigh}
\end{function}

\section{Modifying String Variables}

\begin{function}{\StrReplaceOnce}
\begin{syntax}
\cs{StrReplaceOnce} \meta{str var} \Arg{old} \Arg{new}
\end{syntax}
Converts the \meta{old} and \meta{new} token lists to strings, then
replaces the first (leftmost) occurrence of \meta{old string} in the
\meta{str var} with \meta{new string}.
\end{function}

\begin{function}{\StrReplaceAll}
\begin{syntax}
\cs{StrReplaceAll} \meta{str var} \Arg{old} \Arg{new}
\end{syntax}
Converts the \meta{old} and \meta{new} token lists to strings, then
replaces all occurrences of \meta{old string} in the
\meta{str var} with \meta{new string}.
As this function
operates from left to right, the pattern \meta{old string}
may remain after the replacement.
\end{function}

\begin{function}{\StrRemoveOnce}
\begin{syntax}
\cs{StrRemoveOnce} \meta{str var} \Arg{token list}
\end{syntax}
Converts the \meta{token list} to a \meta{string} then
removes the first (leftmost) occurrence of \meta{string} from the
\meta{str var}.
\end{function}

\begin{function}{\StrRemoveAll}
\begin{syntax}
\cs{StrRemoveAll} \meta{str var} \Arg{token list}
\end{syntax}
Converts the \meta{token list} to a \meta{string} then
removes all occurrences of \meta{string} from the \meta{str var}.
As this function operates from left to right,
the pattern \meta{string} may remain after the removal, for instance,
\begin{demohigh}
\StrSet \lTmpaStr {abbccd}
\StrRemoveAll \lTmpaStr {bc}
\TlUse \lTmpaStr
\end{demohigh}
\end{function}

\section{Working with the Content of Strings}

\begin{function}{\StrCount}
\begin{syntax}
\cs{StrCount} \Arg{token list}
\end{syntax}
Returns the number of characters in the string representation of \meta{token list},
as an integer denotation. All characters including spaces are counted.
\par
Due to naming conflict, you need to use \cs{StrSize} instead of \cs{StrCount}
if you want to use \verb!functional! package together with \verb!xstring! package.
\end{function}

\begin{function}{\StVarCount}
\begin{syntax}
\cs{StrVarCount} \meta{tl var}
\end{syntax}
Returns the number of characters in the string representation of the \meta{tl var},
as an integer denotation. All characters including spaces are counted.
\end{function}

\begin{function}{\StrHead}
\begin{syntax}
\cs{StrHead} \Arg{token list}
\end{syntax}
Converts the \meta{token list} into a \meta{string}.
The first character in the \meta{string} is then returned,
with category code \enquote{other}. If the first character is a space,
it returns a space token with category code $10$ (blank space).
If the \meta{string} is empty, then nothing is returned.
\end{function}

\begin{function}{\StrVarHead}
\begin{syntax}
\cs{StrVarHead} \meta{tl var}
\end{syntax}
Converts the \meta{tl var} into a \meta{string}.
The first character in the \meta{string} is then returned,
with category code \enquote{other}. If the first character is a space,
it returns a space token with category code $10$ (blank space).
If the \meta{string} is empty, then nothing is returned.
\end{function}

\begin{function}{\StrTail}
\begin{syntax}
\cs{StrTail} \Arg{token list}
\end{syntax}
Converts the \meta{token list} to a \meta{string}, removes the first
character, and returns the remaining characters (if any)
with category codes $12$ and $10$ (for spaces).
If the first character is a space, it only trims that space.
If the \meta{token list} is empty,
then nothing is left on the input stream.
\end{function}

\begin{function}{\StrVarTail}
\begin{syntax}
\cs{StrVarTail} \meta{tl var}
\end{syntax}
Converts the \meta{tl var} to a \meta{string}, removes the first
character, and returns the remaining characters (if any)
with category codes $12$ and $10$ (for spaces).
If the first character is a space, it only trims that space.
If the \meta{token list} is empty,
then nothing is left on the input stream.
\end{function}

\begin{function}{\StrItem}
\begin{syntax}
\cs{StrItem} \Arg{token list} \Arg{integer expression}
\end{syntax}
Converts the \meta{token list} to a \meta{string},
and returns the character in position \meta{integer expression} of
the \meta{string}, starting at $1$ for the first (left-most)
character. All characters including spaces are taken into account.
If the \meta{integer expression} is negative,
characters are counted from the end of the \meta{string}. Hence,
$-1$ is the right-most character, \emph{etc.}
\end{function}

\begin{function}{\StrVarItem}
\begin{syntax}
\cs{StrVarItem} \meta{tl var} \Arg{integer expression}
\end{syntax}
Converts the \meta{tl var} to a \meta{string},
and returns the character in position \meta{integer expression} of
the \meta{string}, starting at $1$ for the first (left-most)
character. All characters including spaces are taken into account.
If the \meta{integer expression} is negative,
characters are counted from the end of the \meta{string}. Hence,
$-1$ is the right-most character, \emph{etc.}
\end{function}

\section{Mapping over Strings}

All mappings are done at the current group level, \emph{i.e.} any
local assignments made by the \meta{function} or \meta{code} discussed
below remain in effect after the loop.

%\begin{function}{\StrMapFunction,\StrVarMapFunction}
%\begin{syntax}
%\cs{StrMapFunction} \Arg{token list} \meta{function}
%\cs{StrVarMapFunction} \meta{str var} \meta{function}
%\end{syntax}
%Converts the \meta{token list} to a \meta{string} then
%applies \meta{function} to every \meta{character} in the
%\meta{string} including spaces.
%\end{function}

\begin{function}{\StrMapInline,\StrVarMapInline}
\begin{syntax}
\cs{StrMapInline} \Arg{token list} \Arg{inline function}
\cs{StrVarMapInline} \meta{str var} \Arg{inline function}
\end{syntax}
Converts the \meta{token list} to a \meta{string} then
applies the \meta{inline function} to every \meta{character} in the
\meta{str var} including spaces.
The \meta{inline function} should consist of code which
receives the \meta{character} as \verb|#1|.
\end{function}

%\begin{function}{\StrMapTokens, \StrVarMapTokens}
%\begin{syntax}
%\cs{StrMapTokens} \Arg{token list} \Arg{code}
%\cs{StrVarMapTokens} \meta{str var} \Arg{code}
%\end{syntax}
%Converts the \meta{token list} to a \meta{string} then applies
%\meta{code} to every \meta{character} in the \meta{string} including spaces.
%The \meta{code} receives each character as a trailing brace group.
%This is equivalent to \cs{StrMapFunction} if the
%\meta{code} consists of a single function.
%\end{function}

\begin{function}{\StrMapVariable,\StrMapVariable}
\begin{syntax}
\cs{StrMapVariable} \Arg{token list} \meta{variable} \Arg{code}
\cs{StrVarMapVariable} \meta{str var} \meta{variable} \Arg{code}
\end{syntax}
Converts the \meta{token list} to a \meta{string} then stores each
\meta{character} in the \meta{string} (including spaces) in turn in
the (string or token list) \meta{variable} and applies the
\meta{code}.  The \meta{code} will usually make use of the
\meta{variable}, but this is not enforced.  The assignments to the
\meta{variable} are local.  Its value after the loop is the last
\meta{character} in the \meta{string}, or its original value if the
\meta{string} is empty.
\end{function}

%\begin{function}{\StrMapBreak}
%\begin{syntax}
%\cs{StrMapBreak}
%\end{syntax}
%Used to terminate a string map function before all
%characters in the \meta{string} have been processed. This
%normally takes place within a conditional statement, for example
%\begin{verbatim}
%\str_map_inline:Nn \l_my_str
%{
%\str_if_eq:nnT { #1 } { bingo } { \str_map_break: }
%Do something useful
%}
%\end{verbatim}
%See also \cs{StrMapBreakDo}.
%Use outside of a string map scenario leads to low
%level \TeX{} errors.
%\begin{texnote}
%When the mapping is broken, additional tokens may be inserted
%before continuing with the
%code that follows the loop.
%This depends on the design of the mapping function.
%\end{texnote}
%\end{function}
%
%\begin{function}{\StrMapBreakDo}
%\begin{syntax}
%\cs{StrMapBreakDo} \Arg{code}
%\end{syntax}
%Used to terminate a string map function before all
%characters in the \meta{string} have been processed, inserting
%the \meta{code} after the mapping has ended. This
%normally takes place within a conditional statement, for example
%\begin{verbatim}
%\str_map_inline:Nn \l_my_str
%{
%\str_if_eq:nnT { #1 } { bingo }
%{ \str_map_break:n { <code> } }
%Do something useful
%}
%\end{verbatim}
%Use outside of a string map scenario leads to low
%level \TeX{} errors.
%\begin{texnote}
%When the mapping is broken, additional tokens may be inserted
%before the \meta{code} is
%inserted into the input stream.
%This depends on the design of the mapping function.
%\end{texnote}
%\end{function}

\section{String Conditionals}

\begin{function}{\StrIfExist,\StrIfExistTF}
\begin{syntax}
\cs{StrIfExist} \meta{str var}
\cs{StrIfExistTF} \meta{str var} \Arg{true code} \Arg{false code}
\end{syntax}
Tests whether the \meta{str var} is currently defined.  This does not
check that the \meta{str var} really is a string.
\end{function}

\begin{function}{\StrVarIfEmpty,\StrVarIfEmptyTF}
\begin{syntax}
\cs{StrVarIfEmpty} \meta{str var}
\cs{StrVarIfEmptyTF} \meta{str var} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the \meta{string variable} is entirely empty
(\emph{i.e.} contains no characters at all). For example
\begin{demohigh}
\StrSet \lTmpaStr {abc}
\StrVarIfEmptyTF \lTmpaStr {\Result{Empty}} {\Result{NonEmpty}}
\StrClear \lTmpaStr
\StrVarIfEmptyTF \lTmpaStr {\Result{Empty}} {\Result{NonEmpty}}
\end{demohigh}
\end{function}

\begin{function}{\StrIfEq,\StrIfEqTF}
\begin{syntax}
\cs{StrIfEq} \Arg{tl_1} \Arg{tl_2}
\cs{StrIfEqTF} \Arg{tl_1} \Arg{tl_2} \Arg{true code} \Arg{false code}
\end{syntax}
Compares the two \meta{token lists} on a character by character
basis (namely after converting them to strings),
and is \texttt{true} if the two \meta{strings} contain the same
characters in the same order.
%Thus for example
%\begin{codehigh}
%\StrIfEq{abc}{\TlToStr{abc}}
%\end{codehigh}
%is logically \texttt{true}.
See \cs{TlIfEq} to compare
tokens (including their category codes) rather than characters.
For example
\begin{demohigh}
\StrIfEqTF {abc} {abc} {\Result{Yes}} {\Result{No}}
\StrIfEqTF {abc} {xyz} {\Result{Yes}} {\Result{No}}
\end{demohigh}
%\begin{demohigh}
%\StrSet\lTmpaStr{abc}
%\StrSet\lTmpbStr{abc}
%\StrSet\lTmpcStr{xyz}
%\StrIfEqTF{\lTmpaStr}{\lTmpbStr}{\Result{Yes}}{\Result{No}}
%\StrIfEqTF{\lTmpaStr}{\lTmpcStr}{\Result{Yes}}{\Result{No}}
%\StrIfEqTF{\StrUse\lTmpaStr}{\StrUse\lTmpbStr}{\Result{Yes}}{\Result{No}}
%\StrIfEqTF{\StrUse\lTmpaStr}{\StrUse\lTmpcStr}{\Result{Yes}}{\Result{No}}
%\end{demohigh}
\end{function}

\begin{function}{\StrVarIfEq,\StrVarIfEqTF}
\begin{syntax}
\cs{StrVarIfEq} \meta{str var_1} \meta{str var_2}
\cs{StrVarIfEqTF} \meta{str var_1} \meta{str var_2} \Arg{true code} \Arg{false code}
\end{syntax}
Compares the content of two \meta{str variables} and
is logically \texttt{true} if the two contain the same characters
in the same order.  See \cs{TlVarIfEq} to compare tokens
(including their category codes) rather than characters.
\begin{demohigh}
\StrSet \lTmpaStr {abc}
\StrSet \lTmpbStr {abc}
\StrSet \lTmpcStr {xyz}
\StrVarIfEqTF \lTmpaStr \lTmpbStr {\Result{Yes}} {\Result{No}}
\StrVarIfEqTF \lTmpaStr \lTmpcStr {\Result{Yes}} {\Result{No}}
\end{demohigh}
\end{function}

\begin{function}{\StrIfIn,\StrIfInTF}
\begin{syntax}
\cs{StrIfInTF} \Arg{tl_1} \Arg{tl_2}
\cs{StrIfInTF} \Arg{tl_1} \Arg{tl_2} \Arg{true code} \Arg{false code}
\end{syntax}
Converts both \meta{token lists} to \meta{strings} and
tests whether \meta{string_2} is found inside \meta{string_1}.
\end{function}

\begin{function}{\StrVarIfIn,\StrVarIfInTF}
\begin{syntax}
\cs{StrVarIfInTF} \meta{str var} \Arg{token list}
\cs{StrVarIfInTF} \meta{str var} \Arg{token list} \Arg{true code} \Arg{false code}
\end{syntax}
Converts the \meta{token list} to a \meta{string} and
tests if that \meta{string} is found in the content of the
\meta{str var}.
\end{function}

\begin{function}{\StrCompare,\StrCompareTF}
\begin{syntax}
\cs{StrCompare} \Arg{tl_1} \meta{relation} \Arg{tl_2}
\cs{StrCompareTF} \Arg{tl_1} \meta{relation} \Arg{tl_2} \Arg{true code} \Arg{false code}
\end{syntax}
Compares the two \meta{token lists} on a character by character
basis (namely after converting them to strings) in a lexicographic
order according to the character codes of the characters.  The
\meta{relation} can be \verb|<|, \verb|=|, or \verb|>| and the test is
\texttt{true} under the following conditions:
\begin{itemize}[nosep]
\item for \verb|<|, if the first string is earlier than the second in lexicographic order;
\item for \verb|=|, if the two strings have exactly the same characters;
\item for \verb|>|, if the first string is later than the second in lexicographic order.
\end{itemize}
For example:
\begin{demohigh}
\StrCompareTF {ab} < {abc} {\Result{Yes}} {\Result{No}}
\StrCompareTF {ab} < {aa} {\Result{Yes}} {\Result{No}}
\end{demohigh}
%\begin{texnote}
%This is a wrapper around the \TeX{} primitive
%\cs{(pdf)strcmp}.  It is meant for programming
%and not for sorting textual contents, as it simply considers
%character codes and not more elaborate considerations of grapheme
%clusters, locale, etc.
%\end{texnote}
\par
Due to naming conflict, you need to use \cs{StrIfCompare}/\cs{StrIfCompareTF} as a replacement
if you want to use \verb!functional! package together with \verb!xstring! package.
\end{function}

\section{String Case Functions}

\begin{function}{\StrCase}
\begin{syntax}
\cs{StrCase} \Arg{test string}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{string case_1} \Arg{code case_1}
~ ~ ~ ~ \Arg{string case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \Arg{string case_n} \Arg{code case_n}
~ ~ \verb|}|
\end{syntax}
Compares the \meta{test string} in turn with each
of the \meta{string cases} (all token lists are converted to strings).
If the two are equal (as described for
\cs{StrIfEq}) then the associated \meta{code} is left in the
input stream and other cases are discarded.
\end{function}

\begin{function}{\StrCaseT}
\begin{syntax}
\cs{StrCaseT} \Arg{test string}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{string case_1} \Arg{code case_1}
~ ~ ~ ~ \Arg{string case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \Arg{string case_n} \Arg{code case_n}
~ ~ \verb|}|
~ ~ \Arg{true code}
\end{syntax}
Compares the \meta{test string} in turn with each
of the \meta{string cases} (all token lists are converted to strings).
If the two are equal (as described for
\cs{StrIfEq}) then the associated \meta{code} is left in the
input stream and other cases are discarded. If any of the
cases are matched, the \meta{true code} is also inserted into the
input stream (after the code for the appropriate case).
\end{function}

\begin{function}{\StrCaseF}
\begin{syntax}
\cs{StrCaseF} \Arg{test string}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{string case_1} \Arg{code case_1}
~ ~ ~ ~ \Arg{string case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \Arg{string case_n} \Arg{code case_n}
~ ~ \verb|}|
~ ~ \Arg{false code}
\end{syntax}
Compares the \meta{test string} in turn with each
of the \meta{string cases} (all token lists are converted to strings).
If the two are equal (as described for
\cs{StrIfEq}) then the associated \meta{code} is left in the
input stream and other cases are discarded. If none
match then the \meta{false code} is inserted.
\end{function}

\begin{function}{\StrCaseTF}
\begin{syntax}
\cs{StrCaseTF} \Arg{test string}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{string case_1} \Arg{code case_1}
~ ~ ~ ~ \Arg{string case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \Arg{string case_n} \Arg{code case_n}
~ ~ \verb|}|
~ ~ \Arg{true code}
~ ~ \Arg{false code}
\end{syntax}
Compares the \meta{test string} in turn with each
of the \meta{string cases} (all token lists are converted to strings).
If the two are equal (as described for
\cs{StrIfEq}) then the associated \meta{code} is left in the
input stream and other cases are discarded. If any of the
cases are matched, the \meta{true code} is also inserted into the
input stream (after the code for the appropriate case), while if none
match then the \meta{false code} is inserted.
\end{function}

\chapter{Integers (\texttt{Int})}

\section{Constant and Scratch Integers}

\begin{variable}{\cZeroInt,\cOneInt}
Integer values used with primitive tests and assignments: their
self-terminating nature makes these more convenient and faster than
literal numbers.
\end{variable}

\begin{variable}{\cMaxInt}
The maximum value that can be stored as an integer.
\end{variable}

\begin{variable}{\cMaxRegisterInt}
Maximum number of registers.
\end{variable}

\begin{variable}{\cMaxCharInt}
Maximum character code completely supported by the engine.
\end{variable}

\begin{variable}{\lTmpaInt,\lTmpbInt,\lTmpcInt,\lTmpiInt,\lTmpjInt,\lTmpkInt}
Scratch integer for local assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\begin{variable}{\gTmpaInt,\gTmpbInt,\gTmpcInt,\gTmpiInt,\gTmpjInt,\gTmpkInt}
Scratch integer for global assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\section{Integer Expressions}

\begin{function}{\IntEval}
\begin{syntax}
\cs{IntEval} \Arg{integer expression}
\end{syntax}
Evaluates the \meta{integer expression} and returns the result:
for positive results an
explicit sequence of decimal digits not starting with~\texttt{0},
for negative results \texttt{-}~followed by such a sequence, and
\texttt{0}~for zero. For example
\begin{demohigh}
\IntEval {(1+4)*(2-3)/5}
\end{demohigh}
\end{function}

\begin{function}{\IntMathAdd}
\begin{syntax}
\cs{IntMathAdd} \Arg{integer expression_1} \Arg{integer expression_2}
\end{syntax}
Adds \Arg{integer expression_1} and \Arg{integer expression_2},
and returns the result. For example
\begin{demohigh}
\IntMathAdd {7} {3}
\end{demohigh}
\end{function}

\begin{function}{\IntMathSub}
\begin{syntax}
\cs{IntMathSub} \Arg{integer expression_1} \Arg{integer expression_2}
\end{syntax}
Subtracts \Arg{integer expression_2} from \Arg{integer expression_1},
and returns the result. For example
\begin{demohigh}
\IntMathSub {7} {3}
\end{demohigh}
\end{function}

\begin{function}{\IntMathMult}
\begin{syntax}
\cs{IntMathMult} \Arg{integer expression_1} \Arg{integer expression_2}
\end{syntax}
Multiplies \Arg{integer expression_1} by \Arg{integer expression_2},
and returns the result. For example
\begin{demohigh}
\IntMathMult {7} {3}
\end{demohigh}
\end{function}

\begin{function}{\IntMathDiv}
\begin{syntax}
\cs{IntMathDiv} \Arg{integer expression_1} \Arg{integer expression_2}
\end{syntax}
Evaluates the two \meta{integer expressions} as described earlier,
then divides the first value by the second, and rounds the result
to the closest integer. Ties are rounded away from zero. Note that
this is identical to using \verb|/| directly in an \meta{integer expression}.
The result is returned as an \meta{integer denotation}.
For example
\begin{demohigh}
\IntMathDiv {8} {3}
\end{demohigh}
\end{function}

\begin{function}{\IntMathDivTrancate}
\begin{syntax}
\cs{IntMathDivTrancate} \Arg{integer expression_1} \Arg{integer expression_2}
\end{syntax}
Evaluates the two \meta{integer expressions} as described earlier,
then divides the first value by the second, and rounds the result towards zero.
Note that division using \verb|/| rounds to the closest integer instead.
The result is returned as an \meta{integer denotation}.
For example
\begin{demohigh}
\IntMathDivTruncate {8} {3}
\end{demohigh}
\end{function}

\begin{function}{\IntMathSign}
\begin{syntax}
\cs{IntMathSign} \Arg{intexpr}
\end{syntax}
Evaluates the \meta{integer expression} then leaves $1$ or $0$ or
$-1$ in the input stream according to the sign of the result.
\end{function}

\begin{function}{\IntMathAbs}
\begin{syntax}
\cs{IntMathAbs} \Arg{integer expression}
\end{syntax}
Evaluates the \meta{integer expression} as described for
\cs{IntEval} and leaves the absolute value of the result in
the input stream as an \meta{integer denotation} after two
expansions.
\end{function}

\begin{function}{\IntMathMax,\IntMathMin}
\begin{syntax}
\cs{IntMathMax} \Arg{intexpr_1} \Arg{intexpr_2}
\cs{IntMathMin} \Arg{intexpr_1} \Arg{intexpr_2}
\end{syntax}
Evaluates the \meta{integer expressions} as described for
\cs{IntEval} and leaves either the larger or smaller value
in the input stream as an \meta{integer denotation} after two
expansions.
\end{function}

\begin{function}{\IntMathMod}
\begin{syntax}
\cs{IntMathMod} \Arg{intexpr_1} \Arg{intexpr_2}
\end{syntax}
Evaluates the two \meta{integer expressions} as described earlier,
then calculates the integer remainder of dividing the first
expression by the second.  This is obtained by subtracting
\cs{IntMathDivTruncate} \Arg{intexpr_1} \Arg{intexpr_2} times
\meta{intexpr_2} from \meta{intexpr_1}.  Thus, the result has the
same sign as \meta{intexpr_1} and its absolute value is strictly
less than that of \meta{intexpr_2}.  The result is left in the input
stream as an \meta{integer denotation} after two expansions.
\end{function}

\begin{function}{\IntMathRand}
\begin{syntax}
\cs{IntMathRand} \Arg{intexpr_1} \Arg{intexpr_2}
\end{syntax}
Evaluates the two \meta{integer expressions} and produces a
pseudo-random number between the two (with bounds included).
%This is not available in older versions of \XeTeX{}.
\end{function}

\section{Creating and Using Integers}

\begin{function}{\IntNew}
\begin{syntax}
\cs{IntNew} \meta{integer}
\end{syntax}
Creates a new \meta{integer} or raises an error if the name is
already taken. The declaration is global. The \meta{integer} is
initially equal to $0$.
\end{function}

\begin{function}{\IntConst}
\begin{syntax}
\cs{IntConst} \meta{integer} \Arg{integer expression}
\end{syntax}
Creates a new constant \meta{integer} or raises an error if the name
is already taken. The value of the \meta{integer} is set
globally to the \meta{integer expression}.
\end{function}

\begin{function}{\IntUse}
\begin{syntax}
\cs{IntUse} \meta{integer}
\end{syntax}
Recovers the content of an \meta{integer} and returns the value.
An error is raised if the variable does not exist or if it is invalid.
\end{function}

\section{Viewing Integers}

\begin{function}{\IntLog}
\begin{syntax}
\cs{IntLog} \Arg{integer expression}
\end{syntax}
Writes the result of evaluating the \meta{integer expression}
in the log file.
\end{function}

\begin{function}{\IntVarLog}
\begin{syntax}
\cs{IntVarLog} \meta{integer}
\end{syntax}
Writes the value of the \meta{integer} in the log file.
\end{function}

\begin{function}{\IntShow}
\begin{syntax}
\cs{IntShow} \Arg{integer expression}
\end{syntax}
Displays the result of evaluating the \meta{integer expression}
on the terminal.
\end{function}

\begin{function}{\IntVarShow}
\begin{syntax}
\cs{IntVarShow} \meta{integer}
\end{syntax}
Displays the value of the \meta{integer} on the terminal.
\end{function}

\section{Setting Integer Variables}

\begin{function}{\IntSet}
\begin{syntax}
\cs{IntSet} \meta{integer} \Arg{integer expression}
\end{syntax}
Sets \meta{integer} to the value of \meta{integer expression},
which must evaluate to an integer (as described for \cs{IntEval}).
For example
\begin{demohigh}
\IntSet \lTmpaInt {3+5}
\IntUse \lTmpaInt
\end{demohigh}
\end{function}

\begin{function}{\IntSetEq}
\begin{syntax}
\cs{IntSetEq} \meta{integer_1} \meta{integer_2}
\end{syntax}
Sets the content of \meta{integer_1} equal to that of
\meta{integer_2}.
\end{function}

\begin{function}{\IntZero}
\begin{syntax}
\cs{IntZero} \meta{integer}
\end{syntax}
Sets \meta{integer} to $0$. For example
\begin{demohigh}
\IntSet \lTmpaInt {5}
\IntZero \lTmpaInt
\IntUse \lTmpaInt
\end{demohigh}
\end{function}

\begin{function}{\IntZeroNew}
\begin{syntax}
\cs{IntZeroNew} \meta{integer}
\end{syntax}
Ensures that the \meta{integer} exists globally by applying
\cs{IntNew} if necessary, then applies \cs{IntZero} to leave
the \meta{integer} set to zero.
\end{function}

\begin{function}{\IntIncr}
\begin{syntax}
\cs{IntIncr} \meta{integer}
\end{syntax}
Increases the value stored in \meta{integer} by $1$.
For example
\begin{demohigh}
\IntSet \lTmpaInt {5}
\IntIncr \lTmpaInt
\IntUse \lTmpaInt
\end{demohigh}
\end{function}

\begin{function}{\IntDecr}
\begin{syntax}
\cs{IntDecr} \meta{integer}
\end{syntax}
Decreases the value stored in \meta{integer} by $1$.
For example
\begin{demohigh}
\IntSet \lTmpaInt {5}
\IntDecr \lTmpaInt
\IntUse \lTmpaInt
\end{demohigh}
\end{function}

\begin{function}{\IntAdd}
\begin{syntax}
\cs{IntAdd} \meta{integer} \Arg{integer expression}
\end{syntax}
Adds the result of the \meta{integer expression} to the current
content of the \meta{integer}. For example
\begin{demohigh}
\IntSet \lTmpaInt {5}
\IntAdd \lTmpaInt {2}
\IntUse \lTmpaInt
\end{demohigh}
\end{function}

\begin{function}{\IntSub}
\begin{syntax}
\cs{IntSub} \meta{integer} \Arg{integer expression}
\end{syntax}
Subtracts the result of the \meta{integer expression} from the
current content of the \meta{integer}. For example
\begin{demohigh}
\IntSet \lTmpaInt {5}
\IntSub \lTmpaInt {3}
\IntUse \lTmpaInt
\end{demohigh}
\end{function}

\section{Integer Step Functions}

%\begin{function}{\IntStepFunction}
%\begin{syntax}
%\cs{IntStepFunction} \Arg{initial value} \Arg{step} \Arg{final value} \meta{function}
%\end{syntax}
%This function first evaluates the \meta{initial value}, \meta{step}
%and \meta{final value}, all of which should be integer expressions.
%The \meta{function} is then placed in front of each \meta{value}
%from the \meta{initial value} to the \meta{final value} in turn
%(using \meta{step} between each \meta{value}).  The \meta{step} must
%be non-zero.  If the \meta{step} is positive, the loop stops when
%the \meta{value} becomes larger than the \meta{final value}.  If the
%\meta{step} is negative, the loop stops when the \meta{value}
%becomes smaller than the \meta{final value}.  The \meta{function}
%should absorb one numerical argument. For example
%\begin{verbatim}
%\cs_set:Npn \my_func:n #1 { [I~saw~#1] \quad }
%\int_step_function:nnnN { 1 } { 1 } { 5 } \my_func:n
%\end{verbatim}
%would print
%\begin{quote}
%[I saw 1] \quad
%[I saw 2] \quad
%[I saw 3] \quad
%[I saw 4] \quad
%[I saw 5] \quad
%\end{quote}
%\end{function}

\begin{function}{\IntStepInline}
\begin{syntax}
\cs{IntStepInline} \Arg{initial value} \Arg{step} \Arg{final value} \Arg{code}
\end{syntax}
This function first evaluates the \meta{initial value}, \meta{step}
and \meta{final value}, all of which should be integer expressions.
Then for each \meta{value} from the \meta{initial value} to the
\meta{final value} in turn (using \meta{step} between each
\meta{value}), the \meta{code} is inserted into the input stream
with \verb|#1| replaced by the current \meta{value}. Thus the
\meta{code} should define a function of one argument~(\verb|#1|).
For example
\begin{codehigh}
\IgnoreSpacesOn
\TlClear \lTmpaTl
\IntStepInline {1} {3} {30} {
  \TlPutRight \lTmpaTl {[#1]}
}
\Result {\Value\lTmpaTl}
\IgnoreSpacesOff
\end{codehigh}
produces [1][4][7][10][13][16][19][22][25][28].
\end{function}

\begin{function}{\IntStepVariable}
\begin{syntax}
\cs{IntStepVariable} \Arg{initial value} \Arg{step} \Arg{final value} \meta{tl var} \Arg{code}
\end{syntax}
This function first evaluates the \meta{initial value}, \meta{step}
and \meta{final value}, all of which should be integer expressions.
Then for each \meta{value} from the \meta{initial value} to the
\meta{final value} in turn (using \meta{step} between each
\meta{value}), the \meta{code} is evaluated,
with the \meta{tl~var} defined as the current \meta{value}. Thus
the \meta{code} should make use of the \meta{tl~var}.
%For example
%\begin{demohigh}
%\IgnoreSpacesOn
%\TlClear\lTmpaTl
%\IntStepVariable{1}{3}{30}\lTmpiTl{
%  \TlPutRight\lTmpaTl{\Value\lTmpiTl}
%  \TlPutRight\lTmpaTl{~}
%}
%\Result{\Value\lTmpaTl}
%\IgnoreSpacesOff
%\end{demohigh}
\end{function}

\section{Integer Conditionals}

\begin{function}{\IntIfExist, \IntIfExistTF}
\begin{syntax}
\cs{IntIfExist} \meta{integer}
\cs{IntIfExistTF} \meta{integer} \Arg{true code} \Arg{false code}
\end{syntax}
Tests whether the \meta{integer} is currently defined.  This does not
check that the \meta{integer} really is an integer variable.
\end{function}

\begin{function}{\IntIfOdd,\IntIfOddTF}
\begin{syntax}
\cs{IntIfOdd} \Arg{integer expression}
\cs{IntIfOddTF} \Arg{integer expression} \Arg{true code} \Arg{false code}
\end{syntax}
This function first evaluates the \meta{integer expression}
as described for \cs{IntEval}.
It then evaluates if this is odd or even, as appropriate.
\end{function}

\begin{function}{\IntIfEven,\IntIfEvenTF}
\begin{syntax}
\cs{IntIfEven} \Arg{integer expression}
\cs{IntIfEvenTF} \Arg{integer expression} \Arg{true code} \Arg{false code}
\end{syntax}
This function first evaluates the \meta{integer expression}
as described for \cs{IntEval}.
It then evaluates if this is even or odd, as appropriate.
\end{function}

\begin{function}{\IntCompare,\IntCompareTF}
\begin{syntax}
\cs{IntCompare} \Arg{intexpr_1} \meta{relation} \Arg{intexpr_2}
\cs{IntCompareTF} \Arg{intexpr_1} \meta{relation} \Arg{intexpr_2} \Arg{true code} \Arg{false code}
\end{syntax}
This function first evaluates each of the \meta{integer expressions}
as described for \cs{IntEval}. The two results are then
compared using the \meta{relation}:\par
{\centering\begin{tabular}{ll}
Equal        & \texttt{=} \\
Greater than & \texttt{>} \\
Less than    & \texttt{<} \\
\end{tabular}\par}
For example
\begin{demohigh}
\IntCompareTF {2} > {1} {\Result{Greater}} {\Result{Less}}
\IntCompareTF {2} > {3} {\Result{Greater}} {\Result{Less}}
\end{demohigh}
\end{function}

\section{Integer Case Functions}

\begin{function}{\IntCase}
\begin{syntax}
\cs{IntCase} \Arg{test integer expression}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{intexpr case_1} \Arg{code case_1}
~ ~ ~ ~ \Arg{intexpr case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \Arg{intexpr case_n} \Arg{code case_n}
~ ~ \verb|}|
\end{syntax}
This function evaluates the \meta{test integer expression} and
compares this in turn to each of the
\meta{integer expression cases}. If the two are equal then the
associated \meta{code} is left in the input stream
and other cases are discarded.
\end{function}

\begin{function}{\IntCaseT}
\begin{syntax}
\cs{IntCaseT} \Arg{test integer expression}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{intexpr case_1} \Arg{code case_1}
~ ~ ~ ~ \Arg{intexpr case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \Arg{intexpr case_n} \Arg{code case_n}
~ ~ \verb|}|
~ ~ \Arg{true code}
\end{syntax}
This function evaluates the \meta{test integer expression} and
compares this in turn to each of the
\meta{integer expression cases}. If the two are equal then the
associated \meta{code} is left in the input stream
and other cases are discarded. If any of the
cases are matched, the \meta{true code} is also inserted into the
input stream (after the code for the appropriate case).
\end{function}

\begin{function}{\IntCaseF}
\begin{syntax}
\cs{IntCaseF} \Arg{test integer expression}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{intexpr case_1} \Arg{code case_1}
~ ~ ~ ~ \Arg{intexpr case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \Arg{intexpr case_n} \Arg{code case_n}
~ ~ \verb|}|
~ ~ \Arg{false code}
\end{syntax}
This function evaluates the \meta{test integer expression} and
compares this in turn to each of the
\meta{integer expression cases}. If the two are equal then the
associated \meta{code} is left in the input stream
and other cases are discarded. If none
match then the \meta{false code} is into the input stream 
(after the code for the appropriate case).
For example
\begin{demohigh}
\IgnoreSpacesOn
\IntCaseF { 2 * 5 }
  {
    { 5 }       { Small }
    { 4 + 6 }   { Medium }
    { -2 * 10 } { Negative }
  }
  { No idea! }
\IgnoreSpacesOff
\end{demohigh}
\end{function}

\begin{function}{\IntCaseTF}
\begin{syntax}
\cs{IntCaseTF} \Arg{test integer expression}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{intexpr case_1} \Arg{code case_1}
~ ~ ~ ~ \Arg{intexpr case_2} \Arg{code case_2}
~ ~ ~ ~ $\cdots$
~ ~ ~ ~ \Arg{intexpr case_n} \Arg{code case_n}
~ ~ \verb|}|
~ ~ \Arg{true code}
~ ~ \Arg{false code}
\end{syntax}
This function evaluates the \meta{test integer expression} and
compares this in turn to each of the
\meta{integer expression cases}. If the two are equal then the
associated \meta{code} is left in the input stream
and other cases are discarded. If any of the
cases are matched, the \meta{true code} is also inserted into the
input stream (after the code for the appropriate case), while if none
match then the \meta{false code} is inserted.
\end{function}

\chapter{Floating Point Numbers (\texttt{Fp})}

\section{Constant and Scratch Floating Points}

\begin{variable}{\cZeroFp, \cMinusZeroFp}
Zero, with either sign.
\end{variable}

\begin{variable}{\cOneFp}
One as an \texttt{fp}: useful for comparisons in some places.
\end{variable}

\begin{variable}{\cInfFp,\cMinusInfFp}
Infinity, with either sign. These can be input directly in a
floating point expression as \texttt{inf} and \texttt{-inf}.
\end{variable}

\begin{variable}{\cEFp}
The value of the base of the natural logarithm, $\mathrm{e} = \exp(1)$.
\end{variable}

\begin{variable}{\cPiFp}
The value of $\pi$.  This can be input directly in a floating point
expression as \texttt{pi}.
\end{variable}

\begin{variable}{\cOneDegreeFp}
The value of $1^{\circ}$ in radians. Multiply an angle given in
degrees by this value to obtain a result in radians.  Note that
trigonometric functions expecting an argument in radians or in
degrees are both available.  Within floating point expressions, this
can be accessed as \texttt{deg}.
\end{variable}

\begin{variable}{\lTmpaFp,\lTmpbFp,\lTmpcFp,\lTmpiFp,\lTmpjFp,\lTmpkFp}
Scratch floating point numbers for local assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\begin{variable}{\gTmpaFp,\gTmpbFp,\gTmpcFp,\gTmpiFp,\gTmpjFp,\gTmpkFp}
Scratch floating point numbers for global assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\section{Floating Point Expressions}

\begin{function}{\FpEval}
\begin{syntax}
\cs{FpEval} \Arg{floating point expression}
\end{syntax}
Evaluates the \meta{floating point expression} and returns the
result as a decimal number with no
exponent.  Leading or trailing zeros may be inserted to compensate
for the exponent.  Non-significant trailing zeros are trimmed, and
integers are expressed without a decimal separator.  The values
$\pm\infty$ and \nan{} trigger an \enquote{invalid operation}
exception.
For a tuple, each item is converted using \cs{FpEval} and they are combined as
\verb|(|\meta{fp_1}\verb*|, |\meta{fp_2}\verb*|, |\ldots{}\meta{fp_n}\verb|)|
if $n>1$ and \verb|(|\meta{fp_1}\verb|,)| or \verb|()| for fewer items.
%This function is identical to \cs{fp_to_decimal:n}.
For example
\begin{demohigh}
\FpEval {(1.2+3.4)*(5.6-7.8)/9}
\end{demohigh}
\end{function}

\begin{function}{\FpMathAdd}
\begin{syntax}
\cs{FpMathAdd} \Arg{fpexpr_1} \Arg{fpexpr_2}
\end{syntax}
Adds \Arg{fpexpr_1} and \Arg{fpexpr_2},
and returns the result. For example
\begin{demohigh}
\FpMathAdd {2.8} {3.7}
\FpMathAdd {3.8-1} {2.7+1}
\end{demohigh}
\end{function}

\begin{function}{\FpMathSub}
\begin{syntax}
\cs{FpMathSub} \Arg{fpexpr_1} \Arg{fpexpr_2}
\end{syntax}
Subtracts \Arg{fpexpr_2} from \Arg{fpexpr_1},
and returns the result. For example
\begin{demohigh}
\FpMathSub {2.8} {3.7}
\FpMathSub {3.8-1} {2.7+1}
\end{demohigh}
\end{function}

\begin{function}{\FpMathMult}
\begin{syntax}
\cs{FpMathMult} \Arg{fpexpr_1} \Arg{fpexpr_2}
\end{syntax}
Multiplies \Arg{fpexpr_1} by \Arg{fpexpr_2},
and returns the result. For example
\begin{demohigh}
\FpMathMult {2.8} {3.7}
\FpMathMult {3.8-1} {2.7+1}
\end{demohigh}
\end{function}

\begin{function}{\FpMathDiv}
\begin{syntax}
\cs{FpMathDiv} \Arg{fpexpr_1} \Arg{fpexpr_2}
\end{syntax}
Divides \Arg{fpexpr_1} by \Arg{fpexpr_2},
and returns the result. For example
\begin{demohigh}
\FpMathDiv {2.8} {3.7}
\FpMathDiv {3.8-1} {2.7+1}
\end{demohigh}
\end{function}

\begin{function}{\FpMathSign}
\begin{syntax}
\cs{FpMathSign} \Arg{fpexpr}
\end{syntax}
Evaluates the \meta{fpexpr} and returns the value
using \cs{FpEval}\verb|{sign(|\meta{result}\verb|)}|: $+1$ for positive
numbers and for $+\infty$, $-1$ for negative numbers and for
$-\infty$, $\pm 0$ for $\pm 0$.  If the operand is a tuple or is
\nan{}, then \enquote{invalid operation} occurs and the result
is $0$. For example
\begin{demohigh}
\FpMathSign {3.5}
\FpMathSign {-2.7}
\end{demohigh}
\end{function}

\begin{function}{\FpMathAbs}
\begin{syntax}
\cs{FpMathAbs} \Arg{floating point expression}
\end{syntax}
Evaluates the \meta{floating point expression} as described for
\cs{FpEval} and returns the absolute value.
If the argument is $\pm\infty$, \nan{} or a tuple,
\enquote{invalid operation} occurs.  Within floating point
expressions, \verb|abs()| can be used; it accepts $\pm\infty$ and \nan{}
as arguments.
\end{function}

\begin{function}{\FpMathMax,\FpMathMin}
\begin{syntax}
\cs{FpMathMax} \Arg{fp expression_1} \Arg{fp expression_2}
\cs{FpMathMin} \Arg{fp expression_1} \Arg{fp expression_2}
\end{syntax}
Evaluates the \meta{floating point expressions} as described for \cs{FpEval}
and returns the resulting larger (\texttt{max}) or smaller (\texttt{min}) value.
If the argument is a tuple, \enquote{invalid operation} occurs,
but no other case raises exceptions. Within floating point expressions,
\verb|max()| and \verb|min()| can be used.
\end{function}

\section{Creating and Using Floating Points}

\begin{function}{\FpNew}
\begin{syntax}
\cs{FpNew} \meta{fp var}
\end{syntax}
Creates a new \meta{fp var} or raises an error if the name is
already taken. The declaration is global. The \meta{fp~var} is
initially $+0$.
\end{function}

\begin{function}{\FpConst}
\begin{syntax}
\cs{FpConst} \meta{fp var} \Arg{floating point expression}
\end{syntax}
Creates a new constant \meta{fp var} or raises an error if the name
is already taken. The \meta{fp var} is set globally equal to
the result of evaluating the \meta{floating point expression}.
For example
\begin{demohigh}
\FpConst \cMyPiFp {3.1415926}
\FpUse \cMyPiFp
\end{demohigh}
\end{function}

\begin{function}{\FpUse}
\begin{syntax}
\cs{FpUse} \meta{fp var}
\end{syntax}
Recovers the value of the \meta{fp var} and returns the value as a
decimal number with no exponent.
%Leading or trailing zeros may be inserted to compensate for the
%exponent.  Non-significant trailing zeros are trimmed.  Integers are
%expressed without a decimal separator.  The values $\pm\infty$
%and \nan{} trigger an \enquote{invalid operation} exception.
%For a tuple, each item is converted using \cs{fp_to_decimal:n} and they are combined as
%|(|\meta{fp_1}\verb*|, |\meta{fp_2}\verb*|, |\ldots{}\meta{fp_n}|)|
%if $n>1$ and |(|\meta{fp_1}|,)| or |()| for fewer items.
%This function is identical to \cs{fp_to_decimal:N}.
\end{function}

\section{Viewing Floating Points}

\begin{function}{\FpLog}
\begin{syntax}
\cs{FpLog} \Arg{floating point expression}
\end{syntax}
Evaluates the \meta{floating point expression} and writes the
result in the log file.
\end{function}

\begin{function}{\FpVarLog}
\begin{syntax}
\cs{FpVarLog} \meta{fp var}
\end{syntax}
Writes the value of \meta{fp var} in the log file.
\end{function}

\begin{function}{\FpShow}
\begin{syntax}
\cs{FpShow} \Arg{floating point expression}
\end{syntax}
Evaluates the \meta{floating point expression} and displays the
result in the terminal.
\end{function}

\begin{function}{\FpVarShow}
\begin{syntax}
\cs{FpVarShow} \meta{fp var}
\end{syntax}
Displays the value of \meta{fp var} in the terminal.
\end{function}

\section{Setting Floating Point Variables}

\begin{function}{\FpSet}
\begin{syntax}
\cs{FpSet} \meta{fp var} \Arg{floating point expression}
\end{syntax}
Sets \meta{fp var} equal to the result of computing the
\meta{floating point expression}. For example
\begin{demohigh}
\FpSet \lTmpaFp {4/7}
\FpUse \lTmpaFp
\end{demohigh}
\end{function}

\begin{function}{\FpSetEq}
\begin{syntax}
\cs{FpSetEq} \meta{fp var_1} \meta{fp var_2}
\end{syntax}
Sets the floating point variable \meta{fp var_1} equal to the current
value of \meta{fp var_2}.
\end{function}

\begin{function}{\FpZero}
\begin{syntax}
\cs{FpZero} \meta{fp var}
\end{syntax}
Sets the \meta{fp var} to $+0$. For example
\begin{demohigh}
\FpSet \lTmpaFp {5.3}
\FpZero \lTmpaFp
\FpUse \lTmpaFp
\end{demohigh}
\end{function}

\begin{function}{\FpZeroNew}
\begin{syntax}
\cs{FpZeroNew} \meta{fp var}
\end{syntax}
Ensures that the \meta{fp var} exists globally
by applying \cs{FpNew} if necessary, then applies
\cs{FpZero} to leave the \meta{fp var} set to $+0$.
\end{function}

\begin{function}{\FpAdd}
\begin{syntax}
\cs{FpAdd} \meta{fp var} \Arg{floating point expression}
\end{syntax}
Adds the result of computing the \meta{floating point expression} to
the \meta{fp var}.
This also applies if \meta{fp var} and \meta{floating point
expression} evaluate to tuples of the same size. For example
\begin{demohigh}
\FpSet \lTmpaFp {5.3}
\FpAdd \lTmpaFp {2.11}
\FpUse \lTmpaFp
\end{demohigh}
\end{function}

\begin{function}{\FpSub}
\begin{syntax}
\cs{FpSub} \meta{fp var} \Arg{floating point expression}
\end{syntax}
Subtracts the result of computing the \meta{floating point
expression} from the \meta{fp var}.
This also applies if \meta{fp var} and \meta{floating point
expression} evaluate to tuples of the same size. For example
\begin{demohigh}
\FpSet \lTmpaFp {5.3}
\FpSub \lTmpaFp {2.11}
\FpUse \lTmpaFp
\end{demohigh}
\end{function}

\section{Floating Point Step Functions}

%\begin{function}{\FpStepFunction}
%\begin{syntax}
%\cs{FpStepFunction} \Arg{initial value} \Arg{step} \Arg{final value} \meta{function}
%\end{syntax}
%This function first evaluates the \meta{initial value}, \meta{step}
%and \meta{final value}, each of which should be a floating point
%expression evaluating to a floating point number, not a tuple.
%The \meta{function} is then placed in front of each \meta{value}
%from the \meta{initial value} to the \meta{final value} in turn
%(using \meta{step} between each \meta{value}).  The \meta{step} must
%be non-zero.  If the \meta{step} is positive, the loop stops when
%the \meta{value} becomes larger than the \meta{final value}.  If the
%\meta{step} is negative, the loop stops when the \meta{value}
%becomes smaller than the \meta{final value}. The \meta{function}
%should absorb one numerical argument. For example
%\begin{verbatim}
%\cs_set:Npn \my_func:n #1 { [I saw #1] \quad }
%\fp_step_function:nnnN { 1.0 } { 0.1 } { 1.5 } \my_func:n
%\end{verbatim}
%would print
%\begin{quote}
%[I saw 1.0] \quad
%[I saw 1.1] \quad
%[I saw 1.2] \quad
%[I saw 1.3] \quad
%[I saw 1.4] \quad
%[I saw 1.5] \quad
%\end{quote}
%\begin{texnote}
%Due to rounding, it may happen that adding the \meta{step} to the
%\meta{value} does not change the \meta{value}; such cases give an
%error, as they would otherwise lead to an infinite loop.
%\end{texnote}
%\end{function}

\begin{function}{\FpStepInline}
\begin{syntax}
\cs{FpStepInline} \Arg{initial value} \Arg{step} \Arg{final value} \Arg{code}
\end{syntax}
This function first evaluates the \meta{initial value}, \meta{step}
and \meta{final value}, all of which should be floating point
expressions evaluating to a floating point number, not a tuple.
Then for each \meta{value} from the \meta{initial value} to the
\meta{final value} in turn (using \meta{step} between each
\meta{value}), the \meta{code} is inserted into the input stream
with \verb|#1| replaced by the current \meta{value}. Thus the
\meta{code} should define a function of one argument (\verb|#1|).
For example
\begin{codehigh}
\IgnoreSpacesOn
\TlClear \lTmpaTl
\FpStepInline {1} {0.1} {1.5} {
  \TlPutRight \lTmpaTl {[#1]}
}
\Result {\Value\lTmpaTl}
\IgnoreSpacesOff
\end{codehigh}
produces [1][1.1][1.2][1.3][1.4][1.5].
\end{function}

\begin{function}{\FpStepVariable}
\begin{syntax}
\cs{FpStepVariable} \Arg{initial value} \Arg{step} \Arg{final value} \meta{tl var} \Arg{code}
\end{syntax}
This function first evaluates the \meta{initial value}, \meta{step}
and \meta{final value}, all of which should be floating point
expressions evaluating to a floating point number, not a tuple.
Then for each \meta{value} from the \meta{initial value} to the
\meta{final value} in turn (using \meta{step} between each
\meta{value}), the \meta{code} is inserted into the input stream,
with the \meta{tl var} defined as the current \meta{value}.  Thus
the \meta{code} should make use of the \meta{tl var}.
%For example
%\begin{demohigh}
%\IgnoreSpacesOn
%\TlClear\lTmpaTl
%\FpStepVariable{1}{0.1}{1.5}\lTmpiTl{
%  \TlPutRight\lTmpaTl{\Value\lTmpiTl}
%  \TlPutRight\lTmpaTl{~}
%}
%\Result{\Value\lTmpaTl}
%\IgnoreSpacesOff
%\end{demohigh}
\end{function}

\section{Float Point Conditionals}

\begin{function}{\FpIfExist,\FpIfExistTF}
\begin{syntax}
\cs{FpIfExist} \meta{fp var}
\cs{FpIfExistTF} \meta{fp var} \Arg{true code} \Arg{false code}
\end{syntax}
Tests whether the \meta{fp var} is currently defined.  This does not
check that the \meta{fp var} really is a floating point variable.
For example
\begin{demohigh}
\FpIfExistTF \lTmpaFp {\Result{Yes}} {\Result{No}}
\FpIfExistTF \lMyUndefinedFp {\Result{Yes}} {\Result{No}}
\end{demohigh}
\end{function}

\begin{function}{\FpCompare,\FpCompareTF}
\begin{syntax}
\cs{FpCompare} \Arg{fpexpr_1} \meta{relation} \Arg{fpexpr_2}
\cs{FpCompareTF} \Arg{fpexpr_1} \meta{relation} \Arg{fpexpr_2} \Arg{true code} \Arg{false code}
\end{syntax}
Compares the \meta{fpexpr_1} and the \meta{fpexpr_2}, and returns
\texttt{true} if the \meta{relation} is obeyed. For example
\begin{demohigh}
\FpCompareTF {1} > {0.9999} {\Result{Greater}} {\Result{Less}}
\FpCompareTF {1} > {1.0001} {\Result{Greater}} {\Result{Less}}
\end{demohigh}
Two floating points
$x$ and $y$ may obey four mutually exclusive relations:
$x<y$, $x=y$, $x>y$, or $x?y$ (\enquote{not ordered}).  The last
case occurs exactly if one or both operands is \nan{} or is a tuple,
unless they are equal tuples.  Note that a \nan{} is distinct from
any value, even another \nan{}, hence $x=x$ is not true for
a \nan{}.  To test if a value is \nan{}, compare it to an arbitrary
number with the \enquote{not ordered} relation.\par
%\begin{demohigh}
%\FpCompareTF{0/0}?{0}{\Result{Is~a~Nan}}{\Result{Isn't~a~NaN}}
%\end{demohigh}
Tuples are equal if they have the same number of items and items
compare equal (in particular there must be no \nan{}).
At present any other comparison with tuples yields \verb|?| (not ordered).
This is experimental.
\end{function}

\chapter{Dimensions (\texttt{Dim})}

\section{Constant and Scratch Dimensions}

\begin{variable}{\cMaxDim}
The maximum value that can be stored as a dimension.  This can also
be used as a component of a skip.
\end{variable}

\begin{variable}{\cZeroDim}
A zero length as a dimension.  This can also be used as a component
of a skip.
\end{variable}

\begin{variable}{\lTmpaDim,\lTmpbDim,\lTmpcDim,\lTmpiDim,\lTmpjDim,\lTmpkDim}
Scratch dimensions for local assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\begin{variable}{\gTmpaDim,\gTmpbDim,\gTmpcDim,\gTmpiDim,\gTmpjDim,\gTmpkDim}
Scratch dimensions for global assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\section{Dimension Expressions}

\begin{function}{\DimEval}
\begin{syntax}
\cs{DimEval} \Arg{dimension expression}
\end{syntax}
Evaluates the \meta{dimension expression}, expanding any
dimensions and token list variables within the \meta{expression}
to their content (without requiring \cs{DimUse}/\cs{TlUse})
and applying the standard mathematical rules. The result of the
calculation is returned as a \meta{dimension denotation}.
%This is expressed in points (\texttt{pt}), and requires suitable
%termination if used in a \TeX{}-style assignment as it is \emph{not}
%an \meta{internal dimension}.
For example
\begin{demohigh}
\DimEval {(1.2pt+3.4pt)/9}
\end{demohigh}
%\DimEval{(1.2pt+3.4pt)*(5.6-7.8)/9}
\end{function}

\begin{function}{\DimMathAdd}
\begin{syntax}
\cs{DimMathAdd} \Arg{dimexpr_1} \Arg{dimexpr_2}
\end{syntax}
Adds \Arg{dimexpr_1} and \Arg{dimexpr_2},
and returns the result. For example
\begin{demohigh}
\DimMathAdd {2.8pt} {3.7pt}
\DimMathAdd {3.8pt-1pt} {2.7pt+1pt}
\end{demohigh}
\end{function}

\begin{function}{\DimMathSub}
\begin{syntax}
\cs{DimMathSub} \Arg{dimexpr_1} \Arg{dimexpr_2}
\end{syntax}
Subtracts \Arg{dimexpr_2} from \Arg{dimexpr_1},
and returns the result. For example
\begin{demohigh}
\DimMathSub {2.8pt} {3.7pt}
\DimMathSub {3.8pt-1pt} {2.7pt+1pt}
\end{demohigh}
\end{function}

\begin{function}{\DimMathRatio}
\begin{syntax}
\cs{DimMathRatio} \Arg{dimexpr_1} \Arg{dimexpr_2}
\end{syntax}
Parses the two \meta{dimension expressions},
then calculates the ratio of the two and returns it.
The result is a ratio expression
between two integers, with all distances converted to scaled points.
For example
\begin{demohigh}
\DimMathRatio {5pt} {10pt}
\end{demohigh}
The returned value is suitable for use inside a \meta{dimension expression}
such as
\begin{codehigh}
\DimSet \lTmpaDim {10pt*\DimMathRatio{5pt}{10pt}}
\end{codehigh}
\end{function}

\begin{function}{\DimMathSign}
\begin{syntax}
\cs{DimMathSign} \Arg{dimexpr}
\end{syntax}
Evaluates the \meta{dimexpr} then returns $1$ or $0$ or $-1$
according to the sign of the result. For example
\begin{demohigh}
\DimMathSign {3.5pt}
\DimMathSign {-2.7pt}
\end{demohigh}
\end{function}

\begin{function}{\DimMathAbs}
\begin{syntax}
\cs{DimMathAbs} \Arg{dimexpr}
\end{syntax}
Converts the \meta{dimexpr} to its absolute value,
returning the result as a \meta{dimension denotation}.
For example
\begin{demohigh}
\DimMathAbs {3.5pt}
\DimMathAbs {-2.7pt}
\end{demohigh}
\end{function}

\begin{function}{\DimMathMax,\DimMathMin}
\begin{syntax}
\cs{DimMathMax} \Arg{dimexpr_1} \Arg{dimexpr_2}
\cs{DimMathMin} \Arg{dimexpr_1} \Arg{dimexpr_2}
\end{syntax}
Evaluates the two \meta{dimension expressions} and returns either the
maximum or minimum value as appropriate as a \meta{dimension denotation}.
For example
\begin{demohigh}
\DimMathMax {3.5pt} {-2.7pt}
\DimMathMin {3.5pt} {-2.7pt}
\end{demohigh}
\end{function}

\section{Creating and Using Dimensions}

\begin{function}{\DimNew}
\begin{syntax}
\cs{DimNew} \meta{dimension}
\end{syntax}
Creates a new \meta{dimension} or raises an error if the name is
already taken. The declaration is global. The \meta{dimension}
is initially equal to $0$\,pt.
\end{function}

\begin{function}{\DimConst}
\begin{syntax}
\cs{DimConst} \meta{dimension} \Arg{dimension expression}
\end{syntax}
Creates a new constant \meta{dimension} or raises an error if the
name is already taken. The value of the \meta{dimension} is set
globally to the \meta{dimension expression}. For example
\begin{demohigh}
\DimConst \cFooSomeDim {1cm}
\DimUse \cFooSomeDim
\end{demohigh}
\end{function}

\begin{function}{\DimUse}
\begin{syntax}
\cs{DimUse} \meta{dimension}
\end{syntax}
Recovers the content of a \meta{dimension} and returns the value.
An error is raised if the variable does not exist or if it is invalid.
\end{function}

\section{Viewing Dimensions}

\begin{function}{\DimLog}
\begin{syntax}
\cs{DimLog} \Arg{dimension expression}
\end{syntax}
Writes the result of evaluating the \meta{dimension expression}
in the log file. For example
\begin{codehigh}
\DimLog {\lFooSomeDim+1cm}
\end{codehigh}
\end{function}

\begin{function}{\DimVarLog}
\begin{syntax}
\cs{DimVarLog} \meta{dimension}
\end{syntax}
Writes the value of the \meta{dimension} in the log file. For example
\begin{codehigh}
\DimVarLog \lFooSomeDim
\end{codehigh}
\end{function}

\begin{function}{\DimShow}
\begin{syntax}
\cs{DimShow} \Arg{dimension expression}
\end{syntax}
Displays the result of evaluating the \meta{dimension expression}
on the terminal. For example
\begin{codehigh}
\DimShow {\lFooSomeDim+1cm}
\end{codehigh}
\end{function}

\begin{function}{\DimVarShow}
\begin{syntax}
\cs{DimVarShow} \meta{dimension}
\end{syntax}
Displays the value of the \meta{dimension} on the terminal. For example
\begin{codehigh}
\DimVarShow \lFooSomeDim
\end{codehigh}
\end{function}

\section{Setting Dimension Variables}

\begin{function}{\DimSet}
\begin{syntax}
\cs{DimSet} \meta{dimension} \Arg{dimension expression}
\end{syntax}
Sets \meta{dimension} to the value of \meta{dimension expression}, which
must evaluate to a length with units.
\end{function}

\begin{function}{\DimSetEq}
\begin{syntax}
\cs{DimSetEq} \meta{dimension_1} \meta{dimension_2}
\end{syntax}
Sets the content of \meta{dimension_1} equal to that of
\meta{dimension_2}. For example
\begin{demohigh}
\DimSet \lTmpaDim {10pt}
\DimSetEq \lTmpbDim \lTmpaDim
\DimUse \lTmpbDim
\end{demohigh}
\end{function}

\begin{function}{\DimZero}
\begin{syntax}
\cs{DimZero} \meta{dimension}
\end{syntax}
Sets \meta{dimension} to $0$\,pt. For example
\begin{demohigh}
\DimSet \lTmpaDim {1em}
\DimZero \lTmpaDim
\DimUse \lTmpaDim
\end{demohigh}
\end{function}

\begin{function}{\DimZeroNew}
\begin{syntax}
\cs{DimZeroNew} \meta{dimension}
\end{syntax}
Ensures that the \meta{dimension} exists globally by applying
\cs{DimNew} if necessary, then applies
\cs{DimZero} to set the \meta{dimension} to zero. For example
\begin{demohigh}
\DimZeroNew \lFooSomeDim
\DimUse \lFooSomeDim
\end{demohigh}
\end{function}

\begin{function}{\DimAdd}
\begin{syntax}
\cs{DimAdd} \meta{dimension} \Arg{dimension expression}
\end{syntax}
Adds the result of the \meta{dimension expression} to the current
content of the \meta{dimension}. For example
\begin{demohigh}
\DimSet \lTmpaDim {5.3pt}
\DimAdd \lTmpaDim {2.11pt}
\DimUse \lTmpaDim
\end{demohigh}
\end{function}

\begin{function}{\DimSub}
\begin{syntax}
\cs{DimSub} \meta{dimension} \Arg{dimension expression}
\end{syntax}
Subtracts the result of the \meta{dimension expression} from the
current content of the \meta{dimension}. For example
\begin{demohigh}
\DimSet \lTmpaDim {5.3pt}
\DimSub \lTmpaDim {2.11pt}
\DimUse \lTmpaDim
\end{demohigh}
\end{function}

\section{Dimension Step Functions}

%\begin{function}{\DimStepFunction}
%\begin{syntax}
%\cs{DimStepFunction} \Arg{initial value} \Arg{step} \Arg{final value} \meta{function}
%\end{syntax}
%This function first evaluates the \meta{initial value}, \meta{step}
%and \meta{final value}, all of which should be dimension expressions.
%The \meta{function} is then placed in front of each \meta{value}
%from the \meta{initial value} to the \meta{final value} in turn
%(using \meta{step} between each \meta{value}).  The \meta{step} must
%be non-zero.  If the \meta{step} is positive, the loop stops when
%the \meta{value} becomes larger than the \meta{final value}.  If the
%\meta{step} is negative, the loop stops when the \meta{value}
%becomes smaller than the \meta{final value}.  The \meta{function}
%should absorb one argument.
%\end{function}

\begin{function}{\DimStepInline}
\begin{syntax}
\cs{DimStepInline} \Arg{initial value} \Arg{step} \Arg{final value} \Arg{code}
\end{syntax}
This function first evaluates the \meta{initial value}, \meta{step}
and \meta{final value}, all of which should be dimension expressions.
Then for each \meta{value} from the \meta{initial value} to the
\meta{final value} in turn (using \meta{step} between each
\meta{value}), the \meta{code} is inserted into the input stream
with \verb|#1| replaced by the current \meta{value}.  Thus the
\meta{code} should define a function of one argument (\verb|#1|).
For example
\begin{codehigh}
\IgnoreSpacesOn
\TlClear \lTmpaTl
\DimStepInline {1pt} {0.1pt} {1.5pt} {
  \TlPutRight \lTmpaTl {[#1]}
}
\Result {\Value\lTmpaTl}
\IgnoreSpacesOff
\end{codehigh}
produces [1.0pt][1.1pt][1.20001pt][1.30002pt][1.40002pt].
\end{function}

\begin{function}{\DimStepVariable}
\begin{syntax}
\cs{DimStepVariable} \Arg{initial value} \Arg{step} \Arg{final value} \meta{tl var} \Arg{code}
\end{syntax}
This function first evaluates the \meta{initial value}, \meta{step}
and \meta{final value}, all of which should be dimension expressions.
Then for each \meta{value} from the \meta{initial value} to the
\meta{final value} in turn (using \meta{step} between each
\meta{value}), the \meta{code} is inserted into the input stream,
with the \meta{tl var} defined as the current \meta{value}.  Thus
the \meta{code} should make use of the \meta{tl var}.
%For example
%\begin{demohigh}
%\IgnoreSpacesOn
%\TlClear\lTmpaTl
%\DimStepVariable{1pt}{0.1pt}{1.5pt}\lTmpiTl{
%  \TlPutRight\lTmpaTl{\Value\lTmpiTl}
%  \TlPutRight\lTmpaTl{~}
%}
%\Result{\Value\lTmpaTl}
%\IgnoreSpacesOff
%\end{demohigh}
\end{function}

\section{Dimension Conditionals}

\begin{function}{\DimIfExist,\DimIfExistTF}
\begin{syntax}
\cs{DimIfExist} \meta{dimension}
\cs{DimIfExistTF} \meta{dimension} \Arg{true code} \Arg{false code}
\end{syntax}
Tests whether the \meta{dimension} is currently defined.  This does
not check that the \meta{dimension} really is a dimension variable.
For example
\begin{demohigh}
\DimIfExistTF \lTmpaDim {\Result{Yes}} {\Result{No}}
\DimIfExistTF \lFooUndefinedDim {\Result{Yes}} {\Result{No}}
\end{demohigh}
\end{function}

\begin{function}{\DimCompare,\DimCompareTF}
\begin{syntax}
\cs{DimCompare} \Arg{dimexpr_1} \meta{relation} \Arg{dimexpr_2}
\cs{DimCompareTF} \Arg{dimexpr_1} \meta{relation} \Arg{dimexpr_2} \Arg{true code} \Arg{false code}
\end{syntax}
This function first evaluates each of the \meta{dimension expressions}
as described for \cs{DimEval}. The two results are then
compared using the \meta{relation}:\par
{\centering
\begin{tabular}{ll}
Equal        & \verb|=| \\
Greater than & \verb|>| \\
Less than    & \verb|<| \\
\end{tabular}\par}
For example
\begin{demohigh}
\DimCompareTF {1pt} > {0.9999pt} {\Result{Greater}} {\Result{Less}}
\DimCompareTF {1pt} > {1.0001pt} {\Result{Greater}} {\Result{Less}}
\end{demohigh}
\end{function}

\section{Dimension Case Functions}

\begin{function}{\DimCase}
\begin{syntax}
\cs{DimCase} \Arg{test dimension expression}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{dimexpr case_1} \Arg{code case_1}
~ ~ ~ ~  \Arg{dimexpr case_2} \Arg{code case_2}
~ ~ ~ ~ \ldots
~ ~ ~ ~ \Arg{dimexpr case_n} \Arg{code case_n}
~ ~ \verb|}|
\end{syntax}
This function evaluates the \meta{test dimension expression} and
compares this in turn to each of the
\meta{dimension expression cases}. If the two are equal then the
associated \meta{code} is left in the input stream
and other cases are discarded.
\end{function}

\begin{function}{\DimCaseT}
\begin{syntax}
\cs{DimCaseT} \Arg{test dimension expression}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{dimexpr case_1} \Arg{code case_1}
~ ~ ~ ~  \Arg{dimexpr case_2} \Arg{code case_2}
~ ~ ~ ~ \ldots
~ ~ ~ ~ \Arg{dimexpr case_n} \Arg{code case_n}
~ ~ \verb|}|
~ ~ \Arg{true code}
\end{syntax}
This function evaluates the \meta{test dimension expression} and
compares this in turn to each of the
\meta{dimension expression cases}. If the two are equal then the
associated \meta{code} is left in the input stream
and other cases are discarded. If any of the
cases are matched, the \meta{true code} is also inserted into the
input stream (after the code for the appropriate case).
\end{function}

\begin{function}{\DimCaseF}
\begin{syntax}
\cs{DimCaseF} \Arg{test dimension expression}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{dimexpr case_1} \Arg{code case_1}
~ ~ ~ ~  \Arg{dimexpr case_2} \Arg{code case_2}
~ ~ ~ ~ \ldots
~ ~ ~ ~ \Arg{dimexpr case_n} \Arg{code case_n}
~ ~ \verb|}|
~ ~ \Arg{false code}
\end{syntax}
This function evaluates the \meta{test dimension expression} and
compares this in turn to each of the
\meta{dimension expression cases}. If the two are equal then the
associated \meta{code} is left in the input stream
and other cases are discarded. If none of the cases
match then the \meta{false code} is inserted.
For example
\begin{demohigh}
\IgnoreSpacesOn
\DimSet \lTmpaDim {5pt}
\DimCaseF {2\lTmpaDim} {
  {5pt}     {\Result{Small}}
  {4pt+6pt} {\Result{Medium}}
  {-10pt}   {\Result{Negative}}
}{
  \Result {No Match}
}
\IgnoreSpacesOff
\end{demohigh}
\end{function}

\begin{function}{\DimCaseTF}
\begin{syntax}
\cs{DimCaseTF} \Arg{test dimension expression}
~ ~ \verb|{|
~ ~ ~ ~ \Arg{dimexpr case_1} \Arg{code case_1}
~ ~ ~ ~  \Arg{dimexpr case_2} \Arg{code case_2}
~ ~ ~ ~ \ldots
~ ~ ~ ~ \Arg{dimexpr case_n} \Arg{code case_n}
~ ~ \verb|}|
~ ~ \Arg{true code}
~ ~ \Arg{false code}
\end{syntax}
This function evaluates the \meta{test dimension expression} and
compares this in turn to each of the
\meta{dimension expression cases}. If the two are equal then the
associated \meta{code} is left in the input stream
and other cases are discarded. If any of the
cases are matched, the \meta{true code} is also inserted into the
input stream (after the code for the appropriate case), while if none
match then the \meta{false code} is inserted.
%For example
%\begin{demohigh}
%\IgnoreSpacesOn
%\DimSet\lTmpaDim{5pt}
%\DimCaseTF{2\lTmpaDim}{
%  {5pt}     {\Result{Small}}
%  {4pt+6pt} {\Result{Medium}}
%  {-10pt}   {\Result{Negative}}
%}{
%  \Result{[Some Match]}
%}{
%  \Result{[No Match]}
%}
%\IgnoreSpacesOff
%\end{demohigh}
\end{function}

\chapter{Comma Separated Lists (\texttt{Clist})}

\section{Constant and Scratch Comma Lists}

\begin{variable}{\cEmptyClist}
Constant that is always empty.
\end{variable}

\begin{variable}{\lTmpaClist,\lTmpbClist,\lTmpcClist,\lTmpiClist,\lTmpjClist,\lTmpkClist}
Scratch comma lists for local assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\begin{variable}{\gTmpaClist,\gTmpbClist,\gTmpcClist,\gTmpiClist,\gTmpjClist,\gTmpkClist}
Scratch comma lists for global assignment. These are never used by
the \verb!functional! package, and so are safe for use with any
function. However, they may be overwritten by other
code and so should only be used for short-term storage.
\end{variable}

\section{Creating and Using Comma Lists}

\begin{function}{\ClistNew}
\begin{syntax}
\cs{ClistNew} \meta{comma list}
\end{syntax}
Creates a new \meta{comma list} or raises an error if the name is
already taken. The declaration is global. The \meta{comma list}
initially contains no items.
\end{function}
 
\begin{function}{\ClistConst}
\begin{syntax}
\cs{ClistConst} \meta{clist var} \Arg{comma list}
\end{syntax}
Creates a new constant \meta{clist var} or raises an error
if the name is already taken. The value of the
\meta{clist var} is set globally to the
\meta{comma list}.
\end{function}

\begin{function}{\ClistVarJoin}
\begin{syntax}
\cs{ClistVarJoin} \meta{clist var} \Arg{separator}
\end{syntax}
Returns the contents of the \meta{clist var},
with the \meta{separator} between the items.
%If the comma list has a single item, it is placed in the input stream,
%and a comma list with no items produces no output.
%An error is raised if the variable does not exist or if it is invalid.
For example,
\begin{demohigh}
\ClistSet \lTmpaClist { a , b , , c , {de} , f }
\ClistVarJoin \lTmpaClist { and }
\end{demohigh}
%\begin{texnote}
%The result is returned within the \tn{unexpanded}
%primitive (\cs{exp_not:n}), which means that the \meta{items}
%do not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}

\begin{function}{\ClistVarJoinExtended}
\begin{syntax}
\cs{ClistVarJoinExtended} \meta{clist var} \Arg{separator between two} \Arg{separator between more than two} \Arg{separator between final two}
\end{syntax}
Returns the contents of the \meta{clist var},
with the appropriate \meta{separator} between the items. Namely, if
the comma list has more than two items, the \meta{separator between
more than two} is placed between each pair of items except the
last, for which the \meta{separator between final two} is used.  If
the comma list has exactly two items, then they are joined with
the \meta{separator between two} and returns.
%If the comma list has a single item, it is placed in the input stream,
%and a comma list with no items produces no output.
%An error is raised if the variable does not exist or if it is invalid.
For example,
\begin{demohigh}
\ClistSet \lTmpaClist { a , b , , c , {de} , f }
\ClistVarJoinExtended \lTmpaClist { and } {, } {, and }
\end{demohigh}
The first separator argument is not used in this case
because the comma list has more than $2$ items.
%\begin{texnote}
%The result is returned within the \tn{unexpanded}
%primitive (\cs{exp_not:n}), which means that the \meta{items}
%do not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}

\begin{function}{\ClistJoin,\ClistJoinExtended}
\begin{syntax}
\cs{ClistJoin} \meta{comma list} \Arg{separator}
\cs{ClistJoinExtended} \meta{comma list} \Arg{separator between two} \Arg{separator between more than two} \Arg{separator between final two}
\end{syntax}
Returns the contents of the \meta{comma list},
with the appropriate \meta{separator} between the items. As for
\cs{ClistSet}, blank items are omitted, spaces are removed from
both sides of each item, then a set of braces is removed if the
resulting space-trimmed item is braced.  The \meta{separators} are
then inserted in the same way as for \cs{ClistVarJoin} and
\cs{ClistVarJoinExtended}, respectively.
%\begin{texnote}
%The result is returned within the \tn{unexpanded}
%primitive (\cs{exp_not:n}), which means that the \meta{items}
%do not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}

\section{Viewing Comma Lists}

\begin{function}{\ClistLog}
\begin{syntax}
\cs{ClistLog} \Arg{tokens}
\end{syntax}
Writes the entries in the comma list in the log file. See also
\cs{ClistShow} which displays the result in the terminal.
\end{function}

\begin{function}{\ClistVarLog}
\begin{syntax}
\cs{ClistVarLog} \meta{comma list}
\end{syntax}
Writes the entries in the \meta{comma list} in the log file. See
also \cs{ClistVarShow} which displays the result in the terminal.
\end{function}

\begin{function}{\ClistShow}
\begin{syntax}
\cs{ClistShow} \Arg{tokens}
\end{syntax}
Displays the entries in the comma list in the terminal.
\end{function}

\begin{function}{\ClistVarShow}
\begin{syntax}
\cs{ClistVarShow} \meta{comma list}
\end{syntax}
Displays the entries in the \meta{comma list} in the terminal.
\end{function}

\section{Setting Comma Lists}

\begin{function}{\ClistSet}
\begin{syntax}
\cs{ClistSet} \meta{comma list} \verb|{|\meta{item_1},\ldots{},\meta{item_n}\verb|}|
\end{syntax}
Sets \meta{comma list} to contain the \meta{items},
removing any previous content from the variable.
Blank items are omitted, spaces are removed from both sides of each
item, then a set of braces is removed if the resulting space-trimmed
item is braced.
To store some \meta{tokens} as a single \meta{item} even if the
\meta{tokens} contain commas or spaces, add a set of braces:
\cs{ClistSet} \meta{comma list} \verb|{| \Arg{tokens} \verb|}|.
\end{function}

\begin{function}{\ClistSetEq}
\begin{syntax}
\cs{ClistSetEq} \meta{comma list_1} \meta{comma list_2}
\end{syntax}
Sets the content of \meta{comma list_1} equal to that of
\meta{comma list_2}.  To set a token list variable equal to a comma
list variable, use \cs{TlSetEq}.  Conversely, setting a comma
list variable to a token list is unadvisable unless one checks
space-trimming and related issues.
\end{function}

%\begin{function}{\ClistSetFromSeq}
%\begin{syntax}
%\cs{ClistSetFromSeq} \meta{comma list} \meta{sequence}
%\end{syntax}
%Converts the data in the \meta{sequence} into a \meta{comma list}:
%the original \meta{sequence} is unchanged.
%Items which contain either spaces or commas are surrounded by braces.
%\end{function}

\begin{function}{\ClistClear}
\begin{syntax}
\cs{ClistClear} \meta{comma list}
\end{syntax}
Clears all items from the \meta{comma list}.
\end{function}

\begin{function}{\ClistClearNew}
\begin{syntax}
\cs{ClistClearNew} \meta{comma list}
\end{syntax}
Ensures that the \meta{comma list} exists globally by applying
\cs{ClistNew} if necessary, then applies \cs{ClistClear} to leave
the list empty.
\end{function}

\begin{function}{\ClistConcat}
\begin{syntax}
\cs{ClistConcat} \meta{comma list_1} \meta{comma list_2} \meta{comma list_3}
\end{syntax}
Concatenates the content of \meta{comma list_2} and \meta{comma list_3}
together and saves the result in \meta{comma list_1}. The items in
\meta{comma list_2} are placed at the left side of the new comma list.
\end{function}

\begin{function}{\ClistPutLeft}
\begin{syntax}
\cs{ClistPutLeft} \meta{comma list} \verb|{|\meta{item_1},\ldots{},\meta{item_n}\verb|}|
\end{syntax}
Appends the \meta{items} to the left of the \meta{comma list}.
Blank items are omitted, spaces are removed from both sides of each
item, then a set of braces is removed if the resulting space-trimmed
item is braced.
To append some \meta{tokens} as a single \meta{item} even if the
\meta{tokens} contain commas or spaces, add a set of braces:
\cs{ClistPutLeft} \meta{comma list} \verb|{| \Arg{tokens} \verb|}|.
\end{function}
 
\begin{function}{\ClistPutRight}
\begin{syntax}
\cs{ClistPutRight} \meta{comma list} \verb|{|\meta{item_1},\ldots{},\meta{item_n}\verb|}|
\end{syntax}
Appends the \meta{items} to the right of the \meta{comma list}.
Blank items are omitted, spaces are removed from both sides of each
item, then a set of braces is removed if the resulting space-trimmed
item is braced.
To append some \meta{tokens} as a single \meta{item} even if the
\meta{tokens} contain commas or spaces, add a set of braces:
\cs{ClistPutRight} \meta{comma list} \verb|{| \Arg{tokens} \verb|}|.
\end{function}
 
\section{Modifying Comma Lists}
 
While comma lists are normally used as ordered lists, it may be
necessary to modify the content. The functions here may be used
to update comma lists, while retaining the order of the unaffected
entries.
 
\begin{function}{\ClistRemoveDuplicates}
\begin{syntax}
\cs{ClistRemoveDuplicates} \meta{comma list}
\end{syntax}
Removes duplicate items from the \meta{comma list}, leaving the
left most copy of each item in the \meta{comma list}.  The \meta{item}
comparison takes place on a token basis, as for \cs{TlIfEqTF}.
\begin{texnote}
This function iterates through every item in the \meta{comma list} and
does a comparison with the \meta{items} already checked. It is therefore
relatively slow with large comma lists.
Furthermore, it may fail if any of the items in the
\meta{comma list} contains \verb|{|, \verb|}|, or \verb|#|
(assuming the usual \TeX{} category codes apply).
\end{texnote}
\end{function}
 
\begin{function}{\ClistRemoveAll}
\begin{syntax}
\cs{ClistRemoveAll} \meta{comma list} \Arg{item}
\end{syntax}
Removes every occurrence of \meta{item} from the \meta{comma list}.
The \meta{item} comparison takes place on a token basis, as for
\cs{TlIfEqTF}.
\begin{texnote}
The function may fail if the \meta{item} contains \verb|{|, \verb|}|, or \verb|#|
(assuming the usual \TeX{} category codes apply).
\end{texnote}
\end{function}
 
\begin{function}{\ClistReverse}
\begin{syntax}
\cs{ClistReverse} \meta{comma list}
\end{syntax}
Reverses the order of items stored in the \meta{comma list}.
\end{function}
 
%\begin{function}{\ClistSort}
%\begin{syntax}
%\cs{ClistSort} \meta{clist var} \Arg{comparison code}
%\end{syntax}
%Sorts the items in the \meta{clist var} according to the
%\meta{comparison code}, and assigns the result to
%\meta{clist var}. The details of sorting comparison are
%described in Section \ref{sec:l3sort:mech}.
%\end{function}

\section{Working with the Contents of Comma Lists}

\begin{function}{\ClistCount,\ClistVarCount}
\begin{syntax}
\cs{ClistCount} \Arg{comma list}
\cs{ClistVarCount} \meta{comma list}
\end{syntax}
Returns the number of items in the \meta{comma list}
as an \meta{integer denotation}. The total number of items
in a \meta{comma list} includes those which are duplicates,
\emph{i.e.} every item in a \meta{comma list} is counted.
\end{function}

\begin{function}{\ClistItem}
\begin{syntax}
\cs{ClistItem} \Arg{comma list} \Arg{integer expression}
\end{syntax}
Indexing items in the \meta{comma list} from $1$ at the top (left), this
function evaluates the \meta{integer expression} and returns the
appropriate item from the comma list. If the
\meta{integer expression} is negative, indexing occurs from the
bottom (right) of the comma list. When the \meta{integer expression}
is larger than the number of items in the \meta{comma list} (as
calculated by \cs{ClistCount}) then the function returns nothing.
%\begin{texnote}
%The result is returned within the \tn{unexpanded}
%primitive (\cs{exp_not:n}), which means that the \meta{item}
%does not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}
 
\begin{function}{\ClistVarItem}
\begin{syntax}
\cs{ClistVarItem} \meta{comma list} \Arg{integer expression}
\end{syntax}
Indexing items in the \meta{comma list} from $1$ at the top (left), this
function evaluates the \meta{integer expression} and returns the
appropriate item from the comma list. If the
\meta{integer expression} is negative, indexing occurs from the
bottom (right) of the comma list. When the \meta{integer expression}
is larger than the number of items in the \meta{comma list} (as
calculated by \cs{ClistVarCount}) then the function returns nothing.
%\begin{texnote}
%The result is returned within the \tn{unexpanded}
%primitive (\cs{exp_not:n}), which means that the \meta{item}
%does not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}
 
\begin{function}{\ClistRandItem,\ClistVarRandItem}
\begin{syntax}
\cs{ClistRandItem} \Arg{comma list}
\cs{ClistVarRandItem} \meta{clist var}
\end{syntax}
Selects a pseudo-random item of the \meta{comma list}.
If the \meta{comma list} has no item, the result is empty.
%\begin{texnote}
%The result is returned within the \tn{unexpanded}
%primitive (\cs{exp_not:n}), which means that the \meta{item}
%does not expand further when appearing in an \texttt{x}-type
%or \texttt{e}-type argument expansion.
%\end{texnote}
\end{function}

\section{Comma Lists as Stacks}
 
Comma lists can be used as stacks, where data is pushed to and popped
from the top of the comma list. (The left of a comma list is the top, for
performance reasons.) The stack functions for comma lists are not
intended to be mixed with the general ordered data functions detailed
in the previous section: a comma list should either be used as an
ordered data type or as a stack, but not in both ways.
 
\begin{function}{\ClistGet,\ClistGetTF}
\begin{syntax}
\cs{ClistGet} \meta{comma list} \meta{token list variable}
\cs{ClistGetT} \meta{comma list} \meta{token list variable} \meta{true code}
\cs{ClistGetF} \meta{comma list} \meta{token list variable} \meta{false code}
\cs{ClistGetTF} \meta{comma list} \meta{token list variable} \meta{true code} \meta{false code}
\end{syntax}
Stores the left-most item from a \meta{comma list} in the
\meta{token list variable} without removing it from the
\meta{comma list}. The \meta{token list variable} is assigned locally.
%In the non-branching version, if the \meta{comma list} is empty the
%\meta{token list variable} is set to the marker value \cs{qNoValue}.
\end{function}
 
\begin{function}{\ClistPop,\ClistPopT,\ClistPopF,\ClistPopTF}
\begin{syntax}
\cs{ClistPop} \meta{comma list} \meta{token list variable}
\cs{ClistPopT} \meta{comma list} \meta{token list variable} \Arg{true code}
\cs{ClistPopF} \meta{comma list} \meta{token list variable} \Arg{false code}
\cs{ClistPopTF} \meta{comma list} \meta{token list variable} \Arg{true code} \Arg{false code}
\end{syntax}
Pops the left-most item from a \meta{comma list} into the
\meta{token list variable}, \emph{i.e.} removes the item from the
comma list and stores it in the \meta{token list variable}.
The assignment of the \meta{token list variable} is local.
If the \meta{comma list} is empty,
the value of the \meta{token list variable} is
not defined in this case and should not be relied upon. 
\end{function}
 
\begin{function}{\ClistPush}
\begin{syntax}
\cs{ClistPush} \meta{comma list} \Arg{items}
\end{syntax}
Adds the \Arg{items} to the top of the \meta{comma list}.
Spaces are removed from both sides of each item as for any
\texttt{n}-type comma list.
\end{function}

\section{Mapping over Comma Lists}
 
%The functions described in this section apply a specified function
%to each item of a comma list.
%All mappings are done at the current group level, \emph{i.e.} any
%local assignments made by the \meta{function} or \meta{code} discussed
%below remain in effect after the loop.
 
When the comma list is given explicitly, %as an \texttt{n}-type argument,
spaces are trimmed around each item.
If the result of trimming spaces is empty, the item is ignored.
Otherwise, if the item is surrounded by braces, one set is removed,
and the result is passed to the mapped function. Thus, if the
comma list that is being mapped is \verb*|{a , {{b} }, ,{}, {c},}|
then the arguments passed to the mapped function are
`\verb*|a|', `\verb*|{b} |', an empty argument, and `\verb*|c|'.
 
When the comma list is given as a variable, spaces
have already been trimmed on input, and items are simply stripped
of one set of braces if any. This case is more efficient than using
explicit comma lists.

%\begin{function}{\ClistMapFunction,\ClistVarMapFunction}
%\begin{syntax}
%\cs{ClistMapFunction} \Arg{comma list} \meta{function}
%\cs{ClistVarMapFunction} \meta{comma list} \meta{function}
%\end{syntax}
%Applies \meta{function} to every \meta{item} stored in the
%\meta{comma list}. The \meta{function} receives one argument for
%each iteration. The \meta{items} are returned from left to right.
%The function \cs{ClistMapInline} is in general more efficient
%than \cs{ClistMapFunction}.
%\end{function}

\begin{function}{\ClistMapInline,\ClistVarMapInline}
\begin{syntax}
\cs{ClistMapInline} \Arg{comma list} \Arg{inline function}
\cs{ClistVarMapInline} \meta{comma list} \Arg{inline function}
\end{syntax}
Applies \meta{inline function} to every \meta{item} stored
within the \meta{comma list}. The \meta{inline function} should
consist of code which receives the \meta{item} as \verb|#1|.
The \meta{items} are returned from left to right.
For example
\begin{codehigh}
\IgnoreSpacesOn
\TlClear \lTmpaTl
\ClistMapInline {one,two,three} {
  \TlPutRight \lTmpaTl {(#1)}
}
\Result {\TlUse\lTmpaTl}
\IgnoreSpacesOff
\end{codehigh}
produces (one)(two)(three).
\end{function}

\begin{function}{\ClistMapVariable,\ClistVarMapVariable}
\begin{syntax}
\cs{ClistMapVariable} \Arg{comma list} \meta{variable} \Arg{code}
\cs{ClistVarMapVariable} \meta{comma list} \meta{variable} \Arg{code}
\end{syntax}
Stores each \meta{item} of the \meta{comma list} in turn in the
(token list) \meta{variable} and applies the \meta{code}.  The
\meta{code} will usually make use of the \meta{variable}, but this
is not enforced.  The assignments to the \meta{variable} are local.
Its value after the loop is the last \meta{item} in the \meta{comma
list}, or its original value if there were no \meta{item}.  The
\meta{items} are returned from left to right.
\end{function}
 
%\begin{function}{\ClistMapTokens,\ClistVarMapTokens}
%\begin{syntax}
%\cs{ClistMapTokens} \Arg{comma list} \Arg{code}
%\cs{ClistVarMapTokens} \meta{clist var} \Arg{code}
%\end{syntax}
%Calls \meta{code} \Arg{item} for every \meta{item} stored in the
%\meta{comma list}. The \meta{code} receives each \meta{item} as a
%trailing brace group.  If the \meta{code} consists of a single
%function this is equivalent to \cs{ClistMapFunction} or \cs{ClistVarMapFunction}.
%\end{function}
 
%\begin{function}{\ClistMapBreak}
%\begin{syntax}
%\cs{ClistMapBreak}
%\end{syntax}
%Used to terminate a clist map function before all
%entries in the \meta{comma list} have been processed. This
%normally takes place within a conditional statement, for example
%\begin{verbatim}
%\clist_map_inline:Nn \l_my_clist
%{
%\str_if_eq:nnTF { #1 } { bingo }
%{ \clist_map_break: }
%{
%Do something useful
%}
%}
%\end{verbatim}
%Use outside of a clist map scenario leads to low
%level \TeX{} errors.
%\begin{texnote}
%When the mapping is broken, additional tokens may be inserted
%before further items are taken
%from the input stream. This depends on the design of the mapping
%function.
%\end{texnote}
%\end{function}
% 
%\begin{function}{\ClistMapBreakDo}
%\begin{syntax}
%\cs{ClistMapBreakDo} \Arg{code}
%\end{syntax}
%Used to terminate a clist map  function before all
%entries in the \meta{comma list} have been processed, inserting
%the \meta{code} after the mapping has ended. This
%normally takes place within a conditional statement, for example
%\begin{verbatim}
%\clist_map_inline:Nn \l_my_clist
%{
%\str_if_eq:nnTF { #1 } { bingo }
%{ \clist_map_break:n { <code> } }
%{
%Do something useful
%}
%}
%\end{verbatim}
%Use outside of a clist map scenario leads to low
%level \TeX{} errors.
%\begin{texnote}
%When the mapping is broken, additional tokens may be inserted
%before the \meta{code} is
%inserted into the input stream.
%This depends on the design of the mapping function.
%\end{texnote}
%\end{function}

\section{Comma List Conditionals}

\begin{function}{\ClistIfExist,\ClistIfExistTF}
\begin{syntax}
\cs{ClistIfExist} \meta{comma list}
\cs{ClistIfExistTF} \meta{comma list} \Arg{true code} \Arg{false code}
\end{syntax}
Tests whether the \meta{comma list} is currently defined.  This does
not check that the \meta{comma list} really is a comma list.
\end{function}

\begin{function}{\ClistIfEmpty,\ClistIfEmptyTF}
\begin{syntax}
\cs{ClistIfEmpty} \Arg{comma list}
\cs{ClistIfEmptyTF} \Arg{comma list} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the \meta{comma list} is empty (containing no items).
The rules for space trimming are as for other \texttt{n}-type
comma-list functions, hence the comma list \verb|{ , ,, }| (without
outer braces) is empty, while \verb|{ ,{},}| (without outer braces)
contains one element, which happens to be empty: the comma-list
is not empty.
\end{function}

\begin{function}{\ClistVarIfEmpty,\ClistVarIfEmptyTF}
\begin{syntax}
\cs{ClistVarIfEmpty} \meta{comma list}
\cs{ClistVarIfEmptyTF} \meta{comma list} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the \meta{comma list} is empty (containing no items).
\end{function}
 
\begin{function}{\ClistIfIn,\ClistIfInTF}
\begin{syntax}
\cs{ClistIfIn} \Arg{comma list} \Arg{item}
\cs{ClistIfInTF} \Arg{comma list} \Arg{item} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the \meta{item} is present in the \meta{comma list}.
In the case of an \texttt{n}-type \meta{comma list}, the usual rules
of space trimming and brace stripping apply. For example
\begin{demohigh}
\ClistIfInTF { a , {b}  , {b} , c } {b} {Yes} {No}
\end{demohigh}
%\begin{texnote}
%The function may fail if the \meta{item} contains \verb|{|, \verb|}|, or \verb|#|
%(assuming the usual \TeX{} category codes apply).
%\end{texnote}
\end{function}

\begin{function}{\ClistVarIfIn,\ClistVarIfInTF}
\begin{syntax}
\cs{ClistVarIfIn} \meta{comma list} \Arg{item}
\cs{ClistVarIfInTF} \meta{comma list} \Arg{item} \Arg{true code} \Arg{false code}
\end{syntax}
Tests if the \meta{item} is present in the \meta{comma list}.
In the case of an \texttt{n}-type \meta{comma list}, the usual rules
of space trimming and brace stripping apply.
%\begin{texnote}
%The function may fail if the \meta{item} contains \verb|{|, \verb|}|, or \verb|#|
%(assuming the usual \TeX{} category codes apply).
%\end{texnote}
\end{function}

\chapter{The Source Code}

%\CodeHigh{lite}
\setlength\parskip{0pt}
\dochighinput[language=latex/latex3]{functional.sty}

\end{document}