summaryrefslogtreecommitdiffstats
path: root/txr.1
blob: ddd334b720e70b7c6f86fe36410032cee3f54b3f (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
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
22879
22880
22881
22882
22883
22884
22885
22886
22887
22888
22889
22890
22891
22892
22893
22894
22895
22896
22897
22898
22899
22900
22901
22902
22903
22904
22905
22906
22907
22908
22909
22910
22911
22912
22913
22914
22915
22916
22917
22918
22919
22920
22921
22922
22923
22924
22925
22926
22927
22928
22929
22930
22931
22932
22933
22934
22935
22936
22937
22938
22939
22940
22941
22942
22943
22944
22945
22946
22947
22948
22949
22950
22951
22952
22953
22954
22955
22956
22957
22958
22959
22960
22961
22962
22963
22964
22965
22966
22967
22968
22969
22970
22971
22972
22973
22974
22975
22976
22977
22978
22979
22980
22981
22982
22983
22984
22985
22986
22987
22988
22989
22990
22991
22992
22993
22994
22995
22996
22997
22998
22999
23000
23001
23002
23003
23004
23005
23006
23007
23008
23009
23010
23011
23012
23013
23014
23015
23016
23017
23018
23019
23020
23021
23022
23023
23024
23025
23026
23027
23028
23029
23030
23031
23032
23033
23034
23035
23036
23037
23038
23039
23040
23041
23042
23043
23044
23045
23046
23047
23048
23049
23050
23051
23052
23053
23054
23055
23056
23057
23058
23059
23060
23061
23062
23063
23064
23065
23066
23067
23068
23069
23070
23071
23072
23073
23074
23075
23076
23077
23078
23079
23080
23081
23082
23083
23084
23085
23086
23087
23088
23089
23090
23091
23092
23093
23094
23095
23096
23097
23098
23099
23100
23101
23102
23103
23104
23105
23106
23107
23108
23109
23110
23111
23112
23113
23114
23115
23116
23117
23118
23119
23120
23121
23122
23123
23124
23125
23126
23127
23128
23129
23130
23131
23132
23133
23134
23135
23136
23137
23138
23139
23140
23141
23142
23143
23144
23145
23146
23147
23148
23149
23150
23151
23152
23153
23154
23155
23156
23157
23158
23159
23160
23161
23162
23163
23164
23165
23166
23167
23168
23169
23170
23171
23172
23173
23174
23175
23176
23177
23178
23179
23180
23181
23182
23183
23184
23185
23186
23187
23188
23189
23190
23191
23192
23193
23194
23195
23196
23197
23198
23199
23200
23201
23202
23203
23204
23205
23206
23207
23208
23209
23210
23211
23212
23213
23214
23215
23216
23217
23218
23219
23220
23221
23222
23223
23224
23225
23226
23227
23228
23229
23230
23231
23232
23233
23234
23235
23236
23237
23238
23239
23240
23241
23242
23243
23244
23245
23246
23247
23248
23249
23250
23251
23252
23253
23254
23255
23256
23257
23258
23259
23260
23261
23262
23263
23264
23265
23266
23267
23268
23269
23270
23271
23272
23273
23274
23275
23276
23277
23278
23279
23280
23281
23282
23283
23284
23285
23286
23287
23288
23289
23290
23291
23292
23293
23294
23295
23296
23297
23298
23299
23300
23301
23302
23303
23304
23305
23306
23307
23308
23309
23310
23311
23312
23313
23314
23315
23316
23317
23318
23319
23320
23321
23322
23323
23324
23325
23326
23327
23328
23329
23330
23331
23332
23333
23334
23335
23336
23337
23338
23339
23340
23341
23342
23343
23344
23345
23346
23347
23348
23349
23350
23351
23352
23353
23354
23355
23356
23357
23358
23359
23360
23361
23362
23363
23364
23365
23366
23367
23368
23369
23370
23371
23372
23373
23374
23375
23376
23377
23378
23379
23380
23381
23382
23383
23384
23385
23386
23387
23388
23389
23390
23391
23392
23393
23394
23395
23396
23397
23398
23399
23400
23401
23402
23403
23404
23405
23406
23407
23408
23409
23410
23411
23412
23413
23414
23415
23416
23417
23418
23419
23420
23421
23422
23423
23424
23425
23426
23427
23428
23429
23430
23431
23432
23433
23434
23435
23436
23437
23438
23439
23440
23441
23442
23443
23444
23445
23446
23447
23448
23449
23450
23451
23452
23453
23454
23455
23456
23457
23458
23459
23460
23461
23462
23463
23464
23465
23466
23467
23468
23469
23470
23471
23472
23473
23474
23475
23476
23477
23478
23479
23480
23481
23482
23483
23484
23485
23486
23487
23488
23489
23490
23491
23492
23493
23494
23495
23496
23497
23498
23499
23500
23501
23502
23503
23504
23505
23506
23507
23508
23509
23510
23511
23512
23513
23514
23515
23516
23517
23518
23519
23520
23521
23522
23523
23524
23525
23526
23527
23528
23529
23530
23531
23532
23533
23534
23535
23536
23537
23538
23539
23540
23541
23542
23543
23544
23545
23546
23547
23548
23549
23550
23551
23552
23553
23554
23555
23556
23557
23558
23559
23560
23561
23562
23563
23564
23565
23566
23567
23568
23569
23570
23571
23572
23573
23574
23575
23576
23577
23578
23579
23580
23581
23582
23583
23584
23585
23586
23587
23588
23589
23590
23591
23592
23593
23594
23595
23596
23597
23598
23599
23600
23601
23602
23603
23604
23605
23606
23607
23608
23609
23610
23611
23612
23613
23614
23615
23616
23617
23618
23619
23620
23621
23622
23623
23624
23625
23626
23627
23628
23629
23630
23631
23632
23633
23634
23635
23636
23637
23638
23639
23640
23641
23642
23643
23644
23645
23646
23647
23648
23649
23650
23651
23652
23653
23654
23655
23656
23657
23658
23659
23660
23661
23662
23663
23664
23665
23666
23667
23668
23669
23670
23671
23672
23673
23674
23675
23676
23677
23678
23679
23680
23681
23682
23683
23684
23685
23686
23687
23688
23689
23690
23691
23692
23693
23694
23695
23696
23697
23698
23699
23700
23701
23702
23703
23704
23705
23706
23707
23708
23709
23710
23711
23712
23713
23714
23715
23716
23717
23718
23719
23720
23721
23722
23723
23724
23725
23726
23727
23728
23729
23730
23731
23732
23733
23734
23735
23736
23737
23738
23739
23740
23741
23742
23743
23744
23745
23746
23747
23748
23749
23750
23751
23752
23753
23754
23755
23756
23757
23758
23759
23760
23761
23762
23763
23764
23765
23766
23767
23768
23769
23770
23771
23772
23773
23774
23775
23776
23777
23778
23779
23780
23781
23782
23783
23784
23785
23786
23787
23788
23789
23790
23791
23792
23793
23794
23795
23796
23797
23798
23799
23800
23801
23802
23803
23804
23805
23806
23807
23808
23809
23810
23811
23812
23813
23814
23815
23816
23817
23818
23819
23820
23821
23822
23823
23824
23825
23826
23827
23828
23829
23830
23831
23832
23833
23834
23835
23836
23837
23838
23839
23840
23841
23842
23843
23844
23845
23846
23847
23848
23849
23850
23851
23852
23853
23854
23855
23856
23857
23858
23859
23860
23861
23862
23863
23864
23865
23866
23867
23868
23869
23870
23871
23872
23873
23874
23875
23876
23877
23878
23879
23880
23881
23882
23883
23884
23885
23886
23887
23888
23889
23890
23891
23892
23893
23894
23895
23896
23897
23898
23899
23900
23901
23902
23903
23904
23905
23906
23907
23908
23909
23910
23911
23912
23913
23914
23915
23916
23917
23918
23919
23920
23921
23922
23923
23924
23925
23926
23927
23928
23929
23930
23931
23932
23933
23934
23935
23936
23937
23938
23939
23940
23941
23942
23943
23944
23945
23946
23947
23948
23949
23950
23951
23952
23953
23954
23955
23956
23957
23958
23959
23960
23961
23962
23963
23964
23965
23966
23967
23968
23969
23970
23971
23972
23973
23974
23975
23976
23977
23978
23979
23980
23981
23982
23983
23984
23985
23986
23987
23988
23989
23990
23991
23992
23993
23994
23995
23996
23997
23998
23999
24000
24001
24002
24003
24004
24005
24006
24007
24008
24009
24010
24011
24012
24013
24014
24015
24016
24017
24018
24019
24020
24021
24022
24023
24024
24025
24026
24027
24028
24029
24030
24031
24032
24033
24034
24035
24036
24037
24038
24039
24040
24041
24042
24043
24044
24045
24046
24047
24048
24049
24050
24051
24052
24053
24054
24055
24056
24057
24058
24059
24060
24061
24062
24063
24064
24065
24066
24067
24068
24069
24070
24071
24072
24073
24074
24075
24076
24077
24078
24079
24080
24081
24082
24083
24084
24085
24086
24087
24088
24089
24090
24091
24092
24093
24094
24095
24096
24097
24098
24099
24100
24101
24102
24103
24104
24105
24106
24107
24108
24109
24110
24111
24112
24113
24114
24115
24116
24117
24118
24119
24120
24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
24131
24132
24133
24134
24135
24136
24137
24138
24139
24140
24141
24142
24143
24144
24145
24146
24147
24148
24149
24150
24151
24152
24153
24154
24155
24156
24157
24158
24159
24160
24161
24162
24163
24164
24165
24166
24167
24168
24169
24170
24171
24172
24173
24174
24175
24176
24177
24178
24179
24180
24181
24182
24183
24184
24185
24186
24187
24188
24189
24190
24191
24192
24193
24194
24195
24196
24197
24198
24199
24200
24201
24202
24203
24204
24205
24206
24207
24208
24209
24210
24211
24212
24213
24214
24215
24216
24217
24218
24219
24220
24221
24222
24223
24224
24225
24226
24227
24228
24229
24230
24231
24232
24233
24234
24235
24236
24237
24238
24239
24240
24241
24242
24243
24244
24245
24246
24247
24248
24249
24250
24251
24252
24253
24254
24255
24256
24257
24258
24259
24260
24261
24262
24263
24264
24265
24266
24267
24268
24269
24270
24271
24272
24273
24274
24275
24276
24277
24278
24279
24280
24281
24282
24283
24284
24285
24286
24287
24288
24289
24290
24291
24292
24293
24294
24295
24296
24297
24298
24299
24300
24301
24302
24303
24304
24305
24306
24307
24308
24309
24310
24311
24312
24313
24314
24315
24316
24317
24318
24319
24320
24321
24322
24323
24324
24325
24326
24327
24328
24329
24330
24331
24332
24333
24334
24335
24336
24337
24338
24339
24340
24341
24342
24343
24344
24345
24346
24347
24348
24349
24350
24351
24352
24353
24354
24355
24356
24357
24358
24359
24360
24361
24362
24363
24364
24365
24366
24367
24368
24369
24370
24371
24372
24373
24374
24375
24376
24377
24378
24379
24380
24381
24382
24383
24384
24385
24386
24387
24388
24389
24390
24391
24392
24393
24394
24395
24396
24397
24398
24399
24400
24401
24402
24403
24404
24405
24406
24407
24408
24409
24410
24411
24412
24413
24414
24415
24416
24417
24418
24419
24420
24421
24422
24423
24424
24425
24426
24427
24428
24429
24430
24431
24432
24433
24434
24435
24436
24437
24438
24439
24440
24441
24442
24443
24444
24445
24446
24447
24448
24449
24450
24451
24452
24453
24454
24455
24456
24457
24458
24459
24460
24461
24462
24463
24464
24465
24466
24467
24468
24469
24470
24471
24472
24473
24474
24475
24476
24477
24478
24479
24480
24481
24482
24483
24484
24485
24486
24487
24488
24489
24490
24491
24492
24493
24494
24495
24496
24497
24498
24499
24500
24501
24502
24503
24504
24505
24506
24507
24508
24509
24510
24511
24512
24513
24514
24515
24516
24517
24518
24519
24520
24521
24522
24523
24524
24525
24526
24527
24528
24529
24530
24531
24532
24533
24534
24535
24536
24537
24538
24539
24540
24541
24542
24543
24544
24545
24546
24547
24548
24549
24550
24551
24552
24553
24554
24555
24556
24557
24558
24559
24560
24561
24562
24563
24564
24565
24566
24567
24568
24569
24570
24571
24572
24573
24574
24575
24576
24577
24578
24579
24580
24581
24582
24583
24584
24585
24586
24587
24588
24589
24590
24591
24592
24593
24594
24595
24596
24597
24598
24599
24600
24601
24602
24603
24604
24605
24606
24607
24608
24609
24610
24611
24612
24613
24614
24615
24616
24617
24618
24619
24620
24621
24622
24623
24624
24625
24626
24627
24628
24629
24630
24631
24632
24633
24634
24635
24636
24637
24638
24639
24640
24641
24642
24643
24644
24645
24646
24647
24648
24649
24650
24651
24652
24653
24654
24655
24656
24657
24658
24659
24660
24661
24662
24663
24664
24665
24666
24667
24668
24669
24670
24671
24672
24673
24674
24675
24676
24677
24678
24679
24680
24681
24682
24683
24684
24685
24686
24687
24688
24689
24690
24691
24692
24693
24694
24695
24696
24697
24698
24699
24700
24701
24702
24703
24704
24705
24706
24707
24708
24709
24710
24711
24712
24713
24714
24715
24716
24717
24718
24719
24720
24721
24722
24723
24724
24725
24726
24727
24728
24729
24730
24731
24732
24733
24734
24735
24736
24737
24738
24739
24740
24741
24742
24743
24744
24745
24746
24747
24748
24749
24750
24751
24752
24753
24754
24755
24756
24757
24758
24759
24760
24761
24762
24763
24764
24765
24766
24767
24768
24769
24770
24771
24772
24773
24774
24775
24776
24777
24778
24779
24780
24781
24782
24783
24784
24785
24786
24787
24788
24789
24790
24791
24792
24793
24794
24795
24796
24797
24798
24799
24800
24801
24802
24803
24804
24805
24806
24807
24808
24809
24810
24811
24812
24813
24814
24815
24816
24817
24818
24819
24820
24821
24822
24823
24824
24825
24826
24827
24828
24829
24830
24831
24832
24833
24834
24835
24836
24837
24838
24839
24840
24841
24842
24843
24844
24845
24846
24847
24848
24849
24850
24851
24852
24853
24854
24855
24856
24857
24858
24859
24860
24861
24862
24863
24864
24865
24866
24867
24868
24869
24870
24871
24872
24873
24874
24875
24876
24877
24878
24879
24880
24881
24882
24883
24884
24885
24886
24887
24888
24889
24890
24891
24892
24893
24894
24895
24896
24897
24898
24899
24900
24901
24902
24903
24904
24905
24906
24907
24908
24909
24910
24911
24912
24913
24914
24915
24916
24917
24918
24919
24920
24921
24922
24923
24924
24925
24926
24927
24928
24929
24930
24931
24932
24933
24934
24935
24936
24937
24938
24939
24940
24941
24942
24943
24944
24945
24946
24947
24948
24949
24950
24951
24952
24953
24954
24955
24956
24957
24958
24959
24960
24961
24962
24963
24964
24965
24966
24967
24968
24969
24970
24971
24972
24973
24974
24975
24976
24977
24978
24979
24980
24981
24982
24983
24984
24985
24986
24987
24988
24989
24990
24991
24992
24993
24994
24995
24996
24997
24998
24999
25000
25001
25002
25003
25004
25005
25006
25007
25008
25009
25010
25011
25012
25013
25014
25015
25016
25017
25018
25019
25020
25021
25022
25023
25024
25025
25026
25027
25028
25029
25030
25031
25032
25033
25034
25035
25036
25037
25038
25039
25040
25041
25042
25043
25044
25045
25046
25047
25048
25049
25050
25051
25052
25053
25054
25055
25056
25057
25058
25059
25060
25061
25062
25063
25064
25065
25066
25067
25068
25069
25070
25071
25072
25073
25074
25075
25076
25077
25078
25079
25080
25081
25082
25083
25084
25085
25086
25087
25088
25089
25090
25091
25092
25093
25094
25095
25096
25097
25098
25099
25100
25101
25102
25103
25104
25105
25106
25107
25108
25109
25110
25111
25112
25113
25114
25115
25116
25117
25118
25119
25120
25121
25122
25123
25124
25125
25126
25127
25128
25129
25130
25131
25132
25133
25134
25135
25136
25137
25138
25139
25140
25141
25142
25143
25144
25145
25146
25147
25148
25149
25150
25151
25152
25153
25154
25155
25156
25157
25158
25159
25160
25161
25162
25163
25164
25165
25166
25167
25168
25169
25170
25171
25172
25173
25174
25175
25176
25177
25178
25179
25180
25181
25182
25183
25184
25185
25186
25187
25188
25189
25190
25191
25192
25193
25194
25195
25196
25197
25198
25199
25200
25201
25202
25203
25204
25205
25206
25207
25208
25209
25210
25211
25212
25213
25214
25215
25216
25217
25218
25219
25220
25221
25222
25223
25224
25225
25226
25227
25228
25229
25230
25231
25232
25233
25234
25235
25236
25237
25238
25239
25240
25241
25242
25243
25244
25245
25246
25247
25248
25249
25250
25251
25252
25253
25254
25255
25256
25257
25258
25259
25260
25261
25262
25263
25264
25265
25266
25267
25268
25269
25270
25271
25272
25273
25274
25275
25276
25277
25278
25279
25280
25281
25282
25283
25284
25285
25286
25287
25288
25289
25290
25291
25292
25293
25294
25295
25296
25297
25298
25299
25300
25301
25302
25303
25304
25305
25306
25307
25308
25309
25310
25311
25312
25313
25314
25315
25316
25317
25318
25319
25320
25321
25322
25323
25324
25325
25326
25327
25328
25329
25330
25331
25332
25333
25334
25335
25336
25337
25338
25339
25340
25341
25342
25343
25344
25345
25346
25347
25348
25349
25350
25351
25352
25353
25354
25355
25356
25357
25358
25359
25360
25361
25362
25363
25364
25365
25366
25367
25368
25369
25370
25371
25372
25373
25374
25375
25376
25377
25378
25379
25380
25381
25382
25383
25384
25385
25386
25387
25388
25389
25390
25391
25392
25393
25394
25395
25396
25397
25398
25399
25400
25401
25402
25403
25404
25405
25406
25407
25408
25409
25410
25411
25412
25413
25414
25415
25416
25417
25418
25419
25420
25421
25422
25423
25424
25425
25426
25427
25428
25429
25430
25431
25432
25433
25434
25435
25436
25437
25438
25439
25440
25441
25442
25443
25444
25445
25446
25447
25448
25449
25450
25451
25452
25453
25454
25455
25456
25457
25458
25459
25460
25461
25462
25463
25464
25465
25466
25467
25468
25469
25470
25471
25472
25473
25474
25475
25476
25477
25478
25479
25480
25481
25482
25483
25484
25485
25486
25487
25488
25489
25490
25491
25492
25493
25494
25495
25496
25497
25498
25499
25500
25501
25502
25503
25504
25505
25506
25507
25508
25509
25510
25511
25512
25513
25514
25515
25516
25517
25518
25519
25520
25521
25522
25523
25524
25525
25526
25527
25528
25529
25530
25531
25532
25533
25534
25535
25536
25537
25538
25539
25540
25541
25542
25543
25544
25545
25546
25547
25548
25549
25550
25551
25552
25553
25554
25555
25556
25557
25558
25559
25560
25561
25562
25563
25564
25565
25566
25567
25568
25569
25570
25571
25572
25573
25574
25575
25576
25577
25578
25579
25580
25581
25582
25583
25584
25585
25586
25587
25588
25589
25590
25591
25592
25593
25594
25595
25596
25597
25598
25599
25600
25601
25602
25603
25604
25605
25606
25607
25608
25609
25610
25611
25612
25613
25614
25615
25616
25617
25618
25619
25620
25621
25622
25623
25624
25625
25626
25627
25628
25629
25630
25631
25632
25633
25634
25635
25636
25637
25638
25639
25640
25641
25642
25643
25644
25645
25646
25647
25648
25649
25650
25651
25652
25653
25654
25655
25656
25657
25658
25659
25660
25661
25662
25663
25664
25665
25666
25667
25668
25669
25670
25671
25672
25673
25674
25675
25676
25677
25678
25679
25680
25681
25682
25683
25684
25685
25686
25687
25688
25689
25690
25691
25692
25693
25694
25695
25696
25697
25698
25699
25700
25701
25702
25703
25704
25705
25706
25707
25708
25709
25710
25711
25712
25713
25714
25715
25716
25717
25718
25719
25720
25721
25722
25723
25724
25725
25726
25727
25728
25729
25730
25731
25732
25733
25734
25735
25736
25737
25738
25739
25740
25741
25742
25743
25744
25745
25746
25747
25748
25749
25750
25751
25752
25753
25754
25755
25756
25757
25758
25759
25760
25761
25762
25763
25764
25765
25766
25767
25768
25769
25770
25771
25772
25773
25774
25775
25776
25777
25778
25779
25780
25781
25782
25783
25784
25785
25786
25787
25788
25789
25790
25791
25792
25793
25794
25795
25796
25797
25798
25799
25800
25801
25802
25803
25804
25805
25806
25807
25808
25809
25810
25811
25812
25813
25814
25815
25816
25817
25818
25819
25820
25821
25822
25823
25824
25825
25826
25827
25828
25829
25830
25831
25832
25833
25834
25835
25836
25837
25838
25839
25840
25841
25842
25843
25844
25845
25846
25847
25848
25849
25850
25851
25852
25853
25854
25855
25856
25857
25858
25859
25860
25861
25862
25863
25864
25865
25866
25867
25868
25869
25870
25871
25872
25873
25874
25875
25876
25877
25878
25879
25880
25881
25882
25883
25884
25885
25886
25887
25888
25889
25890
25891
25892
25893
25894
25895
25896
25897
25898
25899
25900
25901
25902
25903
25904
25905
25906
25907
25908
25909
25910
25911
25912
25913
25914
25915
25916
25917
25918
25919
25920
25921
25922
25923
25924
25925
25926
25927
25928
25929
25930
25931
25932
25933
25934
25935
25936
25937
25938
25939
25940
25941
25942
25943
25944
25945
25946
25947
25948
25949
25950
25951
25952
25953
25954
25955
25956
25957
25958
25959
25960
25961
25962
25963
25964
25965
25966
25967
25968
25969
25970
25971
25972
25973
25974
25975
25976
25977
25978
25979
25980
25981
25982
25983
25984
25985
25986
25987
25988
25989
25990
25991
25992
25993
25994
25995
25996
25997
25998
25999
26000
26001
26002
26003
26004
26005
26006
26007
26008
26009
26010
26011
26012
26013
26014
26015
26016
26017
26018
26019
26020
26021
26022
26023
26024
26025
26026
26027
26028
26029
26030
26031
26032
26033
26034
26035
26036
26037
26038
26039
26040
26041
26042
26043
26044
26045
26046
26047
26048
26049
26050
26051
26052
26053
26054
26055
26056
26057
26058
26059
26060
26061
26062
26063
26064
26065
26066
26067
26068
26069
26070
26071
26072
26073
26074
26075
26076
26077
26078
26079
26080
26081
26082
26083
26084
26085
26086
26087
26088
26089
26090
26091
26092
26093
26094
26095
26096
26097
26098
26099
26100
26101
26102
26103
26104
26105
26106
26107
26108
26109
26110
26111
26112
26113
26114
26115
26116
26117
26118
26119
26120
26121
26122
26123
26124
26125
26126
26127
26128
26129
26130
26131
26132
26133
26134
26135
26136
26137
26138
26139
26140
26141
26142
26143
26144
26145
26146
26147
26148
26149
26150
26151
26152
26153
26154
26155
26156
26157
26158
26159
26160
26161
26162
26163
26164
26165
26166
26167
26168
26169
26170
26171
26172
26173
26174
26175
26176
26177
26178
26179
26180
26181
26182
26183
26184
26185
26186
26187
26188
26189
26190
26191
26192
26193
26194
26195
26196
26197
26198
26199
26200
26201
26202
26203
26204
26205
26206
26207
26208
26209
26210
26211
26212
26213
26214
26215
26216
26217
26218
26219
26220
26221
26222
26223
26224
26225
26226
26227
26228
26229
26230
26231
26232
26233
26234
26235
26236
26237
26238
26239
26240
26241
26242
26243
26244
26245
26246
26247
26248
26249
26250
26251
26252
26253
26254
26255
26256
26257
26258
26259
26260
26261
26262
26263
26264
26265
26266
26267
26268
26269
26270
26271
26272
26273
26274
26275
26276
26277
26278
26279
26280
26281
26282
26283
26284
26285
26286
26287
26288
26289
26290
26291
26292
26293
26294
26295
26296
26297
26298
26299
26300
26301
26302
26303
26304
26305
26306
26307
26308
26309
26310
26311
26312
26313
26314
26315
26316
26317
26318
26319
26320
26321
26322
26323
26324
26325
26326
26327
26328
26329
26330
26331
26332
26333
26334
26335
26336
26337
26338
26339
26340
26341
26342
26343
26344
26345
26346
26347
26348
26349
26350
26351
26352
26353
26354
26355
26356
26357
26358
26359
26360
26361
26362
26363
26364
26365
26366
26367
26368
26369
26370
26371
26372
26373
26374
26375
26376
26377
26378
26379
26380
26381
26382
26383
26384
26385
26386
26387
26388
26389
26390
26391
26392
26393
26394
26395
26396
26397
26398
26399
26400
26401
26402
26403
26404
26405
26406
26407
26408
26409
26410
26411
26412
26413
26414
26415
26416
26417
26418
26419
26420
26421
26422
26423
26424
26425
26426
26427
26428
26429
26430
26431
26432
26433
26434
26435
26436
26437
26438
26439
26440
26441
26442
26443
26444
26445
26446
26447
26448
26449
26450
26451
26452
26453
26454
26455
26456
26457
26458
26459
26460
26461
26462
26463
26464
26465
26466
26467
26468
26469
26470
26471
26472
26473
26474
26475
26476
26477
26478
26479
26480
26481
26482
26483
26484
26485
26486
26487
26488
26489
26490
26491
26492
26493
26494
26495
26496
26497
26498
26499
26500
26501
26502
26503
26504
26505
26506
26507
26508
26509
26510
26511
26512
26513
26514
26515
26516
26517
26518
26519
26520
26521
26522
26523
26524
26525
26526
26527
26528
26529
26530
26531
26532
26533
26534
26535
26536
26537
26538
26539
26540
26541
26542
26543
26544
26545
26546
26547
26548
26549
26550
26551
26552
26553
26554
26555
26556
26557
26558
26559
26560
26561
26562
26563
26564
26565
26566
26567
26568
26569
26570
26571
26572
26573
26574
26575
26576
26577
26578
26579
26580
26581
26582
26583
26584
26585
26586
26587
26588
26589
26590
26591
26592
26593
26594
26595
26596
26597
26598
26599
26600
26601
26602
26603
26604
26605
26606
26607
26608
26609
26610
26611
26612
26613
26614
26615
26616
26617
26618
26619
26620
26621
26622
26623
26624
26625
26626
26627
26628
26629
26630
26631
26632
26633
26634
26635
26636
26637
26638
26639
26640
26641
26642
26643
26644
26645
26646
26647
26648
26649
26650
26651
26652
26653
26654
26655
26656
26657
26658
26659
26660
26661
26662
26663
26664
26665
26666
26667
26668
26669
26670
26671
26672
26673
26674
26675
26676
26677
26678
26679
26680
26681
26682
26683
26684
26685
26686
26687
26688
26689
26690
26691
26692
26693
26694
26695
26696
26697
26698
26699
26700
26701
26702
26703
26704
26705
26706
26707
26708
26709
26710
26711
26712
26713
26714
26715
26716
26717
26718
26719
26720
26721
26722
26723
26724
26725
26726
26727
26728
26729
26730
26731
26732
26733
26734
26735
26736
26737
26738
26739
26740
26741
26742
26743
26744
26745
26746
26747
26748
26749
26750
26751
26752
26753
26754
26755
26756
26757
26758
26759
26760
26761
26762
26763
26764
26765
26766
26767
26768
26769
26770
26771
26772
26773
26774
26775
26776
26777
26778
26779
26780
26781
26782
26783
26784
26785
26786
26787
26788
26789
26790
26791
26792
26793
26794
26795
26796
26797
26798
26799
26800
26801
26802
26803
26804
26805
26806
26807
26808
26809
26810
26811
26812
26813
26814
26815
26816
26817
26818
26819
26820
26821
26822
26823
26824
26825
26826
26827
26828
26829
26830
26831
26832
26833
26834
26835
26836
26837
26838
26839
26840
26841
26842
26843
26844
26845
26846
26847
26848
26849
26850
26851
26852
26853
26854
26855
26856
26857
26858
26859
26860
26861
26862
26863
26864
26865
26866
26867
26868
26869
26870
26871
26872
26873
26874
26875
26876
26877
26878
26879
26880
26881
26882
26883
26884
26885
26886
26887
26888
26889
26890
26891
26892
26893
26894
26895
26896
26897
26898
26899
26900
26901
26902
26903
26904
26905
26906
26907
26908
26909
26910
26911
26912
26913
26914
26915
26916
26917
26918
26919
26920
26921
26922
26923
26924
26925
26926
26927
26928
26929
26930
26931
26932
26933
26934
26935
26936
26937
26938
26939
26940
26941
26942
26943
26944
26945
26946
26947
26948
26949
26950
26951
26952
26953
26954
26955
26956
26957
26958
26959
26960
26961
26962
26963
26964
26965
26966
26967
26968
26969
26970
26971
26972
26973
26974
26975
26976
26977
26978
26979
26980
26981
26982
26983
26984
26985
26986
26987
26988
26989
26990
26991
26992
26993
26994
26995
26996
26997
26998
26999
27000
27001
27002
27003
27004
27005
27006
27007
27008
27009
27010
27011
27012
27013
27014
27015
27016
27017
27018
27019
27020
27021
27022
27023
27024
27025
27026
27027
27028
27029
27030
27031
27032
27033
27034
27035
27036
27037
27038
27039
27040
27041
27042
27043
27044
27045
27046
27047
27048
27049
27050
27051
27052
27053
27054
27055
27056
27057
27058
27059
27060
27061
27062
27063
27064
27065
27066
27067
27068
27069
27070
27071
27072
27073
27074
27075
27076
27077
27078
27079
27080
27081
27082
27083
27084
27085
27086
27087
27088
27089
27090
27091
27092
27093
27094
27095
27096
27097
27098
27099
27100
27101
27102
27103
27104
27105
27106
27107
27108
27109
27110
27111
27112
27113
27114
27115
27116
27117
27118
27119
27120
27121
27122
27123
27124
27125
27126
27127
27128
27129
27130
27131
27132
27133
27134
27135
27136
27137
27138
27139
27140
27141
27142
27143
27144
27145
27146
27147
27148
27149
27150
27151
27152
27153
27154
27155
27156
27157
27158
27159
27160
27161
27162
27163
27164
27165
27166
27167
27168
27169
27170
27171
27172
27173
27174
27175
27176
27177
27178
27179
27180
27181
27182
27183
27184
27185
27186
27187
27188
27189
27190
27191
27192
27193
27194
27195
27196
27197
27198
27199
27200
27201
27202
27203
27204
27205
27206
27207
27208
27209
27210
27211
27212
27213
27214
27215
27216
27217
27218
27219
27220
27221
27222
27223
27224
27225
27226
27227
27228
27229
27230
27231
27232
27233
27234
27235
27236
27237
27238
27239
27240
27241
27242
27243
27244
27245
27246
27247
27248
27249
27250
27251
27252
27253
27254
27255
27256
27257
27258
27259
27260
27261
27262
27263
27264
27265
27266
27267
27268
27269
27270
27271
27272
27273
27274
27275
27276
27277
27278
27279
27280
27281
27282
27283
27284
27285
27286
27287
27288
27289
27290
27291
27292
27293
27294
27295
27296
27297
27298
27299
27300
27301
27302
27303
27304
27305
27306
27307
27308
27309
27310
27311
27312
27313
27314
27315
27316
27317
27318
27319
27320
27321
27322
27323
27324
27325
27326
27327
27328
27329
27330
27331
27332
27333
27334
27335
27336
27337
27338
27339
27340
27341
27342
27343
27344
27345
27346
27347
27348
27349
27350
27351
27352
27353
27354
27355
27356
27357
27358
27359
27360
27361
27362
27363
27364
27365
27366
27367
27368
27369
27370
27371
27372
27373
27374
27375
27376
27377
27378
27379
27380
27381
27382
27383
27384
27385
27386
27387
27388
27389
27390
27391
27392
27393
27394
27395
27396
27397
27398
27399
27400
27401
27402
27403
27404
27405
27406
27407
27408
27409
27410
27411
27412
27413
27414
27415
27416
27417
27418
27419
27420
27421
27422
27423
27424
27425
27426
27427
27428
27429
27430
27431
27432
27433
27434
27435
27436
27437
27438
27439
27440
27441
27442
27443
27444
27445
27446
27447
27448
27449
27450
27451
27452
27453
27454
27455
27456
27457
27458
27459
27460
27461
27462
27463
27464
27465
27466
27467
27468
27469
27470
27471
27472
27473
27474
27475
27476
27477
27478
27479
27480
27481
27482
27483
27484
27485
27486
27487
27488
27489
27490
27491
27492
27493
27494
27495
27496
27497
27498
27499
27500
27501
27502
27503
27504
27505
27506
27507
27508
27509
27510
27511
27512
27513
27514
27515
27516
27517
27518
27519
27520
27521
27522
27523
27524
27525
27526
27527
27528
27529
27530
27531
27532
27533
27534
27535
27536
27537
27538
27539
.\" t '\" vim:set syntax=groff:
.\"Copyright (C) 2009-2015 Kaz Kylheku <kaz@kylheku.com>.
.\"All rights reserved.
.\"
.\"BSD License:
.\"
.\"Redistribution and use in source and binary forms, with or without
.\"modification, are permitted provided that the following conditions
.\"are met:
.\"
.\"  1. Redistributions of source code must retain the above copyright
.\"     notice, this list of conditions and the following disclaimer.
.\"  2. Redistributions in binary form must reproduce the above copyright
.\"     notice, this list of conditions and the following disclaimer in
.\"     the documentation and/or other materials provided with the
.\"     distribution.
.\"  3. The name of the author may not be used to endorse or promote
.\"     products derived from this software without specific prior
.\"     written permission.
.\"
.\"THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
.\"IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
.\"WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
.\"
.\" Useful groff definitions.
.\"
.\" Some constants that depend on troff/nroff mode:
.ie n \{\
.ds vspc 1
.\}
.el \{\
.ds vspc 0.5
.\}
.\" Mount numeric fonts when not running under man2html
.if !\n(M2 \{\
. fp 4 CR
. fp 5 CI
.\}
.\" Base font
.nr fsav 1
.\" start of code block: switch to monospace font
.de cblk
.  ft 4
..
.\" end of code block: restore font
.de cble
.  ft 1
..
.\" typeset arguments in monospace
.\" .code x y z ->  \f[CR]x y z\f[]
.de code
\f[4]\\$*\f[]
..
.\" like .code typesets meta-syntax
.\" which is done in angle brackets in nroff or oblique
.\" courier in PDF/HTML.
.de meta
.  ie n \{\
<\\$*>
.  \}
.  el \{\
\f[5]\\$*\f[]
.  \}
..
.\" like .meta but tack on second agument with no space.
.de metn
.  ie n \{\
<\\$1>\\$2
.  \}
.  el \{\
\f[5]\\$1\f[]\\$2
.  \}
..
.\" like .code but wraps in quotes
.\" .str x y z ->  \f[CR]"x y z"\f[].
.de str
\f[4]"\\$*"\f[]
..
.\" wrap first argument in quotes, tack no second one with no space
.\" .strn x y ->  \f[CR]"x"\f[]y.
.de strn
\f[4]"\\$1"\f[]\\$2
..
.\" like .IP but use monospace too
.de coIP
.  IP "\\f[4]\\$*\\f[]"
..
.\" Directive heading
.de dir
.  SS* The \f[4]\\$1\f[] directive
..
.\" Multiple directive heading
.de dirs
.  ds s "
.  while (\\n[.$]>2) \{\
.    as s \f[4]\\$1\f[], 
.    shift
.  \}
.  if (\\n[.$]>1) \{\
.    as s \f[4]\\$1\f[] 
.    shift
.  \}
.  if (\\n[.$]>0) \{\
.    as s and \f[4]\\$1\f[]
.  \}
.  SS* The \\*s directives
..
.\" heading with code in position 1
.de c1SS
.  ds s \\f[4]\\$1\\f[]
.  shift
.  as s " \\$*
.  SS* \\*s
..
.\" utility macro for gathering material into "s" string.
.\" a pair of arguments "@ arg" becomes arg set in code
.\" a pair of arguments "@, arg" becomes "arg," where
.\" arg is set in code, followed by comma not in code.
.de gets
.  ds s "
.  while (\\n[.$]>0) \{\
.    ie "\\$1"@" \{\
.      shift
.      as s \f[4]\\$1\f[] 
.      shift
.    \}
.    el \{\
.      ie "\\$1"@," \{\
.        shift
.        as s \f[4]\\$1\f[], 
.        shift
.      \}
.      el \{\
.        as s \\$1 
.        shift
.      \}
.    \}
.  \}
..
.\" a macro for gathering material into "s"
.\" a pair of arguments "< arg" is typeset like
.\" .meta arg. "<< arg arg" is like .metn arg arg.
.ie n \{\
.  de getm
.    ds s "
.    while (\\n[.$]>0) \{\
.      ie "\\$1"<" \{\
.        shift
.        as s <\\$1> 
.        shift
.      \}
.      el \{\
.        ie "\\$1"<<" \{\
.          shift
.          as s <\\$1>\\$2 
.          shift
.          shift
.        \}
.        el \{\
.          ie "\\$1">>" \{\
.            shift
.            as s \\$1<\\$2> 
.            shift
.            shift
.          \}
.          el \{\
.            ie "\\$1"<>" \{\
.              shift
.              as s \\$1<\\$2>\\$3 
.              shift
.              shift
.              shift
.            \}
.            el \{\
.              as s \\$1 
.              shift
.            \}
.          \}
.        \}
.      \}
.    \}
.  .
.\}
.el \{\
.  de getm
.    ds s "
.    while (\\n[.$]>0) \{\
.      ie "\\$1"<" \{\
.        shift
.        as s \\f5\\$1\\f4 
.        shift
.      \}
.      el \{\
.        ie "\\$1"<<" \{\
.          shift
.          as s \\f5\\$1\\f4\\$2 
.          shift
.          shift
.        \}
.        el \{\
.          ie "\\$1">>" \{\
.            shift
.            as s \\$1\\f5\\$2\\f4 
.            shift
.            shift
.          \}
.          el \{\
.            ie "\\$1"<>" \{\
.              shift
.              as s \\$1\\f5\\$2\f4\\$3 
.              shift
.              shift
.              shift
.            \}
.            el \{\
.              as s \\$1 
.              shift
.            \}
.          \}
.        \}
.      \}
.    \}
.  .
.\}
.\" typeset all arguments by the last one in monospace, followed
.\" by the last one in previous font with no space
.\" .codn x y ... z ->  \f[CR]x y ...\f[]z
.de codn
.  ds s "
.  while (\\n[.$]>2) \{\
.    as s \\$1 
.    shift
.  \}
\f[4]\\*s\\$1\f[]\\$2
..
.\" .cod1 a b c ->  abc  where a is typeset as code
.de cod1
\\$1\f[4]\\$2\f[]\$3
..
.\" .cod2 a b ->  ab  where b is typeset as code
.de cod2
\\$1\f[4]\\$2\f[]
..
.\" .cod3 a b c ->  abc  where a and c are typeset as code
.de cod3
\f[4]\\$1\f[]\\$2\f[4]\\$3\f[]
..
.\" Syntax section markup
.de synb
.  TP* Syntax:
.  cblk
..
.de syne
.  cble
..
.\" Require section markup
.de reqb
.  TP* Requires:
.  cblk
..
.de reqe
.  cble
..
.\" Used for meta-variables in syntax blocks
.de mets
.  nr fsav \\n[.f]
.  getm \\$*
.  \"workaround for man2html:
.  as s \\f\\n[fsav]
  \\*s
.  ft \\n[fsav]
..
.\" Used for meta-variables in inline blocks
.de meti
.  nr fsav \\n[.f]
.  getm \\$*
.  \"workaround for man2html:
.  as s \\f\\n[fsav]
\\*s
.  ft \\n[fsav]
..
.\" Used for meta-variables in .coIP
.de meIP
.  nr fsav \\n[.f]
.  getm \\$*
.  \"workaround for man2html:
.  as s \\f\\n[fsav]
.coIP \\*s
.  ft \\n[fsav]
..
.\" Description section
.de desc
.  TP* Description:
..
.\" Section counters: heading, section, paragraph.
.nr shco 0 1
.nr ssco 0 1
.nr spco 0 1
.\" wrapper for .SH
.de SH*
.  SH \\n+[shco] \\$*
.  rs
.  nr ssco 0
.  nr spco 0
.  sp \*[vspc]
.  ns
..
.\" wrapper for .SS
.de SS*
.  SS \\n[shco].\\n+[ssco] \\$*
.  rs
.  nr spco 0
.  sp \*[vspc]
.  ns
..
.\" wrapper for .TP
.de TP*
.  ds s \\$1
.  shift
.  TP \\$*
\\*s
.  sp \*[vspc]
.  ns
..
.\" numbered paragraph
.de NP*
.  ie \n(M2 \{\
.  M2SS 2 H4 "\\n[shco].\\n[ssco].\\n+[spco] \\$*"
.  \}
.  el \{\
.  TP* "\f[B]\\n[shco].\\n[ssco].\\n+[spco] \\$*\f[]"
.  \}
.  PP
..
.\" process arguments using .gets so that some material
.\" is typeset as code. Then pass to .SS* section macro.
.de coSS
.  gets \\$*
.  SS* \\*s
..
.\" like coSS but targetting NP*
.de coNP
.  gets \\$*
.  NP* \\*s
..
.\" like coSS but use monospace IP
.de ccIP
.  gets \\$*
.  IP "\\*s"
..
.\" TXR name
.ds TX \f[B]TXR\f[]
.ds TL \f[B]TXR Lisp\f[]
.\" Start of man page:
.TH TXR 1 2015-03-14 "Utility Commands" "TXR Data Processing Language" "Kaz Kylheku"
.SH* NAME
\*(TX \- text processing language (version 105)
.SH* SYNOPSIS
.cblk
.meti txr >> [ options ] < query-file < data-files ..
.cble
.sp
.SH* DESCRIPTION
\*(TX is a language oriented toward processing text from files or streams, using
multiple programming paradigms.

A \*(TX script is called a query, and it specifies a pattern which matches (a
prefix of) an entire file, or multiple files.  Patterns can consists of large
chunks of multi-line free-form text, which is matched literally against
material in the input sources. Free variables occurring in the pattern
(denoted by the
.code @
symbol) are bound to the pieces of text occurring in the
corresponding positions. If the overall match is successful, then
\*(TX can do one of two things: it can report the list of variables which were
bound, in the form of a set of variable assignments which can be evaluated by
the
.B eval
command of the POSIX shell language, or generate a custom report according
to special directives in the query. Patterns can be arbitrarily complex,
and can be broken down into named pattern functions, which may be mutually
recursive. \*(TX patterns can work horizontally (characters within a line)
or vertically (spanning multiple lines). Multiple lines can be treated
as a single line.


In addition to embedded variables which implicitly match text, the
\*(TX query language supports a number of directives, for matching text using
regular expressions, for continuing a match in another file, for searching
through a file for the place where an entire sub-query matches, for collecting
lists, and for combining sub-queries using logical conjunction, disjunction and
negation, and numerous others.

Furthermore, embedded within \*(TX is a powerful Lisp dialect.  \*(TL supports
functional and imperative programming, and provides data types such as symbols,
strings, vectors, hash tables with weak reference support, lazy lists, and
arbitrary-precision (bignum integers).

.SH* ARGUMENTS AND OPTIONS

Options which don't take an argument may be combined together.
The
.code -v
and
.code -q
options are mutually exclusive. Of these two, the one which
occurs in the rightmost position in the argument list dominates.
The
.code -c
and
.code -f
options are also mutually exclusive; if both are specified,
it is a fatal error.

.meIP >> -D var=value
Bind the variable
.meta var
to the value
.meta value
prior to processing the query. The name is in scope over the entire
query, so that all occurrence of the variable are substituted and
match the equivalent text.  If the value contains commas, these
are interpreted as separators, which give rise to a list value.
For instance
.code -Da,b,c
creates a list of the strings
.strn "a" ,
.str "b"
and
.strn "c" .
(See Collect Directive bellow). List variables provide a multiple
match. That is to say, if a list variable occurs in a query, a successful
match occurs if any of its values matches the text. If more than one
value matches the text, the first one is taken.

.meIP >> -D var
Binds the variable
.meta var
to an empty string value prior to processing the query.

.coIP -q
Quiet operation during matching. Certain error messages are not reported on the
standard error device (but the if the situations occur, they still fail the
query). This option does not suppress error generation during the parsing
of the query, only during its execution.

.coIP -d
.coIP --debugger

Invoke the interactive \*(TX debugger. See the DEBUGGER section.

.coIP -v
Verbose operation. Detailed logging is enabled.

.coIP -b
This is a deprecated option, which is silently ignored. In \*(TX versions
prior to 90, the printing of variable bindings (see
.code -B
option) was
implicit behavior which was automatically suppressed in certain situations.
The -b option suppressed it unconditionally.

.coIP -B
If the query is successful, print the variable bindings as a sequence
of assignments in shell syntax that can be
.IR eval -ed
by a POSIX shell.
II the query fails, print the word "false". Evaluation of this word
by the shell has the effect of producing an unsuccessful termination
status from the shell's
.I eval
command.

.coIP "-l or --lisp-bindings"
This option implies
.codn -B .
Print the variable bindings in Lisp syntax instead of
shell syntax.

.meIP -a < num
This option implies
.codn -B .
The decimal integer argument
.meta num
specifies the maximum
number of array dimensions to use for list-valued variable bindings.
The default is 1. Additional dimensions are expressed using numeric suffixes
in the generated variable names.
For instance, consider the three-dimensional list arising out of a triply
nested collect:
.cblk
((("a" "b") ("c" "d")) (("e" "f") ("g" "h"))).
.cble
Suppose this is bound to a variable V.  With
.codn -a 1 ,
this will be
reported as:

.cblk
  V_0_0[0]="a"
  V_0_1[0]="b"
  V_1_0[0]="c"
  V_1_1[0]="d"
  V_0_0[1]="e"
  V_0_1[1]="f"
  V_1_0[1]="g"
  V_1_1[1]="h"
.cble

With
.codn -a 2 ,
it comes out as:

.cblk
  V_0[0][0]="a"
  V_1[0][0]="b"
  V_0[0][1]="c"
  V_1[0][1]="d"
  V_0[1][0]="e"
  V_1[1][0]="f"
  V_0[1][1]="g"
  V_1[1][1]="h"
.cble

The leftmost bracketed index is the most major index. That is to say,
the dimension order is:
.codn "NAME_m_m+1_..._n[1][2]...[m-1]" .

.meIP -c < query
Specifies the query in the form of a command line argument. If this option is
used, the query-file argument is omitted. The first non-option argument,
if there is one, now specifies the first input source rather than a query.
Unlike queries read from a file, (non-empty) queries specified as arguments
using -c do not have to properly end in a newline. Internally,
\*(TX adds the missing newline before parsing the query. Thus
.code -c
.str @a
is a valid query which matches a line.

Example:

Shell script which uses \*(TX to read two lines
.str 1
and
.str 2
from standard input,
binding them to variables
.code a
and
.codn b .
Standard
input is specified as
.code -
and the data
comes from shell "here document" redirection:
.RS
.IP code:
.cblk
\ #!/bin/sh

 txr -B -c "@a
 @b" - <<!
 1
 2
 !
.cble

.IP output:
.cblk
\ a=1
 b=2
.cble
.PP

The
.code @;
comment syntax can be used for better formatting:

.cblk
  txr -B -c "@;
  @a
  @b"
.cble
.RE

.meIP -f < query-file
Specifies the file from which the query is to be read, instead of the
.meta query-file
argument. This is useful in
.code #!
("hash bang") scripts. (See Hash Bang Support below).

.meIP -e < expression
Evaluates a \*(TL expression for its side effects, without printing
its value. Can be specified more than once. The
.meta query-file
argument becomes optional if
.code -e
is used at least once.

.meIP -p < expression
Evaluates a \*(TL expression and prints its value.  Can be specified more
than once. The
.meta query-file
argument becomes optional if
.code -p
is used at least once.

.meIP -C < number
.meIP >> --compat= number

Requests \*(TX to behave in a manner that is compatible with the specified
version of \*(TX. This makes a difference in situations when a release of
\*(TX breaks backward compatibility. If some version N+1 deliberately introduces
a change which is backward incompatible, then
.code -C N
can be used to request the old behavior.

The requested value of N can be too low, in which case \*(TX will
complain and exit with an unsuccessful termination status. This indicates
that \*(TX refuses to be compatible with such an old version. Users requiring
the behavior of that version will have to install an older version of \*(TX which
supports that behavior, or even that exact version.

If the option is specified more than once, the behavior is not specified.

For more information, see the COMPATIBILITY section.

.meIP >> --gc-delta= number

The
.meta number
argument to this option must be a decimal integer. It represents
a megabyte value, the "GC delta": one megabyte is 1048576 bytes.  The "GC
delta" controls an aspect of the garbage collector behavior.
See the
.code gc-set-delta
function for a description.

.coIP --help
Prints usage summary on standard output, and terminates successfully.

.coIP --license
Prints the software license. This depends on the software being
installed such that the LICENSE file is in the data directory.
Use of \*(TX implies agreement with the liability disclaimer in the license.

.coIP --version
Prints program version standard output, and terminates successfully.

.coIP --args
The
.code --args
option provides a way to encode multiple arguments as a single
argument, which is useful on some systems which have limitations in
their implementation of the "hash bang" mechanism. For details about
its special syntax, See Hash Bang Support below.

.coIP --
Signifies the end of the option list.

.coIP -
This argument is not interpreted as an option, but treated as a filename
argument. After the first such argument, no more options are recognized. Even
if another argument looks like an option, it is treated as a name.
This special argument
.code -
means "read from standard input" instead of a file.
The query file, or any of the data files, may be specified using this option.
If two or more files are specified as
.codn - ,
the behavior is system-dependent.
It may be possible to indicate EOF from the interactive terminal, and
then specify more input which is interpreted as the second file, and so forth.

.PP
After the options, the remaining arguments are files. The first file argument
specifies the query, and is mandatory.  A file argument consisting of a single
.code -
means to read the standard input instead of opening a file. A file argument
which begins with an exclamation symbol means that the rest of the argument is
a shell command which is to be run as a coprocess, and its output read like a
file.

.PP
\*(TX begins by reading the query. The entire query is scanned, internalized
and then begins executing, if it is free of syntax errors.  The reading of
data, on the other hand, is lazy.  A file isn't opened until the query demands
material from that file, and then the contents are read on demand, not all at
once.

If no files arguments are specified on the command line, it is up to the
query to open a file, pipe or standard input via the
.code @(next)
directive
prior to attempting to make a match. If a query attempts to match text,
but has run out of files to process, the match fails.

.SH* STATUS AND ERROR REPORTING
\*(TX sends errors and verbose logs to the standard error device. The following
paragraphs apply when \*(TX is run without enabling verbose mode with
.codn -v ,
or the printing of variable
bindings with
.code -B
or
.codn -a .

If the command line arguments are incorrect, or the query has a malformed
syntax, \*(TX issues an error diagnostic and terminates with a failed status.

If the query fails due to a mismatch, \*(TX terminates
with a failed status. No diagnostics are issued.

If the query is well-formed, and matches, then \*(TX issues
no diagnostics, and terminates with a successful status.

In verbose mode (option
.codn -v ),
\*(TX issues diagnostics on the standard error device even in situations which
are not erroneous.

In bindings-printing mode (options
.code -B
or
.codn -a) ,
\*(TX prints the word
.code false
if the query fails, and exits with a failed
termination status. If the query succeeds, the variable bindings, if any,
are output on standard output.

.SH* BASIC QUERY SYNTAX AND SEMANTICS
.SS* Comments
A query may contain comments which are delimited by the sequence
.code @;
and extend to the end of the line. No whitespace can occur between the
.code @
and
.codn ; .
A comment which begins on a line swallows that entire line, as well as the
newline which terminates it. In essence, the entire comment line disappears.
If the comment follows some material in a line, then it does not consume
the newline. Thus, the following two queries are equivalent:
.IP 1.
.cblk
\ @a@; comment: match whole line against variable @a
 @; this comment disappears entirely
 @b
.cble

.IP 2.
.cblk
\ @a
 @b
.cble

.PP

The comment after the
.code @a
does not consume the newline, but the
comment which follows does. Without this intuitive behavior,
line comment would give rise to empty lines that must match empty
lines in the data, leading to spurious mismatches.

Instead of the
.code ;
character, the
.code #
character can be used. This is an obsolescent feature.

.SS* Hash Bang Support
\*(TX has several features which support use of the "hash bang" convention
for creating apparently stand-alone executable programs.

If the first line of a query begins with the characters
.codn #! ,
that entire line is deleted from the query. This allows
for \*(TX queries to be turned into standalone executable programs in the POSIX
environment.

Shell example: create a simple executable program called
.str "twoline.txr"
and
run it. This assumes \*(TX is installed in
.codn /usr/bin .

.cblk
  $ cat > hello.txr
  #!/usr/bin/txr
  @(bind a "Hey")
  @(output)
  Hello, world!
  @(end)
  $ chmod a+x hello.txr
  $ ./hello.txr
  Hello, world!
.cble

When this plain hash bang line is used, \*(TX receives the name of the script
as an argument.  Therefore, it is not possible to pass additional options
to \*(TX.  For instance, if the above script is invoked like this

.cblk
  $ ./hello.txr -B
.cble

the -B option isn't processed by \*(TX, but treated as an additional argument,
just as if
.cblk
.meti txr < scriptname -B
.cble
had been executed directly.

This behavior is useful if the script author wants not to expose the
\*(TX options to the user of the script.

However, the hash bang line can use the
.code -f
option:

.cblk
  #!/usr/bin/txr -f
.cble

Now, the name of the script is passed as an argument to the
.code -f
option, and \*(TX will look for more options after that, so that the resulting
program appears to accept \*(TX options. Now we can run

.cblk
  $ ./hello.txr -B
  Hello, world!
  a="Hey"
.cble

The
.code -B
option is honored.

On some operating systems, it is not possible to pass more than one
argument through the hash bang mechanism. That is to say, this will
not work.

.cblk
  #!/usr/bin/txr -B -f
.cble

To support systems like this, \*(TX supports the special argument
.codn --args .
With
.codn --args ,
it is possible to encode multiple arguments
into one argument.  The
.code --args
option must be followed by a separator
character, chosen by the programmer. The characters after that are
split into multiple arguments on the separator character. The
.code --args
option is then removed from the argument list and replaced with these
arguments, which are processed in its place.

Example:

.cblk
  #!/usr/bin/txr --args:-B:-f
.cble

The above has the same behavior as

.cblk
  #!/usr/bin/txr -B -f
.cble

on a system which supports multiple arguments in hash bang.
The separator character is the colon, and so the remainder
of that argument,
.codn -B:-f ,
is split into the two arguments
.codn "-B -f" .

.SS* Whitespace
Outside of directives, whitespace is significant in \*(TX queries, and represents
a pattern match for whitespace in the input.  An extent of text consisting of
an undivided mixture of tabs and spaces is a whitespace token.  

Whitespace tokens match a precisely identical piece of whitespace in the input,
with one exception: a whitespace token consisting of precisely one space has a
special meaning. It is equivalent to the regular expression
.codn "@/[ ]+/" :
match an extent of one or more spaces (but not tabs!). Multiple consecutive
spaces do not have this meaning.

Thus, the query line
.str "a b"
(one space between
.code a
and
.codn b )
matches
.str "a b"
with any number of spaces between the two letters.

For matching a single space, the syntax
.code "@\e "
can be used (backslash-escaped space).

It is more often necessary to match multiple spaces than to exactly
match one space, so this rule simplifies many queries and adds inconvenience
to only few.

In output clauses, string and character literals and quasiliterals, a space
token denotes a space.

.SS* Text
Query material which is not escaped by the special character
.code @
is literal text, which matches input character for character. Text which occurs at
the beginning of a line matches the beginning of a line.  Text which starts in
the middle of a line, other than following a variable, must match exactly at
the current position, where the previous match left off. Moreover, if the text
is the last element in the line, its match is anchored to the end of the line.

An empty query line matches an empty line in the input. Note that an
empty input stream does not contain any lines, and therefore is not matched
by an empty line. An empty line in the input is represented by a newline
character which is either the first character of the file, or follows
a previous newline-terminated line.  

Input streams which end without terminating their last line with a newline are
tolerated, and are treated as if they had the terminator.

Text which follows a variable has special semantics, discussed in the
section Variables below.

A query may not leave a line of input partially matched. If any portion of a
line of input is matched, it must be entirely matched, otherwise a matching
failure results. However, a query may leave unmatched lines. Matching only
four lines of a ten line file is not a matching failure. The
.code eof
directive can be used to explicitly match the end of a file.

In the following example, the query matches the text, even though
the text has an extra line.
.IP code:
.cblk
\ Four score and seven
 years ago our
.cble

.IP data:
.cblk
\ Four score and seven
 years ago our
 forefathers
.cble
.PP

In the following example, the query
.B fails
to match the text, because the text has extra material on one
line that is not matched:
.IP code:
.cblk
\ I can carry nearly eighty gigs
 in my head
.cble

.IP data:
.cblk
\ I can carry nearly eighty gigs of data
 in my head
.cble
.PP

Needless to say, if the text has insufficient material relative
to the query, that is a failure also.

To match arbitrary material from the current position to the end
of a line, the "match any sequence of characters, including empty"
regular expression
.code @/.*/
can be used. Example:
.IP code:
.cblk
\ I can carry nearly eighty gigs@/.*/
.cble

.IP data:
.cblk
\ I can carry nearly eighty gigs of data
.cble
.PP

In this example, the query matches, since the regular expression
matches the string "of data". (See Regular Expressions section below).

Another way to do this is:
.IP code:
.cblk
\ I can carry nearly eighty gigs@(skip)
.cble

.SS* Special Characters in Text
Control characters may be embedded directly in a query (with the exception of
newline characters). An alternative to embedding is to use escape syntax.
The following escapes are supported:

.meIP >> @\e  newline
A backslash immediately followed by a newline introduces a physical line
break without breaking up the logical line. Material following this sequence
continues to be interpreted as a continuation of the previous line, so
that indentation can be introduced to show the continuation without appearing
in the data.
.meIP >> @\e  space
A backslash followed by a space encodes a space. This is useful in line
continuations when it is necessary for some or all of the leading spaces to be
preserved.  For instance the two line sequence

.cblk
  abcd@\e
    @\e  efg
.cble

is equivalent to the line

.cblk
  abcd  efg
.cble

The two spaces before the
.code @\e
in the second line are consumed. The spaces after are preserved.

.coIP @\ea
Alert character (ASCII 7, BEL).
.coIP @\eb
Backspace (ASCII 8, BS).
.coIP @\et
Horizontal tab (ASCII 9, HT).
.coIP @\en
Line feed (ASCII 10, LF). Serves as abstract newline on POSIX systems.
.coIP @\ev
Vertical tab (ASCII 11, VT).
.coIP @\ef
Form feed (ASCII 12, FF). This character clears the screen on many
kinds of terminals, or ejects a page of text from a line printer.
.coIP @\er
Carriage return (ASCII 13, CR).
.coIP @\ee
Escape (ASCII 27, ESC)
.meIP @\ex < hex-digits
A
.code @\ex
immediately followed by a sequence of hex digits is interpreted as a hexadecimal
numeric character code. For instance
.code @\ex41
is the ASCII character A.
.meIP @\e < octal-digits

A
.code @\e
immediately followed by a sequence of octal digits (0 through 7) is interpreted
as an octal character code. For instance
.code @\e010
is character 8, same as
.codn @\eb .
.PP

Note that if a newline is embedded into a query line with
.code @\en,
this does not split the line into two; it's embedded into the line and thus
cannot match anything. However,
.code @\en
may be useful in the
.code @(cat)
directive and
in
.codn @(output) .

.SS* Character Handling and International Characters

\*(TX represents text internally using wide characters, which are used to
represent Unicode code points. The query language, as well as all data sources,
are assumed to be in the UTF-8 encoding.  In the query language, extended
characters can be used directly in comments, literal text, string literals,
quasiliterals and regular expressions.  Extended characters can also be
expressed indirectly using hexadecimal or octal escapes.
On some platforms, wide characters may be restricted to 16 bits, so that
\*(TX can only work with characters in the BMP (Basic Multilingual Plane)
subset of Unicode.

\*(TX does not use the localization features of the system library;
its handling of extended characters is not affected by environment variables
like
.code LANG
and
.codn L_CTYPE .
The program reads and writes only the UTF-8 encoding.

If
\*(TX encounters an invalid bytes in the UTF-8 input, what happens depends on
the context in which this occurs. In a query, comments are read without regard
for encoding, so invalid encoding bytes in comments are not detected. A comment
is simply a sequence of bytes terminated by a newline.  In lexical elements
which represent text, such as string literals, invalid or unexpected encoding
bytes are treated as syntax errors. The scanner issues an error message,
then discards a byte and resumes scanning.  Certain sequences pass through the
scanner without triggering an error, namely some UTF-8 overlong sequences.
These are caught when when the lexeme is subject to UTF-8 decoding, and treated
in the same manner as other UTF-8 data, described in the following paragraph.

Invalid bytes in data are treated as follows. When an invalid byte is
encountered in the middle of a multibyte character, or if the input
ends in the middle of a multibyte character, or if a character is extracted
which is encoded as an overlong form, the UTF-8 decoder returns to the starting
byte of the ill-formed multibyte character, and extracts just that byte,
mapping it to the Unicode character range U+DC00 through U+DCFF.  The decoding
resumes afresh at the following byte, expecting that byte to be the start
of a UTF-8 code.

Furthermore, because \*(TX internally uses a null-terminated character
representation of strings which easily interoperates with C language
interfaces, when a null character is read from a stream, \*(TX converts it to
the code U+DC00. On output, this code converts back to a null byte,
as explained in the previous paragraph. By means of this representational
trick, \*(TX can handle textual data containing null bytes.

.SS* Regular Expression Directives

In place of a piece of text (see section Text above), a regular expression
directive may be used, which has the following syntax:

.cblk
  @/RE/
.cble

where the RE part enclosed in slashes represents regular expression
syntax (described in the section Regular Expressions below).

Long regular expressions can be broken into multiple lines using a
backslash-newline sequence.  Whitespace before the sequence or after the
sequence is not significant, so the following two are equivalent:

.cblk
  @/reg \e
    ular/

  @/regular/
.cble

There may not be whitespace between the backslash and newline.

Whereas literal text simply represents itself, regular expression denotes a
(potentially infinite) set of texts.  The regular expression directive
matches the longest piece of text (possibly empty) which belongs to the set
denoted by the regular expression. The match is anchored to the current
position; thus if the directive is the first element of a line, the match is
anchored to the start of a line. If the regular expression directive is the
last element of a line, it is anchored to the end of the line also: the regular
expression must match the text from the current position to the end of the
line.

Even if the regular expression matches the empty string, the match will fail if
the input is empty, or has run out of data. For instance suppose the third line
of the query is the regular expression
.codn @/.*/ ,
but the input is a file which has
only two lines. This will fail: the data has no line for the regular expression to
match. A line containing no characters is not the same thing as the absence of
a line, even though both abstractions imply an absence of characters.

Like text which follows a variable, a regular expression directive which
follows a variable has special semantics, discussed in the section Variables
below.

.SS* Variables

Much of the query syntax consists of arbitrary text, which matches file data
character for character. Embedded within the query may be variables and
directives which are introduced by a
.code @
character.  Two consecutive
.code @@
characters encode a literal
.codn @ .

A variable matching or substitution directive is written in one of several
ways:

.cblk
.mets >> @ sident
.mets <> @{ bident }
.mets >> @* sident
.mets <> @*{ bident }
.mets >> @{ bident <> / regex /}
.mets >> @{ bident >> ( fun >> [ arg ... ])}
.mets >> @{ bident << number }
.cble

The forms with an
.code *
indicate a long match, see Longest Match below.
The last two three forms with the embedded regexp
.cblk
.meti <> / regex /
.cble
or
.meta number
or function
have special semantics; see Positive Match below.

The identifier
.code t
cannot be used as a name; it is a reserved symbol which
denotes the value true. An attempt to use the variable
.code @t
will result in an exception.  The symbol
.code nil
can be used where a variable name is required syntacticaly,
but it has special semantics, described in a section below.

A
.meta sident
is a "simple identifier" form which is not delimited by
braces.

A
.meta sident
consists of any combination of
one or more letters, numbers, and underscores. It may not look like a number,
so that for instance
.code 123
is not a valid
.metn sident ,
but
.code 12A
is valid.  Case is
sensitive, so that
.code FOO
is different from
.codn foo ,
which is different from
.codn Foo .

The braces around an identifier can be used when material which follows would
otherwise be interpreted as being part of the identifer. When a name is
enclosed in braces it is a
.metn bident .

The following additional characters may be used as part of
.meta bident
which are not allowed in a
.metn sident :

.cblk
 ! $ % & * + - < = > ? \e _ ~
.cble

The rule still holds that a name cannot look like a number so
.code +123
is not a valid
.meta bident
but these are valid:
.codn a->b ,
.codn *xyz* ,
.codn foo-bar .

The syntax
.code @FOO_bar
introduces the name
.codn FOO_bar ,
whereas
.code @{FOO}_bar
means the
variable named
.str FOO
followed by the text
.strn _bar .
There may be whitespace
between the
.code @
and the name, or opening brace. Whitespace is also allowed in the
interior of the braces. It is not significant.

If a variable has no prior binding, then it specifies a match. The
match is determined from some current position in the data: the
character which immediately follows all that has been matched previously.
If a variable occurs at the start of a line, it matches some text
at the start of the line. If it occurs at the end of a line, it matches
everything from the current position to the end of the line.

.SS* Negative Match

If a variable is one of the plain forms

.cblk
.mets >> @ sident
.mets <> @{ bident }
.mets >> @* sident
.mets <> @*{ bident }
.cble

then this is a "negative match".  The extent of the matched text (the text
bound to the variable) is determined by looking at what follows the variable,
and ranges from the current position to some position where the following
material finds a match. This is why this is called a "negative match": the
spanned text which ends up bound to the variable is that in which the match for
the trailing material did not occur.

A variable may be followed by a piece of text, a regular expression directive,
a function call, a directive, another variable, or nothing (i.e.  occurs at the
end of a line). These cases are discussed in detail below.

.NP* Variable Followed by Nothing
If the variable is followed by nothing, the negative match extends from the
current position in the data, to the end of the line.  Example:
.IP code:
.cblk
\ a b c @FOO
.cble
.IP data:
.cblk
\ a b c defghijk
.cble
.IP result:
.cblk
\ FOO="defghijk"
.cble

.NP* Variable Followed by Text
For the purposes of determining the negative match, text is defined as a
sequence of literal text and regular expressions, not divided by a directive.
So for instance in this example:

.cblk
  @a:@/foo/bcd e@(maybe)f@(end)
.cble
.PP

the variable @a is considered to be followed by
.strn ":@/foo/bcd e" .

If a variable is followed by text, then the extent of the negative match is
determined by searching for the first occurrence of that text within the line,
starting at the current position.

The variable matches everything between the current position and the matching
position (not including the matching position). Any whitespace which follows
the variable (and is not enclosed inside braces that surround the variable
name) is part of the text. For example:
.IP code:
.cblk
\ a b @FOO e f
.cble
.IP data:
.cblk
\ a b c d e f
.cble
.IP result:
.cblk
\ FOO="c d"
.cble
.PP

In the above example, the pattern text
.str "a b "
matches the
data
.strn "a b " .
So when the
.code @FOO
variable is processed, the data being
matched is the remaining
.strn "c d e f" .
The text which follows
.code @FOO
is
.strn " e f" .
This is found within the data
.str "c d e f"
at position 3 (counting from 0).  So positions 0-2
.cblk
("c d")
.cble
constitute the matching text which is bound to FOO.

.NP* Variable Followed by a Function Call or Directive

If the variable is followed by a function call, or a directive, the extent is
determined by scanning the text for the first position where a match occurs
for the entire remainder of the line. (For a description of functions,
see FUNCTIONS.)

For example:

.cblk
  @foo@(bind a "abc")xyz
.cble

Here,
.code foo
will match the text from the current position to where
.str "xyz"
occurs, even though there is a
.code @(bind)
directive. Furthermore, if
more material is added after the xyz, it is part of the search.
Note the difference between the following two:

.cblk
  @foo@/abc/@(func)
  @foo@(func)@/abc/
.cble

In the first example, the variable foo matches the text from the current
position until the match for the regular expression abc.
.code @(func)
is not
considered when processing
.codn @foo .
In the second example, the variable foo
matches the text from the current position until the position which matches
the function call, followed by a match for the regular expression.
The entire sequence
.code @(func)@/abc/
is considered.

.NP* Consecutive Variables
If an unbound variable specified a fixed-width match or a regular expression,
then the issue of consecutive variables does not arise. Such a variable
consumes text regardless of any context which follows it.

However, what if an unbound variable with no modifier is followed by another
variable? The behavior depends on the nature of the other variable.

If the other variable also has no modifier, this is a semantic error which
will cause the query to fail.  A diagnostic message will be issued, unless
operating in quiet mode via
.codn -q .
The reason is that there is no way to bind two
consecutive variables to an extent of text; this is an ambiguous situation,
since there is no matching criterion for dividing the text between two
variables.  (In theory, a repetition of the same variable, like
.codn @FOO@FOO ,
could find a solution by dividing the match extent in half, which would work
only in the case when it contains an even number of characters.  This behavior
seems to have dubious value).

An unbound variable may be followed by one which is bound. The bound
variable is replaced by the text which it denotes, and the logic proceeds
accordingly.

It is possible for a variable to be bound to a regular expression.
If
.code x
is an unbound variable and
.code y
is bound to a regular expression
.codn RE ,
then
.code @x@y
means
.codn @x@/RE/ .
A variable
.code v
can be bound to a regular expression using, for example,
.codn @(bind v #/RE/) .

The
.code @*
syntax for longest match is available. Example:
.IP code:
.cblk
\ @FOO:@BAR@FOO
.cble
.IP data:
.cblk
\ xyz:defxyz
.cble
.IP result:
.cblk
\ FOO=xyz, BAR=def
.cble
.PP

Here,
.code FOO
is matched with
.strn "xyz" ,
based on the delimiting around the
colon. The colon in the pattern then matches the colon in the data,
so that
.code BAR
is considered for matching against
.strn "defxyz" .
.code BAR
is followed by
.codn FOO ,
which is already bound to
.strn "xyz" .
Thus
.str "xyz"
is located in the
.str "defxyz"
data following
.strn "def" ,
and so BAR is bound to
.strn "def" .

If an unbound variable is followed by a variable which is bound to a list, or
nested list, then each character string in the list is tried in turn to produce
a match. The first match is taken.

An unbound variable may be followed by another unbound variable which specifies
a regular expression or function call match. This is a special case called a
"double variable match".  What happens is that the text is searched using the
regular expression or function.  If the search fails, than neither variable is
bound: it is a matching failure.  If the search succeeds, than the first
variable is bound to the text which is skipped by the search.  The second
variable is bound to the text matched by the regular expression or function.
Examples:
.IP code:
.cblk
\ @foo@{bar /abc/}
.cble
.IP data:
.cblk
\ xyz@#abc
.cble
.IP result:
.cblk
\ foo="xyz@#", BAR="abc"
.cble
.PP

.NP* Consecutive Variables Via Directive
Two variables can be de facto consecutive in a manner shown in the
following example:

.cblk
  @var1@(all)@var2@(end)
.cble

This is treated just like the variable followed by directive. No semantic
error is identified, even if both variables are unbound. Here,
.code @var2
matches everything at the current position, and so
.code @var1
ends up bound to the empty string.

Example 1:
.code b
matches at position 0 and
.code a
binds the empty string:
.IP code:
.cblk
\ @a@(all)@b@(end)
.cble
.IP data:
.cblk
\ abc
.cble
.IP result:
.cblk
\ a=""
 b="abc"
.cble
.PP

Example 2:
.code *a
specifies longest match (see Longest Match below), and so it takes
everything:
.IP code:
.cblk
\ @*a@(all)@b@(end)
.cble
.IP data:
.cblk
\ abc
.cble
.IP result:
.cblk
\ a="abc"
 b=""
.cble
.PP

.NP* Longest Match
The closest-match behavior for the negative match can be overridden to longest
match behavior. A special syntax is provided for this: an asterisk between the
.code @
and the variable, e.g:
.IP code:
.cblk
\ a @*{FOO}cd
.cble
.IP data:
.cblk
\ a b cdcdcdcd
.cble
.IP result:
.cblk
\ FOO="b cdcdcd"
.cble
.PP
.IP code:
.cblk
\ a @{FOO}cd
.cble
.IP data:
.cblk
\ a b cdcdcd
.cble
.IP result:
.cblk
\ FOO="b "
 b=""
.cble
.PP

In the former example, the match extends to the rightmost occurrence of
.strn "cd" ,
and so
.code FOO
receives
.strn "b cdcdcd" .
In the latter example, the
.code *
syntax isn't used, and so a leftmost match takes place. The extent
covers only the
.strn "b " ,
stopping at the first
.str "cd"
occurrence.

.SS* Positive Match

There are syntactic variants of variable syntax which have an embedded expression
enclosed with the variable in braces:

.cblk
.mets >> @{ bident <> / regex /}
.mets >> @{ bident >> ( fun >> [args ...])}
.mets >> @{ bident << number }
.cble

These specify a variable binding that is driven by a positive match derived
from a regular expression, function or character count, rather than from
trailing material (which is regarded as a "negative" match, since the
variable is bound to material which is
.B skipped
in order to match the trailing material). In the
.cblk
.meti <> / regex /
.cble
form, the match
extends over all characters from the current position which match
the regular expression
.metn regex .
(see Regular Expressions section below).
In the
.cblk
.meti >> ( fun >> [ args ...])
.cble
form, the match extends over characters which
are matched by the call to the function, if the call
succeeds. Thus
.code @{x (y z w)}
is just like
.codn "@(y z w)" ,
except that the region of
text skipped over by
.code @(y z w)
is also bound to the variable
.codn x .
See FUNCTIONS below.

In the
.meta number
form, the match processes a field of text which
consists of the specified number of characters, which must be non-negative
number.  If the data line doesn't have that many characters starting at the
current position, the match fails. A match for zero characters produces an
empty string.  The text which is actually bound to the variable
is all text within the specified field, but excluding leading and
trailing whitespace. If the field contains only spaces, then an empty
string is extracted.

This syntax is processed without consideration of what other
syntax follows.  A positive match may be directly followed by an unbound
variable.

.coSS Special Symbols @ nil and @ t

Just like in the Common Lisp language, the names
.code nil
and
.code t
are special.

.code nil
symbol stands for the empty
list object, an object which marks the end of a list, and boolean false. It is
synonymous with the syntax
.code ()
which may be used interchangeably with
.code nil
in most constructs.

In \*(TL,
.code nil
and
.code t
cannot be used as variables. When evaluated, they evaluate to themselves.

In the \*(TX pattern language,
.code nil
can be used in the variable binding syntax, but does not create a binding;
it has a special meaning.  It allows the variable matching syntax to be used to
skip material, in ways similar to the
.codn skip
directive.

The
.code nil
symbol is also used as a
.code block
name, both in the \*(TX pattern language and in \*(TL.
A block named
.code nil
is considered to be anonymous.

.SS* Keyword Symbols

Names whose names begin with the
.code :
character are keyword symbols.  These also
may not be used as variables either and stand for themselves. Keywords are
useful for labeling information and situations.

.SS* Regular Expressions
Regular expressions are a language for specifying sets of character strings.
Through the use of pattern matching elements, regular expression is
able to denote an infinite set of texts.
\*(TX contains an original implementation of regular expressions, which
supports the following syntax:
.coIP .
The period is a "wildcard" that matches any character.
.coIP []
Character class: matches a single character, from the set specified by
special syntax written between the square brackets.
This supports basic regexp character class syntax. POSIX
notation like
.code [:digit:]
is not supported.
The regex tokens
.codn \es ,
.code \ed
and
.code \ew
are permitted in character classes, but not their complementing counterparts.
These tokens simply contribute their characters to the class.
The class
.code [a-zA-Z]
means match an uppercase
or lowercase letter; the class
.code [0-9a-f]
means match a digit or
a lowercase letter; the class
.code [^0-9]
means match a non-digit, and so forth.
There are no locale-specific behaviors in \*(TX regular expressions;
.code [A-Z]
denotes an ASCII/Unicode range of characters.
The class
.code [\ed.]
means match a digit or the period character.
A
.code ]
or
.code -
can be used within a character class, but must be escaped
with a backslash. A
.code ^
in the first position denotes a complemented
class, unless it is escaped by backslash. In any other position, it denotes
itself.  Two backslashes code for one backslash. So for instance
.code [\e[\e-]
means match a
.code [
or
.code -
character,
.code [^^]
means match any character other
than
.codn ^ ,
and
.code [\e^\e\e]
means match either a
.code ^
or a backslash. Regex operators
such as
.codn * ,
.code +
and
.code &
appearing in a character class represent ordinary
characters. The characters
.codn - ,
.code ]
and
.code ^
occurring outside of a character class
are ordinary. Unescaped
.code /
characters can appear within a character class. The
empty character class
.code []
matches no character at all, and its complement
.code [^]
matches any character, and is treated as a synonym for the 
.code .
(period) wildcard operator.
.ccIP @, \es @ \ew and @ \ed
These regex tokens each match a single character. 
The
.code \es
regex token matches a wide variety of ASCII whitespace characters
and Unicode spaces. The
.code \ew
token matches alphabetic word characters; it
is equivalent to the character class
.codn [A-Za-z_] .
The
.code \ed
token matches a digit, and is equivalent to
.codn [0-9] .
.ccIP @, \eS @ \eW and @ \eD
These regex tokens are the complemented counterparts of
.codn \es ,
.code \ew
and
.codn \ed .
The
.code \eS
token matches all those characters which
.code \es
does not match,
.code \eW
matches all characters that
.code \ew
does not match and
.code \eD
matches nondigits.
.coIP empty
An empty expression is a regular expression. It represents the set of strings
consisting of the empty string; i.e. it matches just the empty string. The
empty regex can appear alone as a full regular expression (for instance the
\*(TX syntax
.code @//
with nothing between the slashes)
and can also be passed as a subexpression to operators, though this
may require the use of parentheses to make the empty regex explicit.  For
example, the expression
.code a|
means: match either
.codn a ,
or nothing.  The forms
.code *
and
.code (*)
are syntax errors; though not useful, the correct way to match the
empty expression zero or more times is the syntax
.codn ()* .
.coIP nomatch
The nomatch regular expression represents the
empty set: it matches no strings at all, not even the empty string.
There is no dedicated syntax to directly express nomatch in the regex language.
However, the empty character class
.code []
is equivalent to nomatch, and may be
considered to be a notation for it. Other representations of nomatch are
possible: for instance, the regex
.code ~.*
which is the complement of the regex that
denotes the set of all possible strings, and thus denotes the empty set. A
nomatch has uses; for instance, it can be used to temporarily "comment out"
regular expressions. The regex
.code ([]abc|xyz)
is equivalent to
.codn (xyz) ,
since the
.code []abc
branch cannot match anything. Using
.code []
to "block" a subexpression allows
you to leave it in place, then enable it later by removing the "block".
.coIP (R)
If
.code R
is a regular expression, then so is
.code (R).
The contents of parentheses denote one regular expression unit, so that for
instance in
.codn (RE)* ,
the
.code *
operator applies to the entire parenthesized group.
The syntax
.code ()
is valid and equivalent to the empty regular expression.
.coIP R?
Optionally match the preceding regular expression
.codn R .
.coIP R*
Match the expression
.code R
zero or more times. This
operator is sometimes called the "Kleene star", or "Kleene closure".
The Kleene closure favors the longest match. Roughly speaking, if there are two
or more ways in which
.code R1*R2
can match, than that match occurs in which
.code R1*
matches the longest possible text.
.coIP R+
Match the preceding expression
.code R
one or more times.  Like
.codn R* ,
this favors the longest possible match:
.code R+
is equivalent to
.codn RR* .
.coIP R1%R2
Match
.code R1
zero or more times, then match
.codn R2 .
If this match can occur in
more than one way, then it occurs such that
.code R1
is matched the fewest
number of times, which is opposite from the behavior of
.codn R1*R2 .
Repetitions of
.code R1
terminate at the earliest
point in the text where a non-empty match for
.code R2
occurs. Because
it favors shorter matches,
.code %
is termed a non-greedy operator.  If
.code R2
is the empty expression, or equivalent to it, then
.code R1%R2
reduces to
. codn R1* .
So for
instance
.code (R%)
is equivalent to
.codn (R*) ,
since the missing right operand is
interpreted as the empty regex. Note that whereas the expression
.code (R1*R2)
is equivalent to
.codn (R1*)R2 ,
the expression
.code (R1%R2)
is
.B not
equivalent to
.codn (R1%)R2 .
.coIP ~R
Match the opposite of the following expression
.codn R ;
that is, match exactly
those texts that
.code R
does not match. This operator is called complement,
or logical not.
.coIP R1R2
Two consecutive regular expressions denote catenation:
the left expression must match, and then the right.
.coIP R1|R2
match either the expression
.code R1
or
.codn R2 .
This operator is known by
a number of names: union, logical or, disjunction, branch, or alternative.
.coIP R1&R2
Match both the expression
.code R1
and
.code R2
simultaneously; i.e. the
matching text must be one of the texts which are in the intersection of the set
of texts matched by
.code R1
and the set matched by
.codn R2 .
This operator is called intersection, logical and, or conjunction.

.PP
Any escaped character which does not fall into the above escaping conventions,
or any unescaped character which is not a regular expression operator, denotes
one-position match of that character itself.

Any of the special characters, including the delimiting
.codn / ,
can be escaped with a backslash to suppress its meaning and denote the
character itself.

Furthermore, all of the same escapes as are described in the section Special
Characters in Text above are supported - the difference is that in regular
expressions, the
.code @
character is not required, so for example a tab is coded as
.code \et
rather than
.codn @\et .
Octal and hex character escapes can be optionally
terminated by a semicolon, which is useful if the following characters are
octal or hex digits not intended to be part of the escape.
.IP "Precedence table, highest to lowest:"
.TS
tab(!);
l l l.
Operators!Class!Associativity
\f[4](R) []\f[]!primary!
\f[4]R? R+ R* R%...\f[]!postfix!left-to-right
\f[4]R1R2\f[]!catenation!left-to-right
\f[4]~R ...%R\f[]\f[]\f[]!unary!right-to-left
\f[4]R1&R2\f[]!intersection!left-to-right
\f[4]R1|R2\f[]!union!left-to-right
.TE
.PP

The
.code %
operator is like a postfix operator with respect to its left
operand, but like a unary operator with respect to its right operand.
Thus
.code a~b%c~d
is
.cblk
a(~(b%(c(~d))))
.cble
, demonstrating right-to-left associativity,
where all of
.code b%
may be regarded as a unary operator being applied to
.codn c~d .
Similarly,
.code a?*+%b
means
.codn (((a?)*)+)%b ,
where the trailing
.code %b
behaves like a postfix operator.

In
\*(TX, regular expression matches do not span multiple lines. The regex
language has no feature for multi-line matching. However, the
.code @(freeform)
directive
allows the remaining portion of the input to be treated as one string
in which line terminators appear as explicit characters. Regular expressions
may freely match through this sequence.

It's possible for a regular expression to match an empty string.
For instance, if the next input character is
.codn z ,
facing a
the regular expression
.codn /a?/ ,
there is a zero-character match:
the regular expression's state machine can reach an acceptance
state without consuming any characters. Examples:
.IP code:
.cblk
\ @A@/a?/@/.*/
.cble
.IP data:
.cblk
\ zzzzz
.cble
.IP result:
.cblk
\ A=""
.cble
.PP

.IP code:
.cblk
\ @{A /a?/}@B
.cble
.IP data:
.cblk
\ zzzzz
.cble
.IP result:
.cblk
\ A="", B="zzzz"
.cble
.PP

.IP code:
.cblk
\ @*A@/a?/
.cble
.IP data:
.cblk
\ zzzzz
.cble
.IP result:
.cblk
\ A="zzzzz"
.cble
.PP

In the first example, variable
.code @A
is followed by a regular expression
which can match an empty string. The expression faces the letter
.code "z"
at position 0 in the data line. A zero-character match occurs there,
therefore the variable
.code A
takes on the empty string. The
.code @/.*/
regular expression then consumes the line.

Similarly, in the second example, the
.code /a?/
regular expression faces a
.codn "z" ,
and thus yields an empty string which is bound to
.codn A .
Variable
.code @B
consumes the entire line.

The third example requests the longest match for the variable binding.
Thus, a search takes place for the rightmost position where the
regular expression matches. The regular expression matches anywhere,
including the empty string after the last character, which is
the rightmost place. Thus variable
.code A
fetches the entire line.

For additional information about the advanced regular expression
operators, NOTES ON EXOTIC REGULAR EXPRESSIONS below.

.SS* Directives
The general syntax of a directive is:

.cblk
.mets >> @ expr
.cble

where
.meta expr
stands for a parenthesized list of subexpressions. A subexpression
is a symbol, number, string literal, character literal, quasiliteral, regular
expression, or a parenthesized expression.  So, examples of syntactically valid
directives are:

.cblk
  @(banana)

  @(a b c (d e f))

  @(  a (b (c d) (e  ) ))

  @("apple" #\eb #\espace 3)

  @(a #/[a-z]*/ b)

  @(_ `@file.txt`)
.cble

A symbol has a slight more permissive lexical syntax than the
.meta bident
in the syntax
.cblk
.meti <> @{ bident }
.cble
introduced earlier. The
.code /
(slash) character may be part of an identifier, or even
constitute an entire identifier. In fact a symbol inside a
directive is a
.metn lident .
This is discussed in the Symbol Tokens section under TXR LISP.
A symbolmust not be a number; tokens that look like numbers are treated as
numbers and not symbols.

.SS* Character Literals

Character literals are introduced by the
.code #\e
syntax, which is either
followed by a character name, the letter
.code x
followed by hex digits,
the letter
.code o
followed by octal digits, or a single character. Valid character
names are:

.cblk
  nul                 linefeed            return
  alarm               newline             esc
  backspace           vtab                space
  tab                 page                pnul
.cble

For instance
.code #\eesc
denotes the escape character.

This convention for character literals is similar to that
of the Scheme language.  Note that
.code #\elinefeed
and
.code #\enewline
are the same
character. The
.code #\epnul
character is specific to \*(TX and denotes the
.code U+DC00
code in Unicode; the name stands for "pseudo-null", which is related to
its special function. For more information about this, see the section
"Character Handling and International Characters".

.SS* String Literals

String literals are delimited by double quotes.
A double quote within a string literal is encoded using
.cblk
\e"
.cble
and a backslash is encoded as
.codn \e\e .
Backslash escapes like
.code \en
and
.code \et
are recognized, as are hexadecimal escapes like
.code \exFF
or
.code \exxabc
and octal
escapes like
.codn \e123 .
Ambiguity between an escape and subsequent
text can be resolved by using trailing semicolon delimiter:
.str "\exabc;d"
is a string consisting of the character
.code "U+0ABC"
followed by
.strn "d" .
The semicolon
delimiter disappears. To write a literal semicolon immediately after a hex
or octal escape, write two semicolons, the first of which will be interpreted
as a delimiter. Thus,
.str "\ex21;;"
represents
.strn "!;" .

If the line ends in the middle of a literal, it is an error, unless the
last character is a backslash. This backslash is a special escape which does
not denote a character; rather, it indicates that the string literal continues
on the next line.  The backslash is deleted, along with whitespace which
immediately precedes it, as well as leading whitespace in the following line.
The escape sequence
.str "\e "
(backslash space) can be used to encode a significant space.

Example:

.cblk
  "foo   \e
   bar"

  "foo   \e
  \e bar"

  "foo\e  \e
   bar"
.cble

The first string literal is the string
.strn "foobar" .
The second two are
.strn "foo bar" .

.SS* Word List Literals

A word list literal (WLL) provides a convenient way to write a list of strings
when such a list can be given as whitespace-delimited words.

There are two flavors of the WLL: the regular WLL which begins with
.cblk
#"
.cble
(hash, double-quote) and the splicing list literal which begins with
.cblk
#*"
.cble
(hash, star, double-quote).

Both types are terminated by a double quote, which may be escaped
as
.cblk
\e"
.cble
in order to include it as a character. All the escaping conventions
used in string literals can be used in word literals.

Unlike in string literals, whitespace (tabs, spaces and newlines) is not
significant in word literals: it separates words.  Whitespace may be
escaped with a backslash in order to include it as a literal character.


Example:

.cblk
  #"abc def ghi"  --> notates ("abc" "def" "ghi")

  #"abc   def
  ghi"            --> notates ("abc" "def" "ghi")

  #"abc\e def ghi" --> notates ("abc def" "ghi")
.cble

A splicing word literal differs from a word literal in that it does not
produce a list of string literals, but rather it produces a sequence of string
literals that is merged into the surrounding syntax.  Thus, the following two
notations are equivalent:

.cblk
  (1 2 3 #*"abc def" 4 5 #"abc def")

  (1 2 3 "abc" "def" 4 5 ("abc" "def"))
.cble

The regular WLL produced a single list object, but the splicing
WLL expanded into multiple string literal objects.

.SS* String Quasiliterals

Quasiliterals are similar to string literals, except that they may
contain variable references denoted by the usual
.code @
syntax. The quasiliteral
represents a string formed by substituting the values of those variables
into the literal template. If
.code a
is bound to
.str "apple"
and
.code b
to
.strn "banana" ,
the quasiliteral
.code `one @a and two @{b}s`
represents the string
.strn "one apple and two bananas" .
A backquote escaped by a backslash represents
itself, and two consecutive
.code @
characters code for a literal
.codn @ .
There is no
.code \e@
escape.  Quasiliterals support the full output variable
syntax. Expressions within variables substitutions follow the evaluation rules
of \*(TL when the quasiliteral occurs in \*(TL, and the rules of
the \*(TX pattern language when the quasiliteral occurs in the pattern language.

Quasiliterals can be split into multiple lines in the same way as ordinary
string literals.

.SS* Quasiword List Literals

The quasiword list literals (QLL-s) are to quasiliterals what WLL-s are to
ordinary literals. (See the above section Word List Literals.)

A QLL combines the convenience of the WLL
with the power of quasistrings.

Just as in the case of WLL-s, there are two flavors of the
QLL: the regular QLL which begins with
.code #`
\ (hash, backquote) and the splicing QLL which begins with
.code #*`
\ (hash, star, backquote).

Both types are terminated by a backquote, which may be escaped
as
.code \e`
\ in order to include it as a character. All the escaping conventions
used in quasiliterals can be used in QLL.

Unlike in quasiliterals, whitespace (tabs, spaces and newlines) is not
significant in QLL: it separates words.  Whitespace may be
escaped with a backslash in order to include it as a literal character.

Note that the delimiting into words is done before the variable
substitution. If the variable a contains spaces, then
.code #`@a`
nevertheless
expands into a list of one item: the string derived from
.codn a .

Examples:

.cblk
  #`abc @a ghi`  --> notates (`abc` `@a` `ghi`)

  #`abc   @d@e@f
  ghi`            --> notates (`abc` `@d@e@f` `ghi`)

  #`@a\e @b @c` --> notates (`@a @b` `@c`)
.cble

A splicing QLL differs from an ordinary QLL in that it does not produce a list
of quasiliterals, but rather it produces a sequence of quasiliterals that is
merged into the surrounding syntax.

.SS* Numbers

\*(TX supports integers and floating-point numbers. 

An integer constant is made up of digits
.code 0
through
.codn 9 ,
optionally preceded by a
.code +
or
.code -
sign.

Examples:

.cblk
  123
  -34
  +0
  -0
  +234483527304983792384729384723234
.cble

An integer constant can also be specified in hexadecimal using the prefix
.code #x
followed by an optional sign, followed by hexadecimal digits:
.code 0
through
.code 9
and the upper or lower case letters
.code A
through
.codn F :

.cblk
  #xFF    ;; 255
  #x-ABC  ;; -2748
.cble

Similarly, octal numbers are supported with the prefix
.code #o
followed by octal digits:

.cblk
  #o777   ;; 511
.cble

and binary numbers can be written with a
.code #b
prefix:

.cblk
  #b1110  ;; 14
.cble

A floating-point constant is marked by the inclusion of a decimal point, the
exponential "e notation", or both. It is an optional sign, followed
by a mantissa consisting of digits, a decimal point, more digits, and then an
optional exponential notation consisting of the letter
.code "e"
or
.codn "E" ,
an optional
.code +
or
.code -
sign, and then digits indicating the exponent value.
In the mantissa, the digits are not optional. At least one digit must either
precede the decimal point or follow. That is to say, a decimal point by itself
is not a floating-point constant.

Examples:

.cblk
  .123
  123.
  1E-3
  20E40
  .9E1
  9.E19
  -.5
  +3E+3
.cble

Examples which are not floating-point constant tokens:

.cblk
  .      ;; consing dot
  123E   ;; the symbol 123E
  1.0E-  ;; syntax error: invalid floating point constant
  1.0E   ;; syntax error: invalid floating point constant
  1.E    ;; 1; consing dot; symbol E
  .e     ;; consing dot followed by symbol e
.cble

In \*(TX there is a special "dotdot" token consisting of two consecutive periods.
An integer constant followed immediately by dotdot is recognized as such; it is
not treated as a floating constant followed by a dot. That is to say,
.code 123..
does not mean
.code 123. .
(floating point
.code 123.0
value followed by dot token).  It means
.code 123 ..
(integer
.code 123
followed by
.code ..
token).

Dialect note: unlike in Common Lisp,
.code 123.
is not an integer, but the floating-point number
.codn 123.0 .

.SS* Comments

Comments of the form
.code @;
were already covered. Inside directives,
comments are introduced just by a
.code ;
character.

Example:

.cblk
  @(foo  ; this is a comment
    bar  ; this is another comment 
    )
.cble

This is equivalent to
.codn "@(foo bar)" .

.SS* Directives-driven Syntax

Some directives not only denote an expression, but are also involved in
surrounding syntax. For instance, the directive

.cblk
  @(collect)
.cble

not only denotes an expression, but it also introduces a syntactic phrase which
requires a matching
.code @(end)
directive. In other words,
.code @(collect)
is not only
an expression, but serves as a kind of token in a higher level phrase structure
grammar.

Usually if this type of "syntactic directive" occurs alone in a line, not
preceded or followed by other material, it is involved in a "vertical" (or line
oriented) syntax.

If such a directive is embedded in a line (has preceding or trailing material)
then it is in a horizontal syntactic and semantic context (character-oriented).

There is an exception: the definition of a horizontal function looks like this:

.cblk
  @(define name (arg))body material@(end)
.cble

Yet, this is considered one vertical item, which means that it does not match
a line of data. (This is necessary because all horizontal syntax matches
something within a line of data, which is undesirable for definitions.)

Many directives exhibit both horizontal and vertical syntax, with different but
closely related semantics. A few are vertical only, and some are
horizontal only.

A summary of the available directives follows:

.coIP @(eof)
Explicitly match the end of file. Fails if unmatched data remains in
the input stream.

.coIP @(eol)
Explicitly match the end of line. Fails if the current position is not the
end of a line. Also fails if no data remains (there is no current line).

.coIP @(next)
Continue matching in another file or other data source.

.coIP @(block)
Groups together a sequence of directives into a logical name block,
which can be explicitly terminated from within using
the
.code @(accept)
and
.code @(fail)
directives.
Blocks are discussed in the section BLOCKS below.

.coIP @(skip)
Treat the remaining query as a subquery unit, and search the lines (or
characters) of the input file until that subquery matches somewhere.  A skip is
also an anonymous block.

.coIP @(trailer)
Treat the remaining query or subquery as a match for a trailing context. That
is to say, if the remainder matches, the data position is not advanced.

.coIP @(freeform)
Treat the remainder of the input as one big string, and apply the following
query line to that string. The newline characters (or custom separators) appear
explicitly in that string.

.coIP @(fuzz)
The fuzz directive, inspired by the patch utility, specifies a partial
match for some lines.

.ccIP @ @(line) and @ @(chr)
These directives match a variable or expression against the current line
number or character position.

.coIP @(some)
Multiple clauses are each applied to the same input. Succeeds if at least one
of the clauses matches the input. The bindings established by earlier
successful clauses are visible to the later clauses.

.coIP @(all)
Multiple clauses are applied to the same input. Succeeds if and only if each
one of the clauses matches. The clauses are applied in sequence, and evaluation
stops on the first failure. The bindings established by earlier successful
clauses are visible to the later clauses.

.coIP @(none)
Multiple clauses are applied to the same input. Succeeds if and only if none of
them match. The clauses are applied in sequence, and evaluation stops on the
first success. No bindings are ever produced by this construct.

.coIP @(maybe)
Multiple clauses are applied to the same input. No failure occurs if none of
them match.  The bindings established by earlier successful clauses are visible
to the later clauses.

.coIP @(cases)
Multiple clauses are applied to the same input. Evaluation stops on the
first successful clause.

.coIP @(require)
The require directive is similar to the do directive: it evaluates one or more
\*(TL expressions.  If the result of the rightmost expression is nil,
then require triggers a match failure.  See the TXR LISP section far below.

.ccIP @, @(if) @, @(elif) and @ @(else)
The
.code if
directive with optional
.code elif
and
.code else
clauses is a syntactic sugar
which translates to a combination of
.code @(cases)
and
.codn @(require) .

.coIP @(choose)
Multiple clauses are applied to the same input. The one whose effect persists
is the one which maximizes or minimizes the length of a particular variable.

.coIP @(empty)
The
.code @(empty)
directive matches the empty string. It is useful in certain
situations, such as expressing an empty match in a directive that doesn't
accept an empty clause. The
.code @(empty)
syntax has another meaning in
.code @(output)
clauses, in conjunction with
.codn @(repeat) .

.meIP @(define < name >> ( args ...))
Introduces a function. Functions are discussed in the FUNCTIONS section below.

.coIP @(gather)
Searches text for matches for multiple clauses which may occur in arbitrary
order. For convenience, lines of the first clause are treated as separate
clauses.

.coIP @(collect)
Search the data for multiple matches of a clause. Collect the
bindings in the clause into lists, which are output as array variables.
The
.code @(collect)
directive is line oriented. It works with a multi-line
pattern and scans line by line. A similar directive called
.code @(coll)
works within one line.

A collect is an anonymous block.

.coIP @(and)
Separator of clauses for
.codn @(some) ,
.codn @(all) ,
.codn @(none) ,
.code @(maybe)
and
.codn @(cases) .
Equivalent to
.codn @(or) .
The choice is stylistic.

.coIP @(or)
Separator of clauses for
.codn @(some) ,
.codn @(all) ,
.codn @(none) ,
.code @(maybe)
and
.codn @(cases) .
Equivalent to
.codn @(and) .
The choice is stylistic.

.coIP @(end)
Required terminator for
.codn @(some) ,
.codn @(all) ,
.codn @(none) ,
.codn @(maybe) ,
.codn @(cases) ,
.codn @(if) ,
.codn @(collect) ,
.codn @(coll) ,
.codn @(output) ,
.codn @(repeat) ,
.codn @(rep) ,
.codn @(try) ,
.code @(block)
and
.codn @(define) .

.coIP @(fail)
Terminate the processing of a block, as if it were a failed match.
Blocks are discussed in the section BLOCKS below.

.coIP @(accept)
Terminate the processing of a block, as if it were a successful match.
What bindings emerge may depend on the kind of block: collect
has special semantics.  Blocks are discussed in the section BLOCKS below.

.coIP @(try)
Indicates the start of a try block, which is related to exception
handling, discussed in the EXCEPTIONS section below.

.ccIP @ @(catch) and @ @(finally)
Special clauses within
.codn @(try) .
See EXCEPTIONS below.

.ccIP @ @(defex) and @ @(throw)
Define custom exception types; throw an exception.  See EXCEPTIONS below.

.coIP @(assert)
The assert directive requires the following material to match, otherwise
it throws an exception. It is useful for catching mistakes or omissions
in parts of a query that are sure-fire matches.

.coIP @(flatten)
Normalizes a set of specified variables to one-dimensional lists. Those
variables which have scalar value are reduced to lists of that value.
Those which are lists of lists (to an arbitrary level of nesting) are converted
to flat lists of their leaf values.

.coIP @(merge)
Binds a new variable which is the result of merging two or more
other variables. Merging has somewhat complicated semantics.

.coIP @(cat)
Decimates a list (any number of dimensions) to a string, by catenating its
constituent strings, with an optional separator string between all of the
values.

.coIP @(bind)
Binds one or more variables against a value using a structural
pattern match. A limited form of unification takes place which can cause a
match to fail.

.coIP @(set)
Destructively assigns one or more existing variables using a structural
pattern, using syntax similar to bind. Assignment to unbound
variables triggers an error.

.coIP @(rebind)
Evaluates an expression in the current binding environment, and
then creates new bindings for the variables in the structural pattern.
Useful for temporarily overriding variable values in a scope.

.coIP @(forget)
Removes variable bindings.

.coIP @(local)
Synonym of
.codn @(forget) .

.coIP @(output)
A directive which encloses an output clause in the query. An output section
does not match text, but produces text. The directives above are not
understood in an output clause.

.coIP @(repeat)
A directive understood within an @(output) section, for repeating multi-line
text, with successive substitutions pulled from lists. The directive
.code @(rep)
produces iteration over lists horizontally within one line.

.coIP @(deffilter)
This directive is used for defining named filters, which are useful
for filtering variable substitutions in output blocks. Filters are useful
when data must be translated between different representations that
have different special characters or other syntax, requiring escaping
or similar treatment. Note that it is also possible to use a function
as a filter. See Function Filters below.

.coIP @(filter)
The filter directive passes one or more variables through a given
filter or chain or filters, updating them with the filtered values.

.coSS @ @(load) and @ @(include)
These directives allow \*(TX programs to be modularized. They bring in
code from a file, in two different ways.

.coIP @(do)
The do directive is used to evaluate \*(TL expressions, discarding their
result values. See the TXR LISP section far below.

.PP

.SH* INPUT SCANNING AND DATA MANIPULATION

.dir next

The
.code next
directive indicates that the remaining directives in the current block
are to be applied against a new input source.

It can only occur by itself as the only element in a query line,
and takes various arguments, according to these possibilities:

.cblk
.mets @(next)
.mets @(next << source )
.mets @(next < source :nothrow)
.mets @(next :args)
.mets @(next :env)
.mets @(next :list << expr )
.mets @(next :string << expr )
.cble

The lone
.code @(next)
without arguments specifies that subsequent directives
will match inside the next file in the argument list which was passed
to \*(TX on the command line.

If
.meta source
is given, it must be string-valued expression which denotes an
input source; it may be a string literal, quasiliteral or a string-valued
variable.  For instance, if variable
.code A
contains the text
.strn "data" ,
then
.code  @(next A)
means switch to the file called
.strn "data" ,
and
.code @(next `@A.txt`)
means to switch to the file
.strn "data.txt" .

If the input source cannot be opened for whatever reason,
\*(TX throws an exception (see EXCEPTIONS below). An unhandled exception will
terminate the program.  Often, such a drastic measure is inconvenient;
if
.code @(next)
is invoked with the
.code :nothrow
keyword, then if the input
source cannot be opened, the situation is treated as a simple
match failure.

The variant
.code @(next :args)
means that the remaining command line arguments are to
be treated as a data source. For this purpose, each argument is considered to
be a line of text. The argument list does include that argument which specifies
the file that is currently being processed or was most recently processed.
As the arguments are matched, they are consumed. This means that if a
.code @(next)
directive without
arguments is executed in the scope of
.codn "@(next :args)" ,
it opens the file named
by the first unconsumed argument.

To process arguments, and then continue with the original file and argument
list, wrap the argument processing in a
.codn @(block) .
When the block terminates, the input source and argument list are restored
to what they were before the block.

The variant
.code @(next :env)
means that the list of process environment variables
is treated as a source of data. It looks like a text file stream
consisting of lines of the form
.strn "name=value" .
If this feature is not available
on a given platform, an exception is thrown.

The syntax
.cblk
.meti @(next :list << expr )
.cble
treats expression
.meta expr
as a source of
text. The value of
.meta expr
is flattened to a simple list in a way similar to the
.code @(flatten)
directive.  The resulting list is treated as if it were the
lines of a text file: each element of the list must be a string,
which represents a line.  If the strings happen contain embedded newline
characters, they are a visible constituent of the line, and do not act as line
separators.

The syntax
.cblk
.meti @(next :string << expr )
.cble
treats expression
.meta expr
as a source of text. The value of the expression must be a string. Newlines in
the string are interpreted as line terminators.

A string which is not terminated by a newline is tolerated, so that:

.cblk
  @(next :string "abc")
  @a
.cble

binds
.code a
to
.strn "abc" .
Likewise, this is also the case with input files and other
streams whose last line is not terminated by a newline.

However, watch out for empty strings, which are analogous to a correctly formed
empty file which contains no lines:

.cblk
  @(next :string "")
  @a
.cble

This will not bind
.code a
to
.strn "" ;
it is a matching failure.  The behavior of
.code :list
is
different. The query

.cblk
  @(next :list "")
  @a
.cble

binds
.code a
to
.strn "" .
The reason is that under
.code :list
the string
.str ""
is flattened to
the list
.cblk
("")
.cble
which is not an empty input stream, but a stream consisting of
one empty line.

Note that the
.code @(next)
directive only redirect the source of input over the scope of subquery in which
the next directive appears, not necessarily all remaining directives.  For
example, the following query looks for the line starting with
.str "xyz"
at the top of the file
.strn "foo.txt" ,
within a some
directive.  After the
.code @(end)
which terminates the
.codn @(some) ,
the
.str "abc"
is matched in the previous input stream which was in effect before
the
.code
@(next)
directive:

.cblk
  @(some)
  @(next "foo.txt")
  xyz@suffix
  @(end)
  abc
.cble

However, if the
.code @(some)
subquery successfully matched
.str "xyz@suffix"
within the
file
.codn foo.text ,
there is now a binding for the
.code suffix
variable, which
is visible to the remainder of the entire query. The variable bindings
survive beyond the clause, but the data stream does not.

The
.code @(next)
directive supports the file name conventions as the command
line. The name
.code -
means standard input. Text which starts with a
.code !
is
interpreted as a shell command whose output is read like a file.  These
interpretations are applied after variable substitution. If the file is
specified as
.codn @a ,
but the variable a expands to
.strn "!echo foo" ,
then the output of
the
.str "echo foo"
command will be processed.

.dir skip

The
.code skip
directive considers the remainder of the query as a search
pattern. The remainder is no longer required to strictly match at the
current line in the current input stream. Rather, the current stream is searched,
starting with the current line, for the first line where the entire remainder
of the query will successfully match. If no such line is found, the
.code skip
directive fails. If a matching position is found, the remainder of
the query is processed from that point.

Of course, the remainder of the query can itself contain skip directives.
Each such directive performs a recursive subsearch.

Skip comes in vertical and horizontal flavors. For instance, skip and match the
last line:

.cblk
  @(skip)
  @last
  @(eof)
.cble

Skip and match the last character of the line:

.cblk
  @(skip)@{last 1}@(eol)
.cble

The skip directive has two optional arguments. If the first argument is a
number, its value limits the range of lines scanned for a match. Judicious use
of this feature can improve the performance of queries.

Example: scan until
.str "size: @SIZE"
matches, which must happen within
the next 15 lines:

.cblk
  @(skip 15)
  size: @SIZE
.cble

Without the range limitation skip will keep searching until it consumes
the entire input source.  In a horizontal
.codn skip ,
the range-limiting numeric argument is expressed in characters, so that

.cblk
  abc@(skip 5)def
.cble

means: there must be a match for
.str "abc"
at the start of the line, and then within the next five characters,
there must be a match for
.strn "def" .

Sometimes a skip is nested within a
.codn collect ,
or
following another skip. For instance, consider:

.cblk
  @(collect)
  begin @BEG_SYMBOL
  @(skip)
  end @BEG_SYMBOL
  @(end)
.cble

The above
.code collect
iterates over the entire input. But, potentially, so does
the embedded
.codn skip .
Suppose that
.str "begin x"
is matched, but the data has no
matching
.strn "end x" .
The skip will search in vain all the way to the end of the
data, and then the collect will try another iteration back at the
beginning, just one line down from the original starting point.  If it is a
reasonable expectation that an
.code "end x"
occurs 15 lines of a
.strn "begin x" ,
this can be specified instead:

.cblk
  @(collect)
  begin @BEG_SYMBOL
  @(skip 15)
  end @BEG_SYMBOL
  @(end)
.cble

If the symbol
.code nil
is used in place of a number, it means to scan
an unlimited range of lines; thus,
.code @(skip nil)
is equivalent to
.codn @(skip) .

If the symbol
.code :greedy
is used, it changes the semantics of the skip
to longest match semantics.  For instance, match the last three space-separated
tokens of the line:

.cblk
  @(skip :greedy) @a @b @c
.cble

Without
.codn :greedy ,
the variable
.code @c
will can match multiple tokens,
and end up with spaces in it, because nothing follows
.code @c
and so it matches from any position which follows a space to the
end of the line.  Also note the space in front of
.codn @a .
Without this
space,
.code @a
will get an empty string.

A line oriented example of greedy skip: match the last line without
using
.codn @eof :

.cblk
  @(skip :greedy)
  @last_line
.cble

There may be a second numeric argument. This specifies a minimum
number of lines to skip before looking for a match. For instance,
skip 15 lines and then search indefinitely for
.codn "begin ..." :

.cblk
  @(skip nil 15)
  begin @BEG_SYMBOL
.cble

The two arguments may be used together. For instance, the following
matches if, and only if, the 15th line of input starts with
.codn "begin " :

.cblk
  @(skip 1 15)
  begin @BEG_SYMBOL
.cble

Essentially,
.cblk
.meti @(skip 1 << n )
.cble
means "hard skip by
.meta n
lines".
.code @(skip 1 0)
is the same as
.codn "@(skip 1)" ,
which is a noop, because it means: "the remainder of the query must match
starting on the very next line", or, more briefly, "skip exactly zero lines",
which is the behavior if the skip directive is omitted altogether.

Here is one trick for grabbing the fourth line from the bottom of the input:

.cblk
  @(skip)
  @fourth_from_bottom
  @(skip 1 3)
  @(eof)
.cble

Or using greedy skip:

.cblk
  @(skip :greedy)
  @fourth_from_bottom
  @(skip 1 3)
.cble

Nongreedy skip with the
.code @(eof)
has a slight advantage because the greedy skip
will keep scanning even though it has found the correct match, then backtrack
to the last good match once it runs out of data. The regular skip with explicit
.code @(eof)
will stop when the
.code @(eof)
matches.

.SS* Reducing Backtracking with Blocks

.code skip
can consume considerable CPU time when multiple skips are nested.  Consider:

.cblk
  @(skip)
  A
  @(skip)
  B
  @(skip)
  C
.cble

This is actually nesting: the second a third skips occur within the body of the
first one, and thus this creates nested iteration. \*(TX is searching for the
combination of skips which find match the pattern of lines
.codn A ,
.code B
and
.codn C ,
with
backtracking behavior. The outermost skip marches through the data until it
finds
.codn A ,
followed by a pattern match for the second skip. The second skip
iterates within to find
.codn B ,
followed by the third skip, and the third skip
iterates to find
.codn C .
If there is only one line
.codn A ,
and one
.codn B ,
then this is reasonably fast. But suppose there are many lines matching
.code A
and
.codn B ,
giving rise to a large number combinations of skips which match
.code A
and
.codn B ,
and yet do not find a match for
.codn C ,
triggering backtracking. The nested stepping which tries
the combinations of
.code A
and
.code B
can give rise to a considerable running time.

One way to deal with the problem is to unravel the nesting with the help of
blocks. For example:

.cblk
  @(block)
  @  (skip)
  A
  @(end)
  @(block)
  @  (skip)
  B
  @(end)
  @(skip)
  C
.cble

Now the scope of each skip is just the remainder of the block in which it
occurs. The first skip finds
.codn A ,
and then the block ends. Control passes to the
next block, and backtracking will not take place to a block which completed
(unless all these blocks are enclosed in some larger construct which
backtracks, causing the blocks to be re-executed.

Of course, this rewrite is not equivalent, and cannot be used for instance
in backreferencing situations such as:

.cblk
  @;
  @; Find three lines anywhere in the input which are identical.
  @;
  @(skip)
  @line
  @(skip)
  @line
  @(skip)
  @line
.cble

This example depends on the nested search-within-search semantics.

.dir trailer

The
.code trailer
directive introduces a trailing portion of a query or subquery
which matches input material normally, but in the event of a successful match,
does not advance the current position. This can be used, for instance, to
cause
.code @(collect)
to match partially overlapping regions.

Example:

.cblk
  @(collect)
  @line
  @(trailer)
  @(skip)
  @line
  @(end)
.cble

This script collects each line which has a duplicate somewhere later
in the input. Without the
.code @(trailer)
directive, this does not work properly
for inputs like:

.cblk
  111
  222
  111
  222
.cble

Without
.codn @(trailer) ,
the first duplicate pair constitutes a match which
spans over the
.codn 222 .
After that pair is found, the matching continues
after the second
.codn 111 .

With the
.code @(trailer)
directive in place, the collect body, on each
iteration, only consumes the lines matched prior to
.codn @(trailer) .

.dir freeform

The
.code freeform
directive provides a useful alternative to
\*(TX's line-oriented matching discipline. The freeform directive treats all
remaining input from the current input source as one big line. The query line
which immediately follows freeform is applied to that line.

The syntax variations are:

.cblk
  @(freeform)
  ... query line ..

.mets @(freeform << number )
  ... query line ..

.mets @(freeform << string )
  ... query line ..

.mets @(freeform < number << string )
  ... query line ..
.cble

If
.meta number
and
.meta string
are both present, they may be given in either order.

If a numeric argument is given, it limits the range of lines which are combined
together. For instance
.code @(freeform 5)
means to only consider the next five lines
to to be one big line. Without a numeric argument, freeform is "bottomless". It
can match the entire file, which creates the risk of allocating a large amount
of memory.

If a string argument is given, it specifies a custom line terminator. The
default terminator is
.strn "\en" .
The terminator does not have to be one character long.

Freeform does not convert the entire remainder of the input into one big line
all at once, but does so in a dynamic, lazy fashion, which takes place as the
data is accessed. So at any time, only some prefix of the data exists as a flat
line in which newlines are replaced by the terminator string, and the remainder
of the data still remains as a list of lines.

After the subquery is applied to the virtual line, the unmatched remainder
of that line is broken up into multiple lines again, by looking for and
removing all occurrences of the terminator string within the flattened portion.

Care must be taken if the terminator is other than the default
.strn "\en" .
All occurrences of the terminator string are treated as line terminators in
the flattened portion of the data, so extra line breaks may be introduced.
Likewise, in the yet unflattened portion, no breaking takes place, even if
the text contains occurrences of the terminator string. The extent of data which
is flattened, and the amount of it which remains, depends entirely on the
query line underneath
.codn @(flatten) .

In the following example, lines of data are flattened using $ as the line
terminator.
.IP code:
.cblk
\ @(freeform "$")
 @a$@b:
 @c
 @d
.cble

.IP data:
.cblk
\ 1
 2:3
 4
.cble

.IP "output (\f[4]-B\f[]):"
.cblk
\ a="1"
 b="2"
 c="3"
 d="4"
.cble
.PP

The data is turned into the virtual line
.codn 1$2:3$4$ .
The
.code @a$@b:
subquery matches
the
.code 1$2:
portion, binding
.code a
to
.strn 1 ,
and
.code b
to
.strn 2 .
The remaining portion
.code 3$4$
is then split into separate lines again according to the line terminator
.codn $i :

.cblk
  3
  4
.cble

Thus the remainder of the query

.cblk
  @c
  @d
.cble

faces these lines, binding
.code c
to
.code 3
and
.code d
to
.codn 4 .
Note that since the data
does not contain dollar signs, there is no ambiguity; the meaning may be
understood in terms of the entire data being flattened and split again.

In the following example,
.code freeform
is used to solve a tokenizing problem.  The
Unix password file has fields separated by colons. Some fields may be empty.
Using freeform, we can join the password file using
.str ":"
as a terminator.
By restricting freeform to one line, we can obtain each line of the password
file with a terminating
.strn ":" ,
allowing for a simple tokenization, because
now the fields are colon-terminated rather than colon-separated.

Example:

.cblk
  @(next "/etc/passwd")
  @(collect)
  @(freeform 1 ":")
  @(coll)@{token /[^:]*/}:@(end)
  @(end)
.cble

.dir fuzz

The
.code fuzz
directive allows for an imperfect match spanning a set number of
lines. It takes two arguments, both expressions that should evaluate
to integers:

.cblk
  @(fuzz m n)
  ...
.cble

This expresses that over the next n query lines, the matching strictness
is relaxed a little bit. Only m out of those n lines have to match.
Afterward, the rest of the query follows normal, strict processing.

In the degenerate situation that there are fewer than n query lines following
the
.code fuzz
directive, then m of them must succeed nevertheless. (If there
are fewer than m, then this is impossible.)

.dirs line chr

The
.code line
and
.code chr
directives perform binding between the current input line number or character
position within a line, against an expression or variable:

.cblk
  @(line 42)
  @(line x)
  abc@(chr 3)def@(chr y)
.cble

The directive
.code @(line 42)
means "match the current input line number against the integer 42". If
the current line is 42, then the directive matches, otherwise it fails.
.code line
is a vertical directive which doesn't consume a line of input. Thus,
the following matches at the beginning of an input stream, and
.code x
ends up bound to the first line of input:

.cblk
  @(line 1)
  @(line 1)
  @(line 1)
  @x
.cble

The directive
.code @(line x)
binds variable
.code x
to the current input line number, if
.code x
is an unbound variable. If
.code x
is already bound, then the value of
.code x
must match the current line number, otherwise the directive fails.

The
.code chr
directive is similar to
.code line
except that it's a horizontal directive, and matches the character position
rather than the line position. Character positions are measured from zero,
rather than one.
.code chr
does not consume a character. Hence the two occurrences of
.code chr
in the following example both match, and
.code x
takes the entire line of input:

.cblk
  @(chr 0)@(chr 0)@x
.cble

The argument of
.code line
or
.code chr
may be a
.codn @ -delimited
Lisp expression. This is useful for matching computed lines or
character positions:

.cblk
  @(line @(+ a (* b c)))
.cble

.dirs some all none maybe cases choose

These directives, called the parallel directives, combine multiple subqueries,
which are applied at the same input position, rather than to consecutive input.

They come in vertical (line mode) and horizontal (character mode) flavors.

In horizontal mode, the current position is understood to be a character
position in the line being processed. The clauses advance this character
position by moving it to the right.  In vertical mode, the current position is
understood to be a line of text within the stream. A clause advances the
position by some whole number of lines.

The syntax of these parallel directives follows this example:

.cblk
  @(some)
  subquery1
  .
  .
  .
  @(and)
  subquery2
  .
  .
  .
  @(and)
  subquery3
  .
  .
  .
  @(end)
.cble

And in horizontal mode:

.cblk
  @(some)subquery1...@(and)subquery2...@(and)subquery3...@(end)
.cble

Long horizontal lines can be broken up with line continuations, allowing the
above example to be written like this, which is considered a single logical
line:

.cblk
  @(some)@\e
     subquery1...@\e
  @(and)@\e
     subquery2...@\e
  @(and)@\e
     subquery3...@\e
  @(end)
.cble

The
.codn @(some) ,
.codn @(all) ,
.codn @(none) ,
.codn @(maybe) ,
.code @(cases)
or
.code @(choose)
must be followed
by at least one subquery clause, and be terminated by
.codn @(end) .
If there are two
or more subqueries, these additional clauses are indicated by
.code @(and)
or
.codn @(or) ,
which are interchangeable.  The separator and terminator directives also must
appear as the only element in a query line.

The
.code choose
directive requires keyword arguments. See below.

The syntax supports arbitrary nesting. For example:

.cblk
  QUERY:            SYNTAX TREE:

  @(all)            all -+
  @  (skip)              +- skip -+
  @  (some)              |        +- some -+
  it                     |        |        +- TEXT
  @  (and)               |        |        +- and
  @    (none)            |        |        +- none -+
  was                    |        |        |        +- TEXT
  @    (end)             |        |        |        +- end
  @  (end)               |        |        +- end
  a dark                 |        +- TEXT
  @(end)                 *- end
.cble

nesting can be indicated using whitespace between
.code @
and the
directive expression. Thus, the above is an
.code @(all)
query containing a
.code @(skip)
clause which applies to a
.code @(some)
that is followed by the text line
.strn "a dark" .
The
.code @(some)
clause combines the text line
.strn it ,
and a
.code @(none)
clause which contains just one clause consisting of the line
.strn was .

The semantics of the parallel directives is:

.coIP @(all)
Each of the clauses is matched at the current position. If any of the
clauses fails to match, the directive fails (and thus does not produce
any variable bindings). Clauses following the failed directive are not
evaluated. Bindings extracted by a successful clause are visible to the clauses
which follow, and if the directive succeeds, all of the combined bindings
emerge.

.meIP @(some [ :resolve >> ( var ...) ])
Each of the clauses is matched at the current position. If any of the clauses
succeed, the directive succeeds, retaining the bindings accumulated by the
successfully matching clauses.  Evaluation does not stop on the first successful
clause. Bindings extracted by a successful clause are visible to the clauses
which follow.

The
.code :resolve
parameter is for situations when the
.code @(some)
directive has
multiple clauses that need to bind some common variables to different
values: for instance, output parameters in functions. Resolve takes
a list of variable name symbols as an argument.  This is called the
resolve set. If the clauses of
.code @(some)
bind variables in the resolve
set, those bindings are not visible to later clauses.  However, those
bindings do emerge out of the
.code @(some)
directive as a whole.
This creates a conflict: what if two or more clauses introduce
different bindings for a variable in the resolve set?
This is why it is called the resolve set: conflicts for variables in the
resolve set are automatically resolved in favor of later directives.

Example:

.cblk
  @(some :resolve (x))
  @  (bind a "a")
  @  (bind x "x1")
  @(or)
  @  (bind b "b")
  @  (bind x "x2")
  @(end)
.cble

Here, the two clauses both introduce a binding for
.codn x .
Without the
.code :resolve
parameter, this would mean that the second clause fails, because
.code x
comes in
with the value
.strn x1 ,
which does not bind with
.strn x2 .
But because
.code x
is placed
into the resolve set, the second clause does not see the 
.str x1
binding. Both
clauses establish their bindings independently creating a conflict over
.codn x .
The conflict is resolved in favor of the second clause, and so the bindings
which emerge from the directive are:

.cblk
  a="a"
  b="b"
  x="x2"
.cble

.coIP @(none)
Each of the clauses is matched at the current position. The
directive succeeds only if all of the clauses fail. If
any clause succeeds, the directive fails, and subsequent clauses are not
evaluated. Thus, this directive never produces variable bindings, only matching
success or failure.

.coIP @(maybe)
Each of the clauses is matched at the current position.  The directive always
succeeds, even if all of the clauses fail.  Whatever bindings are found in any
of the clauses are retained. Bindings extracted by any successful clause are
visible to the clauses which follow.

.coIP @(cases)
Each of the clauses is matched at the current position.
The clauses are matched, in order, at the current position.
If any clause matches, the matching stops and the bindings
collected from that clause are retained. Any remaining clauses
after that one are not processed. If no clause matches, the
directive fails, and produces no bindings.

.meIP @(choose [ :longest < var | :shortest < var ])
Each of the clauses is matched at the current position in order. In this
construct, bindings established by an earlier clause are not visible to later
clauses.  Although any or all of the clauses can potentially match, the clause
which succeeds is the one which maximizes or minimizes the length of the
text bound to the specified variable. The other clauses have no effect.

For all of the parallel directives other than
.code @(none)
and
.codn @(choose) ,
the query
advances the input position by the greatest number of lines that match in any
of the successfully matching subclauses that are evaluated.  The
.code @(none)
directive does not advance the input position.

For instance if there are two subclauses, and one of them matches three lines,
but the other one matches five lines, then the overall clause is considered to
have made a five line match at its position. If more directives follow, they
begin matching five lines down from that position.

.dir require

The syntax of
.code @(require)
is:

.cblk
.mets @(require << lisp-expression )
.cble

The require directive evaluates a \*(TL expression. (See TXR LISP far
below.) If the expression yields a true value, then it succeeds, and matching
continues with the directives which follow. Otherwise the directive fails.

In the context of the
.code require
directive, the expression should not be introduced by the
.code @
symbol; it is expected to be a Lisp expression.

Example:

.cblk
  @; require that 4 is greater than 3
  @; This succeeds; therefore, @a is processed
  @(require (> (+ 2 2) 3))
  @a
.cble

.dir if

The syntax of the
.code if
directive can be exemplified as follows:

.cblk
.mets @(if << lisp-expr )
  .
  .
  .
.mets @(elif << lisp-expr )
  .
  .
  .
.mets @(elif << lisp-expr )
  .
  .
  .
  @(else)
  .
  .
  .
  @(end)
.cble

The
.code @(elif)
and
.code @(else)
clauses are all optional. If
.code @(else)
is present, it must be
last, before
.codn @(end) ,
after any
.code @(elif)
clauses. Any of the clauses may be empty.

See the \*(TL section about \*(TL expressions. In this directive,
\*(TL expressions are not introduced by the
.code @
symbol, just like in the
.code require
directive.

For example:

.cblk
  @(if (> (length str) 42))
  foo: @a @b
  @(else)
  {@c}
  @(end)
.cble

In this example, if the length of the variable
.code str
is greater than
.codn 42 ,
then matching continues with
.strn "foo: @a b" ,
otherwise it proceeds with
.codn {@c} .

The
.code if
directive is actually a syntactic sugar which is translated to
.code @(cases)
and
.codn @(require) .
That is to say, the following pattern:

.cblk
  @(cases)
.mets @(require << lisp-expr-1 )
  A
  @(or)
.mets @(require << lisp-expr-2 )
  B
  @(or)
  C
  @(end)
.cble

corresponds to the somewhat shorter and clearer:

.cblk
.mets @(if << lisp-expr-1 )
  A
.mets @(elsif << lisp-expr-2 )
  B
  @(else)
  C
  @(end)
.cble

.dir gather

Sometimes text is structured as items that can appear in an arbitrary order.
When multiple matches need to be extracted, there is a combinatorial explosion
of possible orders, making it impractical to write pattern matches for all
the possible orders.

The
.code gather
directive is for these situations. It specifies multiple clauses
which all have to match somewhere in the data, but in any order.

For further convenience, the lines of the first clause of the
.code gather
directive
are implicitly treated as separate clauses.

The syntax follows this pattern

.cblk
  @(gather)
  one-line-query1
  one-line-query2
  .
  .
  .
  one-line-queryN
  @(and)
  multi
  line
  query1
  .
  .
  .
  @(and)
  multi
  line
  query2
  .
  .
  .
  @(end)
.cble

Of course the multi-line clauses are optional.   The
.code gather
directive takes
keyword parameters, see below.

.coNP The @ until / @ last clause in @ gather

Similarly to
.codn collect ,
.code gather
has an optional
.cod3 until / last
clause:

.cblk
  @(gather)
  ...
  @(until)
  ...
  @(end)
.cble

How gather works is that the text is searched for matches for the single line
and multi-line queries. The clauses are applied in the order in which they appear.
Whenever one of the clauses matches, any bindings it produces are retained and
it is removed from further consideration. Multiple clauses can match at the
same text position.  The position advances by the longest match from among the
clauses which matched.  If no clauses match, the position advances by one line.
The search stops when all clauses are eliminated, and then the cumulative
bindings are produced.  If the data runs out, but unmatched clauses remain, the
directive fails.

Example: extract several environment variables, which do not appear in a particular
order:

.cblk
  @(next :env)
  @(gather)
  USER=@USER
  HOME=@HOME
  SHELL=@SHELL
  @(end)
.cble

If the until or last clause is present and a match occurs, then the matches
from the other clauses are discarded and the gather terminates. The difference
between
.cod3 until / last
is that any bindings bindings established in last are
retained, and the input position is advanced past the matched material.
The
.cod3 until / last
clause has visibility to bindings established in the
previous clauses in that same iteration, even though those bindings
end up thrown away.

.coNP Keyword parameters in @ gather
The
.code gather
directive accepts the keyword parameter
.codn :vars .
The argument to vars is
a list of required and optional variables.  Optional variables are denoted by
the specification of a default value. Example:

.cblk
  @(gather :vars (a b c (d "foo")))
  ...
  @(end)
.cble

Here,
.codn a ,
.codn b ,
.code c
and
.code e
are required variables, and
.code d
is optional.  Variable
.code e
is
required because its default value is the empty list
.codn () ,
same as the symbol
.codn nil .

The presence of
.code :vars
changes the behavior in three ways.

Firstly, even if all the clauses in the gather match successfully and are
eliminated, the directive will fail if the required variables do not have
bindings. It doesn't matter whether the bindings are existing, or whether they
are established by the gather.

Secondly, if some of the clauses of the gather did not match, but all
of the required variables have bindings, then the directive succeeds.
Without the presence of
.codn :vars ,
it would fail in this situation.

Thirdly, if
.code gather
succeeds (all required variables have bindings),
then all of the optional variables which do not have bindings are given
bindings to their default values.

.dir collect

The syntax of the
.code collect
directive is:

.cblk
  @(collect)
  ... lines of subquery
  @(end)
.cble

or with an until or last clause:

.cblk
  @(collect)
  ... lines of subquery: main clause
  @(until)
  ... lines of subquery: until clause
  @(end)

  @(collect)
  ... lines of subquery: main clause
  @(last)
  ... lines of subquery: last clause
  @(end)
.cble

The
.code repeat
symbol may be specified instead of
.codn collect ,
which changes the meaning, see below:

.cblk
  @(repeat)
  ... lines of subquery
  @(end)
.cble

The subquery is matched repeatedly, starting at the current line.
If it fails to match, it is tried starting at the subsequent line.
If it matches successfully, it is tried at the line following the
entire extent of matched data, if there is one. Thus, the collected regions do
not overlap. (Overlapping behavior can be obtained: see the
.code @(trailer)
directive).

Unless certain keywords are specified, or unless the collection is explicitly
failed with
.codn @(fail) ,
it always succeeds, even if it collects nothing,
and even if the
.cod3 until / last
clause never finds a match.

If no
.cod3 until / last
last clause is specified, and the collect is not limited
using parameters, the collection is unbounded: it consumes the entire data
file. If any query material follows such the
.code collect
clause, it will fail if it
tries to match anything in the current file; but of course, it is possible to
continue matching in another file by means of
.codn @(next) .

.coNP The @ until / @ last clause in @ collect

If an
.cod3 until / last
last clause is specified, the collection stops when that clause
matches at the current position.

If an
.code until
clause terminates collect, no bindings are collected at that
position, even if the main clause matches at that position also. Moreover, the
position is not advanced.  The remainder of the query begins matching at that
position.

If a last clause terminates collect, the behavior is different. Any bindings
captured by the main clause are thrown away, just like with the until clause.
However, the bindings in the last clause itself survive, and the position is
advanced to skip over that material.

Example:
.IP code:
.cblk
\ @(collect)
 @a
 @(until)
 42
 @b
 @(end)
 @c
.cble
.IP data:
.cblk
\ 1
 2
 3
 42
 5
 6
.cble
.IP result:
.cblk
\ a[0]="1"
 a[1]="2"
 a[2]="3"
 c="42"
.cble
.PP

The line
.code 42
is not collected, even though it matches
.con @a .
Furthermore,
the
.code @(until)
does not advance the position, so variable
.code c
takes
.codn 42 .

If the
.code @(until)
is changed to
.code @(last)
the output will be different:
.IP result:
.cblk
\ a[0]="1"
 a[1]="2"
 a[2]="3"
 b="5"
 c="6"
.cble
.PP

The
.code 42
is not collected into the a list, just like before. But now
the binding captured by
.code @b
emerges. Furthermore, the position advances
so variable now takes
.codn 6 .

The binding variables within the clause of a collect are treated specially.
The multiple matches for each variable are collected into lists,
which then appear as array variables in the final output.

Example:
.IP code:
.cblk
\ @(collect)
 @a:@b:@c
 @(end)
.cble
.IP data:
.cblk
\ John:Doe:101
 Mary:Jane:202
 Bob:Coder:313
.cble
.IP result:
.cblk
\ a[0]="John"
 a[1]="Mary"
 a[2]="Bob"
 b[0]="Doe"
 b[1]="Jane"
 b[2]="Coder"
 c[0]="101"
 c[1]="202"
 c[2]="313"
.cble
.PP

The query matches the data in three places, so each variable becomes
a list of three elements, reported as an array.

Variables with list bindings may be referenced in a query. They denote a
multiple match. The
.code -D
command line option can establish a one-dimensional
list binding.

The clauses of
.code collect
may be nested.   Variable matches collated into lists in an
inner collect, are again collated into nested lists in the outer collect.
Thus an unbound variable wrapped in N nestings of
.code @(collect)
will
be an N-dimensional list. A one dimensional list is a list of strings;
a two dimensional list is a list of lists of strings, etc.

It is important to note that the variables which are bound within the main
clause of a collect. That is, the variables which are subject to
collection appear, within the collect, as normal one-value bindings. The
collation into lists happens outside of the collect. So for instance in the
query:

.cblk
 @(collect)
 @x=@x
 @(end)
.cble

The left
.code @x
establishes a binding for some material preceding an equal sign.
The right
.code @x
refers to that binding. The value of
.cod @x
is different in each
iteration, and these values are collected. What finally comes out of the
collect clause is a single variable called
.code x
which holds a list containing each value that
was ever instantiated under that name within the collect clause.

Also note that the until clause has visibility over the bindings
established in the main clause. This is true even in the terminating
case when the until clause matches, and the bindings of the main clause
are discarded.

.coNP Keyword Parameters in @ collect
By default,
.code collect
searches the rest of the input indefinitely,
or until the
.cod3 until / last
clause matches. It skips arbitrary amounts of
nonmatching material before the first match, and between matches.

Within the
.code @(collect)
syntax, it is possible to specify some useful keyword
parameters for additional control of the behavior. For instance

.cblk
  @(collect :maxgap 5)
.cble

means that the collect will terminate if it does not find a match within five
lines of the starting position, or if more than five lines are skipped since
any successful match. A
.code :maxgap
of
.code 0
means that the collected regions must be
adjacent. For instance:

.cblk
  @(collect :maxgap 0)
  M @a
  @(end)
.cble

means: from here, collect consecutive lines of the form
.strn "M ..." .
This will not
search for the first such line, nor will it skip lines which do not match this
form.

Other keywords are
.codn :mingap ,
and
.codn :gap .
The
.code :mingap
keyword specifies a minimum
gap between matches, but has no effect on the distance to the first match. The
.code :gap
keyword effectively specifies
.code :mingap
and
.code :maxgap
at the same time, and can only be
used if these other two are not used. Thus:

.cblk
  @(collect :gap 1)
  @a
  @(end)
.cble

means collect every other line starting with the current line. Several
other supported keywords are
.codn :times ,
.codn :mintimes ,
.code :maxtimes
and
.codn :lines .
The shorthand
.code :times N
means the same thing as
.codn ":mintimes N :maxtimes N" .
These specify how many matches should be collected. If there are fewer
than
.code :mintimes
matches, the collect fails. If
.code :maxtimes
matches are collected,
collect stops collecting immediately. Example:

.cblk
  @(collect :times 3)
  @a @b
  @(end)
.cble

This will collect a match for
.str @a @b
exactly three times. If three
matches are not found, it will fail.

The
.code :lines
parameter specifies the upper bound on how many lines
should be scanned by collect, measuring from the starting position.
The extent of the collect body is not counted. Example:

.cblk
  @(collect :lines 2)
  foo: @a
  bar: @b
  baz: @c
  @(end)
.cble

The above
.code collect
will look for a match only twice: at the current position,
and one line down.

There is one more keyword,
.codn :vars ,
discussed in the following section.

.coNP Specifying Variables in @ collect
Normally, any variable for which a new binding occurs in a
.code collect
block is collected. A collect clause may be "sloppy": it can neglect to collect
some variables on some iterations, or bind some variables which are intended to
behave like local temporaries, but end up collated into lists. Another issue is
that the collect clause might not match anything at all, and then none of the
variables are bound.

The
.code :vars
keyword allows the query writer to add discipline the
.code collect
body.

The argument to
.code :vars
is a list of variable specs. A variable spec is either a
symbol, or a
.cblk
.meti >> ( symbol << expression )
.cble
pair, where the expression specifies a
default value.

When a
.code :vars
list is specified, it means that only the given variables can
emerge from the successful collect. Any newly introduced bindings for other
variables do not propagate.

Furthermore, for any variable which is not specified with a default value, the
collect body, whenever it matches successfully, must bind that variable. If it
neglects to bind the variable, an exception of type query-error is thrown.
(If a
.code collect
body matches successfully, but produces no new bindings, then
this error is suppressed.)

For any variable which does have a default value, if the
.code collect
body neglects
to bind that variable, the behavior is as if
.code collect
did bind that variable
to that default value.

The default values are expressions, and so can be quasiliterals.

Lastly, if in the event that
.code collect
does not match anything, the variables
specified in vars (whether or not they have a default value) are all bound to
empty lists. (These bindings are established after the processing of the
.cod3 until / last
last clause, if present.)

Example:

.cblk
  @(collect :vars (a b (c "foo")))
  @a @c
  @(end)
.cble

Here, if the body
.str @a @c
matches, an error will be thrown because one of the
mandatory variables is
.codn b ,
and the body neglects to produce a binding for
.codn b .

Example:

.cblk
  @(collect :vars (a (c "foo")))
  @a @b
  @(end)
.cble

Here, if
.str @a @b
matches, only
.code a
will be collected, but not
.codn b ,
because
.code b
is not
in the variable list. Furthermore, because there is no binding for
.code c
in the
body, a binding is created with the value
.strn foo ,
exactly as if
.code c
matched
such a piece of text.

In the following example, the assumption is that
.code THIS NEVER MATCHES
is not found anywhere in the input but the line
.code THIS DOES MATCH
is found and has a successor which is bound to
.codn a .
Because the body did not
match, the
.code :vars
.code a
and
.cod b
should be bound to empty lists. But
.code a
is bound by the last clause to some text, so this takes precedence. Only
.code b
is bound to an empty list.

.cblk
  @(collect :vars (a b))
  THIS NEVER MATCHES
  @(last)
  THIS DOES MATCH
  @a
  @(end)
.cble

The following means: do not allow any variables to propagate out of any
iteration of the collect and therefore collect nothing:

.cblk
  @(collect :vars nil)
  ...
  @(end)
.cble

Instead of writing
.codn "@(collect :vars nil)" ,
it is possible to write
.codn @(repeat) .
.code @(repeat)
takes all collect keywords, except for
.codn :vars .
There is a
.code @(repeat)
directive used in
.code @(output)
clauses; that is a different directive.

.dir coll

The
.code coll
directive is the horizontal version of
.codn collect .
Whereas
.code collect
works with multi-line clauses on line-oriented
material,
.code coll
works within a single line. With
.codn coll ,
it is possible to
recognize repeating regularities within a line and collect lists.

Regular-expression based Positive Match variables work well with coll.

Example: collect a comma-separated list, terminated by a space.
.IP code:
.cblk
\ @(coll)@{A /[^, ]+/}@(until) @(end)@B
.cble
.IP data:
.cblk
\ foo,bar,xyzzy blorch
.cble
.IP result:
.cblk
\ A[0]="foo"
 A[1]="bar"
 A[2]="xyzzy"
 B=blorch
.cble
.PP

Here, the variable
.code A
is bound to tokens which match the regular
expression
.code /[^, ]+/:
non-empty sequence of characters other than commas or
spaces.

Like
.codn collect ,
.code coll
searches for matches.  If no match
occurs at the current character position, it tries at the next character
position. Whenever a match occurs, it continues at the character position which
follows the last character of the match, if such a position exists.

If not bounded by an until clause, it will exhaust the entire line.  If the
until clause matches, then the collection stops at that position,
and any bindings from that iteration are discarded.
Like collect, coll also supports an
.cod3 until / last
clause, which propagates variable
bindings and advances the position.

.code coll
clauses nest, and variables bound within a coll are available to clauses
within the rest of the
.code coll
clause, including the
.cod3 until / last
clause, and appear as
single values. The final list aggregation is only visible after the
.code coll
clause.

The behavior of
.code coll
leads to difficulties when a delimited variable are used
to match material which is delimiter separated rather than terminated.
For instance, entries in a comma-separated files usually do
not appear as
.str a,b,c,
but rather
.strn a,b,c .

So for instance, the following result is not satisfactory:
.IP code:
.cblk
\ @(coll)@a @(end)
.cble
.IP data:
.cblk
\ 1 2 3 4 5
.cble
.IP result:
.cblk
\ a[0]="1"
 a[1]="2"
 a[2]="3"
 a[3]="4"
.cble
.PP

The
.code 5
is missing because it isn't followed by a space, which the text-delimited
variable match
.str "@a "
looks for.  After matching "4 ", coll continues to look for
matches, and doesn't find any.  It is tempting to try to fix it like this:
.IP code:
.cblk
\ @(coll)@a@/ ?/@(end)
.cble
.IP data:
.cblk
\ 1 2 3 4 5
.cble
.IP result:
.cblk
\ a[0]=""
 a[1]=""
 a[2]=""
 a[3]=""
 a[4]=""
 a[5]=""
 a[6]=""
 a[7]=""
 a[8]=""
.cble
.PP

The problem now is that the regular expression
.code / ?/
(match either a space or nothing), matches at any position.
So when it is used as a variable
delimiter, it matches at the current position, which binds the empty string to
the variable, the extent of the match being zero. In this situation, the
.code coll
directive proceeds character by character. The solution is to use
positive matching: specify the regular expression which matches the item,
rather than a trying to match whatever follows.  The collect directive will
recognize all items which match the regular expression:
.IP code:
.cblk
\ @(coll)@{a /[^ ]+/}@(end)
.cble
.IP data:
.cblk
\ 1 2 3 4 5
.cble
.IP result:
.cblk
\ a[0]="1"
 a[1]="2"
 a[2]="3"
 a[3]="4"
 a[4]="5"
.cble
.PP

The
.code until
clause can specify a pattern which, when recognized, terminates
the collection. So for instance, suppose that the list of items may
or may not be terminated by a semicolon. We must exclude
the semicolon from being a valid character inside an item, and
add an until clause which recognizes a semicolon:
.IP code:
.cblk
\ @(coll)@{a /[^ ;]+/}@(until);@(end);
.cble
.IP data:
.cblk
\ 1 2 3 4 5;
.cble
.IP result:
.cblk
\ a[0]="1"
 a[1]="2"
 a[2]="3"
 a[3]="4"
 a[4]="5"
.cble
.PP

Whether followed by the semicolon or not, the items are collected properly.

Note that the
.code @(end)
is followed by a semicolon. That's because
when the
.code @(until)
clause meets a match, the matching material
is not consumed.

This repetition can, of course, be avoided by using
.code @(last)
instad of
.code @(until)
since
.code @(last)
consumes the terminating material.

Instead of the above regular-expresion-based approach, this extraction problem
can also be solved with
.codn cases :
.IP code:
.cblk
\ @(coll)@(cases)@a @(or)@a@(end)@(end)
.cble
.IP data:
.cblk
\ 1 2 3 4 5
.cble
.IP result:
.cblk
\ a[0]="1"
 a[1]="2"
 a[2]="3"
 a[3]="4"
 a[4]="5"
.cble
.PP

.coNP Keyword parameters in @ coll
The
.code @(coll)
directive takes most of the same parameters as
.codn @(collect) .
See the section Collect Keyword Parameters above.
So for instance
.code @(coll :gap 0)
means that the collects must be
consecutive, and
.code @(coll :maxtimes 2)
means that at most two matches
will be collected.  The
.code :lines
keyword does not exist, but there is
an analogous
.code :chars
keyword.

.dir flatten

The
.code flatten
directive can be used to convert variables to one dimensional
lists. Variables which have a scalar value are converted to lists containing
that value. Variables which are multidimensional lists are flattened to
one-dimensional lists.

Example (without
.codn @(flatten) )
.IP code:
.cblk
\ @b
 @(collect)
 @(collect)
 @a
 @(end)
 @(end)
.cble
.IP data:
.cblk
\ 0
 1
 2
 3
 4
 5
.cble
.IP result:
.cblk
\ b="0"
 a_0[0]="1"
 a_1[0]="2"
 a_2[0]="3"
 a_3[0]="4"
 a_4[0]="5"
.cble
.PP

Example (with
.codn @(flatten) ):
.IP code:
.cblk
\ @b
 @(collect)
 @(collect)
 @a
 @(end)
 @(end)
 @(flatten a b)
.cble
.IP data:
.cblk
\ 0
 1
 2
 3
 4
 5
.cble
.IP result:
.cblk
\ b="0"
 a[0]="1"
 a[1]="2"
 a[2]="3"
 a[3]="4"
 a[4]="5"
.cble
.PP


.dir merge

The
.code merge
directive provides a way of combining two or more variables
in a somewhat complicated but very useful way.

To understand what merge does we first have to define a property called depth.
The depth of an atom such as a string is defined as
.codn 1 .
The depth of an empty
list is
.codn 0 .
The depth of a nonempty list is one plus the depth of its deepest
element. So for instance
.str foo
has depth 1,
.cblk
("foo")
.cble
has depth 2, and
.cblk
("foo" ("bar"))
.cble
has depth three.

We can now define the binary (two argument) merge operation as follows.
.IP 1
.code (merge A B)
first normalizes the values
.code A
and
.code B
such that they have equal depth.
.IP 2
A value which has depth zero is put into a one element list. 
.IP 3
If either value has a smaller depth than the other, it is wrapped
in a list as many times as needed to give it equal depth.
Finally, the values are appended together.
.PP

Merge takes more than two arguments. These are merged by a left reduction. The
leftmost two values are merged, and then this result is merged with the third
value, and so on.

Merge is useful for combining the results from collects at different
levels of nesting such that elements are at the appropriate depth.

.dir cat

The
.code cat
directive converts a list variable into a single
piece of text. The syntax is:

.cblk
.mets @(cat < var <> [ sep ])
.cble

The
.meta sep
argument specifies a separating piece of text. If no separator
is specified, then a single space is used.

Example:
.IP code:
.cblk
\ @(coll)@{a /[^ ]+/}@(end)
 @(cat a ":")
.cble
.IP data:
.cblk
\ 1 2 3 4 5
.cble
.IP result:
.cblk
\ a="1:2:3:4:5"
.cble
.PP

.dir bind

The syntax of the
.code bind
directive is:

.cblk
.mets @(bind < pattern < expression >> { keyword << value }*)
.cble

The
.code bind
directive is a kind of pattern match, which matches one or more
variables on the left hand side pattern to the value of a variable on the
right hand side.  The right hand side variable must have a binding, or else the
directive fails. Any variables on the left hand side which are unbound receive
a matching piece of the right hand side value. Any variables on the left which
are already bound must match their corresponding value, or the bind fails. Any
variables which are already bound and which do match their corresponding value
remain unchanged (the match can be inexact).

The simplest bind is of one variable against itself, for instance bind
.code A
against
.codn A :

.cblk
  @(bind A A)
.cble

This will fail if
.code A
is not bound, (and complain loudly). If
.code A
is bound, it
succeeds, since
.code A
matches
.codn A .

The next simplest bind binds one variable to another:

.cblk
  @(bind A B)
.cble

Here, if
.code A
is unbound, it takes on the same value as
.codn B .
If
.code A
is bound, it has
to match
.codn B ,
or the bind fails. Matching means that either
.IP -
.code A
and
. code B
are the same text
.IP -
.code A
is text,
.code B
is a list, and
.code A
occurs within
.codn B .
.IP -
vice versa:
.code B
is text,
.code A
is a list, and
.code B
occurs within
.codn A .
.IP -
.code A
and
.code B
are lists and are either identical, or one is
found as substructure within the other.
.PP
The right hand side does not have to be a variable. It may be some other
object, like a string, quasiliteral, regexp, or list of strings, et cetera. For
instance

.cblk
  @(bind A "ab\etc")
.cble

will bind the string
.str ab\etc
to the variable
.code A
if
.code A
is unbound. If
.code A
is bound, this will fail unless
.code A
already contains an identical string. However, the right hand side of a bind
cannot be an unbound variable, nor a complex expression that contains unbound
variables.

The left hand side of
.code bind
can be a nested list pattern containing variables.
The last item of a list at any nesting level can be preceded by a
.code .
(dot), which means that the variable matches the rest of the list from that
position.

Example: suppose that the list A contains
.cblk
("now" "now" "brown" "cow").
.cble
Then the
directive
.codn "@(bind (H N . C) A)" ,
assuming that
.codn H ,
.code N
and
.code C are unbound variables,
will bind
.code H
to
.strn how ,
code N
to
.strn now ,
and
.code C
to the remainder of the list
.cblk
("brown" "cow").
.cble

Example: suppose that the list
.code A
is nested to two dimensions and  contains
.cblk
(("how" "now") ("brown" "cow")).
.cble
Then
.code @(bind ((H N) (B C)) A)
binds
.code H to
.strn how ,
.code N
to
.strn now ,
.code B
to
.str brown
and
.code C
to
.strn cow .

The dot notation may be used at any nesting level. it must be followed
by an item.  The forms
.code (.)
and
.code (X .)
are invalid, but
.code (. X)
is valid and equivalent to
.codn X .

The number of items in a left pattern match must match the number of items in
the corresponding right side object. So the pattern
.code ()
only matches
an empty list. The notations
.code ()
and
.code nil
mean exactly the same thing.

The symbols
.codn nil ,
.code t
and keyword symbols may be used on either side.
They represent themselves.  For example
.code @(bind :foo :bar)
fails,
but
.code @(bind :foo :foo)
succeeds since the two sides denote the same
keyword symbol object.

.coNP Keywords in the @ bind directive
The
.code bind
directive accepts these keywords:

.coIP :lfilt
The argument to
.code :lfilt
is a filter specification. When the left side pattern
contains a binding which is therefore matched against its counterpart from the
right side expression, the left side is filtered through the filter specified
by
.code :lfilt
for the purposes of the comparison. For example:

.cblk
  @(bind "a" "A" :lfilt :upcase)
.cble

produces a match, since the left side is the same as the right after
filtering through the :upcase filter.

.coIP :rfilt
The argument to
.code :rfilt
is a filter specification. The specified filter is
applied to the right hand side material prior to matching it against
the left side. The filter is not applied if the left side is a variable
with no binding. It is only applied to determine a match. Binding takes
place the unmodified right hand side object.

For example, the following produces a match:

.cblk
  @(bind "A" "a" :rfilt :upcase)
.cble

.coIP :filter
This keyword is a shorthand to specify both filters to the same value.
For instance
.code :filter :upcase
is equivalent to
.codn ":lfilt :upcase :rfilt :upcase" .

For a description of filters, see Output Filtering below.

Of course, compound filters like
.code (:from_html :upcase)
are supported with all these keywords. The filters apply across arbitrary
patterns and nested data.

Example:

.cblk
  @(bind (a b c) ("A" "B" "C"))
  @(bind (a b c) (("z" "a") "b" "c") :rfilt :upcase)
.cble

Here, the first bind establishes the values for
.codn a ,
.code b
and
.codn c ,
and the second bind
succeeds, because the value of a matches the second element of the list
.code ("z" "a")
if it is upcased, and likewise
.code b
matches
.str "b"
and
.code c
matches
.str c
if these are upcased.

.coNP Lisp forms in the @ bind directive

\*(TL forms, introduced by
.code @
may be used on either side of
.codn bind .


Example:

.cblk
  @(bind a @(+ 2 2))
  @(bind @(+ 2 2) @(* 2 2))
.cble

Here,
.code a
is bound to the integer
.codn 4 .
The second
.code bind
then succeeds because the forms
.code (+ 2 2)
and
.code (* 2 2)
produce equal values.

.dir set

The
.code set
directive syntactically resembles
.codn bind ,
but is not a pattern match. It overwrites
the previous values of variables with new values from the right hand side.
Each variable that is assigned must have an existing binding:
.code set
will not induce binding.

Examples follow.

Store the value of
.code A
back into
.codn A ,
an operation with no effect:

.cblk
  @(set A A)
.cble

Exchange the values of
.code A
and
.codn B :

.cblk
  @(set (A B) (B A))
.cble

Store a string into
.codn A :

.cblk
  @(set A "text")
.cble

Store a list into
.codn A :

.cblk
  @(set A ("line1" "line2"))
.cble

Destructuring assignment.
.code A
ends up with
.strn A ,
.code B
ends up with
.cblk
("B1" "B2")
.cble
and
.code C
binds to
.cblk
("C1" "C2").
.cble

.cblk
  @(bind D ("A" ("B1" "B2") "C1" "C2"))
  @(bind (A B C) (() () ()))
  @(set (A B . C) D)
.cble

Note that
.code set
does not support a \*(TL expression on the left side, so the following
are invalid syntax:

.cblk
  @(set @(+ 1 1) @(* 2 2))
  @(set @b @(list "a"))
.cble

The second one is erroneous even though there is a variable on the left.
Because it is preceded by the
.code @
escape, it is a Lisp variable, and not a pattern variable.

.dir rebind

The
.code rebind
directive resembles
.code set
but it is not an assignment.
It combines the semantics of
.codn local ,
.code bind
and
.codn set .
The expression on the right hand side is evaluated in the current
environment. Then the variables in the pattern on the left are introduced
as new bindings, whose values come from the pattern.

.code rebind
makes it easy to create temporary bindings based on existing bindings.

.cblk
  @(define pattern-function (arg))
  @;; inside a pattern function:
  @(rebind recursion-level @(+ recursion-level 1))
  @;; ...
  @(end)
.cble

When the function terminates, the previous value of recursion-level
is restored. The effect is like the following, but much easier
to write and faster to execute:

.cblk
  @(define pattern-function (arg))
  @;; inside a pattern function:
  @(local temp)
  @(set temp recursion-level)
  @(local recursion-level)
  @(set recursion-level @(+ temp 1))
  @;; ...
  @(end)
.cble

.dir forget

The
.code forget
has two spellings:
.code @(forget)
and
.codn @(local).

The arguments are one or more symbols, for example:

.cblk
  @(forget a)
  @(local a b c)
.cble

this can be written

.cblk
  @(local a)
  @(local a b c)
.cble

Directives which follow the forget or local directive no longer see
any bindings for the symbols mentioned in that directive, and
can establish new bindings.

It is not an error if the bindings do not exist.

It is strongly recommended to use the
.code @(local)
spelling in functions,
because the forgetting action simulates local variables:
for the given symbols, the machine forgets any earlier variables
from outside of the function, and consequently, any new bindings
for those variables belong to the function. (Furthermore,
functions suppress the propagation of variables that are not
in their parameter list, so these locals will be automatically
forgotten when the function terminates.)

.dir do

The syntax of
.code @(do)
is:

.cblk
.mets @(do << lisp-expression )
.cble

The do directive evaluates a \*(TL expression. (See TXR LISP far
below.) The value of the expression is ignored, and matching continues
continues with the directives which follow the
.code do
directive, if any.

In the context of the
.code do
directive, the expression should not be introduced by the
.code @
symbol; it is expected to be a Lisp expression.

Example:

.cblk
  @; match text into variables a and b, then insert into hash table h
  @(bind h (hash :equal-based))
  @a:@b
  @(do (set [h a] b))
.cble

.SH* BLOCKS
.SS* Overview
Blocks are sections of a query which are either denoted by a name,
or are anonymous. They may nest: blocks can occur within blocks
and other constructs.

Blocks are useful for terminating parts of a pattern matching search
prematurely, and escaping to a higher level. This makes blocks not only
useful for simplifying the semantics of certain pattern matches,
but also an optimization tool.

Judicious use of blocks and escapes can reduce or eliminate the amount of
backtracking that \*(TX performs.

.dir block

The
.cblk
.meti @(block << name )
.cble
directive introduces a named block, except when
.meta name
is the symbol
.codn nil .
The
.code @(block)
directive introduces an unnamed block, equivalent
to
.codn "@(block nil)" .

The
.code @(skip)
and
.code @(collect)
directives introduce implicit anonymous blocks,
as do function bodies.

.SS* Block Scope

The names of blocks are in a distinct namespace from the variable binding
space. So
.code @(block foo)
is unrelated to the variable
.codn @foo .

A block extends from the
.code @(block ...)
directive which introduces it,
until the matching
.codn @(end) ,
and may be empty.  For instance:

.cblk
  @(some)
  abc
  @(block foo)
  xyz
  @(end)
  @(end)
.cble

Here, the block foo occurs in a
.code @(some)
clause, and so it extends to the
.code @(end)
which terminates the block.  After that
.codn @(end) ,
the name foo is not
associated with a block (is not "in scope"). The second
.code @(end)
terminates
the
.code @(some)
block.

The implicit anonymous block introduced by
.code @(skip)
has the same scope
as the
.codn @(skip) :
it extends over all of the material which follows the skip,
to the end of the containing subquery.

.SS* Block Nesting

Blocks may nest, and nested blocks may have the same names as blocks in
which they are nested. For instance:

.cblk
  @(block)
  @(block)
  ...
  @(end)
  @(end)
.cble

is a nesting of two anonymous blocks, and

.cblk
  @(block foo)
  @(block foo)
  @(end)
  @(end)
.cble

is a nesting of two named blocks which happen to have the same name.
When a nested block has the same name as an outer block, it creates
a block scope in which the outer block is "shadowed"; that is to say,
directives which refer to that block name within the nested block refer to the
inner block, and not to the outer one.

.SS* Block Semantics

A block normally does nothing. The query material in the block is evaluated
normally. However, a block serves as a termination point for
.code @(fail)
and
.code @(accept)
directives which are in scope of that block and refer to it.

The precise meaning of these directives is:

.meIP @(fail << name )
Immediately terminate the enclosing query block called
.metn name ,
as if that block
failed to match anything. If more than one block by that name encloses
the directive, the inner-most block is terminated. No bindings emerge from
a failed block.

.coIP @(fail)
Immediately terminate the innermost enclosing anonymous block, as if
that block failed to match.

If the implicit block introduced by
.code @(skip)
is terminated in this manner,
this has the effect of causing
.code skip
itself to fail. I.e. the behavior
is as if skip search did not find a match for the trailing material,
except that it takes place prematurely (before the end of the available
data source is reached).

If the implicit block associated with a
.code @(collect)
is terminated this way,
then the entire
.code collect
fails. This is a special behavior, because a
collect normally does not fail, even if it matches nothing and collects nothing!

To prematurely terminate a collect by means of its anonymous block, without
failing it, use
.codn @(accept) .

.meIP @(accept << name )
Immediately terminate the enclosing query block called
.metn name ,
as if that block
successfully matched. If more than one block by that name encloses the
directive, the inner-most block is terminated.  Any bindings established within
that block until this point emerge from that block.

.coIP @(accept)
Immediately terminate the innermost enclosing anonymous block, as if
that block successfully matched. Any bindings established within
that block until this point emerge from that block.

If the implicit block introduced by
.code @(skip)
is terminated in this manner,
this has the effect of causing the skip itself to succeed, as if
all of the trailing material had successfully matched.

If the implicit block associated with a
.code @(collect)
is terminated this way,
then the collection stops. All bindings collected in the current iteration of
the collect are discarded. Bindings collected in previous iterations are
retained, and collated into lists in accordance with the semantics of collect.

Example: alternative way to achieve
.code @(until)
termination:

.cblk
  @(collect)
  @  (maybe)
  ---
  @  (accept)
  @  (end)
  @LINE
  @(end)
.cble

This query will collect entire lines into a list called
.codn LINE .
However,
if the line
.code ---
is matched (by the embedded
.codn @(maybe)),
the collection
is terminated. Only the lines up to, and not including the
.code ---
line, are collected. The effect is identical to:

.cblk
  @(collect)
  @LINE
  @(until)
  ---
  @(end)
.cble

The difference (not relevant in these examples) is that the until clause has
visibility into the bindings set up by the main clause.

However, the following example has a different meaning:

.cblk
  @(collect)
  @LINE
  @  (maybe)
  ---
  @  (accept)
  @  (end)
  @(end)
.cble

Now, lines are collected until the end of the data source, or until a line is
found which is followed by a
.code ---
line. If such a line is found,
the collection stops, and that line is not included in the collection!
The
.code @(accept)
terminates the process of the collect body, and so the
action of collecting the last
.code @LINE
binding into the list is not performed.
.PP

.SS* Data Extent of Terminated Blocks

A query block may have matched some material prior to being terminated by
.codn accept .
In that case, it is deemed to have only matched that material,
and not any material which follows. This may matter, depending on the context
in which the block occurs.

Example:
.IP code:
.cblk
\ @(some)
 @(block foo)
 @first
 @(accept foo)
 @ignored
 @(end)
 @second
.cble
.IP data:
.cblk
\ 1
 2
 3
.cble
.IP result:
.cblk
\ first="1"
 second="2"
.cble
.PP

At the point where the
.code accept
occurs, the foo block has matched the first line,
bound the text
.str 1
to the variable
.codn @first .
The block is then terminated.
Not only does the
.code @first
binding emerge from this terminated block, but
what also emerges is that the block advanced the data past the first line to
the second line. Next, the
.code @(some)
directive ends, and propagates the
bindings and position. Thus the
.code @second
which follows then matches the second
line and takes the text
.strn 2 .

In the following query, the foo block occurs inside a maybe clause.
Inside the foo block there is a
.code @(some)
clause. Its first subclause
matches variable
.code @first
and then terminates block foo. Since block foo is
outside of the
.code @(some)
directive, this has the effect of terminating the
.code @(some)
clause:
.IP code:
.cblk
\ @(maybe)
 @(block foo)
 @  (some)
 @first
 @  (accept foo)
 @  (or)
 @one
 @two
 @three
 @four
 @  (end)
 @(end)
 @second
.cble
.IP data:
.cblk
\ 1
 2
 3
 4
 5
.cble
.IP result:
.cblk
\ first="1"
 second="2"
.cble
.PP

The second clause of the
.code @(some)
directive, namely:

.cblk
  @one
  @two
  @three
  @four
.cble

is never processed. The reason is that subclauses are processed in top
to bottom order, but the processing was aborted within the
first clause the
.codn "@(accept foo)" .
The
.code @(some)
construct never gets the
opportunity to match four lines.

If the
.code @(accept foo)
line is removed from the above query, the output
is different:
.IP code:
.cblk
\ @(maybe)
 @(block foo)
 @  (some)
 @first
 @#          <--  @(accept foo) removed from here!!!
 @  (or)
 @one
 @two
 @three
 @four
 @  (end)
 @(end)
 @second
.cble
.IP data:
.cblk
\ 1
 2
 3
 4
 5
.cble
.IP result:
.cblk
\ first="1"
 one="1"
 two="2"
 three="3"
 four="4"
 second="5"
.cble
.PP

Now, all clauses of the
.code @(some)
directive have the opportunity to match.
The second clause grabs four lines, which is the longest match.
And so, the next line of input available for matching is
.codn 5 ,
which goes
to the
.code @second
variable.

.coSS Interaction Between the @ trailer and @ accept Directives

If one of the clauses which follow a
.code @(trailer)
requests a successful
termination to an outer block via
.codn @(accept) ,
then
.code @(trailer)
intercepts the escape and adjusts the data extent to the position
that it was given.

Example:
.IP code:
.cblk
\ @(block)
 @(trailer)
 @line1
 @line2
 @(accept)
 @(end)
 @line3
.cble
.IP data:
.cblk
\ 1
 2
 3
.cble
.IP result:
.cblk
\ line1="1"
 line2="2"
 line3="1"
.cble
.PP

The variable
.code line3
is bound to
.str 1
because although
.code @(accept)
yields a data
position which has advanced to the third line, this is intercepted by
.code @(trailer)
and adjusted back to the first line. Neglecting to do this adjustment
would violate the semantics of
.codn trailer .

Directives other than
.code @(trailer)
have no such special interaction with accept.

.SH* FUNCTIONS
.SS* Overview
\*(TX functions allow a query to be structured to avoid repetition.
On a theoretical note, because
\*(TX functions support recursion, functions enable \*(TX to match some
kinds of patterns which exhibit self-embedding, or nesting,
and thus cannot be matched by a regular language.

Functions in \*(TX are not exactly like functions in mathematics or functional
languages, and are not like procedures in imperative programming languages.
They are not exactly like macros either. What it means for a
\*(TX function to take arguments and produce a result is different from
the conventional notion of a function.

A \*(TX function may have one or more parameters. When such a function is
invoked, an argument must be specified for each parameter.  However, a special
behavior is at play here. Namely, some or all of the argument expressions may
be unbound variables.  In that case, the corresponding parameters behave like
unbound variables also.  Thus \*(TX function calls can transmit the "unbound"
state from argument to parameter.

It should be mentioned that functions have access to all bindings that are
visible in the caller; functions may refer to variables which are not
mentioned in their parameter list.

With regard to returning, \*(TX functions are also unconventional. If the
function fails, then the function call is considered to have failed. The
function call behaves like a kind of match; if the function fails, then the
call is like a failed match.

When a function call succeeds, then the bindings emanating from that function
are processed specially. Firstly, any bindings for variables which do not
correspond to one of the function's parameters are thrown away. Functions may
internally bind arbitrary variables in order to get their job done, but only
those variables which are named in the function argument list may propagate out
of the function call.  Thus, a function with no arguments can only indicate
matching success or failure, but not produce any bindings. Secondly,
variables do not propagate out of the function directly, but undergo
a renaming. For each parameter which went into the function as an unbound
variable (because its corresponding argument was an unbound variable),
if that parameter now has a value, that value is bound onto the corresponding
argument.

Example:

.cblk
  @(define collect-words (list))
  @(coll)@{list /[^ \et]+/}@(end)
  @(end)
.cble

The above function
.code collect-words
contains a query which collects words from a
line (sequences of characters other than space or tab), into the list variable
called
.codn list .
This variable is named in the parameter list of the function,
therefore, its value, if it has one, is permitted to escape from the function
call.

Suppose the input data is:

.cblk
  Fine summer day
.cble

and the function is called like this:

.cblk
  @(collect-words wordlist)
.cble

The result (with
.codn "txr -B" )
is:

.cblk
  wordlist[0]=Fine
  wordlist[1]=summer
  wordlist[1]=day
.cble

How it works is that in the function call
.codn "@(collect-words wordlist)" ,
.code wordlist
is an unbound variable. The parameter corresponding to that
unbound variable is the parameter
.codn list .
Therefore, that parameter
is unbound over the body of the function.  The function body collects the
words of
.str Fine summer day
into the variable
.codn list,
and then
yields the that binding.   Then the function call completes by
noticing that the function parameter
.code list
now has a binding, and
that the corresponding argument
.code wordlist
has no binding. The binding
is thus transferred to the
.code wordlist
variable.  After that, the
bindings produced by the function are thrown away. The only enduring
effects are:
.IP -
the function matched and consumed some input; and
.IP -
the function succeeded; and
.IP -
the
.code wordlist
variable now has a binding.
.PP
Another way to understand the parameter behavior is that function
parameters behave like proxies which represent their arguments.  If an argument
is an established value, such as a character string or bound variable, the
parameter is a proxy for that value and behaves just like that value. If an
argument is an unbound variable, the function parameter acts as a proxy
representing that unbound variable. The effect of binding the proxy is
that the variable becomes bound, an effect which is settled when the
function goes out of scope.

Within the function, both the original variable and the proxy are
visible simultaneously, and are independent.  What if a function binds both of
them? Suppose a function has a parameter called
.codn P ,
which is called with an argument
.codn A ,
which is an unbound variable, and then, in the function, both
.code A
and
.code P
bound.  This is
permitted, and they can even be bound to different values.  However, when the
function terminates, the local binding of A simply disappears (because
the symbol
.code A
is not among the paramters of the function).
Only the value bound to
.code P
emerges, and is bound to
.codn A ,
which still appears unbound at that point. The
.code P
binding disappears also, and the net effect is that
.code A
is now bound. The "proxy" binding of
.code A
thorugh the parameter
.code P
"wins" the conflict with the direct binding.

.SS* Definition Syntax

Function definition syntax comes in two flavors: vertical and horizontal.
Horizontal definitions actually come in two forms, the distinction
between which is hardly noticeable, and the need for which is
made clear below.

A function definition begins with a
.code @(define ...)
directive. For vertical
functions, this is the only element in a line.

The
.code define
symbol must be followed by a symbol, which is the name of the
function being defined. After the symbol, there is a parenthesized optional
argument list. If there is no such list, or if the list is specified as
.code ()
or
the symbol
.code nil
then the function has no parameters. Examples of valid
.code define
syntax are:

.cblk
  @(define foo)
  @(define bar ())
  @(define match (a b c))
.cble

If the define directive is followed by more material on the same line, then
it defines a horizontal function:

.cblk
  @(define match-x)x@(end)
.cble

If the define is the sole element in a line, then it
is a vertical function, and the function definition continues below:

.cblk
  @(define match-x)
  x
  @(end)
.cble

The difference between the two is that a horizontal function matches
characters within a line, whereas a vertical function matches lines
within a stream. The former
.code match-x
matches the character
.codn x ,
advancing
to the next character position.  The latter
.code match-x
matches a line consisting
of the character
.codn x ,
advancing to the next line.

Material between
.code @(define)
and
.code @(end)
is the function body.  The define
directive may be followed directly by the
.code @(end)
directive, in which case the
function has an empty body.

Functions may be nested within function bodies. Such local functions have
dynamic scope. They are visible in the function body in which they are defined,
and in any functions invoked from that body.

The body of a function is an anonymous block. (See BLOCKS above).

.SS* Two Forms of The Horizontal Function

If a horizontal function is defined as the only element of a line,
it may not be followed by additional material. The following
construct is erroneous:

.cblk
  @(define horiz (x))@foo:@bar@(end)lalala
.cble

This kind of definition is actually considered to be in the vertical context,
and like other directives that have special effects and that do not match
anything, it does not consume a line of input. If the above syntax were
allowed, it would mean that the line would not only define a function but also
match
.codn "lalala" .
This would, in turn, would mean that the
.code @(define)...@(end)
is
actually in horizontal mode, and so it matches a span of zero characters within
a line (which means that is would require a line of input to match: a
surprising behavior for a non-matching directive!)

A horizontal function can be defined in an actual horizontal context. This
occurs if its is in a line where it is preceded by other material.
For instance:

.cblk
  X@(define fun)...@(end)Y
.cble

This is a query line which must match the text
.codn XY .
It also defines the function
.codn fun .
The main use of this form is for nested horizontal functions:

.cblk
  @(define fun)@(define local_fun)...@(end)@(end)
.cble

.SS* Vertical-Horizontal Overloading

A function of the same name may be defined as both vertical and horizontal.
Both functions are available at the same time. Which one is used by
a call is resolved by context. See the section Vertical Versus Horizontal Calls
below.

.SS* Call Syntax

A function is invoked by compound directive whose first symbol is the name of
that function. Additional elements in the directive are the arguments.
Arguments may be symbols, or other objects like string and character
literals, quasiliterals ore regular expressions.

Example:
.IP code:
.cblk
\ @(define pair (a b))
 @a @b
 @(end)
 @(pair first second)
 @(pair "ice" cream)
.cble
.IP data:
.cblk
\ one two
 ice milk
.cble
.IP result:
.cblk
\ first="one"
 second="two"
 cream="milk"
.cble
.PP

The first call to the function takes the line
.strn "one two" .
The parameter
.code a
takes
.str one
and parameter
.code b
takes
.strn two .
These are rebound to the arguments
.code first
and
.codn second .
The second call to the function binds the a parameter to the word
.strn "ice" ,
and the
.code b
is unbound, because the
corresponding argument
.code cream
is unbound. Thus inside the function,
.code a
is forced to match
.codn "ice" .
Then a space is matched and
.code b
collects the text
.strn milk .
When the function returns, the unbound
.str cream
variable gets this value.

If a symbol occurs multiple times in the argument list, it constrains
both parameters to bind to the same value. That is to say, all parameters
which, in the body of the function, bind a value, and which are all derived
from the same argument symbol must bind to the same value. This is settled when
the function terminates, not while it is matching. Example:
.IP code:
.cblk
\ @(define pair (a b))
 @a @b
 @(end)
 @(pair same same)
.cble
.IP data:
.cblk
\ one two
.cble
.IP result:
.cblk
\ [query fails]
.cble
.PP

Here the query fails because
.code a
and
.code b
are effectively proxies for the same unbound variable
.code same
and are bound to different values, creating a conflict which
constitutes a match failure.

.SS* Vertical Versus Horizontal Calls

A function call which is the only element of the query line in
which it occurs is ambiguous. It can go either to a vertical
function or to the horizontal one. If both are defined, then
it goes to the vertical one.

Example:
.IP code:
.cblk
\ @(define which (x))@(bind x "horizontal")@(end)
 @(define which (x))
 @(bind x "vertical")
 @(end)
 @(which fun)
.cble
.IP result:
.cblk
\ fun="vertical"
.cble
.PP

Not only does this call go to the vertical function, but
it is in a vertical context.

If only a horizontal function is defined, then that is the one which is called,
even if the call is the only element in the line. This takes place in a
horizontal character-matching context, which requires a line of input which can
be traversed:

Example:
.IP code:
.cblk
\ @(define which (x))@(bind x "horizontal")@(end)
 @(which fun)
.cble
.IP data:
.cblk
\ ABC
.cble
.IP result:
.cblk
\ [query fails]
.cble
.PP

The query fails because since
.code @(which fun)
is in horizontal mode,
it matches characters in a line. Since the function body consists
only of
.code @(bind ...)
which doesn't match any characters, the function
call requires an empty line to match. The line
.code ABC
is not empty,
and so there is a matching failure. The following
example corrects this:

Example:
.IP code:
.cblk
\ @(define which (x))@(bind x "horizontal")@(end)
 @(which fun)
.cble
.IP data:
.cblk
\ [empty line]
.cble
.IP result:
.cblk
\ fun="horizontal"
.cble
.PP

A call made in a clearly horizontal context will prefer the
horizontal function, and only fall back on the vertical one
if the horizontal one doesn't exist. (In this fall-back case,
the vertical function is called with empty data; it is useful
for calling vertical functions which process arguments and
produce values.)

In the next example, the call is followed by trailing material,
placing it in a horizontal context. Leading material will
do the same thing:

Example:
.IP code:
.cblk
\ @(define which (x))@(bind x "horizontal")@(end)
 @(define which (x))
 @(bind x "vertical")
 @(end)
 @(which fun)B
.cble
.IP data:
.cblk
\ B
.cble
.IP result:
.cblk
\ fun="horizontal"
.cble
.PP

.SS* Local Variables

As described earlier, variables bound in a function body which are not
parameters of the function are discarded when the function returns. However,
that, by itself, doesn't make these variables local, because pattern functions
have visibility to all variables in their calling environment. If a variable
.code x
exists already when a function is called, then an attempt to bind it inside a
function may result in a failure.  The
.code local
directive must be used in a
pattern function to list which variables are local. 

Example:

.cblk
  @(define path (path))@\e
    @(local x y)@\e
    @(cases)@\e
      (@(path x))@(path y)@(bind path `(@x)@y`)@\e
    @(or)@\e
      @{x /[.,;'!?][^ \et\ef\ev]/}@(path y)@(bind path `@x@y`)@\e
    @(or)@\e
      @{x /[^ .,;'!?()\et\ef\ev]/}@(path y)@(bind path `@x@y`)@\e
    @(or)@\e
      @(bind path "")@\e
    @(end)@\e
  @(end)
.cble

This is a horizontal function which matches a path, which lands into four
recursive cases. A path can be parenthesized path followed by a path; it can be
a certain character followed by a path, or it can be empty

This function ensures that the variables it uses internally,
.code x
and
.codn y ,
do not have anything to do with any inherited bindings for
.code x
and
.codn y .

Note that the function is recursive, which cannot work without
.code x
and
.code y
being local, even if no such bindings exist prior to the top-level invocation of the
function. The invocation
.code @(path x)
causes
.code x
to be bound, which is
visible inside the invocation
.codn "@(path y)" ,
but that invocation needs to have its own binding of
.code x
for local use.

.SS* Nested Functions

Function definitions may appear in a function. Such definitions
are visible in all functions which are invoked from the body
(and not necessarily enclosed in the body). In other words, the
scope is dynamic, not lexical.  Inner definitions shadow outer
definitions. This means that a caller can redirect the function
calls that take place in a callee, by defining local functions
which capture the references.

Example:
.IP code:
.cblk
\ @(define which)
 @  (fun)
 @(end)
 @(define fun)
 @  (output)
 toplevel fun!
 @  (end)
 @(end)
 @(define callee)
 @  (define fun)
 @    (output)
 local fun!
 @    (end)
 @  (end)
 @  (which)
 @(end)
 @(callee)
 @(which)
.cble
.IP output:
.cblk
\ local fun!
 toplevel fun!
.cble
.PP

Here, the function
.code which
is defined which calls
.codn fun .
A toplevel definition of
.code fun
is introduced which
outputs
.strn "toplevel fun!" .
The function
.code callee
provides its own local
definition of
.code fun
which outputs
.str "local fun!"
before calling
.codn which .
When
.code callee
is invoked, it calls
.codn which ,
whose
.code @(fun)
call is routed to callee's
local definition.  When
.code which
is called directly from the top level, its
.code fun
call goes to the toplevel definition.

.SH* MODULARIZATION

.dirs load include

The syntax of the
.code load
and
.code include
directives is:

.cblk
.mets @(load << expr )
.mets @(include << expr )
.cble

Where
.meta expr
evaluates to a string giving the path of the file to load.
Unless the path is absolute, it is interpreted relative to the directory of the
source file from which the
.code @(load)
syntax was read. If there was no such
source file (for instance, the script was read from standard input),
then it is resolved relative to the current working directory.

If the file cannot be opened, then the
.code .txr
suffix is added and another
attempt is made. Thus load expressions need not refer to the suffix.
In the future, additional suffixes may be searched (compiled versions
of a file).

The two directives differ as follows. The action of
.code load
is not performed immediately but at evaluation time. Evaluation time
occurs after a \*(TX program is read from beginning to end and parsed.
The action of
.code include
is performed immediately, as the code is being scanned and parsed.
That is to say, as the \*(TX parser encounters
.code @(include)
it processes it immediately. The included material is read and parsed, and its
syntax tree is substituted in place of the
.code include
directive. The parser then
continues processing the original file after the
.code include
directive.

Note: the
.code include
directive is useful for loading \*(TX files which contain Lisp macros
which are needed by the parent program. The parent program cannot use
.code load
to bring in macros because macros are required during expansion, which
takes place prior to evaluation time, whereas
.code load
doesn't execute until evaluation time.

See also: the
.code *self-path*
and
.code stdlib
variables in \*(TL.

.SH* OUTPUT

.SS* Introduction

A \*(TX query may perform custom output. Output is performed by
.code output
clauses,
which may be embedded anywhere in the query, or placed at the end.  Output
occurs as a side effect of producing a part of a query which contains an
.code @(output)
directive, and is executed even if that part of the query ultimately
fails to find a match. Thus output can be useful for debugging.
An
.code output
clause specifies that its output goes to a file, pipe, or (by
default) standard output. If any output clause is executed whose destination is
standard output, \*(TX makes a note of this, and later, just prior to
termination, suppresses the usual printing of the variable bindings or the word
false.

.dir output

The syntax of the
.code @(output)
directive is:

.cblk
.mets @(output [ < destination ] { < bool-keyword | < keyword < value }* )
  .
  . one or more output directives or lines
  .
  @(end)
.cble

The optional
.meta destination
is a string which gives the path name of
a file to open for output. If the name is
.code -
it instead denotes standard output, and if it begins with
.code !
then the rest of the shell is treated as a shell command
to which the output is piped.

The destination may be specified as a variable
which holds text, as a string literal or as a quasiliteral

The keyword list consists of a mixture of boolean keywords which
do not have an argument, or keywords with arguments.

The following boolean keywords are supported:

.coIP :nothrow
The output directive throws an exception if the output destination
cannot be opened, unless the
.code :nothrow
keyword is present, in which
case the situation is treated as a match failure.

Note that since command pipes are processes that report errors
asynchronously, a failing command will not throw an immediate exception that
can be suppressed with
.codn :nothrow .
This is for synchronous errors, like
trying to open a destination file, but not having permissions, etc.

.coIP :append
This keyword is meaningful for files, specifying append mode: the output is to
be added to the end of the file rather than overwriting the file.

The following value keywords are supported by
.codn :append :

.coIP :filter
The argument can be a symbol, which specifies a filter to be applied to
the variable substitutions occurring within the
.code output
clause.
The argument can also be a list of filter symbols, which specifies
that multiple filters are to be applied, in left to right order.

See the later sections Output Filtering below, and The Deffilter Directive.

.coIP :into
The argument of
.code :into
is a symbol which denotes a variable.
The output will go into that variable.  If the variable is unbound,
it will be created. Otherwise, its contents are overwritten
unless the
.code :append
keyword is used. If
.code :append
is used, then
the new content will be appended to the previous content of
the variable, after flattening the content to a list,
as if by the
.code flatten
directive.

.coIP :named
The argument of
.code :named
is a symbol which denotes a variable.
The file or pipe stream which is opened for the output is
stored in this variable, and is not closed at the end of the
output block. This allows a subsequent output block to continue
output on the same stream, which is possible using the
next two keywords,
.code :continue
or
.codn :finish .
A new binding is established for the variable, even if it
already has an existing binding.

.coIP :continue
A destination should not be specified if
.code :continue
is used.  The argument of
.code :continue
is an expression, such as a variable name, that evaluates to a
stream object. That stream object is used for the output block.
At the end of the output block, the stream is flushed, but not
closed.  A usage example is given in the documentation for the Close Directive
below.

.coIP :finish
A destination should not be specified if
.code :finish
is used.  The argument of
.code :finish
is an expression, such as a variable name, that evaluates to a
stream object. That stream object is used for the output block.
At the end of the output block, the stream is closed.
An example is given in the documentation for the Close Directive
below.

.SS* Output Text

Text in an output clause is not matched against anything, but is output
verbatim to the destination file, device or command pipe.

.SS* Output Variables

Variables occurring in an output clause do not match anything; instead
their contents are output.

A variable being output can be any object. If it is of a type other
than a list or string, it will be converted to a string as if by the
.code tostring
function in \*(TL.

A list is converted to a string in a special way: the elements are
individually converted to a string and then they are catenated together.
The default separator string is a single space: an alternate separation
can be specified as an argument in the brace substitution syntax.
Empty lists turn into an empty string.

Lists may be output within
.code @(repeat)
or
.code @(rep)
clauses. Each nesting of
these constructs removes one level of nesting from the list variables
that it contains.

In an output clause, the
.cblk
.meti >> @{ name << number }
.cble
variable syntax generates fixed-width
field, which contains the variable's text.  The absolute value of the
number specifies the field width. For instance
.code -20
and
.code 20
both specify a field
width of twenty.  If the text is longer than the field, then it overflows the
field. If the text is shorter than the field, then it is left-adjusted within
that field, if the width is specified as a positive number, and right-adjusted
if the width is specified as negative.

An output variable may specify a filter which overrides any filter established
for the output clause. The syntax for this is
.cblk
.meti @{NAME :filter << filterspec }.
.cble
The filter specification syntax is the same as in the output clause.
See Output Filtering below.

.SS* Output Variables: Indexing

Additional syntax is supported in output variables that does not appear
in pattern matching variables.

A square bracket index notation may be used to extract elements or
ranges from a variable, which works with strings, vectors and lists.  Elements
are indexed from zero. This notation is only available in brace-enclosed
syntax, and looks like this:

.meIP <> @{name[ expr ]}
Extract the element at the position given by
.metn expr .

.meIP <> @{name[ expr1..expr2 ]}
Extract a range of elements from the position given by
.metn expr1 ,
up to
one position less than the position given by
.metn expr2 .

If the variable is a list, it is treated as a list substitution,
exactly as if it were the value of an unsubscripted list variable.
The elements of the list are converted to strings and catenated
together wit ha separator string between them, the default one being
a single space.

An alternate character may be given as a string argument in the brace
notation.
.PP

Example:

.cblk
  @(bind a ("a" "b" "c" "d"))
  @(output)
  @{a[1..3] "," 10}
  @(end)
.cble

The above produces the text
.str b,c
in a field
.code 10
spaces wide. The
.code [1..3]
argument extracts a range of
.codn a ;
the
.str ","
argument specifies an alternate
separator string, and
.code 10
specifies the field width.

.SS* Output Substitutions

The brace syntax has another syntactic and semantic extension in
.code output
clauses. In place
of the symbol, an expression may appear. The value of that expression
is substituted.

Example:

.cblk
 @(bind a "foo")
 @(output)
 @{`@a:` -10}
.cble

Here, the quasiliteral expression
.code `@a:`
is evaluated, producing the string
.strn foo: .
This string is printed right-adjusted in a
.code 10
character field.

.dir repeat

The
.code repeat
directive generates repeated text from a "boilerplate",
by taking successive elements from lists. The syntax of repeat is
like this:

.cblk
  @(repeat)
  .
  .
  main clause material, required
  .
  .
  special clauses, optional
  .
  .
  @(end)
.cble

.code repeat
has four types of special clauses, any of which may be
specified with empty contents, or omitted entirely. They are described
below.

.code repeat
takes arguments, also described below.

All of the material in the main clause and optional clauses
is examined for the presence of variables.  If none of the variables
hold lists which contain at least one item, then no output is performed,
(unless the repeat specifies an
.code @(empty)
clause, see below).
Otherwise, among those variables which contain non-empty lists, repeat finds
the length of the longest list. This length of this list determines the number
of repetitions, R.

If the
.code repeat
contains only a main clause, then the lines of this clause is
output R times. Over the first repetition, all of the variables which, outside
of the repeat, contain lists are locally rebound to just their first item. Over
the second repetition, all of the list variables are bound to their second
item, and so forth. Any variables which hold shorter lists than the longest
list eventually end up with empty values over some repetitions.

Example: if the list
.code A
holds
.strn 1 ,
.str 2
and
.strn 3 ;
the list
.code B
holds
.strn A ,
.strn B ;
and the variable
.code C
holds
.strn X ,
then

.cblk
  @(repeat)
  >> @C
  >> @A @B
  @(end)
.cble

will produce three repetitions (since there are two lists, the longest
of which has three items). The output is:

.cblk
  >> X
  >> 1 A
  >> X
  >> 2 B
  >> X
  >> 3
.cble

The last line has a trailing space, since it is produced by
.strn "@A @B" ,
where
.code B
has an empty value. Since
.code C
is not a list variable, it
produces the same value in each repetition.

The special clauses are:

.coIP @(single)
If the
.code repeat
produces exactly one repetition, then the contents of this clause
are processed for that one and only repetition, instead of the main clause
or any other clause which would otherwise be processed.

.coIP @(first)
The body of this clause specifies an alternative body to be used for the first
repetition, instead of the material from the main clause.

.coIP @(last)
The body of this clause is used instead of the main clause for the last
repetition.

.coIP @(empty)
If the repeat produces no repetitions, then the body of this clause is output.
If this clause is absent or empty, the repeat produces no output.

.coIP "@(mod n m)"
The forms
.code n
and
.code m
are expressions that evaluate to integers. The value of
.code m
should be nonzero. The clause denoted this way is active if the repetition
modulo
.code m
is equal to
.codn n .
The first repetition is numbered zero.
For instance the clause headed by
.code @(mod 0 2)
will be used on repetitions 
0, 2, 4, 6, ...  and
.code @(mod 1 2)
will be used on repetitions 1, 3, 5, 7, ...

.coIP "@(modlast n m)"
The meaning of
.code n
and
.code m
is the same as in
.codn "@(mod n m)" ,
but one more condition
is imposed. This clause is used if the repetition modulo
.code m
is equal to
.codn n ,
and if it is the last repetition.
.PP

The precedence among the clauses which take an iteration is:
.codn "single > first > mod > modlast > last > main" .
That is if two or more of these
clauses can apply to a repetition, then the leftmost one in this precedence
list applies. For instance, if there is just a single repetition, then any of
these special clause types can apply to that repetition, since it is the only
repetition, as well as the first and last one. In this situation, if there is a
.code @(single)
clause present, then the repetition is processed using that clause.
Otherwise, if there is a
.code @(first)
clause present, that clause is used. Failing
that,
.code @(mod)
is used if there is such a clause and its numeric conditions
are satisfied. If there isn't, then
.code @(modlast)
clauses are considered, and if there
are none, or none of them activate, then
.code @(last)
is considered. Finally if none
of all these clauses are present or apply, then the repetition is processed
using the main clause.

Repeat supports arguments.

.cblk
.mets @(repeat [:counter << symbol ] [:vars <> ( symbol *)])
.cble

The
.code :counter
argument designates a symbol which will behave as an integer
variable over the scope of the clauses inside the repeat. The variable provides
access to the repetition count, starting at zero, incrementing with each
repetition.

The
.code :vars
argument specifies a list of variables. The repeat directive
will pick out from this list those variables which have bindings.
It will assume that all these variables occur in the repeat block and
are to be iterated. This syntax is needed for situations in which
.code @(repeat)
is not able to deduce the existence of a variable in the block.
It does not dig very deeply to discover variables, and does not "see"
variables that are referenced via embedded \*(TL expressions.
For instance, the following produces no output:

.cblk
  @(bind list ("a" "b" "c"))
  @(output)
  @(repeat)
  @(format nil "<~a>" list)
  @(end)
  @(end)
.cble

Although the list variable appears in the repeat block, it is embedded
in a \*(TL construct. That construct will never be evaluated because
no repetitions take place: the repeat construct doesn't find any variables
and so doesn't iterate. The remedy is to provide a little help via
the :vars parameter:

.cblk
  @(bind list ("a" "b" "c"))
  @(output)
  @(repeat :vars (list))
  @(format nil "<~a>" list)
  @(end)
  @(end)
.cble

Now the repeat block iterates over list and the output is:

.cblk
  <a>
  <b>
  <c>
.cble

.coSS Nested @ repeat directives

If a
.code repeat
clause encloses variables which hold multidimensional lists,
those lists require additional nesting levels of repeat (or rep).
It is an error to attempt to output a list variable which has not been
decimated into primary elements via a repeat construct.

Suppose that a variable
.code X
is two-dimensional (contains a list of lists).
.code X
must be twice nested in a
.codn repeat .
The outer repeat will traverse the lists
contained in
.codn X .
The inner repeat will traverse the elements of each of these
lists.

A nested repeat may be embedded in any of the clauses of a repeat,
not only the main clause.

.dir rep

The
.code rep
directive is similar to
.codn repeat .
Whereas
.code repeat
is line oriented,
.code rep
generates material within a line. It has all the same clauses,
but everything is specified within one line:

.cblk
  @(rep)... main material ... .... special clauses ...@(end)
.cble

More than one
.code @(rep)
can occur within a line, mixed with other material.
A
.code @(rep)
can be nested within a
.code @(repeat)
or within another
.codn @(rep) .

Also,
.code @(rep)
accepts the same
.code :counter
and
.code :vars
arguments.

.coSS @ repeat and @ rep Examples

Example 1: show the list
.code L
in parentheses, with spaces between
the elements, or the word
.code EMPTY
if the list is empty:

.cblk
  @(output)
  @(rep)@L @(single)(@L)@(first)(@L @(last)@L)@(empty)EMPTY@(end)
  @(end)
.cble

Here, the
.code @(empty)
clause specifies
.codn EMPTY .
So if there are no repetitions,
the text
.code EMPTY
is produced. If there is a single item in the list
.codn L ,
then
.code @(single)(@L)
produces that item between parentheses.  Otherwise
if there are two or more items, the first item is produced with
a leading parenthesis followed by a space by
.code @(first)(@L
and the last item is produced with a closing parenthesis:
.codn @(last)@L) .
All items in between are emitted with a trailing space by
the main clause:
.codn @(rep)@L .

Example 2: show the list L like Example 1 above, but the empty list is
.codn () .

.cblk
  @(output)
  (@(rep)@L @(last)@L@(end))
  @(end)
.cble

This is simpler. The parentheses are part of the text which
surrounds the
.code @(rep)
construct, produced unconditionally.
If the list
.code L
is empty, then
.code @(rep)
produces no output, resulting in
.codn () .
If the list
.code L
has one or more items, then they are produced with
spaces each one, except the last which has no space.
If the list has exactly one item, then the
.code @(last)
applies to it
instead of the main clause: it is produced with no trailing space.

.dir close

The syntax of the
.code close
directive is:

.cblk
.mets @(close << expr)
.cble

Where
.meta expr
evaluates to a stream. The
.code close
directive can be
used to explicitly close streams created using
.cblk
.meti @(output ... :named << var )
.cble
syntax, as an alternative to
.cblk
.meti @(output :finish << expr ).
.cble

Examples:

Write two lines to
.str foo.txt
over two output blocks using
a single stream:

.cblk
  @(output "foo.txt" :named foo)
  Hello,
  @(end)
  @(output :continue foo)
  world!
  @(end)
  @(close foo)
.cble

The same as above, using
.code :finish
rather than
.code :continue
so that the stream is closed at the end of the second block:

.cblk
  @(output "foo.txt" :named foo)
  Hello,
  @(end)
  @(output :finish foo)
  world!
  @(end)
.cble

.SS* Output Filtering

Often it is necessary to transform the output to preserve its meaning
under the convention of a given data format. For instance, if a piece of
text contains the characters
.code <
or
.codn > ,
then if that text is being
substituted into HTML, these should be replaced by
.code &lt;
and
.codn &gt; .
This is what filtering is for.  Filtering is applied to the contents of output
variables, not to any template text.
\*(TX implements named filters.  Built-in filters are named by keywords, given
below. User-defined filters are possible, however.  See notes on the deffilter
directive below.

Instead of a filter name, the syntax
.cblk
.meti (fun << name )
.cble
can be used. This
denotes that the function called
.meta name
is to be used as a filter.
This is discussed in the next section Function Filters below.

Built-in filters named by keywords:

.coIP :to_html
Filter text to HTML, representing special characters using HTML
ampersand sequences. For instance
.code >
is replaced by
.codn &gt; .

.coIP :from_html
Filter text with HTML codes into text in which the codes are replaced by the
corresponding characters. For instance
.code &gt;
is replaced by
.codn > .

.coIP :upcase
Convert the 26 lower case letters of the English alphabet to upper case.

.coIP :downcase
Convert the 26 upper case letters of the English alphabet to lower case.

.coIP :frompercent
Decode percent-encoded text. Character triplets consisting
of the
.code %
character followed by a pair of hexadecimal digits (case insensitive)
are are converted to bytes having the value represented by the hexadecimal
digits (most significant nybble first). Sequences of one or more such bytes are
treated as UTF-8 data  and decoded to characters.

.coIP :topercent
Convert to percent encoding according to RFC 3986. The text is first converted
to UTF-8 bytes. The bytes are then converted back to text as follows.
Bytes in the range 0 to 32, and 127 to 255 (note: including the ASCII DEL), 
bytes whose values correspond to ASCII characters which are listed by RFC 3986 
as being in the "reserved set", and the byte value corresponding to the
ASCII
.code %
character are encoded as a three-character sequence consisting
of the
.code %
character followed by two hexadecimal digits derived from the
byte value (most significant nybble first, upper case). All other bytes
are converted directly to characters of the same value without any such
encoding.

.coIP :fromurl
Decode from URL encoding, which is like percent encoding, except that
if the unencoded
.code +
character occurs, it is decoded to a space character.  Of course
.code %20
still decodes to space, and
.code %2B
to the
.code +
character.

.coIP :tourl
Encode to URL encoding, which is like percent encoding except that
a space maps to
.code +
rather than
.codn %20 .
The
.code +
character, being in the
reserved set, encodes to
.codn %2B .

.coIP :tonumber
Converts strings to numbers. Strings that contain a period,
.code e
or
.code E
are converted to floating point as if by the Lisp function
.codn flo-str .
Otherwise they are converted to integer as if using
.code int-str
with a radix of 10.
Non-numeric junk results in the object
.codn nil .

.coIP :tointeger
Converts strings to integers as if using
.code int-str
with a radix of 10.
Non-numeric junk results in the object
.codn nil .

.coIP :tofloat
Converts strings to floating-point values as if using the function
.codn flo-str .
Non-numeric junk results in the object
.codn nil.

.coIP :hextoint
Converts strings to integers as if using
.code int-str
with a radix of 16.
Non-numeric junk results in the object
.codn nil.

.PP

Examples:

To escape HTML characters in all variable substitutions occurring in an
output clause, specify
.code :filter :to_html
in the directive:

.cblk
  @(output :filter :to_html)
  ...
  @(end)
.cble

To filter an individual variable, add the syntax to the variable spec:

.cblk
  @(output)
  @{x :filter :to_html}
  @(end)
.cble

Multiple filters can be applied at the same time. For instance:

.cblk
  @(output)
  @{x :filter (:upcase :to_html)}
  @(end)
.cble

This will fold the contents of
.code x
to upper case, and then encode any special
characters into HTML. Beware of combinations that do not make sense.
For instance, suppose the original text is HTML, containing codes
like
.codn &quot; .
The compound filter
.code (:upcase :from_html)
will not work
because
.code &quot;
will turn to
.code &QUOT;
which no longer be recognized by the
.code :from_html
filter, sonce the entity names in HTML codes
are case-sensitive.

Capture some numeric variables and convert to numbers:

.cblk
  @date @time @temperature @pressure
  @(filter :tofloat temperature pressure)
  @;; temperature and pressure can now be used in calculations
.cble

.SS* Function Filters

A function can be used as a filter. For this to be possible, the function must
conform to certain rules:
.IP 1.
The function must take two special arguments, which may be followed
by additional arguments.

.IP 2.
When the function is called, the first argument will be bound to a string,
and the second argument will be unbound. The function must produce a 
value by binding it to the second argument. If the filter is to be used
as the final filter in a chain, it must produce a string.

For instance, the following is a valid filter function:

.cblk
  @(define foo_to_bar (in out))
  @  (next :string in)
  @  (cases)
  foo
  @    (bind out "bar")
  @  (or)
  @    (bind out in)
  @  (end)
  @(end)
.cble

This function binds the
.code out
parameter to
.str bar
if the in parameter
is
.strn foo ,
otherwise it binds the
.code out
parameter to a copy of the
.code in
parameter.
This is a simple filter.

To use the filter, use the syntax
.code (:fun foo_to_bar)
in place of a filter name.
For instance in the bind directive:

.cblk
  @(bind "foo" "bar" :lfilt (:fun foo_to_bar))
.cble

The above should succeed since the left side is filtered from
.str foo
to
.strn bar ,
so that there is a match.

Of course, function filters can be used in a chain:

.cblk
  @(output :filter (:downcase (:fun foo_to_bar) :upcase))
  ...
  @(end)
.cble

Here is a split function which takes an extra argument
which specifies the separator:

.cblk
  @(define split (in out sep))
  @  (next :list in)
  @  (coll)@(maybe)@token@sep@(or)@token@(end)@(end)
  @  (bind out token)
  @(end)
.cble

Furthermore, note that it produces a list rather than a string.
This function separates the argument in into tokens according to the
separator text carried in the variable
.codn sep .

Here is another function,
.codn join ,
which catenates a list:

.cblk
  @(define join (in out sep))
  @  (output :into out)
  @  (rep)@in@sep@(last)@in@(end)
  @  (end)
  @(end)
.cble

Now here is these two being used in a chain:

.cblk
  @(bind text "how,are,you")
  @(output :filter (:fun split ",") (:fun join "-"))
  @text
  @(end)
.cble

Output:

.cblk
  how-are-you
.cble

When the filter invokes a function, it generates the first two arguments
internally to pass in the input value and capture the output. The remaining
arguments from the
.code (:fun ...)
construct are also passed to the function.
Thus the string objects
.str ","
and
.str "-"
are passed as the
.code sep
argument to
.code split
and
.codn join .

Note that
.code split
puts out a list, which
.code join
accepts. So the overall filter
chain operates on a string: a string goes into split, and a string comes out of
join.

.dir deffilter

The
.code deffilter
directive allows a query to define a custom filter, which
can then be used in
.code output
clauses to transform substituted data.

This directive's syntax is illustrated in this example:
.IP code:
.cblk
\ @(deffilter rot13
    ("a" "n")
    ("b" "o")
    ("c" "p")
    ("d" "q")
    ("e" "r")
    ("f" "s")
    ("g" "t")
    ("h" "u")
    ("i" "v")
    ("j" "w")
    ("k" "x")
    ("l" "y")
    ("m" "z")
    ("n" "a")
    ("o" "b")
    ("p" "c")
    ("q" "d")
    ("r" "e")
    ("s" "f")
    ("t" "g")
    ("u" "h")
    ("v" "i")
    ("w" "j")
    ("x" "k")
    ("y" "l")
    ("z" "m"))
 @(collect)
 @line
 @(end)
 @(output :filter rot13)
 @(repeat)
 @line
 @(end)
 @(end)
.cble
.IP data:
.cblk
\ hey there!
.cble
.IP output:
.cblk
\ url gurer!
.cble
.PP

The
.code deffilter
symbol must be followed by the name of the filter to be defined,
followed by forms which evaluate to lists of strings. Each list must
be at least two elements long and specifies one or more texts which are mapped
to a replacement text. For instance, the following specifies a telephone keypad
mapping from upper case letters to digits.

.cblk
  @(deffilter alpha_to_phone ("E" "0")
                             ("J" "N" "Q" "1")
                             ("R" "W" "X" "2")
                             ("D" "S" "Y" "3")
                             ("F" "T" "4")
                             ("A" "M" "5")
                             ("C" "I" "V" "6")
                             ("B" "K" "U" "7")
                             ("L" "O" "P" "8")
                             ("G" "H" "Z" "9"))

  @(deffilter foo (`@a` `@b`) ("c" `->@d`))

  @(bind x ("from" "to"))
  @(bind y ("---" "+++"))
  @(deffilter sub x y)
.cble

The last deffilter above equivalent to
.codn "@(deffilter sub ("from" "to") ("---" "+++"))" .

Filtering works using a longest match algorithm. The input is scanned from left
to right, and the longest piece of text is identified at every character
position which matches a string on the left hand side, and that text is
replaced with its associated replacement text. The scanning then continues
at the first character after the matched text.

If none of the strings matches at a given character position, then that
character is passed through the filter untranslated, and the scan continues at
the next character in the input.

Filtering is not in-place but rather instantiates a new text, and so
replacement text is not re-scanned for more replacements.

If a filter definition accidentally contains two or more repetitions of the
same left hand string with different right hand translations, the later ones
take precedence. No warning is issued.


.dir filter

The syntax of the
.code filter
directive is:

.cblk
  @(filter FILTER { VAR }+ )
.cble

A filter is specified, followed by one or more variables whose values
are filtered and stored back into each variable.

Example: convert
.codn a ,
.codn b ,
and
.code c
to upper case and HTML encode:

.cblk
  @(filter (:upcase :to_html) a b c)
.cble

.SH* EXCEPTIONS

.SS* Introduction

The exceptions mechanism in \*(TX is another
disciplined form of non-local transfer, in addition to the blocks
mechanism (see BLOCKS above).  Like blocks, exceptions provide a construct
which serves as the target for a dynamic exit.  Both blocks and exceptions
can be used to bail out of deep nesting when some condition occurs.
However, exceptions provide more complexity. Exceptions are useful for
error handling, and \*(TX in fact maps certain error situations to exception
control transfers. However, exceptions are not inherently an error-handling
mechanism; they are a structured dynamic control transfer mechanism, one
of whose applications is error handling.

An exception control transfer (simply called an exception) is always identified
by a symbol, which is its type. Types are organized in a subtype-supertype
hierarchy.  For instance, the
.code file-error
exception type is a subtype of the
.code error
type. This means that a file error is a kind of error. An exception
handling block which catches exceptions of type
.code error
will catch exceptions of
type
.codn file-error ,
but a block which catches
.code file-error
will not catch all
exceptions of type
.codn error .
A
.code query-error
is a kind of error, but not a kind of
.codn file-error .
The symbol
.code t
is the supertype of every type: every exception type
is considered to be a kind of
.codn t .
(Mnemonic:
.code t
stands for type, as in any type).

Exceptions are handled using
.code @(catch)
clauses within a
.code @(try)
directive.

In addition to being useful for exception handling, the
.code @(try)
directive
also provides unwind protection by means of a
.code @(finally)
clause,
which specifies query material to be executed unconditionally when
the try clause terminates, no matter how it terminates.

.dir try

The general syntax of the
.code try
directive is

.cblk
  @(try)
  ... main clause, required ...
  ... optional catch clauses ...
  ... optional finally clause
  @(end)
.cble

A
.code catch
clause looks like:

.cblk
  @(catch TYPE [ PARAMETERS ])
  .
  .
  .
.cble

and also this simple form:

.cblk
  @(catch)
  .
  .
  .
.cble

which catches all exceptions, and is equivalent
to
.codn "@(catch t)" .

A
.code finally
clause looks like:

.cblk
  @(finally)
  ...
  .
  .
.cble

The main clause may not be empty, but the catch and finally may be.

A try clause is surrounded by an implicit anonymous block (see BLOCKS section
above). So for instance, the following is a no-op (an operation with no effect,
other than successful execution):

.cblk
  @(try)
  @(accept)
  @(end)
.cble

The
.code @(accept)
causes a successful termination of the implicit anonymous block.
Execution resumes with query lines or directives which follow, if any.

.code try
clauses and blocks interact. For instance, an
.code accept
from within
a try clause invokes a
.codn finally .
.IP code:
.cblk
\ @(block foo)
 @  (try)
 @    (accept foo)
 @  (finally)
 @     (output)
 bye!
 @     (end)
 @  (end)
.cble
.IP output:
.cblk
\ bye!
.cble
.PP

How this works: the
.code try
block's main clause is
.codn "@(accept foo)" .
This causes
the enclosing block named
.code foo
to terminate, as a successful match.
Since the
.cod try
is nested within this block, it too must terminate
in order for the block to terminate. But the try has a
.code finally
clause,
which executes unconditionally, no matter how the try block
terminates. The
.code finally
clause performs some output, which is seen.

.coSS The @ finally clause

A
.code try
directive can terminate in one of three ways. The main clause
may match successfully, and possibly yield some new variable bindings.
The main clause may fail to match. Or the main clause may be terminated
by a non-local control transfer, like an exception being thrown or a block
return (like the block foo example in the previous section).

No matter how the
.code try
clause terminates, the
.code finally
clause is processed.

The
.code finally
clause is itself a query which binds variables, which leads to
questions: what happens to such variables? What if the
.code finally
block fails
as a query? As well as: what if a
.code finally
clause itself initiates a
control transfer?  Answers follow.

Firstly, a
.code finally
clause will contribute variable bindings only if the main
clause terminates normally (either as a successful or failed match).
If the main clause of the
.code try
block successfully matches, then the
.code finally
block continues
matching at the next position in the data, and contributes bindings.
If the main clause fails, then the
.code finally
block tries to match at the same position where the main clause failed.

The overall
.code try
directive succeeds as a match if either the main clause
or the
.code finally
clause succeed. If both fail, then the try directive is
a failed match.

Example:
.IP code:
.cblk
\ @(try)
 @a
 @(finally)
 @b
 @(end)
 @c
.cble
.IP data:
.cblk
\ 1
 2
 3
.cble
.IP result:
.cblk
\ a="1"
 b="2"
 c="3"
.cble
.PP

In this example, the main clause of the
.code try
captures line
.str 1
of the data as
variable
.codn a ,
then the finally clause captures
.str 2
as
.codn b ,
and then the query continues with the
.code @c
line after try block, so that
.code c
captures
.strn "3" .

Example:
.IP code:
.cblk
\ @(try)
 hello @a
 @(finally)
 @b
 @(end)
 @c
.cble
.IP data:
.cblk
\ 1
 2
.cble
.IP result:
.cblk
\ b="1"
 c="2"
.cble
.PP

In this example, the main clause of the
.code try
fails to match, because
the input is not prefixed with
.srn "hello " .
However, the
.code finally
clause
matches, binding
.code b
to
.strn "1" .
This means that the try block is a successful
match, and so processing continues with
.code @c
which captures
.strn "2" .

When
.code finally
clauses are processed during a non-local return,
they have no externally visible effect if they do not bind variables.
However, their execution makes itself known if they perform side effects,
such as output.

A
.code finally
clause guards only the main clause and the
.code catch
clauses. It does not
guard itself.   Once the finally clause is executing, the
.code try
block is no
longer guarded.  This means if a nonlocal transfer, such as a block accept
or exception, is initiated within the finally clause, it will not re-execute
the
.code finally
clause. The
.code finally
clause is simply abandoned.

The disestablishment of blocks and
.code try
clauses is properly interleaved
with the execution of
.code finally
clauses. This means that all surrounding
exit points are visible in a
.code finally
clause, even if the
.code finally
clause
is being invoked as part of a transfer to a distant exit point.
The finally clause can make a control transfer to an exit point which
is more near than the original one, thereby "hijacking" the control
transfer. Also, the anonymous block established by the
.code try
directive
is visible in the
.code finally
clause.

Example:

.cblk
  @(try)
  @  (try)
  @    (next "nonexistent-file")
  @  (finally)
  @    (accept)
  @  (end)
  @(catch file-error)
  @  (output)
  file error caught
  @  (end)
  @(end)
.cble

In this example, the
.code @(next)
directive throws an exception of type
.codn file-error ,
because the given file does not exist. The exit point for this exception is the
.code @(catch file-error)
clause in the outer-most
.code try
block. The inner block is
not eligible because it contains no catch clauses at all. However, the inner
try block has a finally clause, and so during the processing of this
exception which is headed for
.codn "@(catch file-error)" ,
the
.code finally
clause performs an anonymous
.codn accept .
The exit point for that
.code accept
is the anonymous block
surrounding the inner
.codn try .
So the original
transfer to the
.code catch
clause is thereby abandoned. The inner
.code try
terminates
successfully due to the
.codn accept ,
and since it constitutes the main clause of the outer try,
that also terminates successfully. The
.str "file error caught"
message is never printed.

.c1SS catch clauses

.code catch
clauses establish their associated
.code try
blocks as potential exit points for
exception-induced control transfers (called "throws").

A
.code catch
clause specifies an optional list of symbols which represent
the exception types which it catches. The
.code catch
clause will catch
exceptions which are a subtype of any one of those exception types.

If a
.code try
block has more than one
.code catch
clause which can match a given
exception, the first one will be invoked.

When a
.code catch
is invoked, it is of course understood that the main clause did
not terminate normally, and so the main clause could not have produced any
bindings.

.code catch
clauses are processed prior to
.codn finally .

If a
.code catch
clause itself throws an exception, that exception cannot
be caught by that same clause or its siblings in the same try block.
The
.code catch
clauses of that block are no longer visible at that point.
Nevertheless, the
.code catch
clauses are still protected by the finally block.
If a catch clause throws, or otherwise terminates, the
.code finally
block is still processed.

If a
.code finally
block throws an exception, then it is simply aborted;
the remaining directives in that block are not processed.

So the success or failure of the
.code try
block depends on the behavior of the
.code catch
clause or the
.ocde finally
clause, if there is one. If either of them succeed, then the try
block is considered a successful match.

Example:
.IP code:
.cblk
\ @(try)
 @  (next "nonexistent-file")
 @  x
 @  (catch file-error)
 @a
 @(finally)
 @b
 @(end)
 @c
.cble
.IP data:
.cblk
\ 1
 2
 3
.cble
.IP result:
.cblk
\ a="1"
 b="2"
 c="3"
.cble
.PP

Here, the
.code try
block's main clause is terminated abruptly by a
.code file-error
exception from the
.code @(next)
directive.   This is handled by the
.code catch
clause, which binds variable
.code a
to the input line
.strn 1 .
Then the
.code finally
clause executes, binding
.code b
to
.strn  2 .
The
.code try
block then terminates successfully, and so
.code @c
takes
.strn "3" .

.coSS @ catch Clauses with Parameters

A
.code catch
clause may have parameters following the type name, like this:

.cblk
  @(catch pair (a b))
.cble

To write a catch-all with parameters, explicitly write the
master supertype t:

.cblk
  @(catch t (arg ...))
.cble

Parameters are useful in conjunction with
.codn throw .
The built-in
.code error
exceptions carry one argument, which is a string containing
the error message. Using
.codn throw ,
arbitrary parameters can be passed
from the throw site to the catch site.

.dir throw

The
.code throw
directive generates an exception. A type must be specified,
followed by optional arguments. For example,

.cblk
  @(throw pair "a" `@file.txt`)
.cble

throws an exception of type
.codn pair ,
with two arguments, being
.str a
and the expansion of the quasiliteral
.codn `@file.txt` .

The selection of the target
.code catch
is performed purely using the type
name; the parameters are not involved in the selection.

Binding takes place between the arguments given in
.code throw
and the target
.codn catch .

If any
.code catch
parameter, for which a
.code throw
argument is given, is a bound
variable, it has to be identical to the argument, otherwise the catch fails.
(Control still passes to the
.codn catch ,
but the catch is a failed match).

.IP code:
.cblk
\ @(bind a "apple")
 @(try)
 @(throw e "banana")
 @(catch e (a))
 @(end)
.cble
.IP result:
.cblk
\ [query fails]
.cble
.PP

If any argument is an unbound variable, the corresponding parameter
in the
.code catch
is left alone: if it is an unbound variable, it remains
unbound, and if it is bound, it stays as is.
.IP code:
.cblk
\ @(try)
 @(trow e "honda" unbound)
 @(catch e (car1 car2))
 @car1 @car2
 @(end)
.cble
.IP data:
.cblk
\ honda toyota
.cble
.IP result:
.cblk
\ car1="honda"
 car2="toyota"
.cble
.PP

If a
.code catch
has fewer parameters than there are throw arguments,
the excess arguments are ignored:
.IP code:
.cblk
\ @(try)
 @(throw e "banana" "apple" "pear")
 @(catch e (fruit))
 @(end)
.cble
.IP result:
.cblk
\ fruit="banana"
.cble
.PP

If a
.code catch
has more parameters than there are throw arguments, the excess
parameters are left alone. They may be bound or unbound variables.
.IP code:
.cblk
\ @(try)
 @(trow e "honda")
 @(catch e (car1 car2))
 @car1 @car2
 @(end)
.cble
.IP data:
.cblk
\ honda toyota
.cble
.IP result:
.cblk
\ car1="honda"
 car2="toyota"
.cble
.PP

A
.code throw
argument passing a value to a
.code catch
parameter which is unbound causes
that parameter to be bound to that value.

.code throw
arguments are evaluated in the context of the
.codn throw ,
and the bindings
which are available there. Consideration of what parameters are bound
is done in the context of the catch.
.IP code:
.cblk
\ @(bind c "c")
 @(try)
 @(forget c)
 @(bind (a c) ("a" "lc"))
 @(throw e a c)
 @(catch e (b a))
 @(end)
.cble
.IP result:
.cblk
\ c="c"
 b="a"
 a="lc"
.cble
.PP

In the above example,
.code c
has a toplevel binding to the string
.strn "c" ,
but then becomes unbound
via
.code forget
within the
.code try
construct, and rebound to the value
.strn lc .
Since the
.code try
construct is terminated by a
.codn throw ,
these modifications of the
binding environment are discarded. Hence, at the end of the query, variable
.code c
ends up bound to the original value
.strn c .
The
.code throw
still takes place
within the scope of the bindings set up by the
.code try
clause, so the values of
.code a
and
.code c
that are thrown are
.str a
and
.strn lc .
However, at the
.code catch
site, variable
.code a
does not have a binding.  At that point, the binding to
.str a
established in
the
.code try
has disappeared already. Being unbound, the
.code catch
parameter
.code a
can take
whatever value the corresponding throw argument provides, so it ends up with
.strn lci .

.dir defex

The
.code defex
directive allows the query writer to invent custom exception types,
which are arranged in a type hierarchy (meaning that some exception types are
considered subtypes of other types).

Subtyping means that if an exception type
.code B
is a subtype of
.codn A ,
then every
exception of type
.code B
is also considered to be of type
.codn A .
So a catch for type
.code A
will also catch exceptions of type
.codn B .
Every type is a supertype of itself: an
.code A
is a kind of
.codn A .
This of course implies that every type is a subtype of itself
also.  Furthermore, every type is a subtype of the type
.codn t ,
which has no
supertype other than itself. Type
.code nil
is is a subtype of every type, including
itself.  The subtyping relationship is transitive also. If
.code A
is a subtype
of
.codn B ,
and
.code B
is a subtype of
.codn C ,
then
.code A
is a subtype of
.codn C .

.code defex
may be invoked with no arguments, in which case it does nothing:

.cblk
  @(defex)
.cble

It may be invoked with one argument, which must be a symbol. This introduces a
new exception type. Strictly speaking, such an introduction is not necessary;
any symbol may be used as an exception type without being introduced by
.codn @(defex) :

.cblk
  @(defex a)
.cble

Therefore, this also does nothing, other than document the intent to use
a as an exception.

If two or more argument symbols are given, the symbols are all introduced as
types, engaged in a subtype-supertype relationship from left to right.
That is to say, the first (leftmost) symbol is a subtype of the next one,
which is a subtype of the next one and so on. The last symbol, if it
had not been already defined as a subtype of some type, becomes a
direct subtype of the master supertype
.codn t .
Example:

.cblk
  @(defex d e)
  @(defex a b c d)
.cble

The first directive defines
.code d
as a subtype of
.codn e ,
and
.code e
as a subtype of
.codn t .
The second defines
.code a
as a subtype of
.codn b ,
.code b
as a subtype of
.codn c ,
and
.code c
as a subtype of
.codn d ,
which is already defined as a subtype of
.codn e .
Thus
.code a
is now a subtype of
.codn e .
The the above can be condensed to:

.cblk
  @(defex a b c d e)
.cble

Example:
.IP code:
.cblk
\ @(defex gorilla ape primate)
 @(defex monkey primate)
 @(defex human primate)
 @(collect)
 @(try)
 @(skip)
 @(cases)
 gorilla @name
 @(throw gorilla name)
 @(or)
 monkey @name
 @(throw monkey name)
 @(or)
 human @name
 @(throw human name)
 @(end)@#cases
 @(catch primate (name))
 @kind @name
 @(output)
 we have a primate @name of kind @kind
 @(end)@#output
 @(end)@#try
 @(end)@#collect
.cble
.IP data:
.cblk
\ gorilla joe
 human bob
 monkey alice
.cble
.IP output:
.cblk
\ we have a primate joe of kind gorilla
 we have a primate bob of kind human
 we have a primate alice of kind monkey
.cble
.PP

Exception types have a pervasive scope. Once a type relationship is introduced,
it is visible everywhere. Moreover, the
.code defex
directive is destructive,
meaning that the supertype of a type can be redefined. This is necessary so
that something like the following works right:

.cblk
  @(defex gorilla ape)
  @(defex ape primate)
.cble

These directives are evaluated in sequence. So after the first one, the
.code ape
type has the type
.code t
as its immediate supertype.  But in the second directive,
.code ape
appears again, and is assigned the
.code primate
supertype, while retaining
.code gorilla
as a subtype.  This situation could be diagnosed as an
error, forcing the programmer to reorder the statements, but instead
\*(TX obliges. However, there are limitations.  It is an error to define a
subtype-supertype relationship between two types if they are already connected
by such a relationship, directly or transitively. So the following
definitions are in error:

.cblk
  @(defex a b)
  @(defex b c)
  @(defex a c)@# error: a is already a subtype of c, through b

  @(defex x y)
  @(defex y x)@# error: circularity; y is already a supertype of x.
.cble

.dir assert

The
.code assert
directive requires the remaining query or sub-query which follows it
to match. If the remainder fails to match, the assert directive throws an
exception. If the directive is simply

.cblk
  @(assert)
.cble

Then it throws an assertion of type assert, which is a subtype of error.
The assert directive also takes arguments similar to the throw
directive. The following assert directive, if it triggers, will throw
an exception of type
.codn foo ,
with arguments
.code 1
and
.strn 2 :

.cblk
  @(assert foo 1 "2")
.cble

Example:

.cblk
  @(collect)
  Important Header
  ----------------
  @(assert)
  Foo: @a, @b
  @(end)
.cble

Without the assertion in places, if the
.code Foo: @a, @b
part does not
match, then the entire interior of the
.code @(collect)
clause fails,
and the collect continues searching for another match.

With the assertion in place, if the text
.str "Important Header"
and its
underline match, then the remainder of the collect body must
match, otherwise an exception is thrown. Now the program will not
silently skip over any Important Header sections due to a problem
in its matching logic. This is particularly useful when the matching is varied
with numerous cases, and they must all be handled.

There is a horizontal
.code assert
directive also. For instance:

.cblk
  abc@(assert)d@x
.cble

asserts that if the prefix
.str abc
is matched, then it must be
followed by a successful match for
.strn "d@x" ,
or else an exception is thrown.

.SH* TXR LISP
The \*(TX language contains an embedded Lisp dialect called \*(TL.

This language is exposed in \*(TX in several ways.

Firstly, in any situation that calls for an expression, a Lisp 
expression can be used, if it is preceded by the
.code @
character. The Lisp expression
is evaluated and its value  becomes the value of that expression.
Thus, \*(TX directives are embedded in literal text using
.codn @ ,
and Lisp expressions
are embedded in directives using
.code @
also.

Secondly, the
.code @(do)
directive can be used for evaluating one or more Lisp
forms, such that their value is thrown away. This is useful for evaluating some
Lisp code for the sake of its side effect, such as defining a variable,
updating a hash table, et cetera.

Thirdly, the
.code @(require)
directive can be used to evaluate Lisp expressions
as part of the matching logic of the \*(TX pattern language. The return value
of the rightmost expression is examined. If it is nil, then the
.code @(require)
directive triggers a match failure. Otherwise, matching proceeds.

Lastly, \*(TL expressions can be evaluated via the
command line, using the
.code -e
and
.code -p
options.

.B
Examples:

Bind variable
.code a
to the integer 4:

.cblk
  @(bind a @(+ 2 2))
.cble

Bind variable
.code b
to the standard input stream:

.cblk
  @(bind a @*stdin*)
.cble

Define several Lisp functions inside
.codn @(do) :

.cblk
  @(do
    (defun add (x y) (+ x y))

    (defun occurs (item list)
      (cond ((null list) nil)
            ((atom list) (eql item list))
            (t (or (eq (first list) item)
                   (occurs item (rest list)))))))
.cble

Trigger a failure unless previously bound variable
.code answer
is greater than 42:

.cblk
  @(require (> (int-str answer) 42)
.cble

.SS* Overview

\*(TL is a small and simple dialect, like Scheme, but much more similar to
Common Lisp than Scheme. It has separate value and function binding namespaces,
like Common Lisp (and thus is a Lisp-2 type dialect), and represents boolean
.B true
and
.B false
with the symbols
.code t
and
.code nil
(note the case sensitivity of
identifiers denoting symbols!) Furthermore, the symbol
.code nil
is also the empty list, which terminates nonempty lists.

\*(TL has lexically scoped local variables and dynamic global variables,
similarly to Common Lisp, including the convention that
.code defvar
marks symbols for dynamic binding in local scopes. Lexical closures
are supported.  Funtions are lexically scoped in \*(TL; they can be
defined in pervasive global environment using
.code defun
or in local scopes using
.code flet
and
.codn labels .

.SS* Additional Syntax

Much of the \*(TL syntax has been introduced in the previous sections of the
manual, since directive forms are based on it. There is some additional syntax
that is useful in \*(TL programming.

.SS* Symbol Tokens

The symbol tokens in \*(TL,
called a
.meta lident
(Lisp identifier) has a similar syntax to the
.meta bident
(braced identifier) in the \*(TX pattern language. It may consist of
all the same characters, as well as the
.code /
(slash) character which may not be used in a
.metn bident .
Thus a
.meta lident
may consist of these characters, in addition to letters and numbers:

.cblk
 ! $ % & * + - < = > ? \e _ ~ /
.cble

and of course, may not look like a number. A lone
.code /
is a symbol in \*(TL. The token
.code /abc/
is also a symbol, and not a regular expression, like it is in the braced
variable syntax. Within \*(TL, regular expressions are written with
a leading
.codn # .

.SS* Consing Dot

Unlike other major Lisp dialects, \*(TL allows a consing dot with no forms
preceding it. This construct simply denotes the form which follows the dot.
That is to say, the parser implements the following transformation:

.cblk
  (. expr) -> expr
.cble

This is convenient in writing function argument lists that only take
variable arguments. Instead of the syntax:

.cblk
  (defun fun args ...)
.cble

the following syntax can be used:

.cblk
  (defun fun (. args) ...)
.cble

When a
.code lambda
form is printed, it is printed in the following style.

.cblk
  (lambda nil ...) -> (lambda () ...)
  (lambda sym ...) -> (lambda (. sym) ...)
  (lambda (sym) ...) -> (lambda (sym) ...)
.cble

In no other circumstances is
.code nil
printed as
.codn () ,
or an atom
.code sym
as
.codn "(. sym)" .

.SS* Quote and Quasiquote

.meIP >> ' expr

The quote character in front of an expression is used for suppressing evaluation,
which is useful for forms that evaluate to something other than themselves.
For instance if
.code '(+ 2 2)
is evaluated, the value is the three-element list
.codn "(+ 2 2)" ,
whereas if
.code (+ 2 2)
is evaluated, the value is
.codn 4 .
Similarly, the value of
.code 'a
is the symbol
.code a
itself, whereas the value of
.code a
is the contents of the variable
.codn a .

.meIP >> ^ qq-template

The caret in front of an expression is a quasiquote. A quasiquote is like
a quote, but with the possibility of substitution of material.

Under a quasiquote, form is considered to be a quasiquote template. The template
is considered to be a literal structure, except that it may contain
the notations
.cblk
.meti >> , expr
.cble
and
.cblk
.meti >> ,* expr
.cble
which denote non-constant parts.

A quasiquote gets translated into code which, when evaluated, constructs
the structure implied by
.metn qq-template ,
taking into account the unquotes and splices.

A quasiquote also processes nested quasiquotes specially.

If
.meta qq-template
does not contain any unquotes or splices (which match its
level of nesting), or is simply an atom, then
.cblk
.meti >> ^ qq-template
.cble
is equivalent to
.cblk
.meti >> ' qq-template .
.cble
in other words, it is like an ordinary quote.  For instance
.code ^(a b ^(c ,d))
is equivalent to
.codn '(a b ^(c ,d)) .
Although there is an unquote ,d it
belongs to the inner quasiquote
.codn ^(c ,d) ,
and the outer quasiquote does not have
any unquotes of its own, making it equivalent to a quote.

Dialect note: in Common Lisp and Scheme,
.code ^form
is written
.codn `form ,
and
quasiquotes are also informally known as backquotes.  In \*(TX, the backquote
character
.code `
used for quasi string literals.

.meIP >> , expr

The comma character is used within a
.meta qq-template
to denote an unquote.  Whereas
the quote suppresses evaluation, the comma introduces an exception: an element
of a form which is evaluated. For example, list
.code ^(a b c ,(+ 2 2) (+ 2 2))
is the list
.codn (a b c 4 (+ 2 2)) .
Everything
in the quasiquote stands for itself, except for the
.code ,(+ 2 2)
which is evaluated.

Note: if a variable is called
.codn *x* ,
then the syntax
.code ,*x*
means
.codn ,* x* :
splice
the value of
.codn x* .
In this situation, whitespace between the comma and the
variable name should be used:
.codn , *x* .

.meIP >> ,* expr

The comma-star operator is used within quasiquote list to denote a splicing
unquote.  The form which follows
.code ,*
must evaluate to a list. That list is spliced into
the structure which the quasiquote denotes. For example:
.code '(a b c ,*(list (+ 3 3) (+ 4 4) d))
evaluates to
.codn "(a b c 6 8 d)" .
The expression
.code (list (+ 3 3) (+ 4 4))
is evaluated to produce the list
.codn "(6 8)" ,
and this list is spliced into the quoted template.

Dialect note: in other Lisp dialects, the equivalent syntax is usually
.code ,@
(comma at). The
.code @
character already has an assigned meaning, so
.code *
is used.

.SS* Quasiquoting non-List Objects
Quasiquoting is supported over hash table and vector literals (see Vectors
and Hashes below).  A hash table or vector literal can be quoted, like any
object, for instance:

.cblk
  '#(1 2 3)
.cble

The
.code #(1 2 3)
literal is turned into a vector atom right in the \*(TX parser,
and this atom is being quoted: this is
.cblk
.meti (quote << atom )
.cble
syntactically, which evaluates to
.metn atom .

When a vector is quasi-quoted, this is a case of
.cblk
.meti >> ^ atom
.cble
which evaluates to
.metn atom .

A vector can be quasiquoted, for example:

.cblk
  ^#(1 2 3)
.cble

Of ourse, unquotes can occur within it.

.cblk
  (let ((a 42))
    ^#(1 ,a 3)) ; value is #(1 42 3)
.cble

In this situation, the
.code ^#(...)
notation produces code which constructs a vector.

The vector in the following example is also a quasivector. It contains
unquotes, and though the quasiquote is not directly applied to it,
it is embedded in a quasiquote:

.cblk
  (let ((a 42))
    ^(a b c #(d ,a))) ; value is (a b c #(d 42))
.cble

Hash table literals have two parts: the list of hash construction
arguments and the key-value pairs. For instance:

.cblk
   #H((:equal-based) (a 1) (b 2))
.cble

where
.code (:equal-based)
is the list of construction arguments and the pairs
.code (a 1)
and
.code (b 2)
are the key/value entries. Hash literals may be quasiquoted.  In
quasiquoting, the arguments and pairs are treated as separate syntax; it is not
one big list.  So the following is not a possible way to express the above
hash:

.cblk
  ;; not supported: splicing across the entire syntax
  (let ((hash-syntax '((:equal-based) (a 1) (b 2))))
    ^#H(,*hash-syntax))
.cble

This is correct:

.cblk
  ;; fine: splicing hash arguments and contents separately
  (let ((hash-args '(:equal-based))
        (hash-contents '((a 1) (b 2))))
    ^#H(,hash-args ,*hash-contents))
.cble

.SS* Quasiquoting combined with Quasiliterals
When a quasiliteral is embedded in a quasiquote, it is possible to use
splicing to insert material into the quasiliteral.

Example:

.cblk
  (eval (let ((a 3)) ^`abc @,a @{,a} @{(list 1 2 ,a)}`))

  -> "abc 3 3 1 2 3"
.cble

.SS* Vectors

.coIP "#(...)"

A hash token followed by a list denotes a vector. For example
.code #(1 2 a)
is a three-element vector containing the numbers
.code 1
and
.codn 2 ,
and the symbol
.codn a .

.SS* Hashes

.meIP <> #H(( hash-argument *) >> ( key << value )*)

The notation
.code #H
followed by a nested list syntax denotes a hash table literal.
The first item in the syntax is a list of keywords. These are the same
keywords as are used when calling the function hash to construct
a hash table. Allowed keywords are:
.codn :equal-based ,
.code :weak-keys
and
.codn :weak-values .
An empty list can be specified as
.code nil
or
.codn () ,
which defaults to a
hash table based on the
.code eql
function, with no weak semantics.

.coSS The @ .. notation
In \*(TL, there is a special "dotdot" notation consisting of a pair of dots.
This can be written between successive atoms or compound expressions, and is a
shorthand for cons.

That is to say,
.code A .. B
translates to
.codn "(cons A B)" ,
and so for instance
.code (a b .. (c d) e .. f . g)
means
.codn "(a (cons b (c d)) (cons e f) . g)" .

This is a syntactic sugar useful in certain situations in which a cons is used
to represent a pair of numbers or other objects. For instance, if
.code L
is a list, then
.code [L 1 .. 3]
computes a sublist of
.code L
consisting of
elements 1 through 2 (counting from zero).

Restrictions:

The notation must be enclosed in a list. For instance
.code a..b
is not, by itself, an
expression, but
.code (a..b)
is. This is important if Lisp data is being parsed from
a string or stream using the read function. If the data
.str "a..b"
is parsed, the symbol
.code a
will be extracted, leaving
.strn ..a ,
which, if parsed,
produces a syntax error since it consists of a "dotdot" token followed by
a symbol, which is not valid syntax.

The notation cannot occur in the dot position; that is, the syntax
.code (a . b .. c)
is invalid. The dotdot operator can only be used between the non-dot-position
elements of a list.

.SS* The DWIM Brackets
\*(TL has a square bracket notation. The syntax
.code [...]
is a shorthand
way of writing
.codn "(dwim ...)" .
The
.code []
syntax is useful for situations
where the expressive style of a Lisp-1 dialect is useful.

For instance if
.code foo
is a variable which holds a function object, then
.code [foo 3]
can be used to call it, instead of
.codn "(call foo 3)" .
If foo is a vector, then
.code [foo 3]
retrieves the fourth element, like
.codn "(vecref foo 3)" .
Indexing over lists,
strings and hash tables is possible, and the notation is assignable.

Furthermore, any arguments enclosed in
.code []
which are symbols are treated
according to a modified namespace lookup rule.

More details are given in the documentation for the
.code dwim
operator.

.SS* Compound Forms
In \*(TL, there are two types of compound forms: the Lisp-2 style
compound forms, denoted by ordinary lists that are expressed with parentheses.
There are Lisp-1 style compound forms denoted by the DWIM Brackets, discussed
in the previous section.

The first position of an ordinary Lisp-2 style compound form, is expected to
have a function or operator name.  Then arguments follow. There may
also be an expression in the dotted position, if the form is a function call.

If the form is a function call then the arguments are evaluated. If any of the
arguments are symbols, they are treated according to Lisp-2 namespacing rules.

Additionally, if there is an expression in the dotted position, it is also
evaluated. It should evaluate to a sequence: a list, vector or string.  The
elements of the sequence generate additional arguments for the function
call. Note, however, that a compound form cannot be used in the dot position,
for obvious reasons, namely that
.code (a b c . (foo z))
does not mean that there is
a compound form in the dot position, but denotes an alternate spelling for
.codn "(a b c foo z)" ,
where foo behaves as a variable.

The DWIM brackets are similar, except that the first position is an arbitrary
expression which is evaluated according to the same rules as the remaining
positions. The first expression must evaluate to a function, or else to some
other object for which the DWIM syntax is defined, such as a vector, string,
list or hash. Operators are not supported.   The dotted syntax for application
of additional arguments from a list or vector is supported in the DWIM
brackets just like in the parentheses.

Examples:

.cblk
  ;; a contains 3
  ;; b contains 4
  ;; c contains #(5 6 7)
  ;; s contains "xyz"

  (foo a b . c) ;; calls (foo 3 4 5 6 7)
  (foo a)       ;; calls (foo 3)
  (foo . s)     ;; calls (foo #\ex #\ey #\ez)

  [foo a b . c] ;; calls (foo 3 4 5 6 7)

  [c 1]         ;; indexes into vector #(5 6 7) to yield 6
.cble

Dialect Note:

In some other Lisp dialects, the improper list syntax is not supported;
a function called apply (or similar) must be used for application even if
the expression which gives the trailing arguments is a symbol. Moreover,
applying sequences other than lists is not supported.

.SS* Regular Expressions
In \*(TL, the
.code /
character can occur in symbol names, and the
.code /
token
is a symbol. Therefore the
.code /regex/
syntax is not used for denoting regular expressions; rather, the
.code #/regex/
syntax is used.

.coSS Generalization of List Accessors
In ancient Lisp in the 1960's, it was not possible to apply the operations
.code car
and
.code cdr
to the
.code nil
symbol (empty list), because it is not a
.code cons
cell. In
the InterLisp dialect, this restriction was lifted: these operations were
extended to accept
.code nil
(and return
.codn nil ).
The convention was adopted in
other Lisp dialects such as MacLisp and eventually in Common Lisp. Thus there
exists an object which is not a cons, yet which takes
.code car
and
.codn cdr .

In \*(TL, this relaxation is extended further. For the sake of convenience,
the operations
.code car
and
.codn cdr ,
are made to work with strings and vectors:

.cblk
  (cdr "") -> nil
  (car "") -> nil

  (car "abc") -> #\ea
  (cdr "abc") -> "bc"

  (cdr #(1 2 3)) -> #(2 3)
  (car #(1 2 3)) -> 1
.cble

The
.code ldiff
function is also extended in a special way. When the right parameter
is a string or vector, then it uses the equal equality test rather than eq
for detecting the tail of the list.

.cblk
  (ldiff "abcd" "cd") -> (#\ea #\eb)
.cble

The
.code ldiff
operation starts with
.str "abcd"
and repeatedly applies
.code cdr
to produce
.str "bcd"
and
.strn "cd" ,
until the suffix is equal to the second argument:
.cblk
(equal "cd" "cd")
.cble
yields true.

Operations based on
.codn car ,
.code cdr
and
.codn ldiff ,
such as
.code keep-if
and
.code remq
extend to
strings and vectors.

Most derived list processing operations such as
.code remq
or
.code mapcar
obey the following
rule: the returned object follows the type of the leftmost input list object.
For instance, if one or more sequences are processed by
.codn mapcar ,
and the
leftmost one is a character string, the function is expected to return
characters, which are converted to a character string. However, in the
event that the objects produced cannot be assembled into that type of
sequence, a list is returned instead.

For example
.cblk
[mapcar list "ab" "12"]
.cble
returns
.codn "((#\ea #\eb) (#\e1 #\e2))" ,
because a string cannot hold lists of characters. However
.cblk
[mappend list "ab" "12"]
.cble
returns
.strn "a1b2" .

The lazy versions of these functions such as
.code mapcar*
do not have this behavior;
they produce lazy lists.

.SS* Callable Objects

In \*(TL, sequences (strings, vectors and lists) and hashes can be used
as functions everywhere, not just with the DWIM brackets. Sequences work
as one or two-argument functions. With a single argument, an element is
selected by position and returned. With two arguments, a range is extracted and
returned. Hashes also work as one or two argument functions, corresponding
to the arguments of the gethash function.

Moreover, when a sequence is used as a function of one argument, and the
argument is a cons cell rather than an integer, then the call becomes a
two-argument call in which the car and cdr of the cell are passed as separate
arguments. This allows for syntax like
.cblk
(call "abc" 0..1).
.cble

.B Example 1:

.cblk
  (mapcar "abc" '(2 0 1)) -> (#\ec #\ea #\eb)
.cble

Here,
.code mapcar
treats the string
.str abc
as a function of one argument (since there
is one list argument). This function maps the indices
.codn 0 ,
.code 1
and
.code 2
to the
corresponding characters of string
.strn abc .
Through this function, the list of 
integer indices
.code (2 0 1)
is taken to the list of characters
.codn (#\ec #\ea #\eb) .

.B Example 2:

.cblk
  (call '(1 2 3 4) 1..3) -> (2 3)
.cble

Here, the shorthand
.code 1 .. 3
denotes
.codn (cons 1 3) .
A cons cell as an argument
to a sequence performs range extraction: taking a slice starting at
index 1, up to and not including index 3, as if by the call
.codn (sub '(1 2 3 4) 1 3) .

.B Example 3:

.cblk
  (call '(1 2 3 4) '(0 2)) -> (1 2)
.cble

A list of indices applied to a sequence is equivalent to using the
select function, as if
.code (select '(1 2 3 4) '(0 2))
were called.

.SS* Special Variables
Similarly to Common Lisp, \*(TL is lexically scoped by default, but
also has dynamically scoped (a.k.a "special") variables.

When a variable is defined with
.codn defvar ,
a binding for the symbol is
introduced in the global name space, regardless of in what scope the
.code defvar
form occurs.

Furthermore, at the time the defvar form is evaluated, the symbol which
names the variable is tagged as special.

When a symbol is tagged as special, it behaves differently when it is used
in a lexical binding construct like
.codn let ,
and all other such constructs
such as function parameter lists. Such a binding is not the usual lexical
binding, but a "rebinding" of the global variable. Over the dynamic scope
of the form, the global variable takes on the value given to it by the
rebinding. When the form terminates, the prior value of the variable
is restored. (This is true no matter how the form terminates; even if by
an exception.)

Because of this "pervasive special" behavior of a symbol that has been
used as the name of a global variable, a good practice is to make global
variables have visually distinct names via the "earmuffs" convention:
beginning and ending the name with an asterisk.

Certain variables in \*(TX's library break this convention; however, they at
least have distinct prefixes, examples being example s-ifmt, log-emerg and
sig-hup.

Example:

.cblk
  (defvar *x* 42)     ;; *x* has a value of 42

  (defun print-x ()
    (format t "~a\en" *x*))

  (let ((*x* "abc"))  ;; this overrides *x*
    (print-x))        ;; *x* is now "abc" and so that is printed

  (print-x)           ;; *x* is 42 again and so "42" is printed
.cble

.SH* TXR LISP OPERATOR AND FUNCTION LIBRARY
A compound expression with a symbol as its first element, if
intended to be evaluated, denotes either an operator invocation or a function
call. This depends on whether the symbol names an operator or a function.

When the form is an operator invocation, the interpretation of the meaning of
that form is under the complete control of that operator.

If the compound form is a function call, the remaining forms, if any, denote
argument expressions to the function.  They are evaluated in left to right
order to produce the argument values, which are passed to the function.  An
exception is thrown if there are not enough arguments, or too many.  Programs
can define named functions with the defun operator

Some operators are macros. There exist predefined macros in the library, and
macro operators can also be user-defined using the macro-defining operator
.codn defmacro .
Operators that are not macros are called special operators.

Macro operators work as functions which are given the source code of the form.
They analyze the form, and translate it to another form which is substituted in
their place.   This happens during a code walking phase called the expansion
phase, which is applied to Lisp code prior to evaluation. All macros are
expanded in the expansion phase, resulting in code which contains only
function calls and the executable forms of the operators.

(Special operators can also perform code transformations during the expansion
phase, but that is not considered macroexpansion, but rather an adjustment
of the representation of the operator into an required executable form.)

The following sections list all of the special operators, macros
and functions in \*(TL.

In these sections Syntax is indicated using these conventions:

.ie n \{\
.  coIP <word>
A symbol in angle brackets
.\}
.el \{\
.  coIP \f[5]word\f[]
A symbol in
.meta fixed-width-italic
font
.\}
denotes some syntactic unit: it
may be a symbol or compound form. The syntactic unit is explained
in the Description section.

.ie n \{\
.coIP {syntax}* <word>*
.\}
.el \{\
.coIP {syntax}* \f[5]word\f[]*
.\}
This indicates a repetition of zero or more of the given
syntax enclosed in the braces or syntactic unit.

.ie n \{\
.coIP {syntax}+ <word>+
.\}
.el \{\
.coIP {syntax}+ \f[5]word\f[]+
.\}
This indicates a repetition of one or more of the given
syntax enclosed in the braces or syntactic unit.

.ie n \{\
.coIP [syntax] [<word>]
.\}
.el \{\
.coIP [syntax] [\f[5]word\f[]]
.\}
Square brackets indicate optional syntax.

.coIP alternative1 | alternative2 | ... | alternativeN
Multiple syntactic variations allowed in one place are
indicated as bar-separated items.

.SS* Control Flow and Sequencing
.coNP Operators @ progn and @ prog1
.synb
.mets (progn << form *)
.mets (prog1 << form *)
.syne
.desc
The
.code progn
operator evaluates forms in order, and returns the value
of the last form. The return value of the form
.code (progn)
is
.codn nil .

The
.code prog1
operator evaluates forms in order, and returns the value
of the first form. The return value of the form
.code (prog1)
is
.codn nil .

Various other operators such as
.code let
also arrange for the evaluation
of a body of forms, the value of the last of which is returned.
These operators are said to feature an implicit
.codn progn .

.coNP Operator @ cond
.synb
.mets (cond >> {( test << form *)}*)
.syne
.desc
The
.code cond
operator provides a multi-branching conditional evaluation of
forms. Enclosed in the cond form are groups of forms expressed as lists.
Each group must be a list of at least one form.

The forms are processed from left to right as follows: the first form,
.metn test ,
in each group is evaluated. If it evaluates true, then the remaining
forms in that group, if any, are also evaluated. Processing then terminates and
the result of the last form in the group is taken as the result of cond.
If
.meta test
is the only form in the group, then result of
.meta test
is taken
as the result of
.codn cond .

If the first form of a group yields
.codn nil ,
then processing continues with the
next group, if any. If all form groups yield
.codn nil ,
then the cond form yields
.codn nil .
This holds in the case that the syntax is empty:
.code (cond)
yields
.codn nil .

.coNP Macros @, caseq @ caseql and @ casequal
.synb
.mets (caseq < test-form << normal-clause * <> [ else-clause ])
.mets (caseql < test-form << normal-clause * <> [ else-clause ])
.mets (caseqqual < test-form << normal-clause * <> [ else-clause ])
.syne
.desc
These three macros arrange for the evaluation of of
.metn test-form ,
whose value is then compared against the key or keys in each
.meta normal-clause
in turn.
When the value matches a key, then the remaining forms of
.meta normal-clause
are evaluated, and the value of the last form is returned; subsequent
clauses are not evaluated. When the value doesn't match any of the keys
of a
.meta normal-clause
then the next
.meta normal-clause
is tested.
If all these clauses are exhausted, and there is no
.metn else-clause ,
then the value nil is returned. Otherwise, the forms in the
.meta else-clause
are evaluated, and the value of the last one is returned.

The syntax of a
.meta normal-clause
takes on these two forms:

.cblk
.mets >> ( key << form *)
.cble

where
.meta key
may be an atom which denotes a single key, or else a list
of keys.  There is a restriction that the symbol
.code t
may not be used
as
.metn key .
The form
.code (t)
may be used as a key to match that symbol.

The syntax of an
.meta else-clause
is:

.cblk
.mets (t << form *)
.cble

which resembles a form that is often used as the final clause
in the
.code cond
syntax.

The three forms of the case construct differ from what type of
test they apply between the value of
.meta test-form
and the keys.
The
.code caseq
macro generates code which uses the
.code eq
function's
equality. The
.code caseql
macro uses
.codn eql ,
and
.code casequal
uses
.codn equal .

.TP* Example
.cblk
  (let ((command-symbol (casequal command-string
                          (("q" "quit") 'quit)
                          (("a" "add") 'add)
                          (("d" "del" "delete") 'delete)
                          (t 'unknown))))
    ...)
.cble

.coNP Macros @ when and @ unless
.synb
.mets (when < expression << form *)
.mets (unless < expression << form *)
.syne
.desc
The when macro operator evaluates
.metn expression .
If
.meta expression
yields
true, and there are additional forms, then each
.meta form
is evaluated.
The value of the last form is becomes the result value of the when form.
If there are no forms, then the result is
.codn nil .

The
.code unless
operator is similar to when, except that it reverses the
logic of the test. The forms, if any, are evaluated if, and only if
.meta expression
is false.

.coNP Macros @ while and @ until
.synb
.mets (while < expression << form *)
.mets (until < expression << form *)
.syne
.desc
The
.code while
macro operator provides a looping construct.  It evaluates
.metn expression .
If
.meta expression
yields
.codn nil ,
then the evaluation of the
.code while
form
terminates, producing the value
.codn nil .
Otherwise, if there are additional forms,
then each
.meta form
is evaluated.  Next, evaluation returns to
.metn expression ,
repeating all of the previous steps.

The
.code until
macro operator is similar to while, except that the until form
terminates when
.meta expression
evaluates true, rather than false.

These operators arrange for the evaluation of all their enclosed forms
in an anonymous block. Any of the
.metn form s,
or
.metn expression ,
may use
the
.code return
operator to terminate the loop, and optionally to specify
a result value for the form.

The only way these forms can yield a value other than
.code nil
is if the
.code return
operator is used to terminate the implicit anonymous block,
and is given an argument, which becomes the result value.

.coNP Operator/function @ if
.synb
.mets (if < cond < t-form <> [ e-form ])
.mets [if < cond < then <> [ else ]]
.syne
.desc
There exist both an
.code if
operator and an
.code if
function. A list form with the symbol
.code if
in the fist position is interpreted as an invocation of the
.code if
operator.
The function can be accessed using the DWIM bracket notation and in other
ways.

The
.code if
operator provides a simple two-way-selective evaluation control.
The
.meta cond
form is evaluated. If it yields true then
.meta t-form
is evaluated, and that form's return value becomes the return value of the
.codn if .
If
.meta cond
yields false, then
.meta e-form
is evaluated and its return value is taken to be that of
.codn if .
If
.meta e-form
is omitted, then the behavior is as if
.meta e-form
were specified as
.codn nil .

The
.code if
function provides no evaluation control. All of arguments
are evaluated from left to right. If the
.meta cond
argument is true, then it
returns the
.meta then
argument, otherwise it returns the value of the
.meta else
argument if present, otherwise it returns
.codn nil .

.coNP Operator/function @ and
.synb
.mets (and << form *)
.mets [and << arg *]
.syne
.desc
There exist both an
.code and
operator and an
.code and
function. A list form with the
symbol
.code and
in the fist position is interpreted as an invocation of the
operator.  The function can be accessed using the DWIM bracket notation and in
other ways.

The
.code and
operator provides three functionalities in one.  It computes the
logical "and" function over several forms.  It controls evaluation (a.k.a.
"short-circuiting").  It also provides an idiom for the convenient substitution
of a value in place of
.code nil
when some other values are all true.

The
.code and
operator evaluates as follows. First, a return value is
established and initialized to the value
.codn t .
The
.metn form s,
if any, are
evaluated from left to right.  The return value is overwritten with
the result of each form. Evaluation stops when all forms are exhausted,
or when
.code nil
is stored in the return value.
When evaluation stops, the operator yields the return value.

The
.code and
function provides no evaluation control; it receives all of its
arguments fully evaluated. If it is given no arguments, it returns
.codn t .
If it is given one or more arguments, and any of them are
.codn nil ,
it returns
.codn nil .
Otherwise it returns the value of the last argument.

.TP* Examples:
.cblk
  (and) -> t
  (and (> 10 5) (stringp "foo")) -> t
  (and 1 2 3) -> 3  ;; short-hand for (if (and 1 2) 3).
.cble

.coNP Operator/function @ or
.synb
.mets (or << form *)
.mets [or << arg *]
.syne
.desc
There exist both an
.code or
operator and an
.code or
function. A list form with the
symbol
.code or
in the fist position is interpreted as an invocation of the
operator.  The function can be accessed using the DWIM bracket notation and in
other ways.

The or operator provides three functionalities in one.  It computes the
logical "or" function over several forms.  It controls evaluation (a.k.a.
"short-circuiting").  The behavior of
.code or
also provides an idiom for the selection of the first non-nil value from a
sequence of forms.

The
.code or
operator evaluates as follows.  First, a return value is
established and initialized to the value
.codn nil .
The
.metn form s,
if any,
are evaluated from left to right. The return value is overwritten
with the result of each
.metn form .
Evaluation stops when all forms are
exhausted, or when a true value is stored into the return value.
When evaluation stops, the operator yields the return value.

The
.code or
function provides no evaluation control; it receives all of its
arguments fully evaluated. If it is given no arguments, it returns
.codn nil .
If all of its arguments are
.codn nil ,
it also returns
.codn nil .
Otherwise, it
returns the value of the first argument which isn't
.codn nil .

.TP* Examples:
.cblk
  (or) -> nil
  (or 1 2) -> 1
  (or nil 2) -> 2
  (or (> 10 20) (stringp "foo")) -> t
.cble

.coNP Operator @ unwind-protect
.synb
.mets (unwind-protect < protected-form << cleanup-form *)
.syne
.desc
The
.cod unwind-protect
operator evaluates
.meta protected-form
in such a way that no matter how the execution of
.meta protected-form
terminates, the
.metn cleanup-form s
will be executed.

The
.metn cleanup-form s,
however, are not protected. If a
.meta cleanup-form
terminates via
some non-local jump, the subsequent
.metn cleanup-form s
are not evaluated.

.metn cleanup-form s
themselves can "hijack" a non-local control transfer such
as an exception. If a
.meta cleanup-form
is evaluated during the processing of
a dynamic control transfer such as an exception, and that
.meta cleanup-form
initiates its own dynamic control transfer, the original control transfer
is aborted and replaced with the new one.

.TP* Example:
.cblk
  (block foo
    (unwind-protect
      (progn (return-from foo 42)
             (format t "not reached!\en"))
      (format t "cleanup!\en")))
.cble

In this example, the protected
.code progn
form terminates by returning from
block
.codn foo .
Therefore the form does not complete and so the
output
.str not reached!
is not produced. However, the cleanup form
executes, producing the output
.strn cleanup! .

.coNP Operator @ block
.synb
.mets (block < name << body-form *)
.syne
.desc
The
.code block
operator introduces a named block around the execution of
some forms. The
.meta name
argument must be a symbol. Since a block name is not
a variable binding, keyword symbols are permitted, and so are the symbols
.code t
and
.codn nil .
A block named by the symbol nil is slightly special: it is
understood to be an anonymous block.

Blocks in \*(TL have dynamic scope. This means that the following
situation is allowed:

.cblk
  (defun func () (return-from foo 42))
  (block foo (func))
.cble

The function can return from the
.code foo
block even though the
.code foo
block
does not lexically surround
.codn foo .

Thus blocks in \*(TL provide dynamic non-local returns, as well
as returns out of lexical nesting.

.TP* "Dialect Note:"
In Common Lisp, blocks are lexical. A separate mechanism consisting of
catch and throw operators performs non-local transfer based on symbols.
The \*(TL example:

.cblk
  (defun func () (return-from foo 42))
  (block foo (func))
.cble

is not allowed in Common Lisp, but can be transliterated to:

.cblk
  (defun func () (throw 'foo 42))
  (catch 'foo (func))
.cble

Note that foo is quoted in CL. This underscores the dynamic nature of
the construct.
.code throw
itself is a function and not an operator.

.coNP Operators @ return and @ return-from
.synb
.mets (return <> [ value ])
.mets (return-from < name <> [ value ])
.syne
.desc
The
.code return
operator must be dynamically enclosed within an anonymous
block (a block named by the symbol
.codn nil ).
It immediately terminates the
evaluation of the innermost anonymous block which encloses it, causing
it to return the specified value. If the value is omitted, the anonymous
block returns
.codn nil .

The
.code return-from
operator must be dynamically enclosed within a named block
whose name matches the
.meta name
argument. It immediately terminates the
evaluation of the innermost such block, causing it to return the specified
value. If the value is omitted, that block returns
.codn nil .

.TP* Example:
.cblk
    (block foo
      (let ((a "abc\en")
            (b "def\en"))
        (pprint a *stdout*)
        (return-from foo 42)
        (pprint b *stdout*)))
.cble

Here, the output produced is
.strn "abc" .
The value of
.code b
is not printed
because.
.code return-from
terminates block
.codn foo ,
and so the second pprint form is not evaluated.

.SS* Evaluation

.coNP Operator @ dwim
.synb
.mets (dwim << argument *)
.mets <> [ argument *]
.syne
.desc
The
.code dwim
operator's name is an acronym: DWIM may be taken to mean
"Do What I Mean", or alternatively, "Dispatch, in a Way that is
Intelligent and Meaningful".

The notation
.code [...]
is a shorthand equivalent to
.code (dwim ...)
and is usually the preferred way for writing
.code dwim
expressions.

The
.code dwim
operator takes a variable number of arguments, which are
all evaluated in the same way: the first argument is not evaluated differently
from the remaining arguments.

This means that the first argument isn't a function name, but an ordinary
expression which can simply compute a function object (or, more generally,
a callable object).

Furthermore, for those arguments of
.code dwim
which are symbols (after all
macro-expansion is performed on the arguments), the evaluation rules are
altered. For the purposes of resolving symbols to values, the function and
variable binding namespaces are considered to be merged into a single space,
creating a situation that is very similar to a Lisp-1 style dialect.

This special Lisp-1 evaluation is not recursively applied.  All arguments of
.code dwim
which, after macro expansion, are not symbols are evaluated using the
normal Lisp-2 evaluation rules. Thus, the DWIM operator must be used
in every expression where the Lisp-1 rules for reducing symbols to
values are desired.

After macro expansion, the first argument of
.code dwim
may not be an operator such
as
.codn let ,
or the name of a macro. Prior to macroexpansion, any argument of
.code dwim
may be a symbol macro.

If a symbol has bindings both in the variable and function namespace in scope,
and is referenced by a dwim argument, this constitutes a conflict which is
resolved according to two rules.  When nested scopes are concerned, then an
inner binding shadows an outer binding, regardless of their kind.  An inner
variable binding for a symbol shadows an outer or global function binding,
and vice versa.

If a symbol is bound to both a function and variable in the global namespace,
then the variable binding is favored.

Macros do not participate in the special scope conflation, with one
exception. What this means is that the space of symbol macros is not folded
together with the space of regular macros. An argument of
.code dwim
that is a symbol might be
symbol macro, variable or function, but it cannot be interpreted as the name of
a regular macro.

The exception is this: from the perspective of a
.code dwim
form, function bindings can shadow symbol macros. If a
function binding is defined in an inner scope relative to a symbol macro for
the same symbol,
using
.code flet
or
.codn labels ,
the function hides the symbol macro. In other words, when
macro expansion processes an argument of a
.code dwim
form, and that argument is a symbol, it is treated specially
in order to provide a consistent name lookup behavior. If the innermost
binding for that symbol is a function binding, it refers to that
function binding, even if a more outer symbol macro binding exists,
and so the symbol is not expanded using the symbol macro.
By contrast, in an ordinary form, a symbolic argument never resolves
to a function binding. The symbol refers to either a symbol macro or a
variable, whichever is nested closer.

How many arguments are required by the
.code dwim
operator depends on the type of
object to which the first argument expression evaluates.  The possibilities
are:
.RS
.meIP >> [ function << argument *]
Call the given function object to the given arguments.

.meIP >> [ symbol << argument *]
If the first expression evaluates to a symbol, that symbol
is resolved in the function namespace, and then
the resulting function, if found, is called with the
given arguments.

.meIP >> [ sequence << index ]
Retrieve an element from
.metn sequence ,
given by the integer
.metn index .

The following equivalences hold:

.cblk
  [seq index] <--> (ref seq index)

  (set [seq index] new) <--> (refset seq index new)
.cble

This form accepts update operators like
.code inc
and
.codn flip .

.meIP >> [ sequence << from-index..to-below-index ]
Retrieve the specified range of elements.
The range of elements is specified in the
.code car
and
.code cdr
fields of a cons cell,
for which the
.code ..
(dotdot) syntactic sugar is useful.
See the section on Range Indexing below.

The following equivalences hold:

.cblk
  [seq from..to] <--> (sub seq from to)

  (set [seq from..to] new) <--> (refset seq new from to)
.cble

This form does not accept update operators like
.code inc
and
.codn flip .

.meIP >> [ sequence << index-list ]
Elements specified
by
.meta index-list
are extracted from
.meta sequence
and returned as a sequence
of the same kind as
.metn sequence .
This form is equivalent to
.cblk
.meti (select < sequence << where-index )
.cble
except when the target of an assignment operation.

The following equivalences hold between index-list-based indexing
and the
.code select
and
.code replace
functions:

.cblk
  [seq idx-list] <--> (select seq idx-list)

  (set [seq idx-list] new) <--> (replace seq new idx-list)
.cble

This form does not accept update operators like
.code inc
and
.codn flip .

Note that unlike the select function, this does not support
.cblk
.meti >> [ hash << index-list ]
.cble
because since hash keys may be lists, that syntax is
indistinguishable from a simple hash lookup where
.meta index-list
is the key.

.meIP >> [ hash < key << default-value ]
Retrieve a value from the hash table corresponding to
.metn key ,
or
.meta default-value
if there is no such entry.

The first argument may not be an operator such as
.codn let ,
only a function.

The places denoted by the
.code dwim
operator can be assigned.  There are some
restrictions. List, string and vector ranges can only be replaced using
the set operator. The other operators like
.code push
do not apply.
Characters in a string can only be assigned with
.code set
or incremented with
.code inc
and dec.

The source of a range assignment can be a string, vector or list,
regardless of whether the target is a string, vector or list.
If the target is a string, the replacement sequence must be
a string, or a list or vector of characters.

.RE
.PP

.TP* "Range Indexing"
Vector and list range indexing is based from zero. The first element element
zero. Furthermore, the value
.code -1
refers to the last element of the vector or
list, and
.code -2
to the second last and so forth. So the range
.code 1 .. -2
means
"everything except for the first element and the last two".

The symbol
.code t
represents the position one past the end of the vector, string or
list, so
.code 0 .. t
denotes the entire list or vector, and the range
.code t .. t
represents the empty range just beyond the last element.
It is possible to assign to
.codn t .. t .
For instance:

.cblk
  (defvar list '(1 2 3))
  (set [list t .. t] '(4)) ;; list is now (1 2 3 4)
.cble

The value zero has a "floating" behavior when used as the end of a range.
If the start of the range is a negative value, and the end of the
range is zero, the zero is interpreted as being the position past the
end of the sequence, rather than the first element. For instance the range
.code -1..0
means the same thing as
.codn -1..t .
Zero at the start of a range
always means the first element, so that
.code 0..-1
refers to all the elements except for the last one.

.TP* Notes:
The dwim operator allows for a Lisp-1 flavor of programming in \*(TL,
which is principally a Lisp-2 dialect.

A Lisp-1 dialect is one in which an expression like
.code (a b)
treats both a and b
as expressions subject to the same evaluation rules\(emat least, when
.code a
isn't an operator or an operator macro. This means that the symbols
.code a
and
.code b
are resolved to values in the same namespace. The form denotes a function call
if the value of variable
.code a
is a function object.  Thus in a Lisp-1, named functions do not exist as
such: they are just variable bindings.  In a Lisp-1
.code (car 1 2)
means that there
is a variable called
.codn car ,
which holds a function. In a Lisp-2
.code (car 1 2)
means that there is a function called
.codn car ,
and so
.code (car car car)
is possible, because there can be also a variable called
.code car
which holds a cons cell object, rather than the
.code car
function.

The Lisp-1 approach is useful for functional programming, because it eliminates
cluttering occurrences of the call and fun operators.  For instance:

.cblk
  ;; regular notation

  (call foo (fun second) '((1 a) (2 b)))

  ;; [] notation

  [foo second '((1 a) (2 b))]
.cble

Lisp-1 dialects can also provide useful extensions by giving a meaning
to objects other than functions in the first position of a form,
and the
.code dwim/[...]
syntax does exactly this.

However, Lisp-2 also has its advantages, whereas Lisp-1 also has disadvantages.
Lisp-2 integrates with macros and operators more naturally than Lisp-1. Macros
come in two flavors: symbol macros and operator macros. This distinction
integrates better into a two namespace model, in which an operator macro is
recognized in the first position of a form, and a symbol macro in other
positions. Moreover, operators and operator macros are not functions; they
cannot work by reducing a symbol to a macro function under ordinary
evaluation rules. A Lisp-1 implementation must inspect the leftmost expression
to determine whether or not it is an operator, which breaks the principle
that all expressions are to be evaluated in the same way. Moreover, macros
and operators are never indirected upon. If
.code x
is an operator or macro, it never makes sense for
.code x
to occur other than in the leftmost position of a form. In Lisp-1 dialects,
the situation of using the name of an operator as a variable other than
in the first position, as in
.code (list let if)
has no meaning, indicating that the single namespace model serves no purpose
for macros and operators.

\*(TL banishes operators and macros (except symbol macros) from Lisp-1
evaluation, thereby achieving a more pure model of Lisp-1 evaluation than
actual Lisp-1 languages, while retaining all the advantages of Lisp-2.
In short, a "best of both worlds" situation is achieved in \*(TL.

.coNP Function @ identity
.synb
.mets (identity << value )
.syne
.desc
The
.code identity
function returns its argument.

.TP* Notes:
The
.code identity
function is useful as a functional argument, when a transformation
function is required, but no transformation is actually desired.

.coNP Function @ eval
.synb
.mets (eval < form <> [ env ])
.syne
.desc
The
.code eval
function treats the
.meta form
object as a Lisp expression, which is
evaluated. The side effects implied by the form are performed, and the value
which it produces is returned. The optional
.meta env
object specifies an environment for
resolving the function and variable references encountered in the expression.
If this argument is omitted
.code nil
then evaluation takes place in the global environment.

See also: the
.code make-env
function.

.coNP Function @ make-env
.synb
.mets (make-env >> [ variable-bindings >> [ function-bindings <> [ next-env ]]])
.syne
.desc
The
.code make-env
function creates an environment object suitable as the
.code env
parameter.

The
.meta variable-bindings
and
.meta function-bindings
parameters, if specified,
should be association lists, mapping symbols to objects.  The objects in
.meta function-bindings
should be functions, or objects callable as functions.

The
.meta next-env
argument, if specified, should be an environment.

Note: bindings can also be added to an environment using the
.code env-vbind
and
.code env-fbind
functions.

.coNP Functions @ env-vbind and @ env-fbind
.synb
.mets (env-vbind < env < symbol << value)
.mets (env-fbind < env < symbol << value)
.syne
.desc
These functions bind a symbol to a value in either the function or variable
space of environment
.codn env .

Values established in the function space should be functions or objects that
can be used as functions such as lists, strings, arrays or hashes.

If
.meta symbol
already exists in the environment, in the given space, then its
value is updated with
.codn value .

.coNP Function @ constantp
.synb
.mets (constantp < form >> [ env ])
.syne
.desc
The
.code constantp
function determines whether
.mode form
is a constant form, with respect to environment
.mode env .

If
.mode env
is absent, the global environment is used.
The
.mode env
argument is used for macro-expanding
.modn form .

Currently,
.code constantp
returns true for any form, which, after macro-expansion is a compound
form with the symbol
.code quote
in its first position, a non-symbolic atom, or one of the symbols
which evaluate to themselves and cannot be bound as variables.
These symbols are the keyword symbols, and the symbols
.code t
and
.codn nil .

In the future,
.code constantp
will be able to recognize more constant forms, such as calls to certain
functions whose arguments are constant forms.

.SS* Mutation

.coNP Operators @ inc @ dec @ set @ push @ pop @ flip and @ del
.synb
.mets (inc < place <> [ delta ])
.mets (dec < place <> [ delta ])
.mets (set < place << new-value )
.mets (push < item << place )
.mets (pop << place )
.mets (flip << place )
.mets (del << place )
.syne
.desc
These destructive operators update the value of a place. A place is a storage
location which is denoted by a form. Place forms are identical to value
accessing forms. That is to say, any form recognized as a place by these
operators can be evaluated by itself to retrieve the value of the storage
location. However, the converse is false: not all forms which access storage
location are recognized as places.

With are exceptions noted below, it is an error if a place does not exist.
For instance, a variable being assigned must exist.

Literal objects which are directly specified in the source code are
considered part of the program body. Modifying parts of these objects
therefore gives rise to self-modifying code. The behavior of self-modifying
code is not specified.

The
.code inc
and
.code dec
update the place by adding or subtracting, respectively, a
displacement to or from that number. If the
.meta delta
expression is
specified, then it is evaluated and its value is used as the increment.
Otherwise, a default increment of
.code 1
is used. The prior value of the place
and the delta must be suitable operands for the
.code +
and
.code -
functions.
.code (inc x)
is equivalent to
.codn (set x (+ 1 x)) ,
except that expression x
is evaluated only once to determine the storage location. The
.code inc
and
.code dec
operators return the new value that was stored.

The
.code set
operator overwrites the previous value of a place with a new value,
and also returns that value.

The
.code push
and
.code pop
operators operate on a place which holds a list.  The
.code push
operator updates the list by replacing it with a new list which has a new item
at the front, followed by the previous list. The item is returned.
The
.code pop
operator performs the reverse operation: it removes the first item
from the list and returns it.
.code (push y x)
is similar to

.cblk
  (let ((temp y)) (set x (cons temp x)) temp)
.cble

except that
.code x
is evaluated only once to determine the storage place, and no
such temporary variable is visible to the program.  Similarly,
.code (pop x)
is much like

.cblk
  (let ((temp (car x))) (set x (cdr x)) temp)
.cble

except that x is evaluated only once, and no such temporary variable
is visible to the program.

The
.code flip
operator toggles a place between true and false. If the place
contains a value other than
.codn nil ,
then its value is replaced with
.codn nil .
If it contains
.codn nil ,
it is replaced with
.codn t .

The
.code del
operator does not modify the value of a place, but rather deletes the
place itself. Index values and ranges of lists denoted using the
.code dwim
operator
indexing notation can be subject to a deletion, as can hash table entries
denoted using
.code dwim
or
.codn gethash .
It is an error to try to delete other kinds of
places such as simple variables. The
.code del
operator returns the value of the
place that was deleted. Deleting from a sequence means removing the element or
elements. Deleting a hash place means removing the corresponding entry from the
hash table.

Currently, these forms are recognized as places:

.meIP < symbol
A .meta symbol place denotes a variable. If the variable does not exist, it is
an error.

.meIP (car << cons )
.meIP (cdr << cons )
These places denote the corresponding slots
of a cons cell. The
.meta cons
form must be an expression which evaluates to a cons.

.meIP (gethash < hash < key << default-value )
The
.code gethash
place denotes a value stored in a hash table.
The form
.meta hash
must evaluate to a hash table. If the place does not exist
in the hash table under the given key, then the destructive operation
will create it. In that case, the
.meta default-value
form is evaluated to
determine the initial value of the place. Otherwise it is ignored.

.meIP (vecref < vector << index )
The
.code vecref
place denotes a vector element, allowing vector elements
to be treated as assignment places.

.meIP (dwim < obj ...)
.meIP >> [ obj  ...]  ;; equivalent
The
.code dwim/[]
place denotes a vector element, list element, string, or hash
table, depending on the type of
.metn obj .

.SS* Binding and Iteration
.coNP Operator @ defvar
.synb
.mets (defvar < sym << value )
.syne

.desc

The
.code defvar
operator binds a variable in the global environment.

If the variable named
.meta sym
already exists in the global environment, the
form has no effect; the
.meta value
form is not evaluated, and the value of the
variable is unchanged.

If the variable does not exist, then it is introduced, with a value given by
evaluating the
.meta value
form. The
.meta value
form is evaluated in the environment
in which the
.code defvar
form occurs, not necessarily in the global environment.

The symbols
.code t
and
.code nil
may not be used as variables, and neither
can be keyword symbols: symbols denoted by a leading colon.

In addition to creating a binding, the
.code defvar
operator also marks
.meta sym
as the name of a special variable. This changes what it means to bind
that symbol in a lexical binding construct such as the
.code let
operator, or a function parameter list. See the section "Special Variables" far
above.

.coNP Operators @ let and @ let*
.synb
.mets (let >> ({ sym | >> ( sym << init-form )}*) << body-form *)
.mets (let* >> ({ sym | >> ( sym << init-form )}*) << body-form *)
.syne
.desc
The.
.code let
and
.code let*
operators introduce a new scope with variables and
evaluate forms in that scope. The operator symbol, either
.code let
or
.codn let* ,
is followed by a list which can contain any mixture of variable
name symbols, or
.cblk
.meti >> ( sym << init-form )
.cble
pairs.  A symbol
denotes the name of variable to be instantiated and initialized
to the value
.codn nil .
A symbol specified with an init-form denotes
a variable which is initialized from the value of the
.metn init-form .

The symbols
.code t
and
.code nil
may not be used as variables, and neither
can be keyword symbols: symbols denoted by a leading colon.

The difference between
.code let
and
.code let*
is that in
.codn let* ,
later
.codn init-form s
have visibility over the variables established by earlier variables
in the same let* construct. In plain
.codn let ,
the variables are not visible to any of the
.metn init-form s.

When the variables are established, then the
.metn body-form s
are evaluated in order. The value of the last
.meta body-form
becomes the return value of the
.codn let .

If there are no
.metn body-form s,
then the return value
.code nil
is produced.

The list of variables may be empty.

.TP* Examples:
.cblk
  (let ((a 1) (b 2)) (list a b)) -> (1 2)
  (let* ((a 1) (b (+ a 1))) (list a b (+ a b))) -> (1 2 3)
  (let ()) -> nil
  (let (:a nil)) -> error, :a and nil can't be used as variables
.cble

.coNP Operators @ for and @ for*
.synb
.mets >> ({ for | for } >> ({ sym | >> ( sym << init-form )}*)
.mets \ \ \ \ \ \ \ \ \ \ \ \ \  << ([ test-form << result-form *])
.mets \ \ \ \ \ \ \ \ \ \ \ \ \  <> ( inc-form *)
.mets \ \  << body-form *)
.syne
.desc

The
.code for
and
.code for*
operators combine variable binding with loop iteration.
The first argument is a list of variables with optional initializers,
exactly the same as in the
.code let
and
.code let*
operators. Furthermore, the
difference between
.code for
and
.code for*
is like that between
.code let
and
.code let*
with regard to this list of variables.

The
.code for
and
.code for*
operators execute these steps:
.RS
.IP 1.
Establish bindings for the specified variables similarly to
.code let
and
.codn let* .
The variable bindings are visible over the
.metn test-form ,
each
.metn result-form ,
each
.meta inc-form
and each
.metn body-form .
.IP 2.
Establish an anonymous block over the remaining forms, allowing
the
.code return
operator to be used to terminate the loop.
.IP 3.
Evaluate
.metn test-form .
If
.meta test-form
yields
.codn nil ,
then the loop terminates.  Each
.meta result-form
is evaluated, and the value of the last of these
forms is is the result value of the loop.
If there are no
.metn result-form s
then the result value is
.codn nil .
If the
.meta test-form
is omitted, then the test
is taken to be true, and the loop does not terminate.
.IP 4.
Otherwise, if
.meta test-form
yields true, then each
.meta body-form
is evaluated in turn. Then, each
.code inc-form
is evaluated in turn and processing resumes at step 2.
.RE

.IP
Furthermore, the
.code for
and
.code for*
operators establish an anonymous block,
allowing the
.code return
operator to be used to terminate at any point.

.coNP Operators @, each @, each* @, collect-each @, collect-each* @ append-each and @ append-each*
.synb
.mets (each >> ({( sym << init-form )}*) << body-form *)
.mets (each* >> ({( sym << init-form )}*) << body-form *)
.mets (collect-each >> ({( sym << init-form )}*) << body-form *)
.mets (collect-each* >> ({( sym << init-form )}*) << body-form *)
.mets (append-each >> ({( sym << init-form )}*) << body-form *)
.mets (append-each* >> ({( sym << init-form )}*) << body-form *)
.syne
.desc
These operators establish a loop for iterating over the elements of one or more
lists. Each
.meta init-form
must evaluate to a list. The lists are then iterated in
parallel over repeated evaluations of the
.metn body-form s,
with each
.meta sym
variable being assigned to successive elements of its list. The shortest list
determines the number of iterations, so if any of the
.metn init-form s
evaluate to
an empty list, the body is not executed.

The body forms are enclosed in an anonymous block, allowing the
.code return
operator to terminate the loop prematurely and optionally specify
the return value.

The
.code collect-each
and
.code collect-each*
variants are like
.code each
and
.codn each* ,
except that for each iteration, the resulting value of the body is collected
into a list. When the iteration terminates, the return value of the
.code collect-each
or
.code collect-each*
operator is this collection.

The
.code append-each
and
.code append-each*
variants are like
.code each
and
.codn each* ,
except that for each iteration other than the last, the resulting value of the
body must be a list. The last iteration may produce either an atom or a list.
The objects produced by the iterations are combined together as if they
were arguments to the append function, and the resulting value is the
value of the
.code append-each
or
.code append-each*
operator.

The alternate forms denoted by the adorned symbols
.codn each* ,
.code collect-each*
and
.codn append-each* ,
differ from
.codn each ,
.code collect-each
and
.code append-each*
in the following way. The plain forms evaluate the
.metn init-form s
in an environment in which none of the
.code sym
variables are yet visible. By contrast, the alternate
forms evaluate each
.meta init-form
in an environment in which bindings for the
previous
.meta sym
variables are visible.  In this phase of evaluation,
.meta sym
variables are list-valued: one by one they are each bound to the list object
emanating from their corresponding
.metn init-form .
Just before the first loop
iteration, however, the
.meta sym
variables are assigned the first item from each
of their lists.

.TP* Example:
.cblk
 ;; print numbers from 1 to 10 and whether they are even or odd
 (each* ((n (range 1 10)) ;; n list a list here!
         (even (collect-each ((n m)) (evenp m))))
   ;; n is an item here!
   (format t "~s is ~s\en" n (if even "even" "odd")))
.cble
.TP* Output:
.cblk
 1 is odd
 2 is even
 3 is odd
 4 is even
 5 is odd
 6 is even
 7 is odd
 8 is even
 9 is odd
 10 is even
.cble

.SS* Function Objects and Named Functions

.coNP Operator @ defun
.synb
.mets (defun < name <> ( param * [: << opt-param *] [. << rest-param ])
.mets \ \  << body-form )
.syne
.desc
The
.code defun
operator introduces a new function in the global function namespace.
The function is similar to a lambda, and has the same parameter syntax
and semantics as the
.code lambda
operator.

Unlike in
.codn lambda ,
the
.metn body-form s
of a
.code defun
are surrounded by a block.
The name of this block is the same as the name of the function, making it
possible to terminate the function and return a value using
.cblk
.meti (return-from < name << value ).
.cble
For more information, see the definition of the block operator.

A function may call itself by name, allowing for recursion.

.coNP Operator @ lambda
.synb
.mets (lambda <> ( param * [: << opt-param *] [. << rest-param ])
.mets \ \  << body-form )
.mets (lambda < rest-param
.mets \ \  << body-form )
.syne
.desc
The
.code lambda
operator produces a value which is a function.  Like in most other
Lisps, functions are objects in \*(TL.  They can be passed to functions as
arguments, returned from functions, aggregated into lists, stored in variables,
et cetera.

The first argument of
.code lambda
is the list of parameters for the function.   It
may be empty, and it may also be an improper list (dot notation) where the
terminating atom is a symbol other than
.codn nil .
It can also be a single symbol.

The second and subsequent arguments are the forms making up the function body.
The body may be empty.

When a function is called, the parameters are instantiated as variables that
are visible to the body forms. The variables are initialized from the values of
the argument expressions appearing in the function call.

The dotted notation can be used to write a function that accepts
a variable number of arguments. There are two ways write a function that
accepts only a variable argument list and no required arguments:

.cblk
.mets (lambda (. << rest-param ) ...)
.mets (lambda < rest-param ...)
.cble

(These notations are syntactically equivalent because the list notation
.code (. X)
actually denotes the object
.code X
which isn't wrapped in any list).

The keyword symbol
.code :
(colon) can appear in the parameter list. This is
the symbol in the keyword package whose name is the empty string.  This
symbol is treated specially: it serves as a separator between
required parameters and optional parameters.  Furthermore, the
.code :
symbol has a role to play in function calls: it can be specified as an argument
value to an optional parameter by which the caller indicates that the
optional argument is not being specified. It will be processed exactly
that way.

An optional parameter can also be written in the form
.cblk
.meti >> ( name < expr <> [ sym ]).
.cble
In this situation, if the call does not specify a value for the parameter
(or specifies a value as the keyword
.code :
(colon)) then the parameter takes on the
value of the expression
.metn expr .
If
.meta sym
is specified, then
.meta sym
will be
introduced as an additional binding with a boolean value which indicates
whether or not the optional parameter had been specified by the caller.

The initializer expressions are evaluated an environment in which
all of the previous parameters are visible, in addition to the surrounding
environment of the lambda. For instance:

.cblk
  (let ((default 0))
    (lambda (str : (end (length str)) (counter default))
      (list str end counter)))
.cble

In this
.codn lambda ,
the initializing expression for the optional parameter
end is
.codn (length str) ,
and the
.code str
variable it refers to is the previous
argument. The initializer for the optional variable counter is
the expression default, and it refers to the binding established
by the surrounding let. This reference is captured as part of the
.codn lambda 's
lexical closure.

.TP* Examples:
.IP "Counting function:"
This function, which takes no arguments, captures the
variable
.codn counter .
Whenever this object is called, it increments
.code counter
by
.code 1
and returns the incremented value.

.cblk
  (let ((counter 0))
    (lambda () (inc counter)))
.cble

.IP "Function that takes two or more arguments:"
The third and subsequent arguments are aggregated into a list passed as the
single parameter
.codn z :

.cblk
  (lambda (x y . z) (list 'my-arguments-are x y z))
.cble

.IP "Variadic function:"

.cblk
  (lambda args (list 'my-list-of-arguments args))
.cble

.IP "Optional arguments:"

.cblk
  [(lambda (x : y) (list x y)) 1] -> (1 nil)
  [(lambda (x : y) (list x y)) 1 2] -> (1 2)
.cble

.coNP Function @ call

.synb
.mets (call < function << argument *)
.syne
.desc
The
.code call
function invokes
.metn function ,
passing it the given arguments, if any.

.TP* Examples:
Apply arguments
.code 1 2
to a
.code lambda
which adds them to produce
.codn 3 :

.cblk
  (call (lambda (a b) (+ a b)) 1 2)
.cble

Useless use of
.code call
on a named function; equivalent to
.codn (list 1 2) :

.cblk
  (call (fun list) 1 2)
.cble

.coNP Operator @ fun
.synb
.mets (fun << function-name )
.syne
.desc
The
.code fun
operator retrieves the function object corresponding to a named
function in the current lexical environment.

The
.meta function-name
is a symbol denoting a named function: a built in
function, or one defined by
.codn defun .

Note: the
.code fun
operator does not see macro bindings. It is possible to
retrieve a global macro expander using
.codn symbol-function .

.TP* "Dialect Note:"
A lambda expression is not a function name in \*(TL. The
syntax
.code (fun (lambda ...))
is invalid.

.coNP Functions @ symbol-function and @ symbol-value
.synb
.mets (symbol-function << symbol )
.mets (symbol-value << symbol )
.syne
.desc

The
.code symbol-function
function retrieves the value of the global function binding of the
given
.meta symbol
if it has one: that is, the function object bound to the
.metn symbol .
If
.meta symbol
has no global function binding, then the value of the global macro
binding is returned. If that doesn't exist, then the value of a global special
operator binding is returned, and if that doesn't exist, then
.code nil
is returned.

The
.code symbol-value
function retrieves the value of a global variable, if it exists,
otherwise
.codn nil .

Note: a function binding is a function, but a macro or special operator binding
binding isn't.  The value of a macro binding is a list of the following form:

.cblk
.mets (#<environment object> < macro-parameter-list << body-form *)
.cble

The value of a special operator binding is a "C pointer" object, whose
printed representation looks like:

.cblk
  #<cptr: 808be4f>
.cble

These details may change in future version of \*(TX.

.TP* "Dialect note:"
Forms which call
.code symbol-function
or
.code symbol-value
are currently not assignable places. Only the
.code defun
operator defines functions.

.coNP Functions @ boundp and @ fboundp
.synb
.mets (boundp << symbol )
.mets (fboundp << symbol )
.syne
.desc
.code boundp
returns
.code t
if the symbol has a variable binding in the global
environment, otherwise
.codn nil .

.code fboundp
returns
.code t
if the symbol has a function or macro binding in the global
environment, or if it is an operator, otherwise
.codn nil .

.coNP Function @ func-get-form
.synb
.mets (func-get-form << func )
.syne
.desc

The
.code func-get-form
function retrieves a source code form of
.metn func ,
which must be an interpreted function. The source code form has the syntax
.cblk
.meti >> ( name < arglist << body-form *) .
.cble

.coNP Function @ func-get-env
.synb
.mets (func-get-env << func )
.syne
.desc
The
.code func-get-env
function retrieves the environment object associated with
function
.metn func .
The environment object holds the captured bindings of a
lexical closure.

.coNP Function @ functionp
.synb
.mets (functionp << obj )
.syne
.desc

The
.code functionp
function returns
.code t
if
.meta obj
is a function, otherwise it returns
.codn nil .

.coNP Function @ interp-fun-p
.synb
.mets (interp-fun-p << obj )
.syne
.desc

The
.code interp-fun-p
function returns
.code t
if
.meta obj
is an interpreted function, otherwise it returns
.codn nil .

.coNP Macros @ flet and @ labels
.synb
.mets (flet >> ({( name < param-list << function-body-form *)}*)
.mets \ \  << body-form *)

.mets (labels >> ({( name < param-list << function-body-form *)}*)
.mets \ \  << body-form *)
.syne
.desc
The
.code flet
and
.code labels
macros bind local, named functions in the lexical scope.
The difference between
.code flet
and
.code labels
is that a function defined by
.code labels
can see itself, and therefore recurse directly by name. Moreover, if multiple
functions are defined by the same labels construct, they all see each other.
By contrast, a
.codn flet -defined
function does not have itself in scope and cannot recurse.
Multiple functions in the same
.code flet
do not have each other's names in their scopes.

More formally, the
.metn function-body-form -s
and
.meta param-list
of the functions defined by
.code labels
are in a scope in which all of the function
names being defined by that same
.code labels
construct are visible.

Under both
.code labels
and
.codn flet ,
the local functions that are defined are
lexically visible to the main
.metn xbody-form .

Note that
.code labels
and
.code flet
are properly scoped with regard to macros.
During macro expansion, function bindings introduced by these
macro operators shadow macros defined by
.code macrolet
and
.codn defmacro .

Furthermore, function bindings introduced by
.code labels
and
.code flet
also shadow symbol macros defined by
.codn symacrolet ,
when those symbol macros occur as arguments of a
.code dwim
form.

See also: the
.code macrolet
operator.

.TP* Examples:
.cblk
  ;; Wastefully slow algorithm for determining even-ness.
  ;; Note:
  ;; - mutual recursion between labels-defined functions
  ;; - inner is-even bound by labels shadows the outer
  ;;   one bound by defun so the (is-even n) call goes
  ;;   to the local function.

  (defun is-even (n)
   (labels ((is-even (n)
              (if (zerop n) t (is-odd (- n 1))))
            (is-odd (n)
              (if (zerop n) nil (is-even (- n 1)))))
     (is-even n)))
.cblk

.SS* Object Type And Equivalence
.coNP Function @ typeof
.synb
.mets (typeof << value )
.syne
.desc
The
.code typeof
function returns a symbol representing the type of
.metn value .

The core types are identified by the following symbols:

.coIP cons
Cons cell.

.coIP str
String.

.coIP lit
Literal string embedded in the \*(TX executable image.

.coIP chr
Character.

.coIP fixnum
Fixnum integer: an integer that fits into the value word, not having to
be heap allocated.

.coIP sym
Symbol.

.coIP pkg
Symbol package.

.coIP fun
Function.

.coIP vec
Vector.

.coIP lcons
Lazy cons.

.coIP lstr
Lazy string.

.coIP env
Function/variable binding environment.

.coIP bignum
A bignum integer: arbitrary precision integer that is heap-allocated.
.PP

There are additional kinds of objects, such as streams.

.coNP Functions @, null @, not and @ false
.synb
.mets (null << value )
.mets (not << value )
.mets (false << value )
.syne
.desc

The
.codn null ,
.code not
and
.code false
functions are synonyms.  They tests whether
.meta value
is
the object
.codn nil .
They return
.code t
if this is the case,
.code nil
otherwise.

.TP* Examples:
.cblk
  (null '()) -> t
  (null nil) -> t
  (null ()) -> t
  (false t) -> nil

  (if (null x) (format t "x is nil!"))

  (let ((list '(b c d)))
    (if (not (memq 'a list))
      (format t "list ~s does not contain the symbol a\en")))
.cble

.coNP Function @ true
.synb
.mets (true << value )
.syne
.desc

The
.code true
function is the complement of the
.codn null ,
.code not
and
.code false
functions.

It return
.code t
if the
.meta value
is any object other than
.codn nil .
If
.meta value
is
.codn nil ,
it returns
.codn nil .

Note: programs should avoid explicitly testing values with true.
For instance
.code (if x ...)
should be favored over
.codn (if (true x) ...) .

.TP* Example:
.cblk
   ;; Compute indices where the list '(1 nil 2 nil 3)
   ;; has true values:
   [where '(1 nil 2 nil 3) true] -> (1 3)
.cble

.coNP Functions @, eq @ eql and @ equal
.synb
.mets (eq < left-obj << right-obj )
.mets (eql < left-obj << right-obj )
.mets (equal < left-obj << right-obj )
.syne
.desc
The principal equality test functions
.codn eq ,
.code eql
and
.code equal
test whether two objects are equivalent, using different criteria. They return
.code t
if the objects are equivalent, and
.code nil
otherwise.

The
.code eq
function uses the strictest equivalence test, called implementation
equality.  The eq function returns
.code t
if, and only if,
.meta left-obj
and
.meta right-obj
are actually the same object. The
.code eq
test is is implemented
by comparing the raw bit pattern of the value, whether or not it is
an immediate value or a pointer to a heaped object.
Two character values are
.code eq
if they are the same character, and two fixnum integers
are
.code eq
if they have the same value.  All other object representations are actually
pointers, and are
.code eq
if, and only, if they point to the same object in memory.
So, for instance, two bignum integers might not be
.code eq
even if they have the same numeric
value, two lists might not be
.code eq
even if all their corresponding elements are
.code eq
and two strings might not be eq even if they hold identical text.

The.
.code eql
function is slightly less strict than
.codn eq .
The difference between
.code eql
and
.code eq
is that if
.meta left-obj
and
.meta right-obj
are numbers which are of the same kind and have the same numeric value,
.code eql
returns
.metn t ,
even if they are different objects.
Note that an integers and a floating-point number are not
.code eql
even if one has a value which converts to the other: thus,
.code (eql 0.0 0)
yields
.codn nil ;
the comparison operation which finds these numbers equal is the
.codn (= 0.0 0) .
For all other object types,
.code eql
behaves like
.codn eq .

The
.code equal
function is less strict still than
.codn eql .
In general, it recurses into some
kinds of aggregate objects to perform a structural equivalence check.

Firstly, if
.meta left-obj
and
.meta right-obj
are
.code eql
then they are also
.codn equal ,
though of course the converse isn't necessarily the case.

If two objects are both cons cells, then they are equal if their
.code car
fields are
.code equal
and their
.code cdr
fields are
.codn equal .

If two objects are vectors, they are
.code equal
if they have the same length, and
their corresponding elements are
.codn equal .

If two objects are strings, they are equal if they are textually identical.

If two objects are functions, they are
.code equal
if they have
.code equal
environments,
and if they have
the same code.  Two compiled functions are considered to have
the same code if and only if they are pointers to the same function.
Two interpreted functions are considered to have the same
code if their list
structure is
.codn equal .

Two hashes are
.code equal
if they use the same equality (both are
.codn :equal-based ,
or both are the default
.codn :eql-based ),
if their associated user data elements are equal (see the function
.codn get-hash-userdata ),
if their sets of keys are identical, and if the data items associated with
corresponding keys from each respective hash are
.code equal
objects.

For some aggregate objects, there is no special semantics.  Two arguments
which are symbols, packages, or streams are
.code equal
if and only if they are the same object.

Certain object types have a custom
.code equal
function.

.SS* Basic List Library
.coNP Function @ cons
.synb
.mets (cons < car-value << cdr-value )
.syne
.desc
The
.code cons
function allocates, initializes and returns a single cons cell.
A cons cell has two fields called
.code car
and
.codn cdr ,
which are accessed by
functions of the same name, or by the functions
.code first
and
.codn rest ,
which are synonyms for these.

Lists are made up of conses. A (proper) list is either the symbol
.code nil
denoting an empty list, or a cons cell which holds the first item of
the list in its
.codn car ,
and the list of the remaining items in
.codn cdr .
The expression
.code (cons 1 nil)
allocates and returns a single cons cell which denotes the one-element
list
.codn (1) .
The
.code cdr
is
.codn nil ,
so there are no additional items.

A cons cell whose
.code cdr
is an atom other than
.code nil
is printed with the dotted
pair notation. For example the cell produced by
.code (cons 1 2)
is denoted
.codn (1 . 2) .
The notation
.code (1 . nil)
is perfectly valid as input, but the cell which it denotes
will print back as
.codn (1) .
The notations are equivalent.

The dotted pair notation can be used regardless of what type
of object is the cons cell's
.codn cdr .
so that for instance
.code (a . (b c))
denotes the cons cell whose
.code car
is the symbol a
.code a
and whose
.code cdr
is the list
.codn (b c) .
This is eactly the same thing as
.codn (a b c) .
In other words
.code (a b ... l m . (n o ... w . (x y z)))
is exactly the same as
.codn (a b ... l m n o ... w x y z).

Every list, and more generally cons cell tree structure, can be written
in a "fully dotted" notation, such that there are as many dots as there
are cells. For instance the cons structure of the nested list
.code (1 (2) (3 4 (5)))
can be made more explicit using
.codn (1 . ((2 . nil) . ((3 . (4 . ((5 . nil) . nil))) . nil)))) .
The structure contains eight conses, and so there are eight dots
in the fully dotted notation.

The number of conses in a linear list like
.code (1 2 3)
is simply the number of items, so that list in particular is
made of three conses. Additional nestings require additional conses,
so for instance
.code (1 2 (3))
requires four conses. A visual way to count the conses from the printed
representation is to count the atoms, then add the count of open parentheses,
and finally subtract one.

A list terminated by an atom other than
.code nil
is called an improper
list, and the dot notation is extended to cover improper lists.
For instance
.code (1 2 . 3)
is an improper list of two elements,
terminated by
.codn 3 ,
and can be constructed using
.codn (cons 1 (cons 2 3)) .
The fully dotted notation for this list is
.codn (1 . (2 . 3)) .

.coNP Function @ atom
.synb
.mets (atom << value )
.syne
.desc
The
.code atom
function tests whether
.meta value
is an atom. It returns
.code t
if this is the
case,
.code nil
otherwise.  All values which are not cons cells are atoms.

.code (atom x)
is equivalent to
.codn (not (consp x)) .

.TP* Examples:
.cblk
  (atom 3) -> t
  (atom (cons 1 2)) -> nil
  (atom "abc") -> t
  (atom '(3)) -> nil
.cble

.coNP Function @ consp
.synb
.mets (consp << value )
.syne
.desc
The
.code consp
function tests whether
.meta value
is a cons.  It returns
.code t
if this is the
case,
.code nil
otherwise.

.code (consp x)
is equivalent to
.codn (not (atom x)) .

Non-empty lists test positive under
.code consp
because a list is represented as a reference to the first cons in a chain of
one or more conses.

Note that a lazy cons is a cons and satisfies the
.code consp
test.  See the function
.codn make-lazy-cons .

.TP* Examples:
.cblk
  (consp 3) -> nil
  (consp (cons 1 2)) -> t
  (consp "abc") -> nil
  (consp '(3)) -> t
.cble

.coNP Functions @ car and @ first
.synb
.mets (car << object )
.mets (first << object )
.syne
.desc
The functions
.code car
and
.code first
are synonyms.

If
.meta obejct
is a cons cell, these functions retrieve the
.code car
field of that cons cell.
.code (car (cons 1 2))
yields
.codn 1 .

For programming convenience,
.meta object
may be of several other kinds in addition to conses.

.code (car nil)
is allowed, and returns
.codn nil .

.meta object
may also be a vector or a string. If it is an empty vector or
string, then
.code nil
is returned. Otherwise the first character of the string or
first element of the vector is returned.

.coNP Functions @ cdr and @ rest
.synb
.mets (cdr << object )
.mets (rest << object )
.syne
.desc
The functions
.code cdr
and
.code rest
are synonyms.

If
.meta obejct
is a cons cell, these functions retrieve the
.code cdr
field of that cons cell.
.code (cdr (cons 1 2))
yields
.codn 2 .

For programming convenience,
.meta object
may be of several other kinds in addition to conses.

.code (cdr nil)
is allowed, and returns
.codn nil .

.meta object
may also be a vector or a string. If it is a non-empty string or vector
containing at least two items, then the remaining part of the object is
returned, with the first element removed. For example
.code (cdr "abc")
yields
.codn "bc" .
If
.meta object
is is a one-element vector or string, or an empty vector or string,
then
.code nil
is returned. Thus
.code (cdr "a")
and
.code (cdr "")
both result in
.codn nil .

.TP*
Example:

Walk every element of the list
.code (1 2 3)
using a
.code for
loop:

.cblk
    (for ((i '(1 2 3))) (i) ((set i (cdr i)))
      (print (car i) *stdout*)
      (print #\enewline *stdout*))
.cble

The variable
.code i
marches over the cons cells which make up the "backbone"
of the list. The elements are retrieved using the
.code car
function.
Advancing to the next cell is achieved using
.codn (cdr i) .
If
.code i
is the
last cell in a (proper) list,
.code (cdr i)
yields
.code nil
and so
.code i
becomes
.codn nil ,
the loop guard expression
.code i
fails and the loop terminates.

.coNP Functions @ rplaca and @ rplacd
.synb
.mets (rplaca < cons << new-car-value )
.mets (rplacd < cons << new-cdr-value )
.syne
.desc
The
.code rplaca
and
.code rplacd
functions assign new values into the
.code car
and
.code cdr
fields of the cell
.metn cons .
Note that
.code (rplaca x y)
is the same as the more generic
.codn (set (car x) y) ,
and likewise
.code (rplacd x y)
can be written as
.codn (set (cdr x) y) .

It is an error if
.meta cons
is not a cons or lazy cons. In particular,
whereas
.code (car nil)
is correct,
.code (rplaca nil ...)
is erroneous.

The
.code rplaca
and
.code rplacd
functions return
.metn cons .

Note: \*(TX versions 89 and earlier, these functions returned the new value.
The behavior was undocumented.

.coNP Functions @, second @, third @, fourth @ fifth and @ sixth
.synb
.mets (first << list )
.mets (second << list )
.mets (third << list )
.mets (fourth << list )
.mets (fifth << list )
.mets (sixth << list )
.syne
.desc
These functions access the elements of a proper list by position.

If the list is shorter than implied, these functions return
.codn nil .

.TP* Examples:
.cblk
  (third '(1 2)) -> nil
  (second '(1 2)) -> 2
  (third '(1 2 . 3)) -> **error**
.cble

.coNP Functions @, append @ nconc and @ append*
.synb
.mets (append <> [ list * << last-arg ])
.mets (nconc <> [ list * << last-arg ])
.mets (append* <> [ list * << last-arg ])
.desc
The
.code append
function creates a new list which is a catenation of the
.meta list
arguments. All arguments are optional;
.code (append)
produces the empty list.

If a single argument is specified, then
.code append
simply returns the value of that
argument. It may be any kind of object.

If N arguments are specified, where N > 1, then the first N-1 arguments must be
proper lists. Copies of these lists are catenated together. The last argument
N, shown in the above syntax as
.metn last-arg ,
may be any kind of object. It is
installed into the
.code cdr
field of the last cons cell of the resulting list.
Thus, if argument N is also a list, it is catenated onto the resulting list,
but without being copied. Argument N may be an atom other than
.codn nil ;
in that case
.code append
produces an improper list.

The
.code nconc
function works like
.codn append ,
but avoids consing. It destructively
manipulates (that is to say, mutates) incoming lists to catenate them, and so
must be used with care.

The
.code append*
function works like
.codn append ,
but returns a lazy list which produces
the catenation of the lists on demand.  If some of the arguments are
themselves lazy lists which are infinite, then
.code append*
can return immediately,
whereas append will get caught in an infinite loop trying to produce a
catenation and eventually exhaust available memory. (However, the last
argument to append may be an infinite lazy list, because append does not
traverse the last argument.)

.TP* Examples:
.cblk
  ;; An atom is returned.
  (append 3) -> 3

  ;; A list is also just returned: no copying takes place.
  ;; The eq function can verify that the same object emerges
  ;; from append that went in.
  (let ((list '(1 2 3)))
    (eq (append list) list)) -> t

  (append '(1 2 3) '(4 5 6) 7) -> '(1 2 3 4 5 6 . 7))

  ;; the (4 5 6) tail of the resulting list is the original
  ;; (4 5 6) object, shared with that list.

  (append '(1 2 3) '(4 5 6)) -> '(1 2 3 4 5 6)

  (append nil) -> nil

  ;; (1 2 3) is copied: it is not the last argument
  (append '(1 2 3) nil) -> (1 2 3)

  ;; empty lists disappear
  (append nil '(1 2 3) nil '(4 5 6)) -> (1 2 3 4 5 6)
  (append nil nil nil) -> nil

  ;; atoms and improper lists other than in the last position
  ;; are erroneous
  (append '(a . b) 3 '(1 2 3)) -> **error**
.cble

.coNP Function @ list
.synb
.mets (list << value *)
.syne
.desc
The
.code list
function creates a new list, whose elements are the
argument values.

.TP* Examples:
.cblk
  (list) -> nil
  (list 1) -> (1)
  (list 'a 'b) -> (a b)
.cble

.coNP Function @ list*
.synb
.mets >> ( list* << value *)
.syne
.desc
The
.code list*
function is a generalization of cons. If called with exactly
two arguments, it behaves exactly like cons:
.code (list* x y)
is identical to
.codn (cons x y) .
If three or more arguments are specified,
the leading arguments specify additional atoms to be consed to the
front of the list. So for instance
.code (list* 1 2 3)
is the same as
.code (cons 1 (cons 2 3))
and produces the improper list
.codn (1 2 . 3) .
Generalizing in the other direction,
.code list*
can be called with just
one argument, in which case it returns that argument, and
can also be called with no arguments in which case it returns
.codn nil .

.TP* Examples:
.cblk
  (list*) -> nil
  (list* 1) -> 1
  (list* 'a 'b) -> (a . b)
  (list* 'a 'b 'c) -> (a b . c)
.cble

.TP* "Dialect Note:"
Note that unlike in some other Lisp dialects, the effect
of
.code (list* 1 2 x)
can also be obtained using
.codn (list 1 2 . x) .
However,
.code (list* 1 2 (func 3))
cannot be rewritten as
.code (list 1 2 . (func 3))
because the latter is equivalent to
.codn (list 1 2 func 3) .

.coNP Function @ sub-list
.synb
.mets (sub-list < list >> [ from <> [ to ]])
.syne
.desc
This function is like the
.code sub
function, except that it operates
strictly on lists.

For a description of the arguments and semantics, refer to the
.code sub
function.

.coNP Function @ replace-list
.synb
.mets (replace-list < list < item-sequence >> [ from <> [ to ]])
.syne
.desc
The
.code replace-list
function is like the replace function, except that the first
argument must be a list.

For a description of the arguments and semantics, refer to the
.code replace
function.

.coNP Functions @ listp and @ proper-listp
.synb
.mets (listp << value )
.mets (proper-listp << value )
.syne
.desc
The
.code listp
and
.code proper-listp
functions test, respectively, whether
.meta value
is a list, or a proper list, and return
.code t
or
.code nil
accordingly.

The
.code listp
test is weaker, and executes without having to traverse
the object.
.code (listp x)
is equivalent to
.codn (or (null x) (consp x)) .
The empty list
.code nil
is a list, and a cons cell is a list.

The
.code proper-listp
function returns
.code t
only for proper lists.  A proper list is
either
.codn nil ,
or a cons whose
.code cdr
is a proper list.
.code proper-listp
traverses the
list, and its execution will not terminate if the list is circular.

.coNP Function @ length-list
.synb
.mets (length-list << list )
.syne
.desc
The
.code length-list
function returns the length of
.metn list ,
which may be
a proper or improper list. The length of a list is the number of conses in that
list.

.coNP Function @ copy-list
.synb
.mets (copy-list << list )
.syne
.desc
The
.code copy-list
function which returns a list similar to
.metn list ,
but with
a newly allocated cons cell structure.

If
.meta list
is an atom, it is simply returned.

Otherwise,
.meta list
is a cons cell, and
.code copy-list
returns the same object as the expression
.cblk
.meti (cons (car << list ) (copy-list (cdr << list ))).
.cble

Note that the object
.cblk
.meti (car << list )
.cble
is not deeply copied, but only
propagated by reference into the new list.
.code copy-list
produces
a new list structure out of the same items that are in
.metn list .

.TP* "Dialect Note:"
Common Lisp does not allow the argument to be an atom, except
for the empty list
.codn nil .

.coNP Function @ copy-cons
.synb
.mets (copy-cons << cons )
.syne
.desc
This function creates a fresh cons cell, whose
.code car
and
.code cdr
fields are copied from
.codn cons .

.coNP Functions @ reverse and @ nreverse
.synb
.mets (reverse << list )
.mets (nreverse << list )
.syne
.desc
Description:

The functions
.code reverse
and
.code nreverse
produce an object which contains
the same items as proper list
.metn list ,
but in reverse order.
If
.meta list
is
.codn nil ,
then both functions return
.codn nil .

The
.code reverse
function is non-destructive: it creates a new list.

The
.code nreverse
function creates the structure of the reversed list out of the
cons cells of the input list, thereby destructively altering it (if it contains
more than one element). How
.code nreverse
uses the material from the original list
is unspecified. It may rearrange the cons cells into a reverse order, or it may
keep the structure intact, but transfer the
.code car
values among cons cells into
reverse order.  Other approaches are possible.

.coNP Function @ ldiff
.synb
.mets (ldiff < list << sublist )
.syne
.desc
The values
.meta list
and
.meta sublist
are proper lists.

The
.code ldiff
function determines whether
.meta sublist
is a structural suffix of
.meta list
(meaning that it actually is a suffix, and is not merely equal to one).

This is true if
.meta list
and
.meta sublist
are the same object, or else,
recursively, if
.meta sublist
is a suffix of
.cblk
.meti (cdr << list ).
.cble

The object
.code nil
is the sublist of every list, including itself.

The ldiff function returns a new list consisting of the elements of
the prefix of
.meta list
which come before the
.meta sublist
suffix. The elements
are in the same order as in
.metn list .
If
.meta sublist
is not a suffix of
.metn list ,
then a copy of
.meta list
is returned.

These functions also work more generally on sequences.
The
.meta list
and
.meta sublist
arguments may be strings or vectors. In this case, the suffixing
matching behavior is relaxed to one of structural equivalence.
See the relevant examples below.

.TP* Examples:
.cblk
  ;;; unspecified: the compiler could make
  ;;; '(2 3) a suffix of '(1 2 3),
  ;;; or they could be separate objects.
  (ldiff '(1 2 3) '(2 3)) -> either (1) or (1 2 3)

  ;; b is the (1 2) suffix of a, so the ldiff is (1)
  (let ((a '(1 2 3)) (b (cdr a)))
    (ldiff a b))
  -> (1)

  ;; string and vector behavior
  (ldiff "abc" "bc") -> "a"

  (ldiff "abc" nil) -> "abc"

  (ldiff #(1 2 3) #(3)) -> #(1 2)

  ;; mixtures do not have above behavior
  (ldiff #(1 2 3) '(3)) -> #(1 2 3)

  (ldiff '(1 2 3) #(3)) -> #(1 2 3)

  (ldiff "abc" #(#\eb #\ec)) -> "abc"
.cble

.coNP Function @ last
.synb
.mets (last << list )
.syne
.desc
If
.meta list
is a nonempty proper or improper list, the
.code last
function
returns the last cons cell in the list: that cons cell whose
.code cdr
field is a terminating atom.

If
.meta list
is
.codn nil ,
then
.code nil
is returned.

.coNP Functions @ flatten and @ flatten*
.synb
.mets (flatten << list )
.mets (flatten* << list )
.syne
.desc
The
.code flatten
function produces a list whose elements are all of the
.cod2 non- nil
atoms contained in the structure of
.metn list .
The
.code flatten*
function
works like
.code flatten
except that
.code flatten
creates and returns a complete
flattened list, whereas
.code flatten*
produces a lazy list which is
instantiated on demand. This is particularly useful when the input
structure is itself lazy.

.TP* Examples:
.cblk
  (flatten '(1 2 () (3 4))) -> (1 2 3 4)

  ;; equivalent to previous, since
  ;; nil is the same thing as ()
  (flatten '(1 2 nil (3 4))) -> (1 2 3 4)

  (flatten nil) -> nil

  (flatten '(((()) ()))) -> nil
.cble

.coNP Functions @, memq @ memql and @ memqual
.synb
.mets (memq < object << list )
.mets (memql < object << list )
.mets (memqual < object << list )
.syne
.desc

The
.codn memq ,
.code memql
and
.code memqual
functions search
.meta list
for a member
which is, respectively,
.codn eq ,
.code eql
or
.coe equal
to
.metn object .
(See the
.codn eq ,
.code eql
and
.code equal
functions above.)

If no such element found,
.code nil
is returned.

Otherwise, that suffix of
.meta list
is returned whose first element
is the matching object.

.coNP Functions @ member and @ member-if
.synb
.mets (member < key < sequence >> [ testfun <> [ keyfun ]])
.mets (member-if < predfun < sequence <> [ keyfun ])
.syne
.desc

The
.code member
and
.code member-if
functions search through
.meta sequence
for an item which
matches a key, or satisfies a predicate function, respectively.

The
.meta keyfun
argument specifies a function which is applied to the elements
of the sequence to produce the comparison key. If this argument is omitted,
then the untransformed elements of the sequence themselves are examined.

The
.code member
function's
.meta testfun
argument specifies the test function which is
used to compare the comparison keys taken from the sequence to the search key.
If this argument is omitted, then the
.code equal
function is used.
If
.code member
does not find a matching element, it returns
.codn nil .
Otherwise it
returns the suffix of
.meta sequence
which begins with the matching element.

The
.code member-if
function's
.meta predfun
argument specifies a predicate function
which is applied to the successive comparison keys pulled from the sequence
by applying the key function to successive elements.  If no match is found,
then
.code nil
is returned, otherwise what is returned is the suffix of
.meta sequence
which begins with the matching element.

.coNP Functions @, remq @ remql and @ remqual
.synb
.mets (remq < object << list )
.mets (remql < object << list )
.mets (remqual < object << list )
.syne
.desc
The
.codn remq ,
.code remql
and
.code remqual
functions produce a new list based on
.metn list ,
removing the items which are
.codn eq ,
.code eql
or
.code equal
to
.metn object .

The input
.meta list
is unmodified, but the returned list may share substructure
with it. If no items are removed, it is possible that the return value
is
.meta list
itself.

.coNP Functions @, remq @ remql* and @ remqual*
.synb
.mets (remq* < object << list )
.mets (remql* < object << list )
.mets (remqual* < object << list )
.syne
.desc
The
.codn remq* ,
.code remql*
and
.code remqual*
functions are lazy versions of
.codn remq ,
.code remql
and
.codn remqual .
Rather than computing the entire new list
prior to returning, these functions return a lazy list.

Caution: these functions can still get into infinite looping behavior.
For instance, in
.codn (remql* 0 (repeat '(0))) ,
.code remql
will keep consuming
the
.code 0
values coming out of the infinite list, looking for the first item that
does not have to be deleted, in order to instantiate the first lazy value.

.TP* Examples:
.cblk
  ;; Return a list of all the natural numbers, excluding 13,
  ;; then take the first 100 of these.
  ;; If remql is used, it will loop until memory is exhausted,
  ;; because (range 1) is an infinite list.

  [(remql* 13 (range 1)) 0..100]
.cble

.coNP Functions @, countqual @ countql and @ countq
.synb
.mets (countq < object << list )
.mets (countql < object << list )
.mets (countqual < object << list )
.syne
.desc
The
.codn countq ,
.code countql
and
.code countqual
functions count the number of objects
in
.meta list
which are
.codn eq ,
.code eql
or
.code equal
to
.metn object ,
and return the count.

.SS* Applicative List Processing
.coNP Functions @, remove-if @, keep-if @ remove-if* and @ keep-if*
.synb
.mets (remove-if < predicate-function < list <> [ key-function ])
.mets (keep-if < predicate-function < list <> [ key-function ])
.mets (remove-if* < predicate-function < list <> [ key-function ])
.mets (keep-if* < predicate-function < list <> [ key-function ])
.syne
.desc

The
.code remove-if
function produces a list whose contents are those of
.meta list
but with those elements removed which satisfy
.metn predicate-function .
Those elements which are not removed appear in the same order.
The result list may share substructure with the input list,
and may even be the same list object if no items are removed.

The optional
.meta key-function
specifies how each element from the
.meta list
is transformed to an argument to
.metn predicate-function .
If this argument is omitted
then the predicate function is applied to the elements directly, a behavior
which is identical to
.meta key-function
being
.codn (fun identity) .

The
.code keep-if
function is exactly like
.codn remove-if ,
except the sense of
the predicate is inverted. The function
.code keep-if
retains those items
which
.code remove-if
will delete, and removes those that
.code remove-if
will preserve.

The
.code remove-if*
and
.code keep-if*
functions are like
.code remove-if
and
.codn keep-if ,
but produce lazy lists.

.TP* Examples:
.cblk
  ;; remove any element numerically equal to 3.
  (remove-if (op = 3) '(1 2 3 4 3.0 5)) -> (1 2 4 5)

  ;; remove those pairs whose first element begins with "abc"
  [remove-if (op equal [@1 0..3] "abc")
             '(("abcd" 4) ("defg" 5))
             car]
  -> (("defg" 5))

  ;; equivalent, without test function
  (remove-if (op equal [(car @1) 0..3] "abc")
             '(("abcd" 4) ("defg" 5)))
  -> (("defg" 5))
.cble

.coNP Function @ count-if
.synb
.mets (count-if < predicate-function < list <> [ key-function ])
.syne
.desc
The
.code count-if
function counts the number of elements of
.meta list
which satisfy
.meta predicate-function
and returns the count.

The optional
.meta key-function
specifies how each element from the
.meta list
is transformed to an argument to
.metn predicate-function .
If this argument is omitted
then the predicate function is applied to the elements directly, a behavior
which is identical to
.meta key-function
being
.codn (fun identity) .

.coNP Functions @, posqual @ posql and @ posq
.synb
.mets (posq < object << list )
.mets (posql < object << list )
.mets (posqual < object << list )
.syne
.desc
The
.codn posq ,
.code posql
and
.code posqual
functions return the zero-based position of the
first item in
.meta list
which is, respectively,
.codn eq ,
.code eql
or
.code equal
to
.metn object .

.coNP Functions @, posqual @ posql and @ posq
.synb
.mets (posq < object << list )
.mets (posql < object << list )
.mets (posqual < object << list )
.syne
.desc
The
.codn posq ,
.code posql
and
.code posqual
functions return the zero-based position of the
first item in
.meta list
which is, respectively,
.codn eq ,
.code eql
or
.code equal
to
.metn object .

.coNP Functions @ pos and @ pos-if
.synb
.mets (pos < key < list >> [ testfun <> [ keyfun ]])
.mets (pos-if < predfun < list <> [ keyfun ])
.syne
.desc

The
.code pos
and
.code pos-if
functions search through
.meta list
for an item which matches
.metn key ,
or satisfies predicate function
.metn predfun ,
respectively.
They return the zero-based position of the matching item.

The
.meta keyfun
argument specifies a function which is applied to the elements
of
.meta list
to produce the comparison key. If this argument is omitted,
then the untransformed elements of
.meta list
are examined.

The
.code pos
function's
.meta testfun
argument specifies the test function which
is used to compare the comparison keys from
.meta list
to
.metn key .
If this argument is omitted, then the
.code equal
function is used.
The position of the first element
.meta list
whose comparison key (as
retrieved by
.metn keyfun )
matches the search (under
.metn testfun )
is
returned. If no such element is found,
.code nil
is returned.

The
.code pos-if
function's
.meta predfun
argument specifies a predicate function
which is applied to the successive comparison keys taken from
.meta list
by applying
.meta keyfun
to successive elements. The position of
the first element for which
.meta predfun
yields true is returned. If
no such element is found,
.code nil
is returned.

.coNP Functions @ pos-max and @ pos-min
.synb
.mets (pos-max > sequence >> [ testfun <> [keyfun]])
.mets (pos-min > sequence >> [ testfun <> [keyfun]])
.syne
.desc

The
.code pos-min
and
.code pos-max
functions implement exactly the same algorithm; they
differ only in their defaulting behavior with regard to the
.meta testfun
argument.  If
.meta testfun
is not given, then the pos-max function defaults
.meta testfun
to the
.code greater
function, whereas
.code pos-min
defaults it to the
.code less
function.

If
.meta sequence
is empty, both functions return
.codn nil .

Without a
.meta testfun
argument, the
.code pos-max
function finds the zero-based
position index of the numerically maximum value occurring in
.metn sequence ,
whereas
.code pos-min
without a
.meta testfun
argument finds the index of the minimum
value.

If a
.meta testfun
argument is given, the two functions are equivalent.
The
.meta testfun
function must be callable with two arguments.
If
.meta testfun
behaves like a greater-than comparison, then
.code pos-max
and
.code pos-min
return the index of the maximum element. If
.meta testfun
behaves like a
.code less-than
comparison, then the functions return
the index of the minimum element.

The
.meta keyfun
argument defaults to the
.code identity
function. Each element
from
.meta sequence
is passed through this one-argument function, and
the resulting value is used in its place.

.coNP Function @ where
.synb
.mets (where < function << object )
.syne
.desc

If
.meta object
is a sequence, the
.code where
function returns
a list of the numeric indices of those of its elements which satisfy
.metn function .
The numeric indices appear in increasing order.

If
.meta object
is a hash, the
.code where
function returns an unordered list
of keys which have values which satisfy
.metn function .

.meta function
must be a function that can be called with one argument.
For each element of
.metn object ,
.meta function
is called with that element
as an argument.  If a
.cod2 non- nil
value is returned, then the zero-based index of
that element is added to a list. Finally, the list is returned.

.coNP Function @ select
.synb
.mets (select < object >> { index-list <> | function })
.syne
.desc

The
.code select
function returns an object, of the same kind as
.metn object ,
which consists of those elements of
.meta object
which are identified by
the indices in
.metn index-list .

If
.meta function
is given instead of
.metn index-list ,
then
.meta function
is invoked with
.meta object
as its argument. The return value is then taken as
if it were the
.meta index-list
argument .

If
.meta object
is a sequence, then
.meta index-list
consists of numeric
indices.  The
.code select
function stops processing
.meta object
upon encountering an
index inside
.meta index-list
which is out of range. (Rationale: without
this strict behavior,
.code select
would not be able to terminate if
.meta index-list
is infinite.)

If
.meta object
is a list, then
.meta index-list
must contain monotonically increasing
numeric values, even if no value is out of range, since the
.code select
function
makes a single pass through the list based on the assumption that indices
are ordered. (Rationale: optimization.)

If
.meta object
is a hash, then
.meta index-list
is a list of keys. A new hash is
returned which contains those elements of
.meta object
whose keys appear
in
.metn index-list .
All of
.meta index-list
is processed, even if it contains
keys which are not in
.metn object .

.coNP Funtion @ in
.synb
.mets (in < sequence < key >> [ testfun <> [ keyfun ]])
.mets (in < hash << key )
.syne
.desc
The
.code in
function tests whether
.meta key
is found inside
.meta sequence
or
.metn hash .

If the
.meta testfun
argument is specified, it specifies the function
which is used to comparison keys from the sequence
to
.metn key .
Otherwise the
.code equal
function is used.

If the
.meta keyfun
argument is specified, it specifies a function which
is applied to the elements of
.meta sequence
to produce the comparison keys. Without this
argument, the elements themselves are taken
as the comparison keys.

If the object being searched is a hash, then the
.meta keyfun
and
.meta testfun
arguments are ignored.

The
.code in
function returns
.code t
if it finds
.meta key
in
.meta sequence
or
.metn hash,
otherwise
.codn nil .

.coNP Funtion @ partition
.synb
.mets (partition < sequence >> { index-list >> | index <> | function })
.syne
.desc

If
.meta sequence
is empty, then
.code partition
returns an empty list, and the
second argument is ignored; if it is
.metn function ,
it is not called.

Otherwise,
.code partition
returns a lazy list of partitions of the sequence

.metn sequence .
Partitions are consecutive, non-empty sub-strings of
.metn sequence ,
of the same kind as
.metn sequence .


If the second argument is of the form
.metn index-list ,
it is a sequence of
strictly increasing, positive integers. First, any leading zeros in this sequence
are dropped. The
.code partition
function then divides
.meta sequence
according to the
indices in index list. The first partition begins with the first element of
.metn sequence .
The second partition begins at the first position in
.metn index-list ,
and so on. Indices beyond the length of the sequence are ignored.

If
.meta index-list
is empty then a one-element list containing the entire
.meta sequence
is returned.

If the second argument is a function, then this function is applied
to
.metn sequence ,
and the return value of this call is then used in place of the
second argument, which must be an
.meta index
or
.metn index-list .

If the second argument is an atom other than a function, it is assumed to be
an integer index, and is turned into an
.meta index-list
of one element.

.TP* Examples:
.cblk
  (partition '(1 2 3) 1) -> ((1) (2 3))

  ;; split the string where there is a "b"
  (partition "abcbcbd" (op where @1 (op eql #\eb))) -> ("a" "bc"
                                                       "bc" "bd")
.cble

.coNP Function @ partition*
.synb
.mets (partition* < sequence >> { index-list >> | index <> | function })
.syne
.desc
If
.meta sequence
is empty, then
.code partition* returns an empty list, and the
second argument is ignored; if it is
.metn function ,
it is not called.

If the second argument is of the form
.metn index-list ,
which is a sequence
of strictly increasing non-negative integers, then
.code partition*
produces
lazy list of pieces taken from
.metn sequence .
The pieces are formed by
deleting from
.meta sequence
the elements at the positions given
in
.metn index-list .
The pieces are the non-empty sub-strings between
the deleted elements.

If
.meta index-list
is empty then a one-element list containing the entire
.meta sequence
is returned.

If the second argument is a function, then this function is applied
to
.metn sequence ,
and the return value of this call is then used in place of the
second argument, which must be an
.meta index
or
.metn index-list .

If the second argument is an atom other than a function, it is assumed to be
an integer index, and is turned into an
.meta index-list
of one element.

.TP* Examples:
.cblk
  (partition* '(1 2 3 4 5) '(0 2 4)) -> ((1) (3) (5))

  (partition* "abcd" '(0 3)) -> "bc"

  (partition* "abcd" '(0 1 2 3)) -> nil
.cble

.coNP Function @ tree-find
.synb
.mets (tree-find < obj < tree << test-function )
.syne
.desc

The
.code tree-find
function searches
.meta tree
for an occurrence of
.metn obj .
Tree can be
any atom, or a cons. If
.meta tree
it is a cons, it is understood to be a proper
list whose elements are also trees.

The equivalence test is performed by
.meta test-function
which must take two
arguments, and has conventions similar to
.codn eq ,
.code eql
or
.codn equal .

.code tree-find
works as follows.  If
.meta tree
is equivalent to
.meta obj
under
.metn test-function ,
then
.code t
is returned to announce a successful finding.
If this test fails, and
.meta tree
is an atom,
.code nil
is returned immediately to
indicate that the find failed.  Otherwise,
.meta tree
is taken to be a proper list,
and tree-find is recursively applied to each element of the list in turn, using
the same
.meta obj
and
.meta test-function
arguments, stopping at the first element
which returns a
.cod2 non- nil
value.

.coNP Functions @ find and @ find-if
.synb
.mets (find < key < sequence >> [ testfun <> [ keyfun ]])
.mets (find-if < predfun < sequence <> [ keyfun ])
.syne
.desc
The
.code find
and
.code find-if
functions search through a sequence for an item which
matches a key, or satisfies a predicate function, respectively.

The
.meta keyfun
argument specifies a function which is applied to the elements
of
.meta sequence
to produce the comparison key. If this argument is omitted,
then the untransformed elements of the
.meta sequence
are searched.

The
.code find
function's
.meta testfun
argument specifies the test function which
is used to compare the comparison keys from
.meta sequence
to the search key.
If this argument is omitted, then the
.code equal
function is used.
The first element from the list whose comparison key (as retrieved by
.metn keyfun )
matches the search (under
.metn testfun )
is returned. If no such element is found,
.code nil
is returned.

The
.code find-if
function's
.meta predfun
argument specifies a predicate function
which is applied to the successive comparison keys pulled from the list
by applying
.meta keyfun
to successive elements. The first element
for which
.meta predfun
yields true is returned. If no such
element is found,
.code nil
is returned.

.coNP Functions @ find-max and @ find-min
.synb
.mets (find-max < sequence >> [ testfun <> [ keyfun ]])
.mets (find-min < sequence >> [ testfun <> [ keyfun ]])
.syne
.desc
The
.code find-min
and
.code find-max
function implement exactly the same algorithm; they
differ only in their defaulting behavior with regard to the
.meta testfun
argument.  If
.meta testfun
is not given, then the find-max function defaults it to
the
.code greater
function, whereas
.code find-min
defaults it to the
.code less
function.

Without a
.meta testfun
argument, the
.code find-max
function finds the numerically
maximum value occurring in
.metn sequence ,
whereas
.code pos-min
without a
.meta testfun
argument finds the minimum value.

If a
.meta testfun
argument is given, the two functions are equivalent.
The
.meta testfun
function must be callable with two arguments.
If
.meta testfun
behaves like a greater-than comparison, then
.code find-max
and
.code find-min
both return the maximum element. If
.meta testfun
behaves like a less-than comparison, then the functions return
the minimum element.

The
.meta keyfun
argument defaults to the
.code identity
function. Each element
from
.meta sequence
is passed through this one-argument function, and
the resulting value is used in its place for the purposes of the
comparison. However, the original element is returned.

.coNP Function @ set-diff
.synb
.mets (set-diff < seq1 < seq2 >> [ testfun <> [ keyfun ]])
.syne
.desc
The
.code set-diff
function treats the sequences
.meta seq1
and
.meta seq2
as if they were sets
and computes the set difference: a sequence which contains those elements in
.meta seq1
which do not occur in
.metn seq2 .

.code set-diff
returns a sequence of the same kind as
.metn seq1 .

Element equivalence is determined by a combination of
.meta testfun
and
.metn keyfun .
Elements are compared pairwise, and each element of a pair is passed through
.meta keyfun
function to produce a comparison value. The comparison values
are compared using
.metn testfun .
If
.meta keyfun
is omitted, then the
untransformed elements themselves are compared, and if
.meta testfun
is omitted,
then the
.code equal
function is used.

If
.meta seq1
contains duplicate elements which do not occur in
.meta seq2
(and thus are preserved in the set difference) then these duplicates appear
in the resulting
sequence. Furthermore, the order of the items from
.meta seq1
is preserved.

.coNP Functions @, mapcar @ mappend @ mapcar* and @ mappend*
.synb
.mets (mapcar < function << sequence *)
.mets (mappend < function << sequence *)
.mets (mapcar* < function << sequence *)
.mets (mappend* < function << sequence *)
.syne
.desc

When given only one argument, the
.code mapcar
function returns
.codn nil .
.meta function
is never called.

When given two arguments, the
.code mapcar
function applies
.meta function
to each elements of
.meta sequence
and returns a sequence of the resulting values
in the same order as the original values.
The returned sequence is the same kind as
.metn sequence ,
if possible. If the accumulated values cannot be
elements of that type of sequence, then a list is returned.

When additional sequences are given as arguments, this filtering behavior is
generalized in the following way:
.code mapcar
traverses the sequences in parallel,
taking a value from each sequence as an argument to the function. If there
are two lists,
.meta function
is called with two arguments and so forth.
The traversal is limited by the length of the shortest sequence.
The return values of the function are collected into a new sequence which is
returned. The returned sequence is of the same kind as the leftmost
input sequence, unless the accumulated values cannot be elements of that type of
sequence, in which case a list is returned.

The
.code mappend
function works like
.codn mapcar ,
with the following difference.
Rather than accumulating the values returned by the function into a sequence,
mappend expects the items returned by the function to be sequences which
are catenated with
.codn append ,
and the resulting sequence is returned. The returned sequence is of the same
kind as the leftmost input sequence, unless the values cannot be elements
of that type of sequence, in which case a list is returned.

The
.code mapcar*
and
.code mappend*
functions work like
.code mapcar
and
.codn mappend ,
respectively.
However, they return lazy lists rather than generating the entire
output list prior to returning.

.TP* Caveats:

Like
.codn mappend ,
.code mappend*
must "consume" empty lists. For instance,
if the function being mapped puts out a sequence of
.codn nil s,
then the result must be the empty list
.codn nil ,
because
.code (append nil nil nil nil ...)
is
.codn nil .

But suppose that
.code mappend*
is used on inputs which are infinite lazy
lists, such that the function returns
.code nil
values indefinitely.
For instance:

.cblk
  ;; Danger: infinite loop!!!
  (mappend* (fun identity) (repeat '(nil))) 
.cble

The
.code mappend*
function is caught in a loop trying to consume
and squash an infinite stream of
.codn nil s,
and so doesn't return.

.TP* Examples:
.cblk
  ;; multiply every element by two
  (mapcar (lambda (item) (* 2 item)) '(1 2 3)) -> (4 6 8)

  ;; "zipper" two lists together
  (mapcar (lambda (le ri) (list le ri)) '(1 2 3) '(a b c)) '((1 a) (2 b) (3 c)))

  ;; like append, mappend allows a lone atom or a trailing atom:
  (mappend (fun identity) 3) -> (3)
  (mappend (fun identity) '((1) 2)) -> (1 . 2)

  ;; take just the even numbers
  (mappend (lambda (item) (if (evenp x) (list x))) '(1 2 3 4 5))
  -> (2 4)
.cble

.coNP Function @ mapdo
.synb
.mets (mapdo < function << sequence *)
.syne
.desc
The
.code mapdo
function is similar to
.codn mapcar ,
but always returns
.codn nil .
It is useful
when
.meta function
performs some kind of side effect, hence the "do" in the name,
which is a mnemonic for the execution of imperative actions.

When only the
.meta function
argument is given,
.meta function
is never called,
and
.code nil
is returned.

If a single
.meta sequence
argument is given, then
.code mapdo
iterates over
.metn sequence ,
invoking
.meta function
on each element.

If two or more
.meta sequence
arguments are given, then
.code mapdo
iterates over
the sequences in parallel, extracting parallel tuples of items. These
tuples are passed as arguments to
.metn function,
which must accept as many
arguments as there are sequences.

.coNP Functions @ transpose and @ zip
.synb
.mets (transpose << sequence )
.mets (zip << sequence *)
.syne
.desc
The
.code transpose
function performs a transposition on
.metn sequence .
This means that the
elements of
.meta sequence
must be sequences.  These sequences are understood to be
columns; transpose exchanges rows and columns, returning a sequence of the rows
which make up the columns.  The returned sequence is of the same kind as
.metn sequence ,
and the rows are also the same kind of sequence as the first column
of the original sequence. The number of rows returned is limited by the
shortest column among the sequences.

All of the input sequences (the elements of
.metn sequence)
must have elements
which are compatible with the first sequence. This means that if the first
element of
.meta sequence
is a string, then the remaining sequences must be
strings, or else sequences of characters, or of strings.

The
.code zip
function takes variable arguments, and is equivalent to calling
.code transpose
on a list of the arguments. The following equivalences hold:

.synb
   (zip . x) <--> (transpose x)

   [apply zip x] <--> (transpose x)
.syne

.TP* Examples:
.cblk
  ;; transpose list of lists
  (transpose '((a b c) (c d e))) ->  ((a c) (b d) (c e))

  ;; transpose vector of strings:
  ;; - string columns become string rows
  ;; - vector input becomes vector output
  (transpose #("abc" "def" "ghij")) -> #("adg" "beh" "cfi")

  ;; error: transpose wants to make a list of strings
  ;; but 1 is not a character
  (transpose #("abc" "def" '(1 2 3))) ;; error!

  ;; String elements are catenated:
  (transpose #("abc" "def" ("UV" "XY" "WZ"))) -> #("adUV" "beXY" "cfWZ")

  (zip '(a b c) '(c d e)) ->  ((a c) (b d) (c e))
.cble

.coNP Function @ interpose
.synb
.mets (interpose < sep << sequence )
.syne
.desc
The
.code interpose
function returns a sequence of the same type as
.metn sequence ,
in which the elements from
.meta sequence
appear with the
.meta sep
value inserted
between them.

If
.meta sequence
is an empty sequence or a sequence of length 1, then a
sequence identical to
.meta sequence
is returned. It may be a copy of
.meta sequence
or it may be
.meta sequence
itself.

If
.meta sequence
is a character string, then the value
.meta sep
must be a character.

It is permissible for
.metn sequence ,
or for a suffix of
.meta sequence
to be a lazy
list, in which case interpose returns a lazy list, or a list with a lazy
suffix.

.TP* Examples:
.cblk
  (interpose #\e- "xyz") -> "x-y-z"
  (interpose t nil) -> nil
  (interpose t #()) -> #()
  (interpose #\ea "") -> ""
  (interpose t (range 0 0)) -> (0)
  (interpose t (range 0 1)) -> (0 t 1)
  (interpose t (range 0 2)) -> (0 t 1 t 2)
.cble

.coNP Functions @ conses and @ conses*
.synb
.mets (conses << list )
.mets (conses* << list )
.syne
.desc
These functions return a list whose elements are the conses which make
up
.metn list .
The
.code conses*
function does this in a lazy way, avoiding the
computation of the entire list: it returns a lazy list of the conses of
.metn list .
The
.code conses
function computes the entire list before returning.

The input
.meta list
may be proper or improper.

The first cons of
.meta list
is that
.meta list
itself. The second cons is the rest
of the list, or
.cblk
.meti (cdr << list ).
.cble
The third cons is
.cblk
.meti (cdr (cdr << list ))
.cble
and so on.

.TP* Example:
.cblk
  (conses '(1 2 3)) -> ((1 2 3) (2 3) (3))
.cble

.TP* "Dialect Note:"

These functions are useful for simulating the
.code maplist
function found in other dialects like Common Lisp.

\*(TL's
.code (conses x)
can be expressed in Common Lisp as
.codn (maplist #'identity x) .

Conversely, the Common Lisp operation
.code (maplist function list)
can be computed in \*(TL as
.codn (mapcar function (conses list)) .

More generally, the Common Lisp operation

.cblk
  (maplist function list0 list1 ... listn)
.cble

can be expressed as:

.cblk
  (mapcar function (conses list0)
                   (conses list1) ... (conses listn))
.cble

.coNP Functions @ apply and @ iapply
.synb
.mets (apply < function <> [ arg * << trailing-args ])
.mets (iapply < function <> [ arg * << trailing-args ])
.syne
.desc
The
.code apply
function invokes
.metn function ,
optionally passing to it an argument
list. The return value of the
.code apply
call is that of
.metn function .

If no arguments are present after
.metn function ,
then
.meta function
is invoked without arguments.

If one argument is present after
.metn function ,
then it is interpreted as
.metn trailing-args .
If this is a sequence (a list, vector or string),
then the elements of the sequence are passed as individual arguments to
.metn function .
If
.meta trailing-args
is not a sequence, then
.meta function
is invoked
with an improper argument list, terminated by the
.meta trailing-args
atom.

If two or more arguments are present after
.metn function ,
then the last of these arguments is interpreted as
.metn trailing-args .
The previous arguments represent leading arguments which are applied to
.metn function ,
prior to the arguments taken from
.metn trailing-args .

The
.code iapply
function ("improper apply") is similar to
.codn apply ,
except with regard to the treatment of
.metn trailing-args .
Firstly, under
.codn iapply ,
if
.meta trailing-args
is an atom other than
.code nil
(possibly a sequence, such as a vector or string),
then it is treated as an ordinary argument:
.meta function
is invoked with a proper argument list, whose last element is
.metn trailing-args .
Secondly, if
.meta trailing-args
is a list, but an improper list, then the terminating atom of
.meta trailing-args
becomes an ordinary argument. Thus, in all possible cases,
.code iapply
treats an extra
.cod2 non- nil
atom as an argument, and never calls
.meta function
with an improper argument list.

.TP* Examples:
.cblk
  ;; '(1 2 3) becomes arguments to list, thus (list 1 2 3).
  (apply (fun list) '(1 2 3)) -> (1 2 3)

  ;; this effectively invokes (list 1 2 3 4)
  (apply (fun list) 1 2 '(3 4)) -> (1 2 3)

  ;; this effectively invokes (list 1 2 . 3)
  (apply (fun list) 1 2 3)) -> (1 2 . 3)

  ;; "abc" is separated into characters which become arguments of list
  (apply (fun list) "abc") -> (#\ea #\eb #\ec)
.cble

.TP* "Dialect Note:"
Note that some uses of this function that are necessary in other Lisp dialects
are not necessary in \*(TL. The reason is that in \*(TL, improper list
syntax is accepted as a compound form, and performs application:

.cblk
  (foo a b . x)
.cble

Here, the variables
.code a
and
.code b
supply the first two arguments for
.codn foo .
In
the dotted position,
.code x
must evaluate to a list or vector. The list or
vector's elements are pulled out and treated as additional arguments for
.codn foo .
Of course, this syntax can only be used if
.code x
is a symbolic form or an atom.  It
cannot be a compound form, because
.code (foo a b . (x))
and
.code (foo a b x)
are equivalent structures.

.coNP Functions @ reduce-left and @ reduce-right
.synb
.mets (reduce-left < binary-function < list
.mets \ \ \ \ \ \ \ \ \ \ \ \  >> [ init-value <> [ key-function ]])

.mets (reduce-right < binary-function < list
.mets \ \ \ \ \ \ \ \ \ \ \ \ \  >> [ init-value <> [ key-function ]])
.syne
.desc
The
.code reduce-left
and
.code reduce-right
functions reduce lists of operands specified
by
.meta list
and
.meta init-value
to a single value by the repeated application of
.metn binary-function .

An effective list of operands is formed by combining
.meta list
and
.metn init-value .
If
.meta key-function
is specified, then the items of
.meta list
are
mapped to a new values through
.metn key-function .
If
.meta init-value
is supplied,
then in the case of
.codn reduce-left ,
the effective list of operands is formed by
prepending
.meta init-value
to
.metn list .
In the case of
.codn reduce-right ,
the effective operand list is produced by appending
.meta init-value
to
.metn list .

The production of the effective list can be expressed like this,
though this is not to be understood as the actual implementation:

.cblk
  (append (if init-value-present (list init-value))
          [mapcar (or key-function identity) list]))))
.cble

In the
.code reduce-right
case, the arguments to
.code append
are reversed.

If the effective list of operands is empty, then
.meta binary-function
is called
with no arguments at all, and its value is returned. This is the only
case in which
.meta binary-function
is called with no arguments; in all
remaining cases, it is called with two arguments.

If the effective list contains one item, then that item is returned.

Otherwise, the effective list contains two or more items, and is decimated as
follows.

Note that an
.meta init-value
specified as
.code nil
is not the same as a missing
.metn init-value ;
this means that the initial value is the object
.codn nil .
Omitting
.meta init-value
is the same as specifying a value of
.code :
(the colon symbol).
It is possible to specify
.meta key-function
while omitting an
.meta init-value
argument. This is achieved by explicitly specifying
.code :
as the
.meta init-value
argument.

Under
.codn reduce-left ,
the leftmost pair of operands is removed
from the list and passed as arguments to
.metn binary-function ,
in the same order
that they appear in the list, and the resulting value initializes an
accumulator. Then, for each remaining item in the list,
.meta binary-function
is invoked on two arguments: the current accumulator value, and the next element
from the list. After each call, the accumulator is updated with the return
value of
.metn binary-function .
The final value of the accumulator is returned.

Under
.codn reduce-right ,
the list is processed right to left.  The rightmost
pair of elements in the effective list is removed, and passed as arguments to
.metn binary-function ,
in the same order that they appear in the list. The
resulting value initializes an accumulator. Then, for each remaining item in
the list,
.meta binary-function
is invoked on two arguments:  the
next element from the list, in right to left order, and the current
accumulator value. After each call, the accumulator is updated with the return
value of
.metn binary-function .
The final value of the accumulator is returned.

.TP* Examples:
.cblk
  ;;; effective list is (1) so 1 is returned
  (reduce-left (fun +) () 1 nil)  ->  1

  ;;; computes (- (- (- 0 1) 2) 3)
  (reduce-left (fun -) '(1 2 3) 0 nil) -> -6

  ;;; computes (- 1 (- 2 (- 3 0)))
  (reduce-right (fun -) '(1 2 3) 0 nil) -> 2

  ;;; computes (* 1 2 3)
  (reduce-left (fun *) '((1) (2) (3)) nil (fun first)) -> 6

  ;;; computes 1 because the effective list is empty
  ;;; and so * is called with no arguments, which yields 1.
  (reduce-left (fun *) nil)
.cble

.coNP Function @, some @ all and @ none
.synb
.mets (some < sequence >> [ predicate-fun <> [ key-fun ]])
.mets (all < sequence >> [ predicate-fun <> [ key-fun ]])
.mets (none < sequence >> [ predicate-fun <> [ key-fun ]])
.syne
.desc
The
.codn some ,
.code all
and
.code none
functions apply a predicate test function
.meta predicate-fun
over a list of elements.  If the argumenti
.meta key-fun
is
specified, then elements of
.meta sequence
are passed into
.metn key-fun ,
and
.meta predicate-fun
is
applied to the resulting values. If
.meta key-fun
is omitted, the behavior is
as if
.meta key-fun
is the identity function. If
.meta predicate-fun
is omitted,
the behavior is as if
.meta predicate-fun
is the identity function.

These functions have short-circuiting semantics and return conventions similar
to the and and or operators.

The some function applies
.meta predicate-fun
to successive values
produced by retrieving elements of
.meta list
and processing them through
.metn key-fun .
If the list is empty, it returns
.codn nil .
Otherwise it returns the
first
.cod2 non- nil
return value returned by a call to
.meta predicate-fun
and
stops evaluating more elements. If
.meta predicate-fun
returns
.code nil
for all
elements, it returns
.metn nil .

The
.code all
function applies
.meta predicate-fun
to successive values
produced by retrieving elements of
.meta list
and processing them through
.metn key-fun .
If the list is empty, it returns
.codn t .
Otherwise, if
.meta predicate-fun
yields
.code nil
for any value, the
.cod all
function immediately
returns without invoking
.meta predicate-fun
on any more elements.
If all the elements are processed, then the all function returns
the value which
.meta predicate-fun
yielded for the last element.

The
.code none
function applies
.meta predicate-fun
to successive values
produced by retrieving elements of
.meta list
and processing them through
.metn key-fun .
If the list is empty, it returns
.codn t .
Otherwise, if
.meta predicate-fun
yields
.cod2 non- nil
for any value, the none function
immediately returns nil. If
.meta predicate-fun
yields nil for all
values, the none function returns
.codn t .

.TP* Examples:

.cblk
  ;; some of the integers are odd
  [some '(2 4 6 9) oddp] -> t

  ;; none of the integers are even
  [none '(1 3 4 7) evenp] -> t
.cble

.coNP Function @ multi
.synb
.mets (multi < function << list *)
.syne
.desc
The
.code multi
function distributes an arbitrary list processing function
.meta multi
over multiple lists given by the
.meta list
arguments.

The
.meta list
arguments are first transposed into a single list of tuples. Each
successive element of this transposed list consists of a tuple of the
successive items from the lists. The length of the transposed list is that
of the shortest
.meta list
argument.

The transposed list is then passed to
.meta function
as an argument.

The
.meta function
is expected to produce a list of tuples, which are transposed
again to produce a list of lists which is then returned.

Conceptually, the input lists are columns and
.meta function
is invoked on
a list of the rows formed from these columns. The output of
.meta function
is a transformed list of rows which is reconstituted into a list of columns.

.TP* Example:

.cblk
  ;; Take three lists in parallel, and remove from all of them
  ;; them the element at all positions where the third list
  ;; has an element of 20.

  (multi (op remove-if (op eql 20) @1 third)
         '(1 2 3)
         '(a b c)
         '(10 20 30))

  -> ((1 3) (a c) (10 30))

  ;; The (2 b 20) "row" is gone from the three "columns".

  ;; Note that the (op remove if (op eql 20) @1 third)
  ;; expression can be simplified using the ap operator:
  ;;
  ;; (op remove-if (ap eql @3 20))
.cble

.SS* Association Lists

Association lists are ordinary lists formed according to a special convention.
Firstly, any empty list is a valid association list. A non-empty association
list contains only cons cells as the key elements. These cons cells are
understood to represent key/value associations, hence the name "association
list".

.coNP Function @ assoc
.synb
.mets (assoc < key << alist )
.syne
.desc
The
.code assoc
function searches an association list
.meta alist
for a cons cell whose
car field is equivalent to
.meta key
(with equality determined by the equal
function). The first such cons is returned. If no such cons is found,
.code nil
is returned.

.coNP Function @ assql
.synb
.mets (assql < key << alist )
.syne
.desc
The
.code assql
function is just like
.codn assoc ,
except that the equality test
is determined using the
.code eql
function rather than
.codn equal .

.coNP Function @ acons
.synb
.mets (acons < car < cdr << alist )
.syne
.desc

The
.code acons
function constructs a new alist by consing a new cons to the
front of
.metn alist .
The following equivalence holds:

.cblk
  (acons car cdr alist) <--> (cons (cons car cdr) alist)
.cble

.coNP Function @ acons-new
.synb
.mets (acons-new < car < cdr << alist )
.syne
.desc

The
.code acons-new
function searches
.metn alist ,
as if using the assoc function,
for an existing cell which matches the key provided by the car argument.
If such a cell exists, then its cdr field is overwritten with the
.meta cdr
argument, and then the
.meta alist
is returned. If no such cell exists, then
a new list is returned by adding a new cell to the input list consisting
of the
.meta car
and
.meta cdr
values, as if by the
.code acons
function.

.coNP Function @ aconsql-new
.synb
.mets (aconsql-new < car < cdr << alist )
.syne
.desc
This function is like
.codn acons-new ,
except that the
.code
eql
function is used
for equality testing. Thus, the list is searched for an existing cell
as if using the
.code assql
function rather than
.codn assoc .

.coNP Function @ alist-remove
.synb
.mets (alist-remove < alist << keys )
.syne
.desc
The
.code alist-remove
function takes association list
.meta alist
and produces a
duplicate from which cells matching the specified keys have been removed. The
.meta keys
argument is a list of the keys not to appear in the output list.

.coNP Function @ alist-nremove
.synb
.mets (alist-nremove < alist << keys )
.syne
.desc
The
.code alist-nremove
function is like
.codn alist-remove ,
but potentially destructive.
The input list
.meta alist
may be destroyed and its structural material re-used to
form the output list. The application should not retain references to the input
list.

.coNP Function @ copy-alist
.synb
.mets (copy-alist << alist )
.syne
.desc
The
.code copy-alist
function duplicates
.codn alist .
Unlike
.codn copy-list ,
which only duplicates list structure,
.code copy-alist
also duplicates each cons
cell of the input alist. That is to say, each element of the output list
is produced as if by the
.code copy-cons
function applied to the corresponding
element of the input list.

.SS* Property Lists
.coNP Function @ prop
.synb
.mets (prop < plist << key )
.syne
.desc
A property list a flat list of even length consisting of interleaved
pairs of property names (usually symbols) and their values (arbitrary
objects). An example property list is (:a 1 :b "two") which contains
two properties, :a having value 1, and :b having value "two".

The
.code prop
function searches property list
.meta plist
for key
.metn key .
If the key is found, then the value next to it is returned. Otherwise
.code nil
is returned.

It is ambiguous whether
.code nil
is returned due to the property not being
found, or due to the property being present with a
.code nil
value.

.SS* List Sorting
.coNP Function @ merge
.synb
.mets (merge < seq1 < seq2  >> [ lessfun <> [ keyfun ]])
.syne
.desc
The
.code merge
function merges two sorted sequences
.meta seq1
and
.meta seq2
into a single
sorted sequence. The semantics and defaulting behavior of the
.meta lessfun
and
.meta keyfun
arguments are the same as those of the sort function.

The sequence which is returned is of the same kind as
.metn seq1 .

This function is destructive of any inputs that are lists. If the output
is a list, it is formed out of the structure of the input lists.

.coNP Function @ multi-sort
.synb
.mets (multi-sort < columns < less-funcs <> [ key-funcs ])
.syne
.desc
The
.code multi-sort
function regards a list of lists to be the columns of a
database. The corresponding elements from each list constitute a record.
These records are to be sorted, producing a new list of lists.

The
.meta columns
argument supplies the list of lists which comprise the columns of
the database. The lists should ideally be of the same length. If the lists are
of different lengths, then the shortest list is taken to be the length of the
database. Excess elements in the longer lists are ignored, and do not appear in
the sorted output.

The
.meta less-funcs
argument supplies a list of comparison functions which are
applied to the columns. Successive functions correspond to successive
columns. If
.meta less-funcs
is an empty list, then the sorted database will
emerge in the original order. If
.meta less-funcs
contains exactly one function,
then the rows of the database is sorted according to the first column. The
remaining columns simply follow their row. If
.meta less-funcs
contains more than
one function, then additional columns are taken into consideration if the items
in the previous columns compare
.codn equal .
For instance if two elements from column
one compare
.codn equal ,
then the corresponding second column elements are compared
using the second column comparison function.

The optional
.meta key-funcs
argument supplies transformation functions through
which column entries are converted to comparison keys, similarly to the single
key function used in the sort function and others.  If there are more key
functions than less functions, the excess key functions are ignored.

.SS* Lazy Lists and Lazy Evaluation
.coNP Function @ make-lazy-cons
.synb
.mets (make-lazy-cons << function )
.syne
.desc
The function
.code make-lazy-cons
makes a special kind of cons cell called a lazy
cons, or lcons.  Lazy conses are useful for implementing lazy lists.

Lazy lists are lists which are not allocated all at once. Rather,
their elements materialize when they are accessed, like
magic stepping stones appearing under one's feet out of thin air.

A lazy cons has
.code car
and
.code cdr
fields like a regular cons, and those
fields are initialized to
.code nil
when the lazy cons is created. A lazy cons also
has an update function, the one which is provided as the
.meta function
argument to
.codn make-lazy-cons .

When either the
.code car
and
.code cdr
fields of a cons are accessed for the first time,
the function is automatically invoked first. That function has the opportunity
to initialize the
.code car
and
.code cdr
fields. Once the function is called, it is removed
from the lazy cons: the lazy cons no longer has an update function.

To continue a lazy list, the function can make another call to
.code make-lazy-cons
and install the resulting cons as the
.code cdr
of the lazy cons.

.TP* Example:

.cblk
  ;;; lazy list of integers between min and max
  (defun integer-range (min max)
    (let ((counter min))
      ;; min is greater than max; just return empty list,
      ;; otherwise return a lazy list
      (if (> min max)
        nil
        (make-lazy-cons
          (lambda (lcons)
            ;; install next number into car
            (rplaca lcons counter)
            ;; now deal wit cdr field
            (cond
              ;; max reached, terminate list with nil!
              ((eql counter max)
               (rplacd lcons nil))
              ;; max not reached: increment counter
              ;; and extend with another lazy cons
              (t
                (inc counter)
                (rplacd lcons (make-lazy-cons
                                (lcons-fun lcons))))))))))
.cble

.coNP Function @ lconsp
.synb
.mets (lconsp << value )
.desc
The
.code lconsp
function returns
.code t
if
.meta value
is a lazy cons cell. Otherwise
it returns
.codn nil ,
even if
.meta value
is an ordinary cons cell.

.coNP Function @ lcons-fun
.synb
.mets (lcons-fun << lazy-cons )
.syne
.desc
The
.code lcons-fun
function retrieves the update function of a lazy cons.
Once a lazy cons has been accessed, it no longer has an update function
and
.code lcons-fun
returns
.codn nil .
While the update function of a lazy cons is
executing, it is still accessible. This allows the update function
to retrieve a reference to itself and propagate itself into
another lazy cons (as in the example under
.codn make-lazy-cons ).

.coNP Functions @ lazy-stream-cons and @ get-lines
.synb
.mets (lazy-stream-cons << stream )
.mets (get-lines << stream )
.syne
.desc
The
.code lazy-stream-cons
and
.code get-lines
functions are synonyms. Thus, the following
description of
.code lazy-stream-cons
also applies to
.codn get-lines .

The
.code lazy-stream-cons
returns a lazy cons which generates a lazy list based on
reading lines of text from input stream
.metn stream ,
which form the elements of
the list. The
.code get-line
function is called on demand to add elements to the
list.

The
.code lazy-stream-cons
function itself makes the first call to
.code get-line
on the stream. If this returns
.codn nil ,
then the stream is closed and
.code nil
is
returned. Otherwise, a lazy cons is returned whose update function will install
that line into the
.code car
field of the lazy cons, and continue the lazy list
by making another call to
.codn lazy-stream-cons ,
installing the result into the
.code cdr
field.

.code lazy-stream-cons
inspects the real-time property of a stream
as if by the
.code real-time-stream-p
function. This determines which of two
styles of lazy list are returned. For an ordinary (non-real-time) stream,
the lazy list treats the end-of-file condition accurately: an empty
file turns into the empty list
.codn nil ,
a one line file into a one-element
list which contains that line and so on. This accuracy requires one
line of lookahead which is not acceptable in real-time streams, and
so a different type of lazy list is used, which generates an extra
.code nil
item after the last line. Under this type of lazy list, an empty input stream
translates to the list
.codn (nil) ;
a one-line stream translates to
.code ("line" nil)
and so forth.

.coNP Macro @ delay
.synb
.mets (delay << expression )
.syne
.desc
The delay operator arranges for the delayed (or "lazy") evaluation of
.metn expression .
This means that the expression is not evaluated immediately.
Rather, the delay expression produces a promise object.

The promise object can later be passed to the
.code force
function (described
later in this document). The force function will trigger the evaluation
of the expression and retrieve the value.

The expression is evaluated in the original scope, no matter where
the
.code force
takes place.

The expression is evaluated at most once, by the first call to
.codn force .
Additional calls to
.code force
only retrieve a cached value.

.TP* Example:

.cblk
  @(do
    ;; list is popped only once: the value is computed
    ;; just once when force is called on a given promise
    ;; for the first time.

    (defun get-it (promise)
      (format t "*list* is ~s\en" *list*)
      (format t "item is ~s\en" (force promise))
      (format t "item is ~s\en" (force promise))
      (format t "*list* is ~s\en" *list*))

    (defvar *list* '(1 2 3))

    (get-it (delay (pop *list*))))

  Output:

  *list* is (1 2 3)
  item is 1
  item is 1
  *list* is (2 3)
.cble

.coNP Function @ force
.synb
.mets (force << promise )
.syne
.desc
The
.code force
function accepts a promise object produced by the
.code delay
macro.
The first time
.code force
is invoked, the
.meta expression
which was wrapped inside
.meta promise
by the
.code delay
macro is evaluated (in its original lexical environment, regardless of where in
the program the
.code force
call takes place). The value of
.meta expression
is
cached inside
.meta promise
and returned, becoming the return value of the
.code force
function call.  If the
.code force
function is invoked additional times on
the same promise, the cached value is retrieved.

.SS* Lazy Sequences, Ranges, Permutations and Combinations
.coNP Functions @ generate and @ giterate
.synb
.mets (generate < while-fun << gen-fun )
.mets (giterate < while-fun < gen-fun <> [ value ])
.syne
.desc
The
.code generate
function produces a lazy list which dynamically produces items
according to the following logic.

The arguments to
.code generate
are functions which do not take any arguments.  The
return value of generate is a lazy list.

When the lazy list is accessed, for instance with the functions car and cdr, it
produces items on demand. Prior to producing each item,
.meta while-fun
is
called. If it returns a true boolean value (any value other than
.codn nil ),
then
the
.meta gen-fun
function is called, and its return value is incorporated as
the next item of the lazy list. But if
.meta while-fun
yields
.codn nil ,
then the lazy list immediately terminates.

Prior to returning the lazy list, generate invokes the
.meta while-fun
one time.
If
.code while-fun
yields
.codn nil ,
then
.code generate
returns the empty list
.code nil
instead of a lazy list. Otherwise, it instantiates a lazy list, and invokes the
.code gen-func
to populate it with the first item.

The
.code giterate
function is similar to
.codn generate ,
except that
.meta while-fun
and
.meta gen-fun
are functions of one argument rather than functions of
no arguments. The optional
.meta value
argument defaults to
.code nil
and is threaded through the function calls. That is to say, the lazy
list returned is
.cblk
.meti >> ( value >> [ gen-fun << value ] >> [ gen-fun >> [ gen-fun  << value ]] ...).
.cble

The lazy list terminates when a value fails to satsify
.metn while-fun .
That is to say, prior to generating each value, the lazy list tests
the value using
.metn while-fun .
If that function returns
.codn nil ,
then the item is not added, and the sequence terminates.

Note:
.code giterate
could be written in terms of
.code generate
like this:

.cblk
  (defun giterate (w g v)
     (generate (lambda () [w v])
               (lambda () (prog1 v (set v [g v])))))
.cble

.TP* Example:

.cblk
  (giterate (op > 5) (op + 1) 0) -> (0 1 2 3 4)
.cble

.coNP Function @ repeat
.synb
.mets (repeat < list <> [ count ])
.syne
.desc
If
.meta list
is empty, then repeat returns an empty list.

If
.meta count
is omitted, the
.code repeat
function produces an infinite lazy list
formed by catenating together copies of
.metn list .

If
.meta count
is specified and is zero or negative, then an empty list is
returned.

Otherwise a list is returned consisting of
.meta count
repetitions of
.meta list
catenated together.

.coNP Function @ pad
.synb
.mets (pad < sequence < object <> [ count ])
.syne
.desc
The
.code pad
function produces a lazy list which consists of all of the
elements of
.meta sequence
followed by repetitions of
.meta object .

If
.meta count
is omitted, then the repetition of
.meta object
is infinite. Otherwise the specified number of repetitions
occur.

Note that
.meta sequence
may be a lazy list which is infinite. In that case, the repetitions
of
.meta object
will never occur.

.coNP Function @ weave
.synb
.mets (weave >> { sequence }*)
.syne
The
.code weave
function interleaves elements from the sequences given as arguments.

If called with no arguments, it returns the empty list.

If called with a single sequence, it returns the elements of that sequence
as a new lazy list.

When called with two or more sequences,
.code
weave
returns a lazy list which draws elements from the sequeces in a round-robin
fashion, repeatedly scanning the sequences from left to right, and
taking an item from each one, removing it from the sequence.
Whenever a sequence runs out of items, it is deleted; the weaving then
continues with the remaining sequences. The weaved sequence terminates
when all sequences are eliminated. (If at least one of the sequences
is an infinite lazy list, then the weaved sequence is infinite.)

.TP* Examples:

.cblk
  ;; Weave negative integers with positive ones:
  (weave (range 1) (range -1 : -1)) -> (1 -1 2 -2 3 -3 ...)

  (weave "abcd" (range 1 3) '(x x x x x x x))
  --> (#\ea 1 x #\eb 2 x #\ec 3 x #\ed x x x x)
.cble

.coNP Macros @ gen and @ gun
.synb
.mets (gen < while-expression << produce-item-expression )
.mets (gun << produce-item-expression )
.syne
.desc
The
.code gen
macro operator produces a lazy list, in a manner similar to the
.code generate
function. Whereas the
.code generate
function takes functional arguments,
the
.code gen
operator takes two expressions, which is often more convenient.

The return value of
.code gen
is a lazy list. When the lazy list is accessed, for
instance with the functions
.code car
and
.codn cdr ,
it produces items on demand. Prior to
producing each item, the
.meta while-expression
is evaluated, in its original
lexical scope.  If the expression yields a
.cod2 non- nil
value, then
.meta produce-item-expression
is evaluated, and its return value is incorporated as
the next item of the lazy list. If the expression yields
.codn nil ,
then the lazy list immediately terminates.

The
.code gen
operator itself immediately evaluates
.meta while-expression
before
producing the lazy list. If the expression yields
.codn nil ,
then the operator
returns the empty list
.codn nil .
Otherwise, it instantiates the lazy list and
invokes the
.meta produce-item-expression
to force the first item.

The
.code gun
macro similarly creates a lazy list according to the following
rules. Each successive item of the lazy list is obtained as a result of
evaluating
.metn produce-item-expression .
However, when
.meta produce-item-expression
yields
.codn nil ,
then the list terminates (without adding that
.code nil
as an item).

Note 1: the form
.code gun
can be implemented as a macro-expanding to
an instance of the
.code gen
operator, like this:

.cblk
  (defmacro gun (expr)
    (let ((var (gensym)))
      ^(let (,var)
         (gen (set ,var ,expr)
              ,var))))
.cble

This exploits the fact that the
.code set
operator returns the value that is
assigned, so the set expression is tested as a condition by
.codn gen ,
while having the side effect of storing the next item temporarily
in a hidden variable.

In turn,
.code gen
can be implemented as a macro expanding to some
.code lambda
functions which are passed to the
.code generate
function:

.cblk
  (defmacro gen (while-expr produce-expr)
    ^(generate (lambda () ,while-expr) (lambda () ,produce-expr)))
.cble

Note 2:
.code gen
can be considered as an acronym for Generate, testing Expression
before Next item, whereas
.code gun
stands for Generate Until Null.

.TP* Example:

.cblk
  @(do
    ;; Make a lazy list of integers up to 1000
    ;; access and print the first three.
    (let* ((counter 0)
           (list (gen (< counter 1000) (inc counter))))
      (format t "~s ~s ~s\en" (pop list) (pop list) (pop list))))

  Output:
  1 2 3
.cble

.coNP Functions @ range and @ range*
.synb
.mets (range <> [[[ from ] << to ] << step ])
.mets (range* <> [[[ from ] << to ] << step ])
.syne
.desc
The
.code range
and
.code range*
functions generate a lazy sequence of integers, with a
fixed step between successive values.

The difference between
.code range
and
.code range*
is that
.code range*
excludes the endpoint.
For instance
.code (range 0 3)
generates the list
.codn (0 1 2 3) ,
whereas
.code (range* 0 3)
generates
.codn (0 1 2) .

All arguments are optional. If the
.meta step
argument is omitted, then it defaults
to
.codn 1 :
each value in the sequence is greater than the previous one by
.codn 1 .
Positive or negative step sizes are allowed. There is no check for a step size
of zero, or for a step direction which cannot meet the endpoint.

The
.meta to
argument specifies the endpoint value, which, if it occurs in the
sequence, is excluded from it by the
.code range*
function, but included by the range
function. If
.meta to
is missing, or specified as
.codn nil ,
then there is no endpoint,
and the sequence which is generated is infinite, regardless of
.metn step .

If
.meta from
is omitted, then the sequence begins at zero, otherwise
.meta from
must be an integer which specifies the initial value.

The sequence stops if it reaches the endpoint value (which is included in the
case of
.codn range ,
and excluded in the case of
.codn range *).
However, a sequence with a stepsize greater than
.code 1
or less than
.code -1
might step over the endpoint value, and
therefore never attain it. In this situation, the sequence also stops, and the
excess value which surpasses the endpoint is excluded from the sequence.

.coNP Function @ perm
.synb
.mets (perm < seq <> [ len ])
.syne
.desc
The
.code rperm
function returns a lazy list which consists of all
length
.meta len
permutations of formed by items taken from
.metn seq .
The permutations do not use any element of
.meta seq
more than once.

Argument
.metn len ,
if present, must be a positive integer, and
.meta seq
must be a sequence.

If
.meta len
is not present, then its value defaults to the length of
.metn seq :
the list of the full permutations of the entire sequence is returned.

The permutations in the returned list are sequences of the same kind as
.codn seq .

If
.meta len
is zero, then a list containing one permutation is returned, and that
permutations is of zero length.

If
.meta len
exceeds the length of
.metn seq ,
then an empty list is returned,
since it is impossible to make a single non-repeating permutation that
requires more items than are available.

The permutations are lexicographically ordered.

.coNP Function @ rperm
.synb
.mets (rperm < seq << len )
.syne
.desc
The
.code rperm
function returns a lazy list which consists of all the repeating
permutations of length
.meta len
formed by items taken from
.metn seq .
"Repeating" means that the items from
.meta seq
can appear more than
once in the permutations.

The permutations which are returned are sequences of the same kind as
.metn seq .

Argument
.meta len
must be a nonnegative integer, and
.meta seq
must be a sequence.

If
.meta len
is zero, then a single permutation is returned, of zero length.
This is true regardless of whether
.meta seq
is itself empty.

If
.meta seq
is empty and
.meta len
is greater than zero, then no permutations are
returned, since permutations of a positive length require items, and the
sequence has no items. Thus there exist no such permutations.

The first permutation consists of
.meta le
repetitions of the first element of
.metn seq .
The next repetition, if there is one, differs from the first
repetition in that its last element is the second element of
.metn seq .
That is to say, the permutations are lexicographically ordered.

.TP* Examples:

.cblk
  (rperm "01" 4) -> ("000" "001" "010" "011"
                     "100" "101" "110" "111")

  (rperm #(1) 3) -> (#(1 1 1))

  (rperm '(0 1 2) 2) -> ((0 0) (0 1) (0 2) (1 0)
                         (1 1) (1 2) (2 0) (2 1) (2 2))
.cble

.coNP Function @ comb
.synb
.mets (comb < seq << len )
.syne
.desc
The
.code comb
function returns a lazy list which consists of all
length
.meta len
non-repeating combinations formed by taking items taken from
.metn seq .
"Non-repeating combinations" means that the combinations do not use any
element of
.meta seq
more than once. If
.meta seq
contains no duplicates, then
the combinations contain no duplicates.

Argument
.meta len
must be a nonnegative integer, and
.meta seq
must be a sequence or a hash table.

The combinations in the returned list are objects of the same kind as
.metn seq .

If
.meta len
is zero, then a list containing one combination is returned, and that
permutations is of zero length.

If
.meta len
exceeds the number of elements in
.metn seq ,
then an empty list is returned, since it is impossible to make a single
non-repeating combination that requires more items than are available.

If
.meta seq
is a sequence, the returned combinations are lexicographically ordered.
This requirement is not applicable when
.meta seq
is a hash table.

.coNP Function @ rcomb
.synb
.mets (rcomb < seq << len )
.syne
.desc
The
.code comb
function returns a lazy list which consists of all
length
.meta len
repeating combinations formed by taking items taken from
.metn seq .
"Repeating combinations" means that the combinations can use
an element of
.meta seq
more than once.

Argument
.meta len
must be a nonnegative integer, and
.meta seq
must be a sequence.

The combinations in the returned list are sequences of the same kind as
.metn seq .

If
.meta len
is zero, then a list containing one combination is returned, and that
permutations is of zero length. This is true even if
.meta seq
is empty.

If
.meta seq
is empty, and
.meta len
is nonzero, then an empty list is returned.

The combinations are lexicographically ordered.

.SS* Characters and Strings
.coNP Function @ mkstring
.synb
.mets (mkstring < length << char )
.syne
.desc
The
.code mkstring
function constructs a string object of a length specified
by the
.meta length
parameter.  Every position in the string is initialized
with
.metn char ,
which must be a character value.

.coNP Function @ copy-str
.synb
.mets (copy-str << string )
.syne
.desc
The
.code copy-str
function constructs a new string whose contents are identical
to
.metn string .

.coNP Function @ upcase-str
.synb
.mets (upcase-str << string )
.syne
.desc
The
.code upcase-str
function produces a copy of
.meta string
such that all lower-case
characters of the English alphabet are mapped to their upper case counterparts.

.coNP Function @ downcase-str
.synb
.mets (downcase-str << string )
.syne
.desc
The
.code downcase-str
function produces a copy of
.meta string
such that
all upper case characters of the English alphabet are mapped to their
lower case counterparts.

.coNP Function @ string-extend
.synb
.mets (string-extend < string << tail )
.syne
.desc
The
.code string-extend
function destructively increases the length of
.metn string ,
which must be an ordinary dynamic string.  It is an error to invoke this
function on a literal string or a lazy string.

The
.meta tail
argument can be a character, string or integer. If it is a string or
character, it specifies material which is to be added to the end of the string:
either a single character or a sequence of characters. If it is an integer, it
specifies the number of characters to be added to the string.

If
.meta tail
is an integer, the newly added characters have indeterminate contents.
The string appears to be the original one because of an internal terminating
null character remains in place, but the characters beyond the terminating zero
are indeterminate.

.coNP Function @ stringp
.synb
.mets (stringp << obj )
.syne
.desc
The
.code stringp
function returns t if
.meta obj
is one of the several
kinds of strings. Otherwise it returns
.codn nil .

.coNP Function @ length-str
.synb
.mets (length-str << string )
.syne
.desc
The
.code length-str
function returns the length
.meta string
in characters.  The argument must be a string.

.coNP Function @ search-str
.synb
.mets (search-str < haystack < needle >> [ start <> [ from-end ]])
.syne
.desc
The
.code search-str
function finds an occurrence of the string
.meta needle
inside
the
.meta haystack
string and returns its position. If no such occurrence exists,
it returns
.codn nil .

If a
.meta start
argument is not specified, it defaults to zero. If it is
a non-negative integer, it specifies the starting character position for
the search.  Negative values of
.meta start
indicate positions from the end of the
string, such that
.code -1
is the last character of the string.

If the
.meta from-end
argument is specified and is not
.codn nil ,
it means
that the search is conducted right-to-left. If multiple matches are possible,
it will find the rightmost one rather than the leftmost one.

.coNP Function @ search-str-tree
.synb
.mets (search-str-tree < haystack < tree >> [ start <> [ from-end ]])
.syne
.desc
The
.code search-str-tree
function is similar to
.codn search-str ,
except that instead of
searching
.meta haystack
for the occurrence of a single needle string, it searches
for the occurrence of numerous strings at the same time.  These search strings
are specified, via the
.meta tree
argument, as an arbitrarily structured tree whose
leaves are strings.

The function finds the earliest possible match, in the given search direction,
from among all of the needle strings.

If
.meta tree
is a single string, the semantics is equivalent to
.codn search-str .

.coNP Function @ match-str
.synb
.mets (match-str < bigstring < littlestring <> [ start ])
.syne
.desc
Without the
.meta start
argument, the
.code match-str
function determines whether
.meta littlestring
is a prefix of
.metn bigstring ,
returning a
.code t
or
.code nil
indication.

If the
.meta start
argument is specified, and is a non-negative integer, then the
function tests whether
.meta littlestring
matches a prefix of that portion of
.meta bigstring
which starts at the given position.

If the
.meta start
argument is a negative integer, then
.code match-str
determines
whether
.meta littlestring
is a suffix of
.metn bigstring ,
ending on that position
of bigstring, where
.code -1
denotes the last character of
.metn bigstring ,
.code -2
the second last one and so on.

If
.meta start
is
.codn -1 ,
then this corresponds to testing whether
.meta littlestring
is a suffix of
.metn bigstring .

.coNP Function @ match-str-tree
.synb
.mets (match-str-tree < bigstring < tree <> [ start ])
.syne
.desc
The
.code match-str-tree
function is a generalization of match-str which matches
multiple test strings against
.meta bigstring
at the same time. The value
reported is the longest match from among any of the strings.

The strings are specified as an arbitrarily shaped tree structure which has
strings at the leaves.

If
.meta tree
is a single string atom, then the function behaves
exactly like match-str.

.coNP Function @ sub-str
.synb
.mets (sub-str < string >> [ from <> [ to ]])
.syne
.desc
The
.code sub-str
function is like the more generic function
.codn sub ,
except that it
operates only on strings.  For a description of the arguments and semantics,
refer to the
.code sub
function.

.coNP Function @ replace-str
.synb
.mets (replace-str < string < item-sequence >> [ from <> [ to ]])
.syne
.desc
The
.code replace-str
function is like the
.code replace
function, except that the first
argument must be a string.

For a description of the arguments and semantics, refer to the
.code replace
function.

.coNP Function @ cat-str
.synb
.mets (cat-str < string-list <> [ sep-string ])
.syne
.desc
The
.code cat-str
function catenates a list of strings given by
.meta string-list
into a
single string.  The optional
.meta sep-string
argument specifies a separator string
which is interposed between the catenated strings.

.coNP Function @ split-str
.synb
.mets (split-str < string << sep )
.syne
.desc
The
.code split-str
function breaks the
.meta string
into pieces, returning a list
thereof. The
.meta sep
argument must be either a string or a regular expression.
It specifies the separator character sequence within
.metn string .

All non-overlapping matches for
.meta sep
within
.meta string
are identified in left
to right order, and are removed from
.metn string .
The string is broken into pieces
according to the gaps left behind by the removed separators, and a list
of the remaining pieces is returned.

If
.meta sep
is the empty string, then the separator pieces removed from the
string are considered to be the empty strings between its
characters. In this case, if
.meta string
is of length one or zero, then it is considered to have no such pieces, and a
list of one element is returned containing the original string.

If a match for
.meta sep
is not found in the string at all, then the string is not
split at all: a list of one element is returned containing the original
string.

If
.meta sep
matches the entire string, then a list of two empty strings is
returned, except in the case that the original string is empty, in which case a
list of one element is returned, containing the empty string.

Whenever two adjacent matches for
.meta sep
occur, they are considered separate
cuts with an empty piece between them.

This operation is nondestructive:
.meta string
is not modified in any way.

Note: To split a string into pieces of length one such that an empty string
produces
.code nil
rather than
.codn ("") ,
use the
.cblk
.meti (tok-str < string #/./)
.cble
pattern.

.coNP Function @ split-str-set
.synb
.mets (split-str-set < string << set )
.syne
.desc
The
.code split-str-set
function breaks the
.meta string
into pieces, returning a list
thereof. The
.meta set
argument must be a string. It specifies a set of
characters.  All occurrences of any of these characters within
.meta string
are
identified, and are removed from
.metn string .
The string is broken into pieces
according to the gaps left behind by the removed separators.

Adjacent occurrences of characters from
.meta set
within
.meta string
are considered to
be separate gaps which come between empty strings.

This operation is nondestructive:
.meta string
is not modified in any way.

.coNP Functions @ tok-str and @ tok-where
.synb
.mets (tok-str < string < regex <> [ keep-between ])
.mets (tok-where < string << regex )
.syne
.desc
The
.code tok-str
function searches
.meta string
for tokens, which are defined as
substrings of
.mta string
which match the regular expression
.meta regex
in the
longest possible way, and do not overlap. These tokens are extracted from the
string and returned as a list.

Whenever
.meta regex
matches an empty string, then an empty token is returned, and
the search for another token within
.meta string
resumes after advancing by one
character position. So for instance,
.code (tok-str "abc" #/a?/)
returns the
.cblk
("a" "" "" "").
.cble
After the token
.str "a"
is extracted from a non-empty match
for the regex, the regex is considered to match three more times: before the
.strn "b" ,
between
.str "b"
and
.strn "c" ,
and after the
.strn "c" .

If the
.meta keep-between
argument is specified, and is not
.codn nil ,
then the behavior
of
.code tok-str
changes in the following way. The pieces of
.meta string
which are
skipped by the search for tokens are included in the output. If no token is
found in
.metn string ,
then a list of one element is returned, containing
.metn string .
Generally, if N tokens are found, then the returned list consists of 2N + 1
elements. The first element of the list is the (possibly empty) substring which
had to be skipped to find the first token. Then the token follows. The next
element is the next skipped substring and so on. The last element is the
substring of
.meta string
between the last token and the end.

The
.code tok-where
function works similarly to
.codn tok-str ,
but instead of returning
the extracted tokens themselves, it returns a list of the character position
ranges within
.meta string
where matches for
.meta regex
occur. The ranges
are pairs of numbers, represented as cons cells, where the first number
of the pair gives the starting character position, and the second number
is one position past the end of the match.  If a match is empty, then the
two numbers are equal.

The tok-where function does not support the
.meta keep-between
parameter.

.coNP Function @ list-str
.synb
.mets (list-str << string )
.syne
.desc
The
.code list-str
function converts a string into a list of characters.

.coNP Function @ trim-str
.synb
.mets (trim-str << string )
.syne
.desc
The
.code trim-str
function produces a copy of
.meta string
from which leading and
trailing whitespace is removed. Whitespace consists of spaces, tabs,
carriage returns, linefeeds, vertical tabs and form feeds.

.coNP Function @ chrp
.synb
.mets (chrp << obj )
.syne
.desc
Returns
.code t
if
.meta obj
is a character, otherwise nil.

.coNP Function @ chr-isalnum
.synb
.mets (chr-isalnum << char )
.syne
.desc
Returns
.code t
if
.meta char
is an alpha-numeric character, otherwise nil. Alpha-numeric
means one of the upper or lower case letters of the English alphabet found in
ASCII, or an ASCII digit. This function is not affected by locale.

.coNP Function @ chr-isalpha
.synb
.mets (chr-isalpha << char )
.syne
.desc
Returns
.code t
if
.meta char
is an alphabetic character, otherwise
.codn nil .
Alphabetic
means one of the upper or lower case letters of the English alphabet found in
ASCII. This function is not affected by locale.

.coNP Function @ chr-isascii
.synb
.mets (chr-isalpha << char )
.syne
.desc
This function returns
.code t
if the code of character
.meta char
is in the range 0 to 127 inclusive. For characters outside of this range, it
returns
.codn nil .

.coNP Function @ chr-iscntrl
.synb
.mets (chr-iscntrl << char )
.syne
.desc
This function returns
.code t
if the character
.meta char
is a character whose code
ranges from 0 to 31, or is 127. In other words, any non-printable ASCII
character. For other characters, it returns
.codn nil .

.coNP Function @ chr-isdigit
.synb
.mets (chr-isdigit << char )
.syne
.desc
This function returns
.code t
if the character
.meta char
is is an ASCII digit.
Otherwise, it returns
.codn nil .

.coNP Function @ chr-isgraph
.synb
.mets (chr-isgraph << char )
.syne
.desc
This function returns
.code t
if
.meta char
is a non-space printable ASCII character.
It returns nil if it is a space or control character.

It also returns nil for non-ASCII characters: Unicode characters with a code
above 127.

.coNP Function @ chr-islower
.synb
.mets (chr-islower << char )
.syne
.desc
This function returns
.code t
if
.meta char
is an ASCII lower case letter. Otherwise it returns
.codn nil .

.coNP Function @ chr-isprint
.synb
.mets (chr-isprint << char )
.syne
.desc
This function returns
.code t
if
.meta char
is an ASCII character which is not a
control character.  It also returns
.code nil
for all non-ASCII characters: Unicode
characters with a code above 127.

.coNP Function @ chr-ispunct
.synb
.mets (chr-ispunct << char )
.syne
.desc
This function returns
.code t
if
.meta char
is an ASCII character which is not a
control character.  It also returns nil for all non-ASCII characters: Unicode
characters with a code above 127.

.coNP Function @ chr-isspace
.synb
.mets (chr-isspace << char )
.syne
.desc
This function returns
.code t
if
.meta char
is an ASCII whitespace character: any of the
characters in the set
.codn #\espace ,
.codn #\etab ,
.codn #\elinefeed ,
.codn #\enewline ,
.codn #\ereturn ,
.code #\evtab
and
.codn #\epage .
For all other characters, it returns
.codn nil .

.coNP Function @ chr-isblank
.synb
.mets (chr-isblank << char )
.syne
.desc
This function returns
.code t
if
.meta char
is a space or tab: the character
.code #\espace
or
.codn #\etab .
For all other characters, it returns
.codn nil .

.coNP Function @ chr-isunisp
.synb
.mets (chr-isunisp << char )
.syne
.desc
This function returns
.code t
if
.meta char
is a Unicode whitespace character. This the case for
all the characters for which
.code chr-isspace
returns
.codn t.
It also returns
.code t
for these additional characters:
.codn #\exa0 ,
.codn #\ex1680 ,
.codn #\ex180e ,
.codn #\ex2000 ,
.codn #\ex2001 ,
.codn #\ex2002 ,
.codn #\ex2003 ,
.codn #\ex2004 ,
.codn #\ex2005 ,
.codn #\ex2006 ,
.codn #\ex2007 ,
.codn #\ex2008 ,
.codn #\ex2009 ,
.codn #\ex200a ,
.codn #\ex2028 ,
.codn #\ex2029 ,
.codn #\ex205f ,
and
.codn #\ex3000 .
For all other characters, it returns
.codn nil .

.coNP Function @ chr-isupper
.synb
.mets (chr-isupper < char )
.syne
.desc
This function returns
.code t
if
.meta char
is an ASCII upper case letter. Otherwise it returns
.codn nil .

.coNP Function @ chr-isxdigit
.synb
.mets (chr-isxdigit << char )
.syne
.desc
This function returns
.code t
if
.meta char
is a hexadecimal digit. One of the ASCII
letters
.code A
through
.codn F ,
or their lower-case equivalents, or an ASCII digit
.code 0
through
.codn 9 .

.coNP Function @ chr-toupper
.synb
.mets (chr-toupper << char )
.syne
.desc
If character
.meta char
is a lower case ASCII letter character, this function
returns the upper case equivalent character. If it is some other
character, then it just returns
.metn char .

.coNP Function @ chr-tolower
.synb
.mets (chr-tolower << char )
.syne
.desc
If character
.meta char
is an upper case ASCII letter character, this function
returns the lower case equivalent character. If it is some other
character, then it just returns
.metn char .

.coNP Functions @ num-chr and @ chr-num
.synb
.mets (num-chr << char )
.mets (chr-num << num )
.syne
.desc
The argument
.meta char
must be a character. The
.code num-chr
function returns that
character's Unicode code point value as an integer.

The argument
.meta num
must be a fixnum integer in the range
.code 0
to
.codn #\ex10FFFF .
The argument is taken to be a Unicode code point value and the
corresponding character object is returned.

.coNP Function @ chr-str
.synb
.mets (chr-str < str << idx )
.syne
.desc
The
.code chr-str
function performs random access on string
.meta str
to retrieve
the character whose position is given by integer
.metn idx ,
which must
be within range of the string.

The index value 0 corresponds to the first (leftmost) character of the string
and so non-negative values up to one less than the length are possible.

Negative index values are also allowed, such that -1 corresponds to the
last (rightmost) character of the string, and so negative values down to
the additive inverse of the string length are possible.

An empty string cannot be indexed. A string of length one supports index 0 and
index -1. A string of length two is indexed left to right by the values 0 and
1, and from right to left by -1 and -2.

.TP* Notes:

Direct use of
.code chr-str
is equivalent to the DWIM bracket notation except
that
.code str
must be a string. The following relation holds:

.cblk
  (chr-str s i) --> [s i]
.cble

since
.codn [s i] <--> (ref s i) ,
this also holds:

.cblk
  (chr-str s i) --> (ref s i)
.cble

.coNP Function @ chr-str-set
.synb
.mets (chr-str-set < str < idx << char )
.syne
.desc
The
.code chr-str
function performs random access on string
.meta str
to overwrite
the character whose position is given by integer
.metn idx ,
which must
be within range of the string. The character at
.meta idx
is overwritten
with character
.metn char .

The
.meta idx
argument works exactly as in
.codn chr-str .

The
.meta str
argument must be a modifiable string.

.TP* Notes:

Direct use of
.code chr-str
is equivalent to the DWIM bracket notation except
that
.meta str
must be a string. The following relation holds:

.cblk
  (chr-str-set s i c) --> (set [s i] c)
.cble

since
.codn (set [s i] c) <--> (refset s i c) ,
this also holds:

.cblk
  (chr-str s i) --> (refset s i c)
.cble

.coNP Function @ span-str
.synb
.mets (span-str < str << set )
.syne
.desc
The
.code span-str
function determines the longest prefix of string
.meta str
which
consists only of the characters in string
.metn set ,
in any combination.

.coNP Function @ compl-span-str
.synb
.mets (compl-span-str < str << set )
.syne
.desc
The
.code compl-span-str
function determines the longest prefix of string
.meta str
which
consists only of the characters which do not appear in
.metn set ,
in any combination.

.coNP Function @ break-str
.synb
.mets (break-str < str << set )
.syne
.desc
The
.code break-str
function returns an integer which represents the position of the
first character in string
.meta str
which appears in string
.metn set .

If there is no such character, then
.code nil
is returned.

.SS* Lazy Strings
Lazy strings are objects that were developed for the \*(TX pattern matching
language, and are exposed via \*(TL. Lazy strings behave much like strings,
and can be substituted for strings. However, unlike regular strings, which
exist in their entirety, first to last character, from the moment they are
created, lazy strings do not exist all at once, but are created on demand.  If
character at index N of a lazy string is accessed, then characters 0 through N
of that string are forced into existence. However, characters at indices
beyond N need not necessarily exist.

A lazy string dynamically grows by acquiring new text from a list of strings
which is attached to that lazy string object.  When the lazy string is accessed
beyond the end of its hitherto materialized prefix, it takes enough strings
from the list in order to materialize the index. If the list doesn't have
enough material, then the access fails, just like an access beyond the end of a
regular string.  A lazy string always takes whole strings from the attached
list.

Lazy string growth is achieved via the
.code lazy-str-force-upto
function which
forces a string to exist up to a given character position. This function is
used internally to handle various situations.

The
.code lazy-str-force
function forces the entire string to materialize.  If the
string is connected to an infinite lazy list, this will exhaust all memory.

Lazy strings are specially recognized in many of the regular string functions,
which do the right thing with lazy strings. For instance when
.code sub-str
is invoked on a lazy string, a special version of the
.code sub-str
logic is
used which handles various lazy string cases, and can potentially return
another lazy string. Taking a
.code sub-str
of a lazy string from a given character position
to the end does not force the entire lazy string to exist,
and in fact the operation will work on a lazy string that is infinite.

Furthermore, special lazy string functions are provided which allow programs to
be written carefully to take better advantage of lazy strings. What carefully
means is code that avoids unnecessarily forcing the lazy string.  For instance,
in many situations it is necessary to obtain the length of a string, only to
test it for equality or inequality with some number. But it is not necessary to
compute the length of a string in order to know that it is greater than some
value.

.coNP Function @ lazy-str
.synb
.mets (lazy-str < string-list >> [ terminator <> [ limit-count ]])
.syne
.desc
The
.code lazy-str
function constructs a lazy string which draws material from
.meta string-list
which is a list of strings.

If the optional
.meta terminator
argument is given, then it specifies a string
which is appended to every string from
.metn string-list ,
before that string is
incorporated into the lazy string. If
.meta terminator
is not given,
then it defaults to the string
.strn "\en" ,
and so the strings from
.meta string-list
are effectively treated as lines which get terminated by newlines
as they accumulate into the growing prefix of the lazy string.
To avoid the use of a terminator string, a null string
.meta terminator
argument
must be explicitly passed. In that case, the lazy string grows simply
by catenating elements from
.metn string-list .

If the
.meta limit-count
argument is specified, it must be a positive integer.  It
expresses a maximum limit on how many elements will be consumed from
.meta string-list
in order to feed the lazy string. Once that many elements are
drawn, the string ends, even if the list has not been exhausted.

.coNP Function @ lazy-stringp
.synb
.mets (lazy-stringp << obj )
.syne
.desc
The
.code lazy-stringp
function returns
.code t
if
.meta obj
is a lazy
string. Otherwise it returns
.codn nil .

.coNP Function @ lazy-str-force-upto
.synb
.mets (lazy-str-force-upto < lazy-str << index )
.syne
.desc
The
.code lazy-str-force-upto
function tries to instantiate the lazy string such that
the position given by
.meta index
materializes. The
.meta index
is a character
position, exactly as used in the
.code chr-str
function.

Some positions beyond
.meta index
may also materialize, as a side effect.

If the string is already materialized through to at least
.metn index ,
or if it is
possible to materialize the string that far, then the value
.code t
is returned to indicate success.

If there is insufficient material to force the lazy string through to the
.meta index
position, then nil is returned.

It is an error if the
.meta lazy-str
argument isn't a lazy string.

.coNP Function @ lazy-str-force
.synb
.mets (lazy-str-force << lazy-str )
.syne
.desc
The
.meta lazy-str
argument must be a lazy string. The lazy string is forced
to fully materialize.

The return value is an ordinary, non-lazy string equivalent to the fully
materialized lazy string.

.coNP Function @ lazy-str-get-trailing-list
.synb
.mets (lazy-str-get-trailing-list < string << index )
.syne
.desc
The
.code lazy-str-get-trailing-list
function can be considered, in some way, an inverse operation to
the production of the lazy string from its associated list.

First,
.meta string
is forced up through the position
.metn index .
That is the only extent to which
.meta string
is modified by this function.

Next, the suffix of the materialized part of the lazy string starting at
position
.metn index ,
is split into pieces on occurrences of the
terminator character (which had been given as the
.meta terminator
argument in the
.code lazy-str
constructor, and defaults to newline).   If the
.meta index
position is beyond the part of the string which can be materialized
(in adherence with the lazy string's
.meta limit-count
constructor parameter), then the list of pieces is considered
to be empty.

Finally, a list is returned consisting of the pieces produced by the split,
to which is appended the remaining list of the string which has not yet been
forced to materialize.

.coNP Functions @, length-str-> @, length-str->= @ length-str-< and @ length-str-<=
.synb
.mets (length-str-> < string << len )
.mets (length-str->= < string << len )
.mets (length-str-< < string << len )
.mets (length-str-<= < string << len )
.syne
.desc
These functions compare the lengths of two strings. The following
equivalences hold, as far as the resulting value is concerned:

.cblk
  (length-str-> s l) <--> (> (length-str s) l)
  (length-str->= s l) <--> (>= (length-str s) l)
  (length-str-< s l) <--> (< (length-str s) l)
  (length-str-<= s l) <--> (<= (length-str s) l)
.cble

The difference between the functions and the equivalent forms is that if the
string is lazy, the
.code length-str
function will fully force it in order to
calculate and return its length.

These functions only force a string up to position
.metn len ,
so they are not
only more efficient, but on infinitely long lazy strings they are usable.

.code length-str
cannot compute the length of a lazy string with an unbounded
length; it will exhaust all memory trying to force the string.

These functions can be used to test such as string whether it is longer
or shorter than a given length, without forcing the string beyond
that length.

.coNP Function @ cmp-str
.synb
.mets (cmp-str < left-string << right-string )
.syne
.desc
The
.code cmp-str
function returns a negative integer if
.meta left-string
is lexicographically prior to
.metn right-string ,
and a positive integer
if the reverse situation is the case. Otherwise the strings are equal
and zero is returned.

If either or both of the strings are lazy, then they are only forced to the
minimum extent necessary for the function to reach a conclusion and return the
appropriate value, since there is no need to look beyond the first character
position in which they differ.

The lexicographic ordering is naive, based on the character code point
values in Unicode taken as integers, without regard for locale-specific
collation orders.

.coNP Functions @, str= @, str< @, str> @ str>= and @ str<=
.synb
.mets (str= < left-string << right-string )
.mets (str< < left-string << right-string )
.mets (str> < left-string << right-string )
.mets (str<= < left-string << right-string )
.mets (str>= < left-string << right-string )
.syne
.desc
These functions compare
.meta left-string
and
.meta right-string
lexicographically,
as if by the
.code cmp-str
function.

The
.code str=
function returns
.code t
if the two strings are exactly the same, character
for character, otherwise it returns
.codn nil .

The
.code str<
function returns
.code t
if
.meta left-string
is lexicographically before
.metn right-string ,
otherwise nil.

The
.code str>
function returns
.code t
if
.meta left-string
is lexicographically after
.metn right-string ,
otherwise
.codn nil .

The
.code str<
function returns
.code t
if
.meta left-string
is lexicographically before
.metn right-string ,
or if they are exactly the same, otherwise
.codn nil .

The
.code str<
function returns
.code t
if
.meta left-string
is lexicographically after
.metn right-string ,
or if they are exactly the same, otherwise
.codn nil .

.coNP Function @ string-lt
.synb
.mets (string-lt < left-str << right-str )
.syne
.desc
The
.code string-lt
is a deprecated alias for
.codn str< .

.SS* Vectors
.coNP Function @ vector
.synb
.mets (vector < length <> [ initval ])
.syne
.desc
The
.code vector
function creates and returns a vector object of the specified
length.  The elements of the vector are initialized to
.metn initval ,
or to nil if
.meta initval
is omitted.

.coNP Function @ vec
.synb
.mets (vec << arg *)
.syne
.desc
The
.code vec
function creates a vector out of its arguments.

.coNP Function @ vectorp
.synb
.mets (vectorp << obj )
.syne
.desc
The
.code vectorp
function returns t if
.meta obj
is a vector, otherwise it returns
.codn nil .

.coNP Function @ vec-set-length
.synb
.mets (vec-set-length < vec << len )
.syne
.desc
The
.code vec-set-length
modifies the length of
.metn vec ,
making it longer or
shorter. If the vector is made longer, then the newly added elements
are initialized to nil. The
.meta len
argument must be nonnegative.

The return value is
.metn vec .

.coNP Function @ vecref
.synb
.mets (vecref < vec << idx )
.syne
.desc
The
.code vecref
function performs indexing into a vector. It retrieves
an element of
.meta vec
at position
.metn idx ,
counted from zero.
The
.meta idx
value must range from 0 to one less than the
length of the vector. The specified element is returned.

.coNP Function @ vec-push
.synb
.mets (vec-push < vec << elem )
.syne
.desc
The
.code vec-push
function extends the length of a vector
.meta vec
by one element, and
sets the new element to the value
.metn elem .

The previous length of the vector (which is also the position of
.metn elem )
is returned.

This function performs similarly to the generic function
.codn ref ,
except that the
first argument must be a vector.

.coNP Function @ length-vec
.synb
.mets (length-vec << vec )
.syne
.desc
The
.code length-vec
function returns the length of vector
.metn vec .
It performs
similarly to the generic
.code length
function, except that the argument must
be a vector.

.coNP Function @ size-vec
.synb
.mets (size-vec << vec )
.syne
.desc
The
.code size-vec
function returns the number of elements for which storage
is reserved in the vector
.metn vec .

.TP* Notes:

The
.code length
of the vector can be extended up to this size without any memory
allocation operations having to be performed.

.coNP Function @ vector-list
.synb
.mets (vector-list << list )
.syne
.desc
This function returns a vector which contains all of the same elements
and in the same order as list
.metn list .

.coNP Function @ list-vector
.synb
.mets (list-vector << vec )
.syne
.desc
The
.code list-vector
function returns a list of the elements of vector
.metn vec .

.coNP Function @ copy-vec
.synb
.mets (copy-vec << vec )
.syne
.desc
The
.code copy-vec
function returns a new vector object of the same length
as
.meta vec
and containing the same elements in the same order.

.coNP Function @ sub-vec
.synb
.mets (sub-vec < vec >> [ from <> [ to ]])
.syne
.desc
The
.code sub-vec
function is like the more generic function
.codn sub ,
except that it
operates only on vectors.

For a description of the arguments and semantics, refer to the
.code sub
function.

.coNP Function @ replace-vec
.synb
.mets (replace-vec < vec < item-sequence >> [ from <> [ to ]])
.syne
.desc
The
.code replace-vec
is like the
.code replace
function, except that the first argument
must be a vector.

For a description of the arguments and semantics, refer to the
.code replace
function.

.coNP Function cat-vec
.synb
.mets (cat-vec << vec-list )
.syne
.desc
The
.meta vec-list
argument is a list of vectors. The
.code cat-vec
function
produces a catenation of the vectors listed in
.metn vec-list .
It returns
a single large vector formed by catenating those vectors together in
order.

.SS* Sequence Manipulation
.coNP Function @ seqp
.synb
.mets (seqp << object )
.syne
.desc
The function
.code seqp
returns
.code t
if
.meta object
is a sequence, otherwise
.codn nil .

A sequence is defined as a list, vector or string. The object
.code nil
denotes
the empty list and so is a sequence.

.coNP Function @ length
.synb
.mets (length << sequence )
.syne
.desc
The
.code length
function returns the number of items in
.metn sequence ,
and
returns it.
.meta sequence
may be a hash, in which case (hash-count
.metn sequence )
is returned.

.coNP Function @ empty
.synb
.mets (empty << sequence )
.syne
.desc
Returns
.code t
if
.cblk
.meti (length << sequence )
.cble
is zero, otherwise
.codn nil .

.coNP Function @ copy
.synb
.mets (copy << object )
.syne
.desc
The
.code copy
function duplicates objects of various supported types: sequences, hashes and random states. If
.meta object
is
.codn nil ,
it
returns
.codn nil .
If
.meta object
is a list, it returns
.cblk
.meti (copy-list << object ).
.cble
If
.meta object
is a string, it returns
.cblk
.meti (copy-str << object ).
.cble
If
.meta object
is a vector, it returns
.cblk
.meti (copy-vec << object ).
.cble
If
.meta object
is a hash, it returns
.cblk
.meti (copy-hash << object )
.cble
Lastly, if
.meta object
is a random state, it returns
.cblk
.meti (make-random-state << object ).
.cble

Except in the case when
.meta sequence
is
.codn nil ,
.code copy
returns a value that
is distinct from (not
.code eq
to)
.metn sequence .
This is different from
the behavior of
.cblk
.meti >> [ sequence 0..t]
.cblk
or
.cblk
.meti (sub < sequence 0 t)
.cble
which recognize
that they need not make a copy of
.metn sequence ,
and just return it.

Note however, that the elements of the returned sequence may be
eq to elements of the original sequence. In other words, copy is
a deeper copy than just duplicating the
.code sequence
value itself,
but it is not a deep copy.

.coNP Function @ sub
.synb
.mets (sub < sequence >> [ from <> [ to ]])
.syne
.desc
The
.code sub
function extracts a slice from input sequence
.metn sequence .
The slice is
a sequence of the same type as
.metn sequence .

If the
.meta from
argument is omitted, it defaults to
.codn 0 .
If the
.meta to
parameter is
omitted, it defaults to
.codn t .
Thus
.code (sub a)
means
.codn (sub a 0 t) .

The following equivalence holds between the
.code sub
function and
the DWIM-bracket syntax:

.cblk
  ;; from is not a list
  (sub seq from to) <--> [seq from..to]
.cble

The description of the
.code dwim
operator\(emin particular, the section
on Range Indexing\(emexplains the semantics of the range specification.

If the sequence is a list, the output sequence may share substructure
with the input sequence.

.coNP Function @ replace
.synb
.mets (replace < sequence < replacement-sequence >> [ from <> [ to ]])
.mets (replace < sequence < replacement-sequence << index-list )
.syne
.desc
The
.code replace
function has two invocation styles, distinguished by the
type of the third argument. If the third argument is a list, then it is deemed to be the
.meta index-list
parameter of the second form.
Otherwise, if the third argument is missing, or is not a list, then
it is deemed to be the
.meta from
argument of the first form.

The first form of the replace function replaces a contiguous subsequence of the
.meta sequence
with
.metn replacement-sequence .
The replaced subsequence may be empty,
in which case an insertion is performed. If
.meta replacement-sequence
is empty
(for example, the empty list
.codn nil ),
then a deletion is performed.

If the
.meta from
and
.meta to
arguments are omitted, their values default
to
.code 0
and
.code t
respectively.

The following equivalence holds between assignment to a place denoted by
DWIM bracket syntax and first form of the replace function:

.cblk
  (set seq (replace seq new fr to)) <--> (set [seq fr..to] new)
.cble

The description of the dwim operator\(emin particular, the section
on Range Indexing\(emexplains the semantics of the range specification.

This operation is destructive: it may work "in place" by modifying
the original sequence. The caller should retain the return value
and stop relying on the original input sequence.

The second form of the replace function replaces a subsequence of
elements from
.meta sequence
given by
.metn index-list ,
with their counterparts
from
.metn replacement-sequence .
This form of the replace function does not insert
or delete; it simply overwrites elements. If
.meta replacement-sequence
and
.meta idex-list
are of different lengths, then the shorter of the two determines
the maximum number of elements which are overwritten.
Furthermore, similar restrictions apply on
.meta index-list
as under the
select function. Namely, the replacement stops when an index value
in
.meta index-list
is encountered which is out of range for
.metn sequence .
furthermore, if
.meta sequence
is a list, then
.meta index-list
must
be monotonically increasing.

The following equivalence holds between assignment to a place denoted by
DWIM bracket syntax and second form of the
.code replace
function:

.cblk
  (set seq (replace seq new ix-list)) <--> (set [seq ix-list] new)
.cble

.coNP Function @ search
.synb
.mets (search < haystack < needle >> [ testfun <> [ keyfun ])
.syne
.desc
The
.code search
function determines whether the sequence
.meta needle
occurs as substring
within
.metn haystack ,
under the given comparison function
.meta testfun
and
key function
.metn keyfun .
If this is the case, then the zero-based position of
the leftmost occurrence of
.meta key
within
.meta haystack
is returned. Otherwise
.code nil
is returned to indicate that
.meta key
does not occur within
.metn haystack .
If
.meta key
is empty, then zero is always returned.

The arguments
.meta haystack
and
.meta needle
are sequences: lists, vectors
or strings, in any combination.

If
.meta needle
is not empty, then occurs at some position N within
.meta haystack
if
the first element of
.meta needle
matches the element at position N of
.metn haystack ,
the second element of
.meta needle
matches the element at position N+1 of
.meta haystack
and so forth, for all elements of
.metn needle .
A match between elements
is determined by passing each element through
.metn keyfun ,
and then comparing the resulting values using
.metn testfun .

If
.meta testfun
is supplied, it must be a function which can be
called with two arguments. If it is not supplied, it defaults to
.codn eql .

If
.meta keyfun
is supplied, it must be a function which can be called
with one argument. If it is not supplied, it defaults to
.codn identity .

.TP* Examples:

.cblk
  ;; fails because 3.0 doesn't match 3
  ;; under the default eql function
  [search #(1.0 3.0 4.0 7.0) '(3 4)] -> nil

  ;; occurrence found at position 1:
  ;; (3.0 4.0) matches (3 4) under =
  [search #(1.0 3.0 4.0 7.0) '(3 4) =] -> 1

  ;; "even odd odd odd even" pattern
  ;; matches at position 2
  [search #(1 1 2 3 5 7 8) '(2 1 1 1 2) : evenp] -> 2

  ;; Case insensitive string search
  [search "abcd" "CD" : chr-toupper] -> 2

  ;; Case insensitive string search
  ;; using vector of characters as key
  [search "abcd" #(#\eC #\eD) : chr-toupper] -> 2
.cble

.coNP Functions @ ref and @ refset
.synb
.mets (ref < seq << index )
.mets (refset < seq < index << new-value )
.syne
.desc
The
.code ref
and
.code refset
functions perform array-like indexing into sequences.
The
.code ref
function retrieves an element of
.metn seq ,
whereas
.code refset
overwrites an
element of
.meta seq
with a new value.

The
.meta index
argument is based from zero, and negative values are permitted,
with a special meaning as described in the Range Indexing section under the
description of the
.code dwim
operator.

The
.code refset
function returns the new value.

The following equivalences hold between
.code ref
and
.codn refset ,
and the DWIM bracket syntax:

.cblk
  (ref seq idx) <--> [seq idx]

  (refset seq idx new) <--> (set [seq idx] new)
.cble

The difference is that
.code ref
and
.code refset
are first class functions which
can be used in functional programming as higher order functions, whereas the
bracket notation is syntactic sugar, and
.code set
is an operator, not a function.
Therefore the brackets cannot replace all uses of
.code ref
and
.codn refset .

.coNP Function @ update
.synb
.mets (update < sequence-or-hash << function )
.syne
.desc
The
.code update
function replaces each elements in a sequence, or each value
in a hash table, with the value of
.meta function
applied to that element
or value.

The sequence or hash table is returned.

.coNP Function @ less
.synb
.mets (less < left-obj << right-obj  )
.mets (less < obj << obj *)
.syne
.desc
The
.code less
function, when called with two arguments, determines whether
.meta left-obj
compares less than
.meta right-obj
in a generic way which handles arguments of various types.

The argument syntax of
.code less
is generalized. It can accept one argument, in which case it unconditionally
returns
.code t
regardless of that argument's value. If more than two arguments are
given, then
.code less
generalizes in a way which can be described by the following equivalence
pattern, with the understanding that each argument expression
is evaluated exactly once:

.cblk
  (less a b c) <--> (and (less a b) (less b c))
  (less a b c d) <--> (and (less a b) (less b c) (less c d))
.cble

The
.code less
function is used as the default for the
.meta lessfun
argument of the functions
.code sort
and
.codn merge ,
as well as the
.meta testfun
argument of the
.code pos-min
and
.codn find-min .

The
.code less
function is capable of comparing numbers, characters, symbols, strings,
as well as lists and vectors of these.

If both arguments are the same object so that
.cblk
.meti (eq < left-obj << right-obj )
.cble
holds true, then the function returns
.code nil
regardless of the type of
.metn left-obj ,
even if the function doesn't handle comparing different instances
of that type. In other words, no object is less than itself, no matter
what it is.

If both arguments are numbers or characters, they are compared as if using the
.code < function.

If both arguments are strings, they are compared as if using the
.code string-lt
function.

If both arguments are symbols, then their names are compared in
their place, as if by the
.code string-lt
function.

If both arguments are conses, then they are compared as follows:
.RS
.IP 1.
The
.code less
function is recursively applied to the
.code car
fields of both arguments. If it yields true, then
.meta left-obj
is deemed to be less than
.metn right-obj .
.IP 2.
Otherwise, if the
.code car
fields are unequal under
the
.code equal
function,
.code less
returns
.codn nil.
.IP 3.
If the
.code car
fields are
.code equal
then
.code less
is recursively applied to the
.code cdr
fields of the arguments, and the result of that comparison is returned.
.RE

.IP
This logic performs a lexicographic comparison on ordinary lists such
that for instance
.code (1 1)
is less than
.code (1 1 1)
but not less than
.code (1 0)
or
.codn (1) .

Note that the empty
.code nil
list nil compared to a cons is handled by type-based precedence, described
below.

If the arguments are vectors, they are compared lexicographically, similar
to strings. Corresponding elements, starting with element 0, of the
vectors are compared until an index position is found where the vectors
differ. If this differing position is beyond the end of one of the two vectors,
then the shorter vector is considered to be lesser. Otherwise, the result
of
.code less
is the outcome of comparing those differing elements themselves
with
.codn less .

If the two arguments are of the above types, but of mutually different types,
then
.code less
resolves the situation based on the following precedence: numbers and
characters are less than strings, which are less than symbols,
which are less than conses, which are less than vectors.

Note that since
.code nil
is a symbol, it is ranked lower than a cons. This interpretation ensures
correct behavior when
.code nil
is regarded as an empty list, since the empty list is lexicographically prior to
a nonempty list.

Finally, if either of the arguments has a type other than the above discussed
types, the situation is an error.

.coNP Function @ greater
.synb
.mets (greater < left-obj << right-obj )
.mets (greater < obj << obj *)
.syne
.desc
The
.code
greater
function is equivalent to
.code less
with the arguments reversed. That is to say, the following
equivalences hold:

.cblk
  (greater a <--> (less a) <--> t
  (greater a b) <--> (less b a)
  (greater a b c ...) <--> (less ... c b a)
.cble

The
.code greater
function is used as the default for the
.meta testfun
argument of the
.code pos-max
and
.code find-max
functions.

.coNP Functions @ lequal and @ gequal
.synb
.mets (lequal < obj << obj *)
.mets (gequal < obj << obj *)
.syne
.desc
The functions
.code lequal
and
.code gequal
are similar to
.code less
and
.code greater
respectively, but differ in the following respect:
when called with two arguments which compare true under the
.code equal
function, the
.code lequal
and
.code gequal
functions return
.codn t .

When called with only one argument, both functions return
.code t
and both functions generalize to three or more arguments
in the same way as do
.code less
and
.codn greater .

.coNP Function @ sort
.synb
.mets (sort < sequence >> [ lessfun <> [ keyfun ]])
.syne
.desc
The
.code sort
function destructively sorts
.metn sequence ,
producing a sequence
which is sorted according to the
.meta lessfun
and
.meta keyfun
arguments.

The
.meta keyfun
argument specifies a function which is applied to elements
of the sequence to obtain the key values which are then compared
using the lessfun. If
.meta keyfun
is omitted, the identity function is used
by default: the sequence elements themselves are their own sort keys.

The
.meta lessfun
argument specifies the comparison function which determines
the sorting order. It must be a binary function which can be invoked
on pairs of keys as produced by the key function. It must
return a
.cod2 non- nil
value if the left argument is considered to be lesser
than the right argument. For instance, if the numeric function
.code <
is used
on numeric keys, it produces an ascending sorted order. If the function
.code >
is used, then a descending sort is produced. If
.meta lessfun
is omitted, then it defaults to the generic
.code less
function.

The
.code sort
function is stable for sequences which are lists. This means that the
original order of items which are considered identical is preserved.
For strings and vectors,
.code sort
is not stable.

.coNP Function @ sort-group
.synb
.mets (sort-group < sequence >> [ keyfun <> [ lessfun ]])
.syne
.desc
The
.code sort-group
function sorts
.meta sequence
according to the
.meta keyfun
and
.meta lessfun
arguments, and then breaks the resulting sequence into groups,
based on the equivalence of the elements under
.metn keyfun .

The following equivalence holds:

.cblk
  (sort-group sq lf kf) <--> (partition-by kf (sort (copy sq) kf lf))
.cble

Note the reversed order of
.meta keyfun
and
.meta lessfun
arguments between
.code sort
and
.codn sort-group .

.coNP Function @ uniq
.synb
.mets (uniq << sequence )
.syne
.desc
The
.code uniq
function returns a sequence of the same kind as
.metn sequence ,
but with
duplicates removed. Elements of
.meta sequence
are considered equal under
the
.code equal
function. The first occurrence of each element is retained,
and the subsequent duplicates of that element, of any, are suppressed,
such that the order of the elements is otherwise preserved.

The following equivalence holds between
.code uniq
and
.codn unique :

.cblk
  (uniq s) <--> [unique s : :equal-based]
.cble

That is,
.code uniq
is like
.code unique
with the default
.meta keyfun
argument (the
.code identity
function) and an
.codn equal -based
hash table.

.coNP Function @ unique
.synb
.mets (uniq < sequence >> [ keyfun <> { hash-arg }* ])
.syne
.desc
The
.code unique
function is a generalization of
.codn uniq .
It returns a sequence of the same kind as
.metn sequence ,
but with duplicates removed.

If neither
.meta keyfun
nor
.metn hash-arg -s
are specified, then elements of sequence are considered equal under the
.code eql
function. The first occurrence of each element is retained,
and the subsequent duplicates of that element, of any, are suppressed,
such that the order of the elements is otherwise preserved.

If
.meta keyfun
is specified, then that function is applied to each element,
and the resulting values are compared for equality.
In other words, the behavior is as if
.meta keyfun
were the
.code identity
function.

If one or more
.metn hash-arg -s
are present, these specify the arguments for the construction of
the internal hash table used by
.codn unique .
The arguments are like those of the
.code hash
function. In particular, the argument
.code :equal-based
causes
.code unique
to use
.code equal
equality.

.coNP Function @ tuples
.synb
.mets (tuples < length < sequence <> [ fill-value ])
.syne
.desc
The
.code tuples
function produces a lazy list which represents a reorganization
of the elements of
.meta sequence
into tuples of
.metn length ,
where
.meta length
must be a positive integer.

The length of the sequence might not be evenly divisible by the tuple length.
In this case, if a
.meta fill-value
argument is specified, then the last tuple
is padded with enough repetitions of
.meta fill-value
to make it have
.meta length
elements. If
.meta fill-value
is not specified, then the last tuple is left
shorter than
.metn length .

The output of the function is a list, but the tuples themselves are sequences
of the same kind as
.metn sequence .
If
.meta sequence
is any kind of list, they
are lists, and not lazy lists.

.TP* Examples:

.cblk
  (tuples 3 #(1 2 3 4 5 6 7 8) 0) -> (#(1 2 3) #(4 5 6) #(7 8 0))
  (tuples 3 "abc") -> ("abc")
  (tuples 3 "abcd") -> ("abc" "d")
  (tuples 3 "abcd" #\ez) -> ("abc" "dzz")
  (tuples 3 (list 1 2) #\ez) -> ((1 2 #\ez))
.cble

.coNP Function @ partition-by
.synb
.mets (partition-by < function << sequence )
.syne
.desc
If
.meta sequence
is empty, then
.code partition-by
returns an empty list,
and
.meta function
is never called.

Otherwise,
.code partition-by
returns a lazy list of partitions of the sequence
.metn sequence .
Partitions are consecutive, non-empty sub-strings of
.metn sequence ,
of the same kind as
.metn sequence .

The partitioning begins with the first element of
.meta sequence
being placed into a partition.

The subsequent partitioning is done according to
.metn function ,
which is applied
to each element of
.metn sequence .
Whenever, for the next element, the function
returns the same value as it returned for the previous element, the
element is placed into the same partition. Otherwise, the next element
is placed into, and begins, a new partition.

The return values of the calls to
.meta function
are compared using the
.code equal
function.

.TP* Examples:

.cblk
  [partition-by identity '(1 2 3 3 4 4 4 5)] -> ((1) (2) (3 3)
                                                 (4 4 4) (5))

  (partition-by (op = 3) #(1 2 3 4 5 6 7)) -> (#(1 2) #(3)
                                               #(4 5 6 7))
.cble

.coNP Function @ make-like
.synb
.mets (make-like < list << ref-sequence )
.syne
.desc
The
.meta list
argument must be a list.  If
.meta ref-sequence
is a sequence type,
then
.meta list
is converted to the same type of sequence and returned.
Otherwise the original
.meta list
is returned.

Note: the
.code make-like
function is a helper which supports the development of
unoptimized versions of a generic function that accepts any type of
sequence as input, and produces a sequence of the same type as output.
The implementation of such a function can internally accumulate a list, and
then convert the resulting list to the same type as an input value
by using
.codn make-like .

.coNP Function @ nullify
.synb
.mets (nullify << sequence )
.syne
.desc
The
.code nullify
function returns
.code nil
if
.meta sequence
is an empty sequence.
Otherwise it returns
.meta sequence
itself.

Note: the
.code nullify
function is a helper to support unoptimized generic
programming over sequences.  Thanks to the generic behavior of
.codn cdr ,
any sequence can be traversed using
.code cdr
functions, checking for the
.code nil
value as a terminator.  This, however, breaks for empty sequences which are not
lists, because they are not equal to
.codn nil :
to
.code car
and
.code cdr
they look like
a one-element sequence containing
.codn nil .
The
.code nullify
function reduces all empty
sequences to
.codn nil ,
thereby correcting the behavior of code which traverses
sequences using
.codn cdr ,
and tests for termination with
.codn nil .

.SS* Math Library
.coNP Functions @ + and @ -
.synb
.mets (+ << number *)
.mets (- < number << number *)
.mets (* << number *)
.syne
.desc
The
.codn + ,
.code -
and
.code *
functions perform addition, subtraction and multiplication,
respectively.  Additionally, the
.code -
function performs additive inverse.

The
.code +
function requires zero or more arguments. When called with no
arguments, it produces 0 (the identity element for addition), otherwise it
produces the sum over all of the arguments.

Similarly, the
.code *
function requires zero or more arguments. When called
with no arguments, it produces 1 (the identity element for multiplication).
Otherwise it produces the product of all the arguments.

The semantics of
.code -
changes from subtraction to additive inverse
when there is only one argument. The argument is treated as a subtrahend,
against an implicit minuend of zero. When there are two or more
argument, the first one is the minuend, and the remaining are subtrahends.

When there are three or more operands, these operations are performed as if by
binary operations, in a left-associative way. That is to say,
.code (+ a b c)
means
.codn (+ (+ a b) c) .
The sum of
.code a
and
.code b
is computed first, and then this is added to
.codn c .
Similarly
.code (- a b c)
means
.codn (- (- a b) c) .
First,
.code b
is subtracted from
.codn a ,
and then
.code c
is subtracted from that result.

The arithmetic inverse is performed as if it were subtraction from integer 0.
That is,
.code (- x)
means the same thing as
.codn (- 0 x) .

The operands of
.codn + ,
.code -
and
.code *
can be characters, integers (fixnum and bignum), and
floats, in nearly any combination.

If two operands have different types, then one of them is converted to the
type of the one with the higher rank, according to this ranking:
character < integer < float.  For instance if one operand is integer, and the
other float, the integer is converted to a float.

.TP* Restrictions:

Characters are not considered numbers, and participate in these operations in
limited ways. Subtraction can be used to computed the displacement between the
Unicode values of characters, and an integer displacement can be added to a
character, or subtracted from a character.  For instance
.codn (- #\e9 #\e0) is 9 .
The Unicode value of a character
.code C
can be found using
.codn (- C #\ex0) :
the displacement from the NUL character.

The rules can be stated as a set of restrictions:
.RS
.IP 1
Two characters may not be added together.
.IP 2
A character may not be subtracted from an integer (which also rules out
the possibility of computing the additive inverse of a character).
.IP 3
A character operand may not be opposite to a floating point operand
in any operation.
.IP 4
A character may not be an operand of multiplication.
.RE

.PP

.coNP Functions @, / @ trunc, @ mod and @ trunc-rem
.synb
.mets (/ <> [ dividend ] << divisor )
.mets (trunc < dividend << divisor )
.mets (mod < dividend << divisor )
.mets (trunc-rem < dividend << divisor )
.syne
.desc
The arguments to these functions are numbers. Characters are not permitted.

The
.code /
function performs floating-point division. Each operands is first
converted to floating-point type, if necessary. If
.meta dividend
is omitted,
then it is taken to be
.code 1.0
and the function calculates the reciprocal.

The
.code trunc
function performs a division of
.meta dividend
by
.meta divisor
whose result
is truncated to integer toward zero. If both operands are integers, then an
integer division is performed and the result is an integer. If either operand
is a floating point value,  a floating point division occurs, and the result is
truncated toward zero to a floating-point integral value.

The
.code mod
function performs a modulus operation. Firstly, the absolute value
of
.meta divisor
is taken to be a modulus. Then a residue of
.meta dividend
with respect to
.meta modulus
is calculated. The residue's sign follows
that of the sign of
.metn divisor .
That is, it is the smallest magnitude
(closest to zero) residue of
.meta dividend
with respect to the absolute
value of
.metn divisor ,
having the same sign as
.metn divisor .
If the operands are integer, the result is an integer. If either operand
is of type float, then the result is a float. The modulus operation is
then generalized into the floating point domain. For instance the expression
.code (mod 0.75 0.5)
yields a residue of 0.25 because 0.5 "goes into" 0.75 only
once, with a "remainder" of 0.25.

The
.code trunc-rem
function returns a list of two values: a
.meta quotient
and a
.metn remainder.
The
.meta quotient
is exactly the same value as what
.code trunc
would return for the same inputs.
The
.meta remainder
obeys the following identity:

.cblk
.mets (eql < remainder (- < dividend >> (* divisor << quotient )))
.cble

.coNP Functions @ wrap and @ wrap*
.synb
.mets (wrap < start < end << number )
.mets (wrap < start < end << number )
.syne
.desc
The
.code wrap
and
.code wrap*
functions reduce
.meta number
into the range specified by
.meta start
and
.metn end .

Under
.code wrap
the range is inclusive of the
.meta end
value, whereas under
.code wrap*
it is exclusive.

The following equivalence holds

.cblk
  (wrap a b c) <--> (wrap* a (succ b) c)
.cble

The expression
.code (wrap* x0 x1 x)
performs the following calculation:

.cblk
.mets (+ (mod (- x x0) (- x1 x0)) x0)
.cble

In other words, first
.meta start
is subtracted from
.metn number .
Then the result is reduced modulo the displacement
between
.code start
and
.codn end .
Finally,
.meta start
is added back to that result, which is returned.

.TP* Example:

.cblk
  ;; perform ROT13 on the string "nop"
  [mapcar (opip (+ 13) (wrap #\ea #\ez)) "nop"] -> "abc"
.cble

.coNP Functions @ gcd and @ lcm
.synb
.mets (gcd << number *)
.mets (lcm << number *)
.syne
.desc
The
.code gcd
function computes the greatest common divisor: the largest positive
integer which divides each
.metn number .

The
.code lcm
function computes the lowest common multiple: the smallest positive
integer which is a multiple of
each
.metn number .

Each
.meta number
must be an integer.

Negative integers are replaced by their absolute values, so
.code (lcm -3 -4)
is
.code 12 
and 
.code (gcd -12 -9)
yields
.codn 3 .

The value of
.code (gcd)
is
.code 0
and that of
.code (lcm)
is 1 .

The value of
.code (gcd x)
and
.code (lcm x)
is
.codn (abs x) .

Any arguments of
.code gcd
which are zero are effectively ignored so that
.code (gcd 0)
and
.code (gcd 0 0 0)
are both the same as
.code (gcd)
and
.code (gcd 1 0 2 0 3)
is the same as
.codn (gcd 1 2 3) .

If
.code lcm
has any argument which is zero, it yields zero.

.coNP Function @ abs
.synb
.mets (abs << number )
.syne
.desc
The
.code abs
function computes the absolute value of
.metn number .
If
.meta number
is positive, it is returned. If
.meta number
is negative, its additive inverse is
returned: a positive number of the same type with exactly the same magnitude.

.coNP Functions @ floor and @ ceil
.synb
.mets (floor << number )
.mets (ceil << number )
.syne
.desc
The
.code floor
function returns the highest integer which does not exceed
the value of
.metn number .
The ceiling function returns the lowest integer which
does not exceed the value of
.metn number .

If
.meta number
an integer, it is simply returned.

If the argument is a float, then the value returned is a float.
For instance
.code (floor 1.1)
returns 1.0 rather than 1.

.coNP Functions @, sin @, cos @, tan @, asin @, acos @ atan and @ atan2
.synb
.mets (sin << radians )
.mets (cos << radians )
.mets (tan << radians )
.mets (atan << slope )
.mets (atan2 < y << x )
.mets (asin << num )
.mets (acos << num )
.syne
.desc
These trigonometric functions convert their argument to floating point and
return a float result. The
.codn sin ,
.code cos
and
.code tan
functions compute the sine and
cosine and tangent of the
.meta radians
argument which represents an angle
expressed in radians. The
.codn atan ,
.code acos
and
.code asin
are their respective inverse
functions.  The
.meta num
argument to
.code asin
and
.code acos
must be in the
range -1.0 to 1.0. The
.code atan2
function converts the rectilinear coordinates
.meta x
and
.meta y
to an angle in polar coordinates in the range [0, 2\(*p).

.coNP Functions @, exp @, log @ log10 and @ log2
.synb
.mets (exp << arg )
.mets (log << arg )
.mets (log10 << arg )
.mets (log2 << arg )
.syne
.desc
The
.code exp
function calculates the value of the transcendental number e raised to
the exponent
.metn arg .

The
.code log
function calculates the base e logarithm of
.metn arg ,
which must be a positive value.

The
.code log10
function calculates the base 10 logarithm of
.metn arg ,
which must be a positive value.

The
.code log2
function calculates the base 2 logarithm of
.metn arg ,
which must be a positive value.

.coNP Functions @, expt @ sqrt and @ isqrt
.synb
.mets (expt < base << exponent *)
.mets (sqrt << arg )
.mets (isqrt << arg )
.syne
.desc
The
.code expt
function raises
.meta base
to zero or more exponents given
by the
.meta exponent
arguments.
.code (expt x)
is equivalent to
.codn (expt x 1) ,
and yields
.code x
for all
.codn x .
For three or more arguments, the operation is right-associative.
That is to say,
.code (expt x y z)
is equivalent to
.codn (expt x (expt y z)) ,
similarly to the way nested exponents work in standard algebraic
notation.

Exponentiation is done pairwise using a binary operation.
If both operands to this binary operation are integers, then the
result is an integer. If either operand is a float, then the other
operand is converted to a float, and a floating point exponentiation
is performed. Exponentiation that would produce a complex number is
not supported.

The
.code sqrt
function produces a floating-point square root of
.metn arg ,
which is converted from integer to floating-point if necessary.  Negative
operands are not supported.

The
.code isqrt
function computes the integer square root of
.metn arg ,
which must be an integer.
The integer square root is a value which is the
greatest integer that is no greater than the real square root of
.metn arg .
The input value must be an integer.

.coNP Function @ exptmod
.synb
.mets (exptmod < base < exponent << modulus )
.syne
.desc
The
.code exptmod
function performs modular exponentiation and accepts only integer
arguments. Furthermore,
.meta exponent
must be a non-negative and
.meta modulus
must be positive.

The return value is
.meta base
raised to
.metn exponent ,
and reduced to the
least positive residue modulo
.metn modulus .

.coNP Function @ cum-norm-dist
.synb
.mets (cum-norm-dist << argument )
.syne
.desc
The
.code cum-norm-dist
function calculates an approximation to the cumulative normal
distribution function: the integral, of the normal distribution function, from
negative infinity to the
.metn argument .

.coNP Functions @ n-choose-k and @ n-perm-k
.synb
.mets (n-choose-k < n << k )
.mets (n-perm-k < n << k )
.syne
.desc
The
.code n-choose-k
function computes the binomial coefficient nCk which
expresses the number of combinations of
.meta k
items that can be chosen from
a set of
.metn n ,
where combinations are subsets.

The
.code n-perm-k
function computes nPk: the number of permutations of size
.meta k
that can be drawn from a set of
.metn n ,
where permutations are sequences,
whose order is significant.

The calculations only make sense when
.meta n
and
.meta k
are nonnegative integers, and
.meta k
does not exceed
.metn n .
The behavior is not specified if these conditions
are not met.

.coNP Functions @, fixnump @, bignump @, integerp @ floatp and @ numberp
.synb
.mets (fixnump << object )
.mets (bignump << object )
.mets (integerp << object )
.mets (floatp << object )
.mets (numberp << object )
.syne
.desc
These functions test the type of
.metn object ,
returning
.code t
if it is an object
of the implied type,
.code nil
otherwise. The
.codn fixnump ,
.code bignump
and
.code floatp
functions return
.code t
if the object is of the basic type
.codn fixnum ,
.code bignum
or
.codn float .
The function
.code integerp
returns true of
.meta object
is either a
.code fixnum
or
a
.codn bignum .
The function
.code numberp
returns
.code t
if
.meta object
is either
a
.codn fixnum ,
.code bignum
or
.codn float .

.coNP Function @ zerop
.synb
.mets (zerop << number )
.syne
.desc
The
.code zerop
function tests
.meta number
for equivalence to zero. The argument must be
a number or character. It returns
.code t
for the integer value
.code 0
and for the floating-point
value
.codn 0.0 .
For other numbers, it returns
.codn nil .
It returns
.code t
for the null character
.code #\enul
and
.code nil
for all other characters.

.coNP Functions @ plusp and @ minusp
.synb
.mets (plusp << number )
.mets (minusp << number )
.syne
.desc
These functions test whether a number is positive or negative,
returning
.code t
or
.codn nil ,
as the case may be.

The argument may also be a character. All characters other than
the null character
.code #\enul
are positive. No character is negative.

.coNP Functions @ evenp and @ oddp
.synb
.mets (evenp << integer )
.mets (oddp << integer )
.syne
.desc
The
.code evenp
and
.code oddp
functions require integer arguments.
.code evenp
returns
.code t
if
.meta integer
is even (divisible by two), otherwise it returns
.codn nil .
.code oddp
returns
.code t
if
.meta integer
is not divisible by two (odd), otherwise
it returns
.codn nil .

.coNP Functions @, succ @, ssucc @, sssucc @, pred @, ppred @ and pppred
.synb
.mets (succ << number )
.mets (ssucc << number )
.mets (sssucc << number )
.mets (pred << number )
.mets (ppred << number )
.mets (pppred << number )
.syne
.desc
The
.code succ
function adds 1 to its argument and returns the resulting value.
If the argument is an integer, then the return value is the successor
of that integer, and if it is a character, then the return value
is the successor of that character according to Unicode.

The
.code pred
function subtracts 1 from its argument, and under similar considerations
as above, the result represents the predecessor.

The
.code ssucc
and
.code sssucc
functions add 2 and 3, respectively. Similarly,
.code ppred
and
.code pppred
subtract 2 and 3 from their argument.

.coNP Functions @, > @, < @, >= @ <= and @ =
.synb
.mets (> < number << number *)
.mets (> < number << number *)
.mets (>= < number << number *)
.mets (<= < number << number *)
.mets (= < number << number *)
.syne
.desc
These relational functions compare characters and numbers for numeric equality
or inequality. The arguments must be one or more numbers or characters.

If just one argument is given, then these functions all return
.codn t .

If two arguments are given then, they are compared as follows.
First, if the numbers do not have the same type, then the one
which has the lower ranking type is converted to the type of
the other, according to this ranking: character < integer < float.
For instance if a character and integer are compared, the character
is converted to integer. Then a straightforward numeric comparison
is applied.

Three or more arguments may be given, in which case the comparison proceeds
pairwise from left to right. For instance in
.codn (< a b c) ,
the comparison
.code (< a b)
is performed in isolation. If the comparison is false, then
.code nil
is returned, otherwise
the comparison
.code (< b c)
is performed in isolation, and if that is false,
.code nil
is returned, otherwise
.code t
is returned.  Note that it is possible for
.code b
to
undergo two different conversions.  For instance in the
.cblk
.meti (< < float < character << integer )
.cble
comparison,
.meta character
will first convert to a floating-point representation
of its Unicode value so that it can be compared to
.metn float ,
and if that comparison succeeds, then in the second comparison,
.meta character
will be converted to integer so that it can be compared to
.metn integer .

.coNP Function @ /=
.synb
.mets (/= << number *)
.syne
.desc
The arguments to
.code /=
may be numbers or characters.  The
.code /=
function returns
.code t
if no two of its arguments are numerically equal. That is to say, if there
exist some
.code a
and
.code b
which are distinct arguments such that
.code (= a b)
is true, then
the function returns
.codn nil .
Otherwise it returns
.codn t .

.coNP Functions @ max and @ min
.synb
.mets (max < first-arg << arg *)
.mets (min < first-arg << args *)
.syne
.desc
The
.code max
and
.code min
functions determine and return the highest or lowest
value from among their arguments.

If only
.meta first-arg
is given, that value is returned.

These functions are type generic, since they compare arguments
using the same semantics as the
.code less
function.

If two or more arguments are given, then
.code (max a b)
is equivalent to
.codn (if (less a b) b a) ,
and
.code (min a b)
is equivalent to
.codn (if (less a b) a b) .
If the operands do not
have the same type, then one of them is converted to the type of the other;
however, the original unconverted values are returned. For instance
.code (max 4 3.0)
yields the integer
.codn 4 ,
not
.codn 4.0 .

If three or more arguments are given,
.code max
and
.code min
reduce the arguments in a left-associative manner.
Thus
.code (max a b c)
means
.codn (max (max a b) c) .

.coNP Functions @, int-str @ flo-str and @ num-str
.synb
.mets (int-str < string <> [ radix ])
.mets (flo-str << string )
.mets (num-str << string )
.syne
.desc
These functions extract numeric values from character string
.metn string .
Leading whitespace in
.metn string ,
if any, is skipped. If no digits can be successfully extracted, then
.code nil
is returned.  Trailing material which does not contribute to the number is
ignored.

The
.code int-str
function converts a string of digits in the specified
radix to an integer value. If the radix isn't specified, it defaults to 10.
For radices above 10, letters of the alphabet
are used for digits:
.code A
represent a digit whose value is 10,
.code B
represents 11 and
so forth until
.codn Z .
For values of radix above 36, the returned value is
unspecified. Upper and lower case letters are recognized.
Any character which is not a digit of the specified radix is regarded
as the start of trailing junk at which the extraction of the digits stops.

The
.code flo-str
function converts a floating-point decimal notation to a nearby
floating point value. The material which contributes to the value
is the longest match for optional leading space, followed by a
mantissa which consists of an optional sign followed by a mixture of at least
one digit, and at most one decimal point, optionally followed by an exponent
part denoted by the letter
.code E
or
.codn e ,
an optional sign and one or more optional exponent digits.

The
.code num-str
function converts a decimal notation to either an integer as if by
a radix 10 application of
.codn int-str ,
or to a floating point value as if by
.codn flo-str .
The floating point interpretation is chosen if the possibly empty
initial sequence of digits (following any whitespace and optional sign) is
followed by a period, or by
.code e
or
.codn E .

.coNP Functions @ int-flo and @ flo-int
.synb
.mets (int-flo << float )
.mets (flo-int << integer )
.syne
.desc
These functions perform numeric conversion between integer and floating point
type. The
.code int-flo
function returns an integer by truncating toward zero.
The
.code flo-int
function returns an exact floating point value corresponding to
.metn integer ,
if possible, otherwise an approximation using a nearby
floating point value.

.coNP Functions @ tofloat and @ toint
.synb
.mets (tofloat << value )
.mets (toint < value <> [ radix ])
.syne
.desc
These convenience functions convert
.meta value
to floating-point or integer, respectively.

If a floating-point value is passed into tofloat, or an integer value into
toint, then the value is simply returned.

If
.meta value
is a character, then it is treated as a string of length one
containing that character.

If
.meta value
is a string, then it is converted by
.code tofloat
as if by the function
.metn flo-str ,
, and by
.code toint
as if by the function
.codn int-str .

If
.meta value
is an integer, then it is converted by
.code tofloat
as if by the function
.codn flo-int .

If
.meta value
is a floating-point number, then it is converted by
.code toint
as if by the function
.codn int-flo .

.coNP Variables @, *flo-min* @, *flo-max* and @ *flo-epsilon*
.desc
These variables hold, respectively: the smallest positive floating-point
value; the largest positive floating-point value; and the difference
between 1.0 and the smallest representable value greater than 1.0.

.code *flo-min*
and
.code *flo-max*
define the floating-point range, which consists
of three regions: values from
.code (- *flo-max*)
to
.codn (- *flo-min*) ;
the value 0.0, and values from
.code *flo-min*
to
.codn *flo-max* .

.coNP Variable @ *flo-dig*
.desc
This variable holds an integer representing the number of decimal digits
in a decimal floating-point number such that this number can be converted
to a \*(TX floating-point number, and back to decimal, without a change in any of
the digits. This holds regardless of the value of the number, provided that it
does not exceed the floating-point range.

.coNP Variables @ *pi* and @ *e*
.desc
These variables hold an approximation of the mathematical constants \(*p and e.
To four digits of precision, \(*p is 3.142 and e is 2.718. The
.code *pi*
and
.code *e*
approximations are accurate to
.code *flo-dig*
decimal digits.

.SS* Bit Operations
In \*(TL, similarly to Common Lisp, bit operations on integers are based
on a concept that might be called "infinite two's-complement".  
Under infinite two's complement, a positive number is regarded as having
a binary representation prefixed by an infinite stream of zero digits (for
example
.code 1
is
.codn ...00001 ).
A negative number
in infinite two's complement is the biwise negation of its positive counterpart,
plus one: it carries an infinite prefix of 1 digits. So for instance the number
.code -1
is represented by
.codn ...11111111 :
an infinite sequence of
1
bits. There
is no specific sign bit; any operation which produces such an infinite sequence
of 1 digits on the left gives rise to a negative number. For instance, consider the
operation of computing the bitwise complement of the number
.codn 1 .
Since the
number
.code 1
is represented as
.codn ...0000001 ,
its complement is
.codn ...11111110 .
Each one of the
.cod 0
digits in the infinite sequence is replaced by
.codn 1 ,
And this leading sequence means that the number
is negative, in fact corresponding to the two's-complement representation of
the value
.codn -2 .
Hence, the infinite digit concept corresponds to an arithmetic
interpretation.

In fact \*(TL's bignum integers do not use a two's complement
representation internally. Numbers are represented as an array which holds a
pure binary number. A separate field indicates the sign: negative,
or non-negative.  That negative numbers appear as two's-complement under the
bit operations is merely a carefully maintained illusion (which makes bit
operations on negative numbers more expensive).

The
.code logtrunc
function, as well as a feature of the
.code lognot
function, allow bit
manipulation code to be written which works with positive numbers only, even if
complements are required. The trade off is that the application has to manage a
limit on the number of bits.

.coNP Functions @, logand @, logior and @ logxor
.synb
.mets (logand << integer *)
.mets (logior << integer *)
.mets (logxor < int1 << int2 )
.syne
.desc
These operations perform the familiar bitwise and, inclusive or, and exclusive
or operations, respectively. Positive values inputs are treated as
pure binary numbers.  Negative inputs are treated as infinite-bit
two's-complement.

For example
.code (logand -2 7)
produces
.codn 6 .
This is because
.code -2
is
.code ...111110
in infinite-bit two's-complement.  And-ing this value with
.code 7
(or
.codn ...000111 )
produces
.codn 110 .

The
.code logand
and
.code logior
functions are variadic, and may be called with zero, one,
two, or more input values. If
.code logand
is called with no arguments, it produces
the value -1 (all bits 1). If
.code logior
is called with no arguments it produces
zero. In the one-argument case, the functions just return their argument value.

.coNP Function @ logtest
.synb
.mets (logtest < int1 << int2 )
.syne
.desc
The
.code logtest
function returns true if
.meta int1
and
.meta int2
have bits in
common. The following equivalence holds:

.cblk
  (logtest a b) <--> (not (zerop (logand a b)))
.cble

.coNP Functions @ lognot and @ logtrunc
.synb
.mets (lognot < value <> [ bits ])
.mets (logtrunc < value << bits )
.syne
.desc
The
.code lognot
function performs a bitwise complement of
.metn value .
When the one-argument form of lognot is used, then if
.meta value
is nonnegative,
then the result is negative, and vice versa, according to the infinite-bit
two's complement representation. For instance
.code (lognot -2)
is
.codn 1 ,
and
.code (lognot 1)
is
.codn -2 .

The two-argument form of
.code lognot
produces a truncated complement. Conceptually,
a bitwise complement is first calculated, and then the resulting number is
truncated to the number of bits given by
.metn bits ,
which must be a nonnegative integer. The following equivalence holds:

.cblk
  (lognot a b) <--> (logtrunc (lognot a) b)
.cble

The
.code logtrunc
function truncates the integer
.meta value
to the  specified number
of bits. If
.meta value
is negative, then the two's-complement representation
is truncated. The return value of
.code logtrunc
is always a non-negative integer.

.coNP Function @ sign-extend
.synb
.mets (sign-extend < value << bits )
.syne
.desc
The
.code sign-extend
function first truncates the infinite-bit two's complement representation of
the integer
.meta value
to the specified number of bits, similarly to the
.code logtrunc
function. Then, this truncated value is regarded as a
.meta bits
wide two's complement integer. The value of this integer is
calculated and returned.

.TP* Examples:

.cblk
  (sign-extend 127 8) -> 127
  (sign-extend 128 8) -> -128
  (sign-extend 129 8) -> -127
  (sign-extend 255 8) -> -1
  (sign-extend 256 8) -> 0
  (sign-extend -1  8) -> -1
  (sign-extend -255 8) -> 0
.cble

.coNP Function @ ash
.synb
.mets (ash < value << bits )
.syne
.desc
The
.code ash
function shifts
.meta value
by the specified number of
.meta bits
producing a
new value.  If
.meta bits
is positive, then a left shift takes place.  If
.meta bits
is negative, then a right shift takes place. If
.meta bit
is zero, then
.meta value
is returned unaltered.  For positive numbers, a left shift by n bits is
equivalent to a multiplication by two to the power of n, or
.codn (expt 2 n) .
A right shift by n bits of a positive integer is equivalent to integer
division by
.codn (expt 2 n) ,
with truncation toward zero.
For negative numbers, the bit shift is performed as if on the two's-complement
representation. Under the infinite two's-complement representation,
a right shift does not exhaust the infinite sequence of
.code 1
digits which
extends to the left. Thus if
.code -4
is shifted right it becomes
.code -2
because
the bitwise representations of these values are
.code ...111100
and
.codn ...11110 .

.coNP Function @ bit
.synb
.mets (bit < value << bit )
.syne
.desc
The
.code bit
function tests whether the integer
.meta value
has a 1 in bit position
.metn bit .
The
.meta bit
argument must be a non-negative integer. A value of zero of
.meta bit
indicates the least significant bit position of
.metn value .

The
.code bit
function has a boolean result, returning the symbol
.code t
if bit
.meta bit
of
.meta value
is set, otherwise
.codn nil .

If
.meta value
is negative, it is treated as if it had an infinite-bit two's
complement representation. For instance, if value is 
.codn -2 ,
then the bit
function returns
.code nil
for a
.meta bit
value of zero, and
.code t
for all other values,
since the infinite bit two's complement representation of
.code -2
is
.codn ...11110 .

.coNP Function @ mask
.synb
.mets (mask << integer *)
.syne
.desc
The
.code mask
function takes zero or more integer arguments, and produces an integer
value which corresponds a bitmask made up of the bit positions specified by the
integer values.

If
.code mask
is called with no arguments, then the return value is zero.

If
.code mask
is called with a single argument
.meta integer
then the return value is the same as
that of the expression
.codn (ash 1 <integer>) :
the value 1 shifted left by
.meta integer
bit positions. If
.meta integer
is zero, then the result is
.codn 1 ;
if
.meta integer
is
.codn 1 ,
the
result is
.code 2
and so forth.  If
.meta value
is negative, then the result is zero.

If
.code mask
is called with two or more arguments, then the result is a bitwise of
the masks individually computed for each of the values.

In other words, the following equivalences hold:

.cblk
  (mask) <--> 0
  (mask a) <--> (ash 1 a)
  (mask a b c ...) <--> (logior (mask a) (mask b) (mask c) ...)
.cble

.coNP Function @ width
.synb
.mets (width << integer *)
.syne
.desc
A two's complement representation of an integer consists of a sign bit and a
manitssa field.
The
.code width
function computes the minimum number of bits required for the mantissa portion
of the two's complement representation of the
.meta integer
argument.

For a nonnegative argument, the width also corresponds to the number of bits
required for a natural binary representation of that value.

Two integer values have a width of zero, namely 0 and -1. This means that these
two values can be represented in a one-bit two's complement, consisting of only
a sign bit: the one-bit two's complement bitfield 1 denotes -1, and 0 denotes
0.

Similarly, two integer values have a width of 1: 1 and -2. The two-bit
two's complement bitfield 01 denotes 1, and 10 denotes -2.

The argument may be a character.


.SS* Exceptions
.coNP Functions @, throw @ throwf and @ error
.synb
.mets (throw < symbol << arg *)
.mets (throwf < symbol < format-string << format-arg *)
.mets (error < format-string << format-arg *)
.syne
.desc
These functions generate an exception. The
.code throw
and
.code throwf
functions generate
an exception identified by
.metn symbol ,
whereas
.code error
throws an exception of
type
.codn error .
The call
.code (error ...)
can be regarded as a shorthand for
.codn (throwf 'error ...) .

The
.code throw
function takes zero or more additional arguments. These arguments
become the arguments of a
.code catch
handler which takes the exception. The
handler will have to be capable of accepting that number of arguments.

The
.code throwf
and
.code error
functions generate an exception which has a single
argument: a character string created by a formatted print to a string stream
using the
.code format
string and additional arguments.

.coNP Operator @ catch
.synb
.mets (catch < try-expression
.mets \ \  >> {( symbol <> ( arg *) << body-form *)}*)
.syne
.desc
The
.code catch
operator establishes an exception catching block around
the
.metan try-expression .
The
.meta try-expression
is followed by zero or more
catch clauses. Each catch clause consists of a symbol which denotes
an exception type, an argument list, and zero or more body forms.

If
.meta try-expression
terminates normally, then the catch clauses
are ignored. The catch itself terminates, and its return value is
that of the
.metn try-expression .

If
.meta try-expression
throws an exception which is a subtype of one or more of
the type symbols given in the exception clauses, then the first (leftmost) such
clause becomes the exit point where the exception is handled.
The exception is converted into arguments for the clause, and the clause
body is executed. When the clause body terminates, the catch terminates,
and the return value of the catch is that of the clause body.

If
.meta try-expression
throws an exception which is not a subtype of any of
the symbols given in the clauses, then the search for an exit point for
the exception continues through the enclosing forms. The catch clauses
are not involved in the handling of that exception.

When a clause catches an exception, the number of arguments in the catch must
match the number of elements in the exception.  A catch argument list
resembles a function or lambda argument list, and may be dotted.  For instance
the clause
.code (foo (a . b))
catches an exception subtyped from
.codn foo ,
with one or
more elements. The first element binds to parameter
.codn a ,
and the rest, if any,
bind to parameter
.codn b .
If there is only one element,
.code b
takes on the value
.codn nil .

Also see: the
.code unwind-protect
operator, and the functions
.codn throw ,
.code throwf
and
.codn error .

.coNP Macro @ ignerr
.synb
.mets (ignerr << form *)
.syne
.desc
The
.code ignerr
macro operator evaluates each
.meta form
similarly to the
.code progn
operator. If no forms are present, it returns
.codn nil .
Otherwise it evaluates each
.meta form
in turn, yielding the value of the last one.

If the evaluation of any
.meta form
is abandoned due to an exception of type
.codn error ,
the code generated by the
.code ignerr
macro catches this exception. In this situation,
the execution of the
.code ignerr
form terminates without evaluating the remaining
forms, and yields
.codn nil .

.coNP Variable @ *unhandled-hook*

The
.code *unhandled-hook*
variable is initialized with
.code nil
by default.

It may instead be assigned a function which is capable of taking
three arguments.

When an exception occurs which has no handler, this function is called,
with the following arguments: the exception type symbol, the exception object,
and a third value which is either
.code nil
or else the form which was being evaluated the exception was thrown.

Otherwise, if the variable is
.code nil
some informational messages are printed about the exception, and the process
exits with a failed termination status.
In the same situation, if the variable contains an object which is not a
function, the process terminates abnormally as if by a call to the
.code abort
function.

Prior to the function being called, the
.code *unhandled-hook*
variable is reset to
.codn nil .

If the function registered in
.code *unhandled-hook*
returns, the process exits with a failed termination status.

Note: the functions
.code source-loc
or
.code source-loc-str
may be applied to the third argument of the
.code *unhandled-hook*
function to obtain more information about the form.

.SS* Regular Expression Library
.coNP Functions @ search-regex and @ range-regex
.synb
.mets (search-regex < string < regex >> [ start <> [ from-end ]])
.mets (range-regex < string < regex >> [ start <> [ from-end ]])
.mets (range-regst < string < regex >> [ start <> [ from-end ]])
.syne
.desc
The
.code search-regex
function searches through
.meta string
starting
at position
.meta start
for a match for
.metn regex .
If
.meta start
is omitted, the search starts at position 0. If
.meta from-end
is specified and has a
.cod2 non- nil
value, the search
proceeds in reverse, from the last position in the string, toward
.metn start .
This function returns
.code nil
if no match is found, otherwise it returns
a cons, whose
.code car
indicates the position of the match, and whose
.code cdr
indicates the length of the match.

The
.code range-regex
function is similar to
.codn search-regex ,
except that when
a match is found, it returns a position range, rather than a position
and length. A cons is returned whose
.code car
indicates the position
of the match, and whose
.code cdr
indicates the position one element past the
last character of the match. If the match is empty, the two integers
are equal.

The
.code search-regst
differs from
.code search-regex
in the representation of the return value in the matching case.
Rather than returning the position and length of the match,
it returns the matching substring of
.metn string .

.coNP Functions @ match-regex and @ match-regst
.synb
.mets (match-regex < string < regex <> [ position ])
.mets (match-regst < string < regex <> [ position ])
.syne
.desc
The
.code match-regex
function tests whether
.meta regex
matches at
.meta position
in
.metn string .
If
.meta position
is not specified, it is taken to be zero.
If the regex matches, then the length of the match is returned.
If it does not match, then
.code nil
is returned.

The
.code match-regst
differs from
.code match-regex
in the representation of the return value in the matching case.
Rather than returning the length of the match, it returns
matching substring of
.metn string .

.coNP Functions @ match-regex-right and @ match-regst-right
.synb
.mets (match-regex-right < string < regex <> [ end-position ])
.mets (match-regst-right < string < regex <> [ end-position ])
.syne
.desc
The
.code match-regex
function tests whether
.meta string
contains a match which ends
precisely on the character just before
.metn end-position .
If
.meta end-position
is not specified, it defaults to the length of the string, and the function
performs a right-anchored regex match.

If a match is found, then the length of the match is returned.

The match must terminate just before
.meta end-position
in the sense that
additional characters at
.meta end-position
and beyond can no longer satisfy the
regular expression. More formally, the function searches, starting from
position zero, for positions where there occurs a match for the regular
expression, taking the longest possible match. The length of first such a match
which terminates on the character just before
.meta end-position
is returned.
If no such a match is found, then
.code nil
is returned.

The
.code match-regst-right
differs from
.code match-regst-right
in the representation of the return value in the matching case.
Rather than returning the length of the match, it returns
matching substring of
.metn string .

.TP* Examples:

.cblk
  ;; Return matching portion rather than length thereof.

  (defun match-regex-right-substring (str reg : end-pos)
    (set end-pos (or end-pos (length str)))
    (let ((len (match-regex-right str reg end-pos)))
      (if len
        [str (- end-pos len)..end-pos]
        nil)))

  (match-regex-right-substring "abc" #/c/) -> ""

  (match-regex-right-substring "acc" #/c*/) -> "cc"

  ;; Regex matches starting at multiple positions, but all
  ;; the matches extend past the limit.
  (match-regex-right-substring "acc" #/c*/ 2) -> nil

  ;; If the above behavior is not wanted, then
  ;; we can extract the string up to the limiting
  ;; position and do the match on that.
  (match-regex-right-substring ["acc" 0..2] #/c*/) -> "c"

  ;; Equivalent of above call
  (match-regex-right-substring "ac" #/c*/) -> "c"
.cble

.coNP Function @ regsub
.synb
.mets (regsub < regex < replacement << string )
.syne
.desc
The
.code regsub
function searches
.meta
string
for multiple occurrences of
non-overlapping matches for
.metn regex .
A new string is constructed
similar to
.meta string
but in which each matching region is replaced
with using
.meta replacement
as follows.

The
.meta replacement
object may be a character or a string, in which
case it is simply taken to be the replacement for each match
of the regular expression.

The
.meta replacement
object may be a function of one argument, in
which case for every match which is found, this function is invoked,
with the matching piece of text as an argument. The function's
return value is then taken to be the replacement text.

.TP* Examples:

.cblk
  ;; match every lower case e or o, and replace by filtering
  ;; through the upcase-str function:

  [regsub #/[eo]/ upcase-str "Hello world!"] -> "HEllO wOrld!"

  ;; Replace Hello with Goodbye:
  (regsub #/Hello/ "Goodbye" "Hello world!") -> "Goodbye world!"
.cble

.coNP Function @ regexp
.synb
.mets (regexp << obj )
.syne
.desc
The
.code regexp
function returns
.code t
if
.meta obj
is a compiled regular expression
object. For any other object type, it returns
.codn nil .

.coNP Function @ regex-compile
.synb
.mets (regex-compile < form-or-string <> [ error-stream ])
.syne
.desc
The
.code regex-compile
function takes the source code of a regular expression,
expressed as a Lisp data structure representing an abstract syntax tree, or
else a regular expression specified as a character string, and compiles it to a
regular expression object.

If
.meta form-or-string
is a character string, it is parsed to an
abstract syntax tree first, if by the
.code regex-parse
function.
If the parse is successful (the result is not
.codn nil )
then
the resulting tree structure is compiled by a recursive call to
.codn regex-compile .

The optional
.meta error-stream
argument is passed down to
.code regex-parse
as well as in the recursive call to
.codn regex-compile ,
if that call takes place.

If
.meta error-stream
is specified, it must be a stream. Any error diagnostics are sent to that
stream.

.TP* Examples:

.cblk
  ;; the equivalent of #/[a-zA-Z0-9_/
  (regex-compile '(set (#\ea . #\ez) (#\eA . #\eZ)
                       (#\e0 . #\e9) #\e_))

  ;; the equivalent of #/.*/ and #/.+/
  (regex-compile '(0+ wild))
  (regex-compile '(1+ wild))

  ;; #/a|b|c/
  (regex-compile '(or (or #\ea #\eb) #\ec))

  ;; string
  (regex-compile "a|b|c")
.cble

.coNP Function @ regex-parse
.synb
.mets (regex-parse < string <> [ error-stream ])
.syne
.desc
The
.code regex-parse
function parses a character string which contains a regular expression
(without any surrounding / characters) and turns it into a Lisp data structure
(the abstract syntax tree representation of the regular expression).

The regular expression syntax
.code #/RE/
produces the same structure, but as a
literal which is processed at the time \*(TX source code is read; the
.code regex-parse
function performs this parsing at run-time.

If there are parse errors, the function returns
.codn nil .

The optional
.meta error-stream
argument specifies a stream to which error messages
are sent from the parser. By default, diagnostic output goes to the
.code *stdnull*
stream, which discards it. If
.meta error-stream
is specified as
.codn t ,
then the diagnostic output goes to the
.code *stdout*
stream.

If
.code regex-parse
returns a
.cod2 non- nil
value, that structure is then something
which is suitable as input to
.codn regex-compile .

.SS* Hashing Library
.coNP Functions @, make-hash and @ hash
.synb
.mets (make-hash < weak-keys < weak-vals << equal-based )
.mets (hash { :weak-keys | :weak-vals | :equal-based }*)
.syne
.desc
These functions construct a new hash table.

A hash table is an object which retains an association between pairs of
objects. Each pair consists of a key and value. Given an object which is
similar to a key in the hash table, it is possible to retrieve the
corresponding value. Entries in a hash table are not ordered in any way, and
lookup is facilitated by hashing: quickly mapping a key object to a numeric
value which is then used to index into one of many buckets where the matching
key will be found (if such a key is present in the hash table).

.code make-hash
takes three mandatory boolean arguments. The
.meta weak-keys
argument specifies whether the hash table shall have weak keys. The
.meta weak-vals
argument specifies whether it shall have weak values, and
.meta equal-based
specifies whether it is
.codn equal- based.
The hash function defaults
all three of these properties to false, and allows them to be overridden to
true by the presence of keyword arguments.

It is an error to attempt to construct an
.codn equal -based
hash table which has weak keys.

The hash function provides an alternative interface. It accepts optional
arguments which are keyword symbols. Any combination of the three symbols
.codn :weak-keys ,
.code :weak-vals
and
.code :equal-based
can be specified in any order
to turn on the corresponding properties in the newly constructed hash table.
If any of the keywords is not specified, the corresponding property defaults to
.codn nil .

If a hash table has weak keys, this means that from the point of view
of garbage collection, that table holds only weak references to the keys
stored in it.  Similarly, if a hash table has weak values, it means that it
holds a weak reference to each value stored. A weak reference is one
which does not prevent the reclamation of an object by the garbage
collector. That is to say, when the garbage collector discovers that the only
references to some object are weak references, then that object is considered
garbage, just as if it had no references to it. The object is reclaimed, and
the weak references "lapse" in some way, which depends on what kind they are.
Hash table weak references lapse by entry removal: if either a key or a value
object is reclaimed, then the corresponding key-value entry is erased from the
hash table.

Important to the operation of a hash table is the criterion by which keys are
considered same. By default, this similarity follows the eql function.  A hash
table will search for a stored key which is
.code eql
to the given search key.
A hash table constructed with the
.codn equal -based
property compares keys using
the
.code equal
function instead.

In addition to storing key-value pairs, a hash table can have a piece of
information associated with it, called the user data.

A hash table can be traversed to visit all of the keys and data.  The order of
traversal bears no relation to the order of insertion, or to any properties of
the key type.

During an open traversal, new keys can be inserted into a hash table or deleted
from it while a a traversal is in progress. Insertion of a new key during
traversal will not cause any existing key to be visited twice or to be skipped;
however, it is not specified whether the new key will be traversed. Similarly,
if a key is deleted during traversal, and that key has not yet been visited, it
is not specified whether it will be visited during the remainder of the
traversal.

An open traversal of a hash table is performed by the
.code maphash
function and the
.code dohash
operator. The traversal is open because code supplied by the program
is evaluated for each entry.

The functions
.codn hash-keys ,
.codn hash-values ,
.codn hash-pairs ,
and
.code hash-alist also perform an open traversal, because they return
lazy lists. The traversal isn't complete until the returned lazy list
is fully instantiated. In the meanwhile, the
\*(TX program can mutate the hash table from which the lazy list
is being generated.

.coNP Function @ hash-construct
.synb
.mets (hash-construct < hash-args << key-val-pairs )
.syne
.desc
The
.code hash-construct
function constructs a populated hash in one step. The
.meta hash-args
argument specifies a list suitable as an argument list in a call to the hash
function.  The
.meta key-val-pairs
is a sequence of pairs, which are two-element
lists representing key-value pairs.

A hash is constructed as if by a call to
.cblk
.meti [apply hash << hash-args ],
.cble
then populated
with the specified pairs, and returned.

.coNP Function @ hash-update
.synb
.mets (hash-update < hash << function )
.syne
.desc
The
.code hash-update
function replaces each values in
.metn hash ,
with the value of
.meta function
applied to that value.

The return value is
.metn hash .

.coNP Function @ hash-update-1
.synb
.mets (hash-update-1 < hash < key < function <> [ init ])
.syne
.desc
The
.code hash-update-1
function operates on a single entry in the hash table.

If
.meta key
exists in the hash table, then its corresponding value is passed
into
.metn function ,
and the return value of
.meta function
is then installed
in place of the key's value. The value is then returned.

If
.meta key
does not exist in the hash table, and no
.meta init
argument is given,
then
.code hash-update-1
does nothing and returns
.codn nil .

If
.meta key
does not exist in the hash table, and an
.meta init
argument is given,
then
.meta function
is applied to
.metn init ,
and then
.meta key
is inserted into
.meta hash
with the value returned by
.meta function
as the datum. This value
is also returned.

.coNP Function @ group-by
.synb
.mets (group-by < func < sequence << option *)
.syne
.desc
The
.code group-by
function produces a hash table from
.metn sequence ,
which is a
list or vector. Entries of the hash table are not elements of
.metn sequence ,
but lists of elements of
.metn sequence .
The function
.meta func
is applied to
each element of
.meta sequence
to compute a key. That key is used to determine
which list the item is added to in the hash table.

The trailing arguments
.cblk
.meti << option *
.cble
if any, consist of the same keywords
that are understood by the hash function, and determine the properties
of the hash.

.TP* Example:
Group the integers from 0 to 10 into three buckets keyed on 0, 1 and 2
according to the modulo 3 congruence:

.cblk
  (group-by (op mod @1 3) (range 0 10)))

  -> #H(() (0 (0 3 6 9)) (1 (1 4 7 10)) (2 (2 5 8)))
.cble

.coNP Functions @ make-similar-hash and @ copy-hash
.synb
.mets (make-similar-hash << hash )
.mets (copy-hash << hash )
.syne
.desc
The
.code make-similar-hash
and copy-hash functions create a new hash object based on
the existing
.meta hash
object.

.code make-similar-hash
produces an empty hash table which inherits all of the
attributes of
.metn hash .
It uses the same kind of key equality, the
same configuration of weak keys and values, and has the same user data (see
the
.code set-hash-userdata
function).

The
.code copy-hash
function is like
.codn make-similar-hash ,
except that instead of
producing an empty hash table, it produces one which has all the same elements
as
.metn hash :
it contains the same key and value objects.

.coNP Function @ inhash
.synb
.mets (inhash < hash < key <> [ init ])
.syne
.desc
The
.code inhash
function searches hash table
.meta hash
for
.metn key .
If
.meta key
is found, then it return the hash table's cons cell which
represents the association between
.meta hash
and
.metn key .
Otherwise, it returns
.codn nil .

If argument
.meta init
is specified, then the function will create
an entry for
.meta key
in
.meta hash
whose value is that of
.metn init .
The cons cell representing that association is returned.

Note: for as long as the
.meta key
continues to exist inside
.metn hash .
modifying the
.code car
field of the returned cons has ramifications for the logical integrity of
the hash. Modifying the
.code cdr
field has the effect of updating the association with a new value.

.coNP Function @ gethash
.synb
.mets (gethash < hash < key <> [ alt ])
.syne
.desc
The
.code gethash
function searches hash table
.meta hash
for key
.metn key .
If the
key is found then the associated value is returned. Otherwise, if
the
.meta alt
argument was specified, it is returned. If the
.meta alt
argument
was not specified,
.code nil
is returned.

.coNP Function @ sethash
.synb
.mets (sethash < hash < key << value )
.syne
.desc
The
.code sethash
function places a value into
.meta hash
table under the given
.metn key .
If a similar key already exists in the hash table, then that key's
value is replaced by
.metn value .
Otherwise, the
.meta key
and
.meta value
pair is
newly inserted into
.metn hash .

.coNP Function @ pushhash
.synb
.mets (pushhash < hash < key << element )
.syne
.desc
The
.code pushhash
function is useful when the values stored in a hash table
are lists.  If the given
.meta key
does not already exist in
.metn hash ,
then a list of
length one is made which contains
.metn element ,
and stored in
.meta hash
table under
.metn key .
If the
.meta key
already exists in the hash table, then the corresponding
value must be a list. The
.meta element
value is added to the front of that list,
and the extended list then becomes the new value under
.metn key .

.coNP Function @ remhash
.synb
.mets (remhash < hash << key )
.syne
.desc
The
.code remhash
function searches
.meta hash
for a key similar to the
.metn key .
If that key is found, then that key and its corresponding value are
removed from the hash table.

.coNP Function @ hash-count
.synb
.mets (hash-count << hash )
.syne
.desc
The
.code hash-count
function returns an integer representing the number of
key-value pairs stored in
.metn hash .

.coNP Function @ get-hash-userdata
.synb
.mets (get-hash-userdata << hash )
.syne
.desc
This function retrieves the user data object associated with
.metn hash .
The user data object of a newly-created hash table is initialized to
.codn nil .

.coNP Function @ set-hash-userdata
.synb
.mets (set-hash-userdata < hash << object )
.syne
.desc
The
.code set-hash-userdata
replaces, with the
.metn object ,
the user data object
associated with
.metn hash .

.coNP Function @ hashp
.synb
.mets (hashp << object )
.syne
.desc
The
.code hashp
function returns
.code t
if the
.meta object
is a hash table,
otherwise it returns
.codn nil .

.coNP Function @ maphash
.synb
.mets (maphash < hash << binary-function )
.syne
.desc
The
.code maphash
function successively invokes
.meta binary-function
for each entry stored in
.metn hash .
Each entry's key and value are passed as arguments
to
.codn binary-function .

The function returns
.codn nil .

.coNP Functions @ hash-eql and @ hash-equal
.synb
.mets (hash-eql << object )
.mets (hash-equal << object )
.syne
.desc
These functions each compute an integer hash value from the internal
representation of
.metn object ,
which satisfies the following properties.
If two objects
.code A
and
.code B
are the same under the
.code eql
function, then
.code (hash-eql A)
and
.code (hash-eql B)
produce the same integer hash value.  Similarly,
if two objects
.code A
and
.code B
are the same under the
.code equal
function, then
.code (hash-equal A)
and
.code (hash-equal B)
each produce the same integer hash value.  In all other
circumstances, the hash values of two distinct objects are unrelated, and
may or may not be the same.

.coNP Functions @, hash_keys @, hash_values @ hash_pairs and @ hash_alist
.synb
.mets (hash-keys << hash )
.mets (hash-values << hash )
.mets (hash-pairs << hash )
.mets (hash-alist << hash )
.syne
.desc
These functions retrieve the bulk key-value data of hash table
.meta hash
in various ways.
.code hash-keys
retrieves a list of the keys.
.code hash-values
retrieves a list of the values.
.code hash-pairs
retrieves a list of pairs,
which are two-element lists consisting of the key, followed by the value.
Finally,
.code hash-alist
retrieves the key-value pairs as a Lisp association list:
a list of cons cells whose
.code car
fields are keys, and whose
.code cdr
fields are the values. Note that
.code hash-alist
returns the actual entries from the hash table, which are
conses. Modifying the
.code cdr
fields of these conses constitutes modifying the hash values
in the original hash table. Modifying the
.code car
fields interferes with the integrity of the hash table.

These functions all retrieve the keys and values in the
same order. For example, if the keys are retrieved with
.codn hash-keys ,
and the values with
.codn hash-values ,
then the corresponding entries from
each list pairwise correspond to the pairs in
.metn hash .

The list returned by each of these functions is lazy, and hence constitutes
an open traversal of the hash table.

.coNP Operator @ dohash
.synb
.mets (dohash ( < key-var < value-var < hash-form <> [ result-form ])
.mets \ \  << body-form *)
.syne
.desc
The
.code dohash
operator iterates over a hash table. The
.meta hash-form
expression must
evaluate to an object of hash table type. The
.meta key-var
and
.meta value-var
arguments must be symbols suitable for use as variable names.
Bindings are established for these variables over the scope of the
.metn body-form s
and the optional
.metn result-form .

For each element in the hash table, the
.meta key-var
and
.meta value-var
variables are set to the key and value of that entry, respectively,
and each
.metn body-form ,
if there are any, is evaluated.

When all of the entries of the table are thus processed, the
.meta result-form
is evaluated, and its return value becomes the return value of the dohash form.
If there is no
.metn result-form ,
the return value is
.codn nil .

The
.meta result-form
and
.metn body-form s
are in the scope of an implicit anonymous
block, which means that it is possible to terminate the execution of
dohash early using
.cblk
.meti (return << value )
.cble
or
.codn (return) .

.coNP Functions @, hash-uni @ hash-diff and @ hash-isec
.synb
.mets (hash-uni < hash1 < hash2 <> [ join-func ])
.mets (hash-diff < hash1 << hash2 )
.mets (hash-isec < hash1 < hash2 <> [ join-func ])
.syne
.desc
These functions perform basic set operations on hash tables in a nondestructive
way, returning a new hash table without altering the inputs. The arguments
.meta hash1
and
.meta hash2
must be compatible hash tables.  This means that their keys
must use the same kind of equality.

The resulting hash table inherits attributes from
.metn hash1 ,
as if created by the
.code make-similar-hash
function. If
.meta hash1
has userdata, the resulting hash table
has the same userdata. If
.meta hash1
has weak keys, the resulting table has weak
keys, and so forth.

The
.code hash-uni
function performs a set union. The resulting hash contains all of
the keys from
.meta hash1
and all of the keys from
.metn hash2 ,
and their corresponding
values.  If a key occurs both in
.meta hash1
and
.metn hash2 ,
then it occurs only once
in the resulting hash. In this case, if the
.meta join-func
argument is not given,
the value associated with this key is the one from
.metn hash1 .
If
.meta join-func
is specified then it is called with two arguments: the respective
data items from
.meta hash1
and
.metn hash2 .
The return value of this function is used
as the value in the union hash.

The
.code hash-diff
function performs a set difference. First, a copy of
.meta hash1
is made as if by the
.code copy-hash
function. Then from this copy, all keys which occur
in
.code hash2
are deleted.

The
.code hash-isec
function performs a set intersection. The resulting hash contains
only those keys which occur both in
.meta hash1
and
.metn hash2 .
If
.meta join-func
is not
specified, the values selected for these common keys are those from
.metn hash1 .
If
.meta join-func
is specified, then for each key which occurs in both
.meta hash1
and
.metn hash2 ,
it is called with two arguments: the respective data items. The return
value is then used as the data item in the intersection hash.

.SS* Partial Evaluation and Combinators
.coNP Macros @ op and @ do
.synb
.mets (op << form +)
.mets (do << form +)
.syne
.desc
The
.code op
and
.code do
macro operators are similar.

Like the lambda operator, the
.code op
operator creates an anonymous function based on its syntax.
The difference is that the arguments of the function are implicit, or
optionally specified within the function body, rather than as a formal
parameter list before the body.

Also, the
.meta form
arguments of
.code op
are implicitly turned into a DWIM expression,
which means that argument evaluation follows Lisp-1 rules.  (See the
.code dwim
operator).

The.
.code do
operator is like the
.code op
operator with the following difference:
the
.meta form
arguments of
.code op
are not implicitly treated as a DWIM expression,
but as an ordinary expression. In particular, this means that operator
syntax is permitted. Note that the syntax
.code (op @1)
makes sense, since
the argument can be a function, which will be invoked, but
.code (do @1)
doesn't
make sense because it will produce a Lisp-2 form like
.code (#:arg1 ...)
referring
to nonexistent function
.codn #:arg1 .
Because it accepts operators,
.code do
can be used with imperative constructs
which are not functions, like set: like set: for instance
.code (do set x)
produces
an anonymous function which, if called with one argument, stores that argument
into
.codn x .

The argument forms are arbitrary expressions, within which a special
convention is permitted:
.RS
.meIP >> @ num
A number preceded by a
.code @
is a metanumber. This is a special syntax
which denotes an argument. For instance
.code @2
means that the second argument of
the anonymous function is to be substituted in place of the
.codn @2 .
.code op
generates a function which has a number of required arguments equal to the
highest value of
.meta num
apearing in a
.cblk
.mati >> @ num
.cble
construct in the body. For instance
.code (op car @3)
generates a three-argument function (which passes its third
argument to
.codn car ,
returning the result, and ignores its first two arguments).
There is no way to use
.code op
to generate functions which have optional arguments.
.meIP < @rest
If the meta-symbol
.meta @rest
appears in the
.code op
syntax, it explicitly denotes the list of trailing arguments,
allowing them to be placed anywhere in the expression.
.RE

.IP
Functions generated by
.code op
are always variadic; they always take additional arguments after
any required ones, whether or not the
.meta @rest
syntax is used.

If the body does not contain
any
.meta @num
or
.meta @rest
syntax, then
.code @rest
is implicitly inserted.  What this means is that, for example, since
the form
.code (op foo)
does not contain any numeric positional arguments like
.codn @1 ,
and does not contain
.codn @rest ,
it is actually a shorthand for
.codn (op foo . @rest) :
a function which applies all of its arguments to
.codn foo .

The actions of
.code op
be understood by these examples, which show
how
.code op
is rewritten to lambda.  However, note that the real translator
uses generated symbols for the arguments, which are not equal to any
symbols in the program.

.cblk
  (op) -> invalid

  (op +) -> (lambda rest [+ . rest])

  (op + foo) -> (lambda rest [+ foo . rest])

  (op @1 @2) -> (lambda (arg1 arg2 . rest) [arg1 arg2])

  (op @1 . @rest) -> (lambda (arg1 . rest) [arg1 . @rest])

  (op @1 @rest) -> (lambda (arg1 . rest) [arg1 @rest])

  (op @1 @2) -> (lambda (arg1 arg2 . rest) [arg1 arg2])

  (op foo @1 (@2) (bar @3)) -> (lambda (arg1 arg2 arg3 . rest)
                                  [foo arg1 (arg2) (bar arg3)])

  (op foo @rest @1) -> (lambda (arg1 . rest) [foo rest arg1])

  (do + foo) -> (lambda rest (+ foo . rest))

  (do @1 @2) -> (lambda (arg1 arg2 . rest) (arg1 arg2))

  (do foo @rest @1) -> (lambda (arg1 . rest) (foo rest arg1))
.cble

Note that if argument
.meta @n
appears, it is not necessary
for arguments
.meta @1
through
.meta @n-1
to appear. The function will have
.code n
arguments:

.cblk
  (op @3) -> (lambda (arg1 arg2 arg3 . rest) [arg3])
.cble

The
.code op
and
.code do
operators can be nested, in any combination. This raises the
question: if a metanumber like
.code @1
or
.code @rest
occurs in an
.code op
that is nested
within an
.codn op , what is the meaning?

A metanumber always belongs with the inner-most op or do operator. So for
instance
.code (op (op @1))
means that an
.code (op @1)
expression is nested
within an
.code op
expression which itself contains no meta-syntax.
The
.code @1
belongs with the inner op.

There is a way for an inner
.code op
to refer to an outer op metanumber argument. This is
expresed by adding an extra
.code @
prefix for every level of escape. For example in
.code (op (op @@1))
the
.code @@1
belongs to the outer
.codn op :
it is the same as
.code @1
appearing in the outer
.codn op .
That is to say,
in the expression
.codn (op @1 (op @@1)) ,
the
.code @1
and
.code @@1
are the same thing:
both are parameter 1 of the lambda function generated by the outer
.codn op .
By contrast, in the expression
.code (op @1 (op @1))
there are two different parameters:
the first
.code @1
is argument of the outer function, and the second
.code @1
is the first argument of the inner function. Of course, if there
are three levels of nesting, then three
.code @
meta-prefixes are needed to insert
a parameter from the outermost
.code op
into the innermost
.codn op .

.TP* Examples:

.cblk
  ;; Take a list of pairs and produce a list in which those pairs
  ;; are reversed.

  (mapcar (op list @2 @1) '((1 2) (a b)))   ->   ((2 1) (b a))
.cble

.coNP Macros @, ap @, ip @ ado and @ ido.
.synb
.mets (ap << form +)
.mets (ip << form +)
.mets (ado << form +)
.mets (ido << form +)
.syne
.desc
The
.code ap
macro is based on the
.code op
macro and has identical argument
conventions.

The
.code ap
macro analyzes its arguments and produces a function
.metn f ,
in exactly the same same way as the
.code op
macro.  However, instead of returning
.metn f ,
directly, it returns a different function
.metn g ,
which is a one-argument function  which accepts a list,
and then applies the list as arguments to
.metn f .

In other words, the following equivalence holds:

.cblk
  (ap form ...) <--> (apf (op form ...))
.cble

The
.code ap
macro nests properly with
.code op
and
.codn do ,
in any combination, in regard to the
.meta ...@@n
notation.

The
.ode ip
macro is very similar to the
.code ap
macro, except that it is based
on the semantics of the function
.code iapply
rather than
.codn apply ,
according
to the following equivalence:

.cblk
  (ip form ...) <--> (ipf (op form ...))
.cble

The
.code ado
and
.code ido
macros are related to do macro in the same way that
.code ap
and
.code ip
are related to
.codn op .
They produce a one-argument function which works
as if by applying its arguments to the function generated by do,
according to the following equivalence:

.cblk
  (ado form ...) <--> (apf (do form ...))

  (ido form ...) <--> (ipf (do form ...))
.cblk

See also: the
.code apf
and
.code ipf
functions.

.coNP Macros @ opip and @ oand
.synb
.mets (opip << clause *)
.mets (oand << clause *)
.syne
.desc
The
.code opip
and
.code oand
operators make it possible to chain together functions which are expressed
using the
.code op
syntax. (See the
.code op
operator for more information).

Both macros perform the same transformation except that
.code opip
translates its arguments to a call to the
.code chain
function, whereas
.code oand
transaltes its arguments in the same way to a call to the
.code chand
function.

More precisely, these macros perform the following rewrites:

.cblk
  (opip arg1 arg2 ... argn) -> [chain {arg1} {arg2} ... {argn}]
  (oand arg1 arg2 ... argn) -> [chand {arg1} {arg2} ... {argn}]
.cble

where the above
.code {arg}
notation denotes the following transformation applied to each argument:

.cblk
  (function ...) -> (op function ...)
  (operator ...) -> (do operator ...)
  (macro ...)    -> (do macro ...)
  (dwim ...)     -> (dwim ...)
  [...]          -> [...]
  atom           -> atom
.cble

In other words, compound forms whose leftmost symbol is a macro or operator
are translated to the
.code do
notation. Compound forms denoting function calls are translated to the
.code op
notation. Compound forms which are
.code dwim
invocations, either explicit or via the DWIM brackets notation, are
preserved, as are any forms which are atoms.

Note: the
.code opip
and
.code oand
macros use their macro environment in determining whether a form is a
macro call, thereby respecting lexical scoping.

.TP* Example:
Take each element from the list
.code (1 2 3 4)
and multiply it by three, then add 1.
If the result is odd, collect that into the resulting list:

.cblk
(mappend (opip (* 3)
               (+ 1)
               [iff oddp list])
         (range 1 4))
.cble

The above is equivalent to:

.cblk
(mappend (chain (op * 3)
                (op + 1)
                [iff oddp list])
         (range 1 4))
.cble

The
.code (* 3)
and
.code (+ 1)
terms are rewriten to
.code (op * 3)
and
.codn (op + 1) ,
respectively, whereas
.code [iff oddp list]
is passed through untransformed.

.coNP Macro @ ret
.synb
.mets (ret << form )
.syne
.desc
The
.code ret
macro's
.meta form
argument is treated similarly to the second and subsequent arguments of the
.code op
operator.

The
.code ret
macro produces a function which takes any number of arguments,
and returns the value specified by
.metn form .

.meta form
can contain
.code op
meta syntax like
.meta @n
and
.codn @rest .

The following equivalence holds:

.cblk
  (ret x) <--> (op identity x))
.cble

Thus the expression
.code (ret @2)
returns a function similar to
.codn (lambda (x y . z) y) ,
and the expression
.code (ret 42)
returns a function similar to
.codn (lambda (. rest) 42) .

.coNP Macro @ aret
.synb
.mets (aret << form )
.syne
.desc
The
.code ret
macro's
.meta form
argument is treated similarly to the second and subsequent arguments of the
.code op
operator.

The
.code aret
macro produces a function which takes any number of arguments,
and returns the value specified by
.metn form .

.meta form
can contain
.code ap
meta syntax like
.meta @n
and
.codn @rest .

The following equivalence holds:

.cblk
  (aret x) <--> (ap identity x))
.cble

Thus the expression
.code (aret @2)
returns a function similar to
.codn (lambda (. rest) (second rest)) ,
and the expression
.code (aret 42)
returns a function similar to
.codn (lambda (. rest) 42) .

.coNP Function @ dup
.synb
.mets (dup << func )
.syne
.desc
The
.code dup
function returns a one-argument function which calls the two-argument
function
.metn func
by duplicating its argument.

.TP* Example:

.cblk
  ;; square the elements of a list
  (mapcar [dup *] '(1 2 3)) -> (1 4 9)
.cble

.coNP Function @ flip
.synb
.mets (flip << func )
.syne
.desc
The
.code flip
function returns a two-argument function which calls the two-argument
function
.metn func
with reversed arguments.

.coNP Functions @ chain and @ chand
.synb
.mets (chain << func *)
.mets (chand << func *)
.syne
.desc
The
.code chain
function accepts zero or more functions as arguments, and returns
a single function, called the chained function, which represents the chained
application of those functions, in left to right order.

If
.code chain
is given no arguments, then it returns a variadic function which
ignores all of its arguments and returns
.codn nil .

Otherwise, the first function may accept any number of arguments. The second
and subsequent functions, if any, must accept one argument.

The chained function can be called with an argument list which is acceptable
to the first function. Those arguments are in fact passed to the first
function. The return value of that call is then passed to the second
function, and the return value of that call is passed to the third function
and so on. The final return value is returned to the caller.

The
.code chand
function is similar, except that it combines the functionality of
.code andf
into chaining. The difference between
.code chain
and
.code chand
is that
.code chand
immediately terminates and returns
.code nil
whenever any of the functions returns
.codn nil ,
without evaluating the remainder of the functions.

.TP* Example:

.cblk
  (call [chain + (op * 2)] 3 4) -> 14
.cble

In this example, a two-element chain is formed from the
.code +
function
and the function produced by
.code (op * 2)
which is a one-argument
function that returns the value of its argument multiplied by two.
(See the definition of the
.code op
operator).

The chained function is invoked using the
.code call
function, with the arguments
.code 3
and
.codn 4 .
The chained evaluation begins by passing
.code 3
and
.code 4
to
.codn + ,
which yields
.codn 7 .
This
.code 7
is then passed to the
.code (op * 2)
doubling function, resulting in
.codn 14 .

A way to write the above example without the use of the DWIM brackets and the
op operator is this:

.cblk
  (call (chain (fun +) (lambda (x) (* 2 x))) 3 4)
.cble

.coNP Function @ juxt
.synb
.mets (juxt << func *)
.syne
.desc
The
.code juxt
function accepts a variable number of arguments which are functions.  It
combines these into a single function which, when invoked, passes its arguments
to each of these functions, and collects the results into a list.

Note: the juxt function can be understood in terms of the following reference
implementation:

.cblk
  (defun juxt (funcs)
    (lambda (. args)
      (mapcar (lambda (fun)
                (apply fun args))
              funcs)))
.cble

.TP* Example:

.cblk
   ;; separate list (1 2 3 4 5 6) into lists of evens and odds,
   ;; which end up juxtaposed in the output list:

   [(op [juxt keep-if remove-if] evenp)
    '(1 2 3 4 5 6)] -> ((2 4 6) (1 3 5))

   ;; call several functions on 1, collecting their results:
   [[juxt (op + 1) (op - 1) evenp sin cos] 1]'
   -> (2 0 nil 0.841470984807897 0.54030230586814)
.cble

.coNP Functions @ andf and @ orf
.synb
.mets (andf << func *)
.mets (orf << func *)
.syne
.desc
The
.code andf
and
.code orf
functions are the functional equivalent of the
.code and
and
.code or
operators. These functions accept multiple functions and return a new function
which represents the logical combination of those functions.

The input functions should have the same arity. Failing that, there should
exist some common argument arity with which each of these can be invoked. The
resulting combined function is then callable with that many arguments.

The
.code andf
function returns a function which combines the input functions with
a short-circuiting logical conjunction. The resulting function passes its
arguments to the functions successively, in left to right order. As soon as any
of the functions returns
.codn nil ,
then nil is returned immediately, and the
remaining functions are not called.  Otherwise, if none of the functions return
.codn nil ,
then the value returned by the last function is returned. If the list of
functions is empty, then
.code t
is returned.  That is,
.code (andf)
returns a function
which accepts any arguments, and returns
.codn t .

The
.code orf
function combines the input functions with a short-circuiting logical
disjunction. The function produced by
.code orf
passes its arguments down to the
functions successively, in left to right order.  As soon as any function
returns a
.cod2 non- nil
value, that value is returned and the remaining functions are
not called. If all functions return
.codn nil ,
then
.code nil
is returned. The expression
.code (orf)
returns a function which accepts any arguments and returns
.codn nil .

.coNP Function @ notf
.synb
.mets (notf << function )
.syne
.desc
The
.code notf
function returns a function which is the boolean negation
of
.metn function .

The returned function takes a variable number of arguments. When
invoked, it passes all of these arguments to
.meta function
and then inverts the result as if by application of the
.codn not .

.coNP Functions @ iff and @ iffi
.synb
.mets (iff < cond-func >> [ then-func <> [ else-func ]])
.mets (iffi < cond-func < then-func <> [ else-func ])
.syne
.desc
The
.code iff
function is the functional equivalent of the
.code if
operator. It accepts
functional arguments and returns a function.

The resulting function takes its arguments, if any, and applies them to
.metn cond-func .
If
.meta cond-func
yields true, then the arguments are passed to
.meta then-func
and the
resulting value is returned. Otherwise the arguments are passed to
.meta else-func
and the resulting value is returned.

If
.meta then-func
is omitted then
.code identity
is used as default. This omission is not permitted by
.codn iffi ,
only
.codn iff .

If
.meta else-func
needs to be called, but is omitted, then
.code nil
is returned.

The
.code iffi
function differs from
.code iff
only in the defaulting behavior with respect
to the
.meta else-func
argument. If
.meta else-func
is omitted in a call to
.code iffi
then the deafult function is
.codn identity .
This is useful in situations when one value is to be
replaced with another one when the condition is true, otherwise
preserved.

The following equivalences hold between
.code iffi
and
.codn iff :

.cblk
  (iffi a b c)            <--> (iff a b c)

  (iffi a b)              <--> (iff a b identity)

  (iffi a b false)        <--> (iff a b)

  (iffi a identity false) <--> (iff a)
.cble

The following equivalence illustrates
.code iff
with both optional arguments omitted:

.cblk
  (iff a)  <--->  (iff a identity false)
.cble

.coNP Functions @ tf and @ nilf
.synb
.mets (tf << arg *)
.mets (nilf << arg *)
.syne
.desc
The
.code tf
and
.code nilf
functions take zero or more arguments, and ignore them.
The
.code tf
function returns
.codn t ,
and the
.code nilf
function returns
.codn nil .

Note: the following equivalences hold between these functions and the
.code ret
operator, and
.code retf
function.

.cblk
  (fun tf) <--> (ret t) <--> (retf t)
  (fun nilf) <--> (ret nil) <--> (ret) <--> (retf nil)
.cble

In Lisp-1-style code,
.code tf
and
.code nilf
behave like constants which can replace uses of
.code (ret t)
and
.codn (ret nil) :

.cblk
  [mapcar (ret nil) list] <--> [mapcar nilf list]
.cble

.TP* Example:

.cblk
  ;; tf and nilf are useful when functions are chained together.
  ;; test whether (trunc n 2) is odd.

  (defun trunc-n-2-odd (n)
    [[chain (op trunc @1 2) [iff oddp tf nilf]] n])
.cble

In this example, two functions are chained together, and
.code n
is passed
through the chain such that it is first divided by two via the
function denoted by
.code (op trunc @1 2)
and then the result is passed into the
function denoted by
.codn [iff oddp tf nilf] .
The
.code iff
function passes its argument into
.codn oddp ,
and if
.code oddp
yields true, it passes the same argument to
.codn tf .
Here
.code tf
proves its utility by ignoring that value and returning
.codn t .
If the argument (the divided value) passed into
.code iff
is even, then iff passes it into the
.code nilf
function, which ignores the value and returns
.codn nil .

.coNP Function retf
.synb
.mets (retf << value )
.syne
.desc
The
.code retf
function returns a function. That function can take zero or
more arguments. When called, it ignores its arguments and returns
.metn value .

See also: the
.code ret
macro.

.TP* Example:

.cblk
  ;; the function returned by (retf 42)
  ;; ignores 1 2 3 and returns 42.
  (call (retf 42) 1 2 3) -> 42
.cble

.coNP Functions @ apf and @ ipf
.synb
.mets (apf << function )
.mets (ipf << function )
.syne
.desc
The
.code apf
function returns a one-argument function which accepts
a list. When the function is called, it treats the list as
arguments which are applied to
.meta function
as if by apply. It returns whatever
.meta function
returns.

The
.code ipf
function is similar to
.codn apf ,
except that the returned
function applies arguments as if by
.code iapply
rather than
.codn apply .

See also: the
.code ap
macro.

.TP* Example:

.cblk
  ;; Function returned by [apf +] accepts the
  ;; (1 2 3) list and applies it to +, as
  ;; if (+ 1 2 3) were called.

  (call [apf +] '(1 2 3)) -> 6
.cble

.coNP Function @ callf
.synb
.mets (callf < main-function << arg-function *)
.syne
.desc
The
.code callf
function returns a function which applies its arguments to each
.metn arg-function ,
juxtaposing the return values of these calls to form arguments
which are then passed to
.metn main-function .
The return value of
.meta main-function
is returned.

The following equivalence holds, except for the order of evaluation of
arguments:

.cblk
  (callf fm f0 f1 f2 ...) <--> (chain (juxt f0 f1 f2 ...) (apf fm))
.cble

.TP* Example:

.cblk
  ;; Keep those pairs which are two of a kind

  (keep-if [callf eql first second] '((1 1) (2 3) (4 4) (5 6)))
  -> ((1 1) (4 4))
.cble

.coNP Function @ mapf
.synb
.mets (mapf < main-function << arg-function *)
.syne
.desc
The
.code mapf
function returns a function which distributes its arguments
into the
.metn arg-function -s.
That is to say, each successive argument of the returned
function is associated with a successive
.metn arg-function .

Each
.metn arg-function
is called, passed the corresponding argument. The return
values of these functions are then passd as arguments
to
.meta main function
and the resulting value is returned.

If the returned function is calle with fewer arguments than there
are
.metn arg-function -s,
then only that many functions are used. Conversely, if the function is
called with more arguments than there are
.metn arg-function -s, then those arguments are ignored.

The following equivalence holds:

.cblk
  (mapf fm f0 f1 ...) <--> (lambda (. rest)
                              [apply fm [mapcar call (list f0 f1 ...) rest]])
.cble

.TP* Example:

.cblk
  ;; Keep those pairs which are two of a kind

  (keep-if [callf eql first second] '((1 1) (2 3) (4 4) (5 6)))
  -> ((1 1) (4 4))
.cble

.SS* Input and Output (Streams)
\*(TL supports input and output streams of various kinds, with
generic operations that work across the stream types.

In general, I/O errors are usually turned into exceptions. When the description
of error reporting is omitted from the description of a function, it can be
assumed that it throws an error.

.coNP Special variables @, *stdout* @, *stddebug* @, *stdin* @ *stderr* and @ *stdnull*
.desc
These variables hold predefined stream objects. The
.codn *stdin* ,
.code *stdout*
and
.code *stderr*
streams closely correspond to the underlying operating system streams.
Various I/O functions require stream objects as arguments.

The
.code *stddebug*
stream goes to the same destination as
.codn *stdout* ,
but is a separate object which can be redirected independently, allowing
debugging output to be separated from normal output.

The
.code *stdnull*
stream is a special kind of stream called a null stream.
This stream is not connected to any device or file.  It is similar to
the
.code /dev/null
device on Unix, but does not involve the operating system.

.coNP Function @ format
.synb
.mets (format < stream-designator < format-string << format-arg *)
.syne
.desc
The
.code format
function performs output to a stream given by
.metn stream-designator ,
by interpreting the actions implicit in a
.metn format-string ,
incorporating material pulled from additional arguments given by
.cblk
.meti << format-arg *.
.cble
Though the function is simple to invoke, there is complexity in format string
language, which is documented below.

The
.meta stream-designator
argument can be a stream object, or one of the values
.code t
or
.codn nil .
The value
.code t
serves as a shorthand for
.codn *stdout* .
The value
.code nil
means that the function will send output into a newly instantiated string
output stream, and then return the resulting string.

.TP* "Format string syntax:"

Within
.metn format-string ,
most characters represent themselves. Those
characters are simply output. The character
.code ~
(tilde) introduces formatting
directives, which are denoted by a single character, usually a letter.

The special sequence
.code ~~
(tilde-tilde) encodes a single tilde. Nothing is
permitted between the two tildes.

The syntax of a directive is generally as follows:

.cblk
.mets <> ~[ width ] <> [, precision ] < letter
.cble

In other words, the
.code ~
(tilde) character, followed by a
.meta width 
specifier, a
.meta precision 
specifier introduced by a comma,
and a
.metn letter ,
such that
.meta width
and
.meta precision
are independently optional: either or both may be omitted.
No whitespace is allowed between these elements.

The
.meta letter
is a single alphabetic character which determines the
general action of the directive. The optional width and precision
are specified as follows:

.RS
.meIP < width
The width specifier consists of an optional
.code <
(left angle bracket) character,
followed by an optional width specification.

If the leading
.code <
character is present, then the printing will be left-adjusted within
this field. Otherwise it will be right-adjusted by default.

The width can be specified as a decimal integer, or as the character
.codn * .
The
.code *
notation means that instead of digits, the value of the next argument is
consumed, and expected to be an integer which specifies the width. If that
integer value is negative, then the field will be left-adjusted.
If the value is positive, but the
.code <
character is present in the width
specifier, then the field is left adjusted.

.meIP < precision
The precision specifier is introduced by a leading comma. If this comma appaers
immediately after the directive's
.code ~
character, then it means that
.meta width
is being omitted; there is only a precision field.

The precision specifier may begin with these optional characters:
.RS
.coIP 0
(the "leading zero flag"),
.coIP +
(print a sign for positive values")
.IP space
(print a space in place of a positive sign).
.RE

The precision specifier itself is either a decimal integer that does not
begin with a zero digit, or the
.code *
character.

The precision field's components have a meaning which depends on the type of
object printed and the conversion specifier.

For integer arguments, the precision value specifies the minimum number of digits
to print.  If the precision field has a leading zero flag, then the integer is
padded with zeros to the required number of digits, otherwise the number is
padded with spaces instead of zeros.  If zero or space padding is present, and
a leading positive or negative sign must be printed, then it is placed before
leading zeros, or after leading spaces, as the case may be.

For floating-point values, the meaning of the precision value depends on which
specific conversion specifier
.cod1 ( f ,
.codn e ,
.code a
or
.codn s )
is used. The details are
documented in the description of each of these, below. The leading zero flag is
ignored for floating-point values regardless of the conversion specifier.

For integer or floating-point arguments, if the precision specifier has a
.code +
sign
among the special characters, then a
.code +
sign is printed for positive numbers. If
the precision specifier has a leading space instead of a
.code +
sign, then the
.ocde +
sign is rendered as a space for positive numbers. If there is no leading space
or
.codn + ,
then a sign character is omitted for positive numbers. Negative
numbers are unconditionally prefixed with a
.code -
sign.

For all other objects, the precision specifies the maximum number of characters
to print.  The object's printed representation is crudely truncated at that
number of characters.
.RE

.TP* "Format directives:"
.RS
Format directives are case sensitive, so that for example
.code ~x
and
.code ~X
have a
different effect, and
.code ~A
doesn't exist whereas
.code ~a
does. They are:

.coIP a
Prints any object in an aesthetic way, as if by the
.code pprint
function.
The aesthetic notation violates read-print consistency: this notation
is not necessarily readable if it is implanted in \*(TX source code.
The field width specifier is honored, including the left-right adjustment
semantics.

When this specifier is used for floating-point values, the precision specifies
the maximum number of total significant figures, which do not include any
digits in the exponent, if one is printed. Numbers are printed in exponential
notation if their magnitude is small, or else if their exponent exceeds their
precision. (If the precision is not specified, then it defaults to the
system-dependent number of digits in a floating point value, derived from the C
language
.code DBL_DIG
constant.) Floating point values which are integers are
printed without a trailing
.code .0
(point zero).

.coIP s
Prints any object in a standard way, as if by the print function.  Objects for
which read-print consistency is possible are printed in a way such that
if their notation is implanted in \*(TX source, they are readable.
The field width specifier is honored, including the left-right adjustment
semantics. The precision field is treated very similarly to the
.code ~a
format directive, except that non-exponentiated floating point numbers that
would be mistaken for integers include a trailing
.code .0
for the sake of read-print
consistency. Objects truncated by precision may not have read-print
consistency. For instance, if a string object is truncated, it loses its
trailing closing quote, so that the resulting representation is no longer
a properly formed string object.

.coIP x
Requires an argument of character or integer type. The integer value or
character code is printed in hexadecimal, using lower-case letters
for the digits
.code a
through
.codn f .
Width and precision semantics
are as described for the
.code a
format directive, for integers.

.coIP X
Like the
.code x
directive, but the hexadecimal digits
.code a
through
.code f
are rendered in upper case.

.coIP o
Like the
.code x
directive, but octal is used instead of hexadecimal.

.coIP f
The
.code f
directive prints numbers in a fixed point decimal notation, with
a fixed number of digits after the decimal point. It requires a numeric
argument. (Unlike
.codn x ,
.code X
and
.codn o ,
it does not allow an argument of character type).
The precision specifier gives the number of digits past the decimal point.
The number is rounded off to the specified precision, if necessary.
Furthermore, that many digits are always printed, regardless of the actual
precision of the number or its type.  If it is omitted, then the default value
is three: three digits past the decimal point. A precision of zero means no
digits pas the decimal point, and in this case the decimal point is suppressed
(regardless of whether the numeric argument is floating-point or integer).  

.coIP e
The
.code e
directive prints numbers in exponential notation. It requires
a numeric argument. (Unlike
.codn x ,
.code X
and
.codn o ,
it does not allow an argument of character type).
The precision specifier gives the number of digits past the decimal point
printed in the exponential notation, not counting the digits in the exponent.
Exactly that many digits are printed, regardless of the precision of the
number.  If the precision is omitted, then the number of digits after the
decimal point is three. If the precision is zero, then a decimal portion is
truncated off entirely, including the decimal point.

.coIP p
The
.code p
directive prints a numeric representation in hexadecimal of the bit pattern
of the object, which is meaningful to someone familiar with the internals
of \*(TX.  If the object is a pointer to heaped data, that value
has a correspondence to its address.
.RE

.PP

.coNP Functions @, print @, pprint @, prinl @, pprinl @ tostring and @ tostringp
.synb
.mets (print < obj <> [ stream ])
.mets (pprint < obj <> [ stream ])
.mets (prinl < obj <> [ stream ])
.mets (pprinl < obj <> [ stream ])
.mets (tostring << obj )
.mets (tostringp << obj )
.syne
.desc
The
.code print
and
.code pprint
functions render a printed character representation of the
.meta obj
argument into
.metn stream .
If a stream argument is not supplied, then
the destination is the stream currently stored in the
.code *stdout*
variable. The
.code print
function renders in a way which strives for read-print
consistency: an object is printed in a notation which is recognized as
a similar object of the same kind when it appears in \*(TX source code.
The
.code pprint
function ("pretty print") does not strive for read-print consistency.
For instance it prints a string object simply by dumping its characters, rather
than by adding the surrounding quotes and rendering escape syntax for
special characters. Both functions return
.metn obj .

The
.code prinl
and
.code pprinl
functions are like
.code print
and
.codn pprint ,
except that they issue a newline character after printing the object.
These functions also return
.metn obj .

The
.code tostring
and
.code tostringp
functions are like
.code print
and
.codn pprint ,
but they do not accept a stream argument. Instead they print to a freshly
instantiated string stream, and return the resulting string.

The following equivalences hold between calls to the
.code format
function and calls to the above functions:

.cblk
  (format stream "~s" obj)  <-->  (print obj stream)
  (format t "~s" obj)       <-->  (print obj)
  (format t "~s\en" obj)     <-->  (prinl obj)
  (format nil "~s" obj)     <-->  (tostring obj)
.cble

For
.codn pprint ,
.code tostringp
and
.codn pprinl ,
the equivalence is produced by using
.code ~a
in format rather than
.codn ~s .

Note: for characters, the print function behaves as follows: most control
characters in the Unicode
.code C0
and
.code C1
range are rendered using the
.code #\ex
notation,
using two hex digits. Codes in the range
.code D800
to
.codn DFFF ,
and the codes
.code FFFE
and
.code FFFF
are printed in the
.code #\exNNNN
with four hexadecimal digits, and
character above this range are printed using the same notation, but with six
hexadecimal digits. Certain characters in the
.code C0
range are printed using
their names such as
.code #\enul
and
.codn #\ereturn ,
which are documented
in the Character Literals section.
The
.code DC00
character is printed as
.codn #\epnul .
All other characters are printed as
.cblk
.meti >> #\e char
.cble
where
.meta char
is the actual character.

Caution: read-print consistency is affected by trailing material. If additional
digits are printed immediately after a number without intervening whitespace,
they extend that number. If hex digits are printed after the character
.codn x ,
which is rendered as
.codn #\ex ,
they look like a hex character code.

.coNP Function @ streamp
.synb
.mets (streamp << obj )
.syne
.desc
The
.code streamp
function returns
.code t
if
.meta obj
is any type of stream.  Otherwise it returns
.codn nil .

.coNP Function @ real-time-stream-p
.synb
.mets (real-time-stream-p << obj )
.syne
.desc
The
.code real-time-streamp-p
function returns
.code t
if
.meta obj
is a stream marked as
"real-time".  If
.meta obj
is not a stream, or not a stream marked as "real-time",
then it returns
.codn nil .

Only certain kinds of streams accept the real-time attribute: file streams and
tail streams. This attribute controls the semantics of the application of
.code lazy-stream-cons
to the stream.  For a real-time stream,
.code lazy-stream-cons
returns a stream with "naive" semantics which returns data as soon as it is
available, at the cost of generating spurious
.code nil
item when the stream
terminates. The application has to recognize and discard that
.code nil
item.
The ordinary lazy streams read ahead by one line and suppress this extra
item, so their representation is more accurate.

Streams connected to TTY devices (devices which for which the
.code isatty
POSIX function reports true) are automatically marked as real-time. This is
only supported on platforms that have an
.code isatty
function.

.coNP Function @ make-string-input-stream
.synb
.mets (make-string-input-stream << string )
.syne
.desc
This function produces an input stream object. Character read operations on the
stream object read successive characters from
.metn string .
Output operations and byte operations are not supported.

.coNP Function @ make-string-byte-input-stream
.synb
.mets (make-string-byte-input-stream << string )
.syne
.desc
This function produces an input stream object. Byte read operations on
this stream object read successive byte values obtained by encoding
.meta string
into UTF-8. Character read operations are not supported, and neither
are output operations.

.coNP Function @ make-string-output-stream
.synb
  (make-string-output-stream)
.syne
.desc
This function, which takes no arguments, creates a string output stream.
Data sent to this stream is accumulated into a string object.
String output streams supports both character and byte output operations.
Bytes are assumed to represent a UTF-8 encoding, and are decoded in order
to form characters which are stored into the string.

If an incomplete UTF-8 code is output, and a character output operation then
takes place, that code is assumed to be terminated and is decoded as invalid
bytes.  The UTF-8 decoding machine is reset and ready for the start of a new
code.

The
.code get-string-from-stream
function is used to retrieve the accumulated string.

If the null character is written to a string output stream, either via
a character output operation or as a byte operation, the resulting string
will appear to be prematurely terminated. \*(TX strings cannot contain null
bytes.

.coNP Function @ get-string-from-stream
.synb
.mets (get-string-from-stream << stream )
.syne
.desc
The
.meta stream
argument must be a string output stream. This function finalizes
the data sent to the stream and retrieves the accumulated character string.

If a partial UTF-8 code has been written to
.metn stream ,
and then this
function is called, the byte stream is considered complete and the partial
code is decoded as invalid bytes.

After this function is called, further output on the stream is not possible.

.coNP Function @ make-strlist-output-stream
.synb
  (make-strlist-output-stream)
.syne
.desc
This function is very similar to
.codn make-string-output-stream .
However, the stream object produced by this function does not produce a string,
but a list of strings.  The data is broken into multiple strings by newline
characters written to the stream. Newline characters do not appear in the
string list. Also, byte output operations are not supported.

.coNP Function @ get-list-from-stream
.synb
.mets (get-list-from-stream << stream )
.syne
.desc
This function returns the string list which has accumulated inside
a string output stream given by
.metn stream .
The string output stream is
finalized, so that further output is no longer possible.

.coNP Function @ close-stream
.synb
.mets (close-stream < stream <> [ throw-on-error-p ])
.syne
.desc
The
.code close-stream
function performs a close operation on
.metn stream ,
whose meaning is depends on the type of the stream. For some types of streams,
such as string streams, it does nothing. For streams which are connected
to operating system files or devices, will perform a close of the underlying
file descriptor, and dissociate that descriptor from the stream. Any buffered
data is flushed first.

.code close-stream
returns a boolean true value if the close has occurred without
errors, otherwise
.codn nil .

For most streams, "without errors" means that any buffered output data is
flushed successfully.

For command and process pipes (see open-command and open-process), success also
means that the process terminates normally, with a successful error code, or an
unsuccessful one. An abnormal termination is considered an error, as
as is the inability to retrieve the termination status, as well as the situation
that the process continues running in spite of the close attempt.
Detecting these situations is platform specific.

If the
.meta throw-on-error-p
argument is specified, and isn't
.codn nil ,
then the
function throws an exception if an error occurs during the close operation
instead of returning
.codn nil .

.coNP Functions @, get-error @ get-error-str and @ clear-error
.synb
.mets (get-error < stream )
.mets (get-error-str < stream )
.mets (clear-error < stream )
.syne
.desc
When a stream operation fails, the
.code get-error
and
.code get-error-str
functions may be used to inquire about a more detailed cause of the error.

Not all streams support these functions to the same extent. For instance,
string input streams have no persistent state. The only error which occurs
is the condition when the string has no more data.

The
.code get-error
inquires
.meta stream
about its error condition.

The function returns
.code nil
to indicate there is no error condition,
.code t
to indicate an end-of-data condition,
or else a value which is specific to the stream type indicating the
specific error type.

Note: for some streams, it is possible for the
.code t
value to be returned even though no operation has failed; that is to say, the
streams "know" they are at the end of the data even though no read operation
has failed. Code which depends on this will not work with streams which
do not thus indicate the end-of-data
.I a priori,
but by means of a read operation which fails.

The
.code get-error-str
function returns a text representation of the error code. The
.code nil
error code is represented as the string
.codn "no error" ;
the
.code t
error code as
.code "eof"
and other codes have a stream-specific representation.

The
.code clear-error
function removes the error situation from a stream. On some streams, it does
nothing. If an error has occurred on a stream, this function should be called
prior to re-trying any I/O or positioning operations.

.coNP Functions @, get-line @ get-char and @ get-byte
.synb
.mets (get-line <> [ stream ])
.mets (get-char <> [ stream ])
.mets (get-byte <> [ stream ])
.syne
.desc
These fundamental stream functions perform input.  The
.meta stream
argument
is optional. If it is specified, it should be an input stream which supports
the given operation. If it is not specified, then the
.code *stdin*
stream is used.

The
.code get-char
function pulls a character from a stream which supports character
input.  Streams which support character input also support the
.code get-line
function which extracts a line of text delimited by the end of the stream or a
newline character and returns it as a string. (The newline character does not
appear in the string which is returned).

Character input from streams based on bytes requires UTF-8 decoding, so that
get-char actually may read several bytes from the underlying low level
operating system stream.

The
.code get-byte
function bypasses UTF-8 decoding and reads raw bytes from
any stream which supports byte input. Bytes are represented as integer
values in the range 0 to 255.

Note that if a stream supports both byte input and character input, then mixing
the two operations will interfere with the UTF-8 decoding.

These functions return
.code nil
when the end of data is reached.  Errors are
represented as exceptions.

See also:
.code get-lines

.coNP Function @ get-string
.synb
.mets (get-string >> [ stream >> [ count <> [ close-after-p ]]])
.syne
.desc
The
.code get-string
function reads characters from a stream, and assembles them into
a string, which is returned. If the
.meta stream
argument is omitted, then the
.code *stdin*
stream is used.

The stream is closed after extracting the data, unless
.meta close-after-p
is specified as
.codn nil .
The default value of this argument is
.codn t .

If the
.meta count
argument is missing, then all of the characters from the
stream are read and assembled into a string.

If present, the
.meta count
argument should be a positive integer indicating
a limit on how many characters to read. The returned string will be no
longer than
.metn count ,
but may be shorter.

.coNP Functions @ unget-char and @ unget-byte
.synb
.mets (unget-char < char <> [ stream ])
.mets (unget-byte < byte <> [ stream ])
.syne
.desc
These functions put back, into a stream, a character or byte which was
previously read.  The character or byte must match the one which was most
recently read. If the
.meta stream
argument is omitted, then the
.code *stdin*
stream is used.

If the operation succeeds, the byte or character value is returned.
A
.code nil
return indicates that the operation is unsupported.

Some streams do not support these operations; some support
only one of them.   In general, if a stream supports
.codn get-char ,
it supports
.codn unget-char ,
and likewise for
.code get-byte
and
.codn unget-byte .

Space is available for only one character or byte of pushback.

Pushing both a byte and a character, in either order, is also unsupported.
Pushing a byte and then reading a character, or pushing a character and
reading a byte, are unsupported mixtures of operations.

If the stream is binary, then pushing back a byte decrements its position,
except if the position is already zero. At that point, the position becomes
indeterminate.

.coNP Functions @, put-string @, put-line @ put-char and @ put-byte
.synb
.mets (put-string < string <> [ stream ])
.mets (put-line >> [ string <> [ stream ]])
.mets (put-char < char <> [ stream ])
.mets (put-byte < byte <> [ stream ])
.syne
.desc
These functions perform output on an output stream. The
.meta stream
argument
must be an output stream which supports the given operation. If it is omitted,
then
.code *stdout*
is used.

The
.code put-char
function writes a character given by
.code char
to a stream. If the
stream is based on bytes, then the character is encoded into UTF-8 and multiple
bytes are written. Streams which support
.code put-char
also support put-line, and
.codn put-string .

The
.code put-string
function writes the characters of a string out to
the stream as if by multiple calls to put-char. The
.meta string
argument
may be a symbol, in which case its name is used as the string.

The
.code put-line
function is like
.codn put-string ,
but also writes an additional newline
character. The string is optional in
.codn put-line ,
and defaults to the empty string.

The
.code put-byte
function writes a raw byte given by the
.meta byte
argument
to
.metn stream ,
if
.meta stream
supports a byte write operation. The byte
value is specified as an integer value in the range 0 to 255.

.coNP Functions @ put-strings and @ put-lines
.synb
.mets (put-strings < sequence <> [ stream ]])
.mets (put-lines < sequence <> [ stream ]])
.syne
.desc
These functions assume
.meta sequence
to be a sequence of strings, or of
symbols, or a mixture thereof. These strings are sent to the stream.  The
.meta stream
argument must be an output stream.  If it is omitted, then
.code *stdout*
is used.

The
.code put-strings
function iterates over
.meta sequence
and writes each element
to the stream as if using the
.code put-string
function.

The
.code put-lines
function iterates over
.code sequence
and writes each element
to the stream as if using the
.code put-line
function.

Both functions return
.codn t .

.coNP Function @ flush-stream
.synb
.mets (flush-stream << stream )
.syne
.desc
This function is meaningful for output streams which accumulate data
which is passed on to the operating system in larger transfer units.
Calling
.code flush-stream
causes all accumulated data inside
.meta stream
to be passed
to the operating system. If called on streams for which this function is not
meaningful, it does nothing.

.coNP Function @ seek-stream
.synb
.mets (seek-stream < stream < offset << whence )
.syne
.desc
The
.code seek-stream
function is meaningful for file streams. It changes the
current read/write position within
.metn stream .
It can also be used to determine the current position: see the notes about the
return value below. 

The
.meta offset
argument is a positive or negative integer which gives a
displacement that is measured from the point identified by the
.meta whence
argument.

Note that for text files, there isn't necessarily a 1:1 correspondence between
characters and positions due to line-ending conversions and conversions
to and from UTF-8.

The
.meta whence
argument is one of three keywords: :from-start, :from-current
and :from-end. These denote the start of the file, the current position
and the end of the file.

If
.meta offset
is zero, and
.meta whence
is
.codn :from-current ,
then
.code seek-stream
returns the current absolute position within the
stream, if it can successfully obtain it. Otherwise, it
returns
.code t
if it is successful.

If a character has been successfully put back into a text stream with
.code unget-char
and is still pending, then the position value is unspecified. If a
byte has been put back into a binary stream with
.codn unget-byte ,
and the previous position wasn't zero, then the position is decremented by one.

On failure, it throws an exception of type
.codn stream-error .

.coNP Functions @ stream-get-prop and @ stream-set-prop
.synb
.mets (stream-get-prop < stream << indicator )
.mets (stream-set-prop < stream < indicator << value )
.syne
.desc
These functions get and set properties on a stream. Only certain properties
are meaningful with certain kinds of streams, and the meaning depends on
the stream. If two or more stream types support a property of the same name, it
is expected that the property has the same or very similar meaning for both
streams to the maximum extent that similarity is possible.

The
.code stream-set-prop
function sets a property on a stream. The
.meta indicator
argument is a symbol, usually a keyword symbol, denoting the property,
and
.meta value
is the property value. If the stream understands and accepts the
property, the function returns
.codn t .
Otherwise it returns
.codn nil .

The
.code stream-get-prop
function inquires about the value of a property on a
stream. If the stream understands the property, then it returns its current
value.  If the stream does not understand a property, nil is returned, which is
also returned if the property exists, but its value happens to be
.codn nil .

Properties are currently used for marking certain streams as "real-time" (see
the
.code stream-real-time-p
function above), and also for setting the priority at
which messages are reported to syslog by the
.code *stdlog*
stream (see
.code *stdlog*
in the UNIX SYSLOG section).

If
.meta stream
is a catenated stream (see the function
.codn make-catenated-stream )
then these functions transparently operate on the current head stream of the
catenation.

.coNP Functions @ make-catenated-stream and @ cat-streams
.synb
.mets (make-catenated-stream << stream *)
.mets (cat-streams << stream-list )
.syne
.desc
The
.code make-catenated-stream
function takes zero or more arguments which
are input streams of the same type, and combines
them into a single virtual stream called a catenated stream.

The
.code cat-streams
function takes a single list of input streams of
the same type, and similarly combines them into a catenated stream.

A catenated stream does not support seeking operations or output,
regardless of the capabilities of the streams in the list.

If the stream list is not empty, then the leftmost element of the
list is called the head stream.

The
.codn get-char ,
.codn get-byte ,
.codn get-line ,
.code unget-char
and
.code unget-byte
functions delegate
to the corresponding operations on the head stream, if it exists.
If the stream list is empty, they return
.code nil
to the caller.

If the
.codn get-char ,
.code get-byte
or
.code get-line
operation on the head stream yields
.codn nil ,
and there are more lists in the stream, then the stream is closed, removed from
the list, and the next stream, if any, becomes the head list. The operation is
then tried again.  If any of these operations fail on the last list, it is not
removed from the list, so that a stream remains in place which can take the
.code unget-char
or
.code unget-byte
operations.

In this manner, the catenated streams appear to be a single stream.

Note that the operations can fail due to being unsupported. It is
the caller's responsibility to make sure all of the streams in the list
are compatible with the intended operations.

If the stream list is empty then an empty catenated stream is produced.
Input operations on this stream yield
.codn nil ,
and the
.code unget-char
and
.code unget-byte
operations throw an exception.

.coNP Functions @ open-files and @ open-files*
.synb
.mets (open-files < path-list <> [ alternative-stream ])
.mets (open-files* < path-list <> [ alternative-stream ])
.syne
.desc
The
.code open-files
and
.code open-files*
functions create a list of streams by invoking
the open-file function on each element of
.metn path-list .
These streams are turned
into a catenated stream as if applied as arguments to
.codn make-catenated-stream .

The effect is that multiple files appear to be catenated together into a single
input stream.

If the optional
.meta alternative-stream
argument is supplied, then if
.meta path-list
is empty,
.meta alternative-stream
is returned instead of an empty catenated stream.

The difference between
.code open-files
and
.code open-files*
is that
.code open-files
creates all of the
streams up-front. So if any of the paths cannot be opened, the operation throws.
The
.code open-files*
variant is lazy: it creates a lazy list of streams out of the
path list. The streams are opened as needed: before the second stream is opened,
the program has to read the first stream to the end, and so on.

.TP* Example:

Collect lines from all files that are given as arguments on the command line. If
there are no files, then read from standard input:

.cblk
   @(next @(open-files *args* *stdin*))
   @(collect)
   @line
   @(end)
.cble

.coNP Function @ abs-path-p
.synb
.mets (abs-path-p << path )
.syne
.desc
The
.code abs-path-function
tests whether the argument
.meta path
is an absolute path, returning a
.code t
or
.code nil
indication.

An absolute path is a string which either begins with a slash or backslash
character, or which begins with an alphanumeric word, followed by a colon,
followed by a slash or backslash.

Examples of absolute paths:

.cblk
  /etc
  c:/tmp
  ftp://user@server
  disk0:/home
  Z:\eUsers
.cble

Examples of strings which are not absolute paths.

.cblk
.mets < (the < empty < string)
  .
  abc
  foo:bar/x
  $:\eabc
.cble

.coNP Function @ read
.synb
.mets (read >> [ source >> [ error-stream <> [ error-return-value ]]])
.syne
.desc
The
.code read
function converts text denoting \*(TL structure, into the
corresponding data structure. The
.meta source
argument may be either a character
string, or a stream.  If it is omitted, then
.code *stdin*
is used as the stream.

The source must provide the text representation of one complete \*(TL object.

Multiple calls to read on the same stream will extract successive objects
from the stream. To parse successive objects from a string, it is necessary
to convert it to a string stream.

The optional
.meta error-stream
argument can be used to specify a stream to which
parse errors diagnostics are sent. If absent, the diagnostics are suppressed.

If there are no parse errors, the function returns the parsed data
structure. If there are parse errors, and the
.meta error-return-value
parameter is
present, its value is returned. If the
.meta error-return-value
parameter
is not present, then an exception of type
.code syntax-error
is thrown.

.SS* Filesystem Access
.coNP Function @ stat
.synb
.mets (stat << path )
.syne
.desc
The
.code stat
function inquires the filesystem about the existence of an object
denoted by the string
.metn path .
If the object is not found or cannot be
accessed, an exception is thrown.

Otherwise, information is retrieved about the object. The information takes the
form of a property list in which keyword symbols denote numerous properties.
An example such property list is:

.cblk
  (:dev 2049 :ino 669944 :mode 16832 :nlink 23
   :uid 500 :gid 500 :rdev 0
   :size 12288 :blksize 4096 :blocks 24
   :atime 1347933533 :mtime 1347933534 :ctime 1347933534)
.cble

These properties correspond to the similarly-named entries of the
.code struct stat
structure in POSIX. For instance, the
.code :dev
property has the same value as the
.code st_dev
field.

.coNP Special variables @, s-ifmt @, s-iflnk @, s-ifreg @, s-ifblk ... , @ s-ixoth

The following variables exist, having integer values. These are bitmasks
which can be applied against the value given by the
.code :mode
property
in the property list returned by the function stat:
.codn s-ifmt ,
.codn s-ifsock ,
.codn s-iflnk ,
.codn s-ifreg ,
.codn s-ifblk ,
.codn s-ifdir ,
.codn s-ifchr ,
.codn s-ififo ,
.codn s-isuid ,
.codn s-isgid ,
.codn s-isvtx ,
.codn s-irwxu ,
.codn s-irusr ,
.codn s-iwusr ,
.codn s-ixusr ,
.codn s-irwxg ,
.codn s-irgrp ,
.codn s-iwgrp ,
.codn s-ixgrp ,
.codn s-irwxo ,
.codn s-iroth ,
.code s-iwoth
and
.codn s-ixoth .

These variables correspond to the C language constants from POSIX:
.codn S_IFMT ,
.codn S_IFLNK ,
.code S_IFREG
and so forth.

The
.code logtest
function can be used to test these against values of mode.
For example
.code (logtest mode s-irgrp)
tests for the group read permission.

.coNP Function @ open-directory
.synb
.mets (open-directory << path )
.syne
.desc
The
.code open-directory
function tries to create a stream which reads the
directory given by the string argument
.metn path .
If a filesystem object exists
under the path, is accessible, and is a directory, then the function
returns a stream. Otherwise, a file error exception is thrown.

The resulting stream supports the get-line operation. Each call to the
.code get-line
operation retrieves a string representing the next directory
entry. The value
.code nil
is returned when there are no more directory entries.
The
.code .
and
.code ..
entries in Unix filesystems are not skipped.

.coNP Function @ open-file
.synb
.mets (open-file < path <> [ mode-string ])
.syne
.desc
The
.code open-file
function creates a stream connected to the file
which is located at the given
.metn path ,
which is a string.

The
.meta mode-string
argument is a string which uses the same
conventions as the mode argument of the C language
.code fopen
function.
The mode string determines whether the stream is an input stream
or output stream. Note that the
.str b
mode is not supported.
Whether a stream is text or binary depends on which operations
are invoked on it.

If the
.meta mode-string
argument is omitted, mode
.str r
is used.

.coNP Function @ open-tail
.synb
.mets (open-tail < path <> [[ mode-string ] << seek-to-end-p ])
.syne
.desc
The
.code open-tail
function creates a tail stream connected to the file which is
located at the given
.metn path .
The
.meta mode-string
argument is a string which uses
the same conventions as the mode argument of the C language
.code fopen
function.
If it is missing, it defaults to
.strn r .
Note that the
.str b
mode is not supported.
Whether a stream is text or binary depends on which operations are invoked on
it.

The
.code seek-to-end-p
argument is a boolean which determines whether the initial
read/write position is at the start of the file, or just past the end.
It defaults to
.codn nil .
This argument only makes a difference if the file exists
at the time
.code open-tail
is called. If the file does not exist, and is later
created, then the tail stream will follow that file from the beginning.  In
other words,
.meta seek-to-end-p
controls whether the tail stream reads all the
existing data in the file, if any, or whether it reads only newly added data
from approximately the time the stream is created.

A tail stream has special semantics with regard to reading at the end
of file. A tail stream never reports an end-of-file condition; instead
it polls the file until more data is added. Furthermore, if the file
is truncated, or replaced with a smaller file, the tail stream follows
this change: it automatically opens the smaller file and starts reading from
the beginning (the
.meta seek-to-end-p
flag only applies to the initial open).
In this manner, a tail stream can dynamically growing rotating log files.

Caveat: since a tail stream can re-open a new file which has the same
name as the original file, it behave incorrectly if the program
changes the current working directory, and the path name is relative.

.coNP Function @ remove-path
.synb
.mets (remove-path << path )
.syne
.desc
The
.code remove-path
function tries to remove the filesystem object named
by
.metn path ,
which may be a file, directory or something else.

If successful, it returns
.codn t .

A failure to remove the object results in an exception of type
.codn file-error .

.coNP Function @ rename-path
.synb
.mets (rename-path < from-path << to-path )
.syne
.desc
The
.code remove-path
function tries to rename filesystem path
.metn from-path ,
which may refer to a file, directory or something else, to the path
.metn to-path .

If successful, it returns
.codn t .

A failure results in an exception of type
.codn file-error .

.SS* Coprocesses
.coNP Functions @ open-command and @ open-process
.synb
.mets (open-command < system-command <> [ mode-string ])
.mets (open-process < program < mode-string <> [ argument-string *])
.syne
.desc
These functions spawn external programs which execute concurrently
with the \*(TX program. Both functions return a unidirectional stream for
communicating with these programs: either an output stream, or an input
stream, depending on the contents of
.metn mode-string .

In
.codn open-command ,
the
.meta mode-string
argument is optional, defaulting to
the value
.str r
if it is missing.

The
.code open-command
function accepts, via the
.meta system-command
string parameter, a
system command, which is in a system-dependent syntax. On a POSIX system, this
would be in the POSIX Shell Command Language.

The
.code open-process
function specifies a program to invoke via the
.meta command
argument. This is subject to the operating system's search strategy.
On POSIX systems, if it is an absolute or relative path, it is treated as
such, but if it is a simple base name, then it is subject to searching
via the components of the PATH environment variable. If open-process
is not able to find
.metn program ,
or is otherwise unable to execute
the program, the child process will exit, using the value of the C variable
.code errno
as its exit status. This value can be retrieved via
.codn close-stream .

The
.meta mode-string
argument follows the convention used by the POSIX
.code popen
function.

The
.meta argument-strings
argument is a list of strings which specifies additional
optional arguments to be passed passed to the program. The
.meta program
argument
becomes the first argument, and
.meta argument-string
become the second and
subsequent arguments. If
.meta argument-strings
is omitted, it defaults to empty.

If a coprocess is open for writing
.cblk
.meti >> ( mode-string
.cble
is specified as
.strn w ),
then
writing on the returned stream feeds input to that program's standard input
file descriptor. Indicating the end of input is performed by closing the
stream.

If a coprocess is open for reading
.cblk
.meti >> ( mode-string
.cble
is specified as
.strn r ),
then
the program's output can be gathered by reading from the returned stream.
When the program finishes output, it will close the stream, which can be
detected as normal end of data.

If a coprocess terminates abnormally or unsuccessfully, an exception is raised.

.SS* Symbols and Packages
A package is an object which serves as a container of symbols.

A symbol which exists inside a package is said to be interned in that package.
A symbol can be interned in at most one package at a time.

Eacy symbol has a name, which is a string. It is not necessarily unique:
two distinct symbols can have the sama name.  However, a symbol name is unique
within a package, because it serves as the key which associates the
symbol with the package. Two symbols cannot be in the same package if they
have the same name.  Moreover, a symbol cannot exist in more than one package
at at time, although it can be relocated from one package to another.  A
symbols exist which is not entered into any package: such a symbol is
called "uninterned".

Packages are held in a global list which can be used to search for a package by
name. The
.code find-package
function performs this lookup.  A package may be
deleted from the list with the
.code delete-package
function, but it continues
to exist until the program loses the last reference to that package.

.coNP Special variables @, *user-package* @, *keyword-package* and @ *system-package*
.desc
These variables hold predefined packages. The
.code *user-package*
is the one
in which symbols are read when a \*(TX program is being scanned.
The
.code *keyword-package*
holds keyword symbols, which are printed with
a leading colon. The
.code *system-package*
is for internal symbols, helping
the implementation avoid name clashes with user code in some situations.

.coNP Function @ make-sym
.synb
.mets (make-sym << name )
.syne
.desc
The
.code make-sym
function creates and returns a new symbol object. The argument
.metn name ,
which must be a string, specifies the name of the symbol.  The symbol
does not belong to any package (it is said to be "uninterned").

Note: an uninterned symbol can be interned into a package with the
.code rehome-sym
function. Also see the
.code intern
function.

.coNP Function @ gensym
.synb
.mets (gensym <> [ prefix ])
.syne
.desc
The
.code gensym
function is similar to make-sym. It creates and returns a new
symbol object. If the
.meta prefix
argument is omitted, it defaults to
.strn g .
Otherwise it must be a string.

The difference between
.code gensym
and
.code make-sym
is that
.code gensym
creates the name
by combining the prefix with a numeric suffix.

The numeric suffix is a decimal digit string, taken from the value of
the variable
.codn *gensym-counter* ,
after incrementing it.

Note: the variation in name is not the basis of the uniqueness assurance
offered by
.code make-sym
and
.codn gensym ;
the basis is that the returned symbol is a freshly instantiated object.
.code make-sym
still returns unique symbols even if repeatedly called with the same
string.

.coNP Special variable @ *gensym-counter*
.desc
This variable is initialized to 0. Each time the
.code gensym
function is called,
it is incremented. The incremented value forms the basis of the numeric
suffix which gensym uses to form the name of the new symbol.

.coNP Function @ make-package
.synb
.mets (make-package << name )
.syne
.desc
The
.code make-package
function creates and returns a package named
.metn name ,
where
.meta name
is a string. It is an error if a package by that name exists already.

.coNP Function @ packagep
.synb
.mets (packagep << obj )
.syne
.desc
The
.code packagep
function returns
.codet
if
.meta obj
is a package, otherwise it returns
.codn nil .

.coNP Function @ find-package
.synb
.mets (find-package << name )
.syne
.desc
The argument
.meta name
should be a string. If a package called
.meta name
exists,
then it is returned. Otherwise
.code nil
is returned.

.coNP Function @ intern
.synb
.mets (intern < name <> [ package ])
.syne
.desc
The argument
.meta name
should be a symbol. The optional argument
.meta package
should be a package. If
.meta package
is not supplied, then the value
taken is that of
.codn *user-package* .

The
.code intern
function searches
.meta package
for a symbol called
.metn name .
If that symbol is found, it is returned. If that symbol is not found,
then a new symbol called
.meta name
is created and inserted into
.metn package ,
and that symbol is returned. In this case, the package becomes the
symbol's home package.

.coNP Function @ rehome-sym
.synb
.mets (rehome-sym < symbol <> [ package ])
.syne
.desc
The arguments
.meta symbol
and
.meta package
must be a symbol and package object,
respectively. If
.meta package
is not given, then it defaults to the value of
.codn *user-package* .

The
.code rehome-sym
function moves
.meta symbol
into
.metn package .
If
.meta symbol
is already in a package, it is first removed from that package.
If a symbol of the same name exists in
.meta package
that symbol is first removed
from
.metn package .

.coNP Function @ symbolp
.synb
.mets (symbolp << obj )
.syne
.desc
The
.code symbolp
function returns
.code t
if
.meta obj
is a symbol, otherwise it returns
.codn nil .

.coNP Function @ symbol-name
.synb
.mets (symbol-name << symbol )
.syne
.desc
The
.code symbol-name
function returns the name of
.metn symbol .

.coNP Function @ symbol-package
.synb
.mets (symbol-package << symbol )
.syne
.desc
The
.code symbol-package
function returns the home package of
.metn symbol .

.coNP Function @ packagep
.synb
.mets (packagep << obj )
.syne
.desc
The
.code packagep
function returns
.code t
if
.meta obj
is a package, otherwise it returns
.codn nil .

.coNP Function @ keywordp
.synb
.mets (keywordp << obj )
.syne
.desc
The
.code keywordp
function returns
.code t
if
.meta obj
is a keyword symbol, otherwise it
returns
.codn nil .

.SS* Pseudo-random Numbers
.coNP Special variable @ *random-state*
.desc
The
.code *random-state*
variable holds an object which encapsulates the state
of a pseudo-random number generator. This variable is the default argument
value for the
.code random-fixnum
and
.codn random functions ,
for the convenience of writing programs which are not concerned about the
management of random state.

On the other hand, programs can create and manage random states, making it
possible to obtain repeatable sequences of pseudo-random numbers which do not
interfere with each other. For instance objects or modules in a program can
have their own independent streams of random numbers which are repeatable,
independently of other modules making calls to the random number functions.

When \*(TX starts up, the
.code *random-state*
variable is initialized with
a newly created random state object, which is produced as if by
the call
.codn (make-random-state 42) .

.coNP Function @ make-random-state
.synb
.mets (make-random-state <> [ seed ])
.syne
.desc
The
.code make-random-state
function creates and returns a new random state,
an object of the same kind as what is stored in the
.code *random-state*
variable.

The seed, if specified, must be either an integer value, or an
existing random state object.

Note that the sign of the seed is ignored, so that negative seed
values are equivalent to their additive inverses.

If the seed is not specified, then
.code make-random-state
produces a seed based
on some information in the process environment, such as current
time of day. It is not guaranteed that two calls to
.code (make-random-state)
that are separated by less than some minimum increment of real time produce
different seeds.  The minimum time increment depends on the platform.

On a platform with a millisecond-resolution real-time clock, the minimum
time increment is a millisecond. Calls to make-random-state less than
a millisecond apart may predictably produce the same seed.

If an integer seed is specified, then the integer value is mapped to a
pseudo-random sequence, in a platform-independent way.

If a random state is specified as a seed, then it is duplicated. The
returned random state object is a distinct object which is in the same
state as the input object. It will produce the same remaining pseudo-random
number sequence, as will the input object.

.coNP Function @ random-state-p
.synb
.mets (random-state-p << obj )
.syne
.desc
The
.code random-state-p
function returns
.code t
if
.meta obj
is a random state, otherwise it
returns
.codn nil .

.coNP Functions @, random-fixnum @ random and @ rand
.synb
.mets (random-fixnum <> [ random-state ])
.mets (random < random-state << modulus )
.mets (rand < modulus <> [ random-state ])
.syne
.desc
All three functions produce pseudo-random numbers, which are positive integers.

The numbers are obtained from a WELLS 512 pseudo-random number generator, whose
state is stored in the random state object.

The
.code random-fixnum
function produces a random fixnum integer: a reduced range
integer which fits into a value that does not have to be heap-allocated.

The
.code random
and
.code rand
functions produce a value in the range [0,
.metn modulus ).
They differ only in the order of arguments. In the
.code rand
function, the random state
object is the second argument and is optional. If it is omitted, the global
.code *random-state*
object is used.

.SS* Time
.coNP Functions @ time and @ time-usec
.synb
  (time)
  (time-usec)
.syne
.desc
The
.code time
function returns the number of seconds that have elapsed since
midnight, January 1, 1970, in the UTC timezone.

The
.code time-usec
function returns a cons cell whose
.code car
field holds the seconds measured in the same way, and whose
.code cdr
field extends the precision by giving
number of microseconds as an integer value between 0 and 999999.

.coNP Functions @ time-string-local and @ time-string-utc
.synb
.mets (time-string-local < time << format )
.mets (time-string-utc < time << format )
.syne
.desc
These functions take the numeric time returned by the
.code time
function, and convert it to a textual representation in a flexible way,
according to the contents of the
.meta format
string.

The
.code time-string-local
function converts the time to the local timezone of
the host system. The
.code time-string-utc
function produces time in UTC.

The
.meta format
argument is a string, and follows exactly the same conventions as
the format string of the C library function
.codn strftime .

The
.meta time
argument is an integer representing seconds obtained from the
time function or from the
.code car
field of the cons returned by the
.code time-usec
function.

.coNP Functions @ time-fields-local and @ time-fields-utc
.synb
.mets (time-fields-local << time )
.mets (time-fields-utc << time )
.syne
.desc
These functions take the numeric time returned by the time function,
and convert it to a list of seven fields.

The
.code time-string-local
function converts the time to the local timezone of
the host system. The
.code time-string-utc
function produces time in UTC.

The fields returned as a list consist of six integers, and a boolean value.
The six integers represent the year, month, day, hour, minute and second.
The boolean value indicates whether daylight savings time is in effect
(always
.code nil
in the case of
.codn time-fields-utc ).

The
.meta time
argument is an integer representing seconds obtained from the
.code time
function or from the
.code time-usec
function.

.coNP Functions @ make-time and @ make-time-utc
.synb
.mets (make-time < year < month < day < hour < minute < second << dst-advice )
.mets (make-time-utc < year < month < day < hour < minute < second << dst-advice )
.syne
.desc
The
.code make-time
function returns a time value, similar to the one returned by the
.code time
function.  The
.code time
value is constructed not from the system clock, but
from a date and time specified as arguments. The
.meta year
argument is a calendar year, like 2014.
The
.meta month
argument ranges from 1 to 12.
The
.meta hour
argument is a 24-hour time, ranging from 0 to 23.
These arguments represent a local time, in the current time zone.

The
.meta dst-advice
argument specifies whether the time is expressed in
daylight savings time (DST). It takes on three possible values:
.codn nil ,
the keyword
.codn :auto ,
or else the symbol
.codn t .
Any other value has the same interpretation as
.codn t .

If
.meta dst-advice
is
.codn t ,
then the time is assumed to be expressed in DST.
If the argument is
.codn nil ,
then the time is assumed not to be in DST.
If
.meta dst-advice
is
.codn :auto ,
then the function tries to determine whether
DST is in effect in the current time zone for the specified date and time.

The
.code make-time-utc
function is similar to
.codn make-time ,
except that
it treats the time as UTC rather than in the local time zone.
The
.meta dst-advice
argument is supported by
.code make-time-utc
for function
call compatibility with
.codn make-time .
It may or may not have any effect
on the output (since the UTC zone by definition doesn't have daylight
savings time).

.SS* Environment Variables and Command Line
.coNP Special variables @ *args* and @ *args-full*
.desc
The
.code *args*
variable holds a list of strings representing the remaining
arguments which follow any options processed by the \*(TX executable,
and the script name.

The
.code *args-full*
variable holds the original, complete list of arguments passed
from the operating system.

Note: the
.code *args*
variable is
.code nil
during the processing of the command line,
so \*(TL expressions invoked using the
.code -p
or
.code -e
option cannot use it.

.coNP Function @ env
.synb
  (env)
.syne
.desc
The
.code env
function retrieves the list of environment variables. Each
variable is represented by a single entry in the list: a string which
contains an
.code =
(equal) character somewhere, separating the variable name
from its value.

See also: the
.code env-hash
function.

.coNP Function @ env-hash
.synb
  (env-hash)
.syne
.desc
The
.code env-hash
function constructs and returns an
.code :equal-based
hash.  The hash is
populated with the environment variables, represented as key-value pairs. 

.SS* System Programming
.coNP Function @ errno
.synb
.mets (errno <> [ new-errno ])
.syne
.desc
The
.code errno
function retrieves the current value of the C library error variable
.codn errno .
If the argument
.meta new-errno
is present and is not
.codn nil ,
then it
specifies a value which is stored into
.codn errno .
The value returned is the prior value.

.coNP Function @ exit
.synb
.mets (exit << status )
.syne
.desc
The
.code exit
function terminates the entire process (running \*(TX image), specifying
the termination status to the operating system. Values of
.meta status
may be
.codn nil ,
.codn t ,
or an integer value.  The value
.code nil
corresponds to the C constant
.codn EXIT_FAILURE ,
and
.code t
corresponds to
.codn EXIT_SUCCESS .
These are platform-independent
indicators of failed or successful termination. The numeric value 0 also
indicates success.

.coNP Function @ abort
.synb
.mets (abort)
.syne
.desc
The
.code abort
function terminates the entire process (running \*(TX image), specifying
an abnormal termination status to the process.

Note:
.code abort
calls the C library function
.code abort
which works by raising the
.code SIG_ABRT
signal, known in \*(TX as the
.code sig-abrt
variable. Abnormal termination of the process is this signal's
default action.

.coNP Function @ usleep
.synb
.mets (usleep << usec )
.syne
.desc
The
.code usleep
function suspends the execution of the program for at least
.meta usec
microseconds.

The return value is
.code t
if the sleep was successfully executed. A
.code nil
value indicates premature wakeup or complete failure.

Note: the actual sleep resolution is not guaranteed, and depends on granularity
of the system timer.  Actual sleep times may be rounded up to the nearest 10
millisecond multiple on a system where timed suspensions are triggered by a 100
Hz tick.

.coNP Functions @ mkdir and @ ensure-dir
.synb
.mets (mkdir < path <> [ mode ])
.mets (ensure-dir < path <> [ mode ])
.syne
.desc
.code mkdir
tries to create the directory named
.meta path
using the POSIX
.code mkdir
function.
An exception of type
.code file-error
is thrown if the function fails. Returns
.code t
on success.

The
.meta mode
argument specifies the request numeric permissions
for the newly created directory. If omitted, the requested permissions are
.code #o777
(511): readable and writable to everyone. The requested permissions
are subject to the system
.codn umask .

The function
.code ensure-dir
is similar to
.code mkdir
except that it attempts to create all the missing parent directories
as necessary, and does not throw an error if the directory exists.

.coNP Function @ chdir
.synb
.mets (chdir << path )
.syne
.desc
.code chdir
changes the current working directory to
.metn path ,
and returns
.metn t ,
or else throws an exception of type
.codn file-error .

.coNP Function @ pwd
.synb
  (pwd)
.syne
.desc
The
.code pwd
function retrieves the current working directory.
If the underlying
.code getcwd
C library function fails with an
.code errno
other than
.codn ERANGE ,
an exception will be thrown.

.coNP Functions @ getpid and @ getppid
.synb
  (getpid)
  (getppid)
.syne
.desc
These functions retrieve the current proces ID and the parent process ID
respectively. They are wrappers for the POSIX functions
.code getpid
and
.codn getppid .

.coNP Function @ daemon
.synb
.mets (daemon < nochdir-p << noclose-p )
.syne
.desc
This is a wrapper for the function
.code daemon
which originated in BSD Unix.

It returns
.code t
if successful,
.code nil
otherwise, and the
.code errno
variable is set in that case.

.coNP Functions @, makedev @ minor and @ major
.synb
.mets (makedev < minor << major )
.mets (minor << dev )
.mets (major << dev )
.syne
.desc
The parameters
.metn minor ,
.meta major
and
.meta dev
are all integers.  The
.code makedev
function constructs a combined device number from a minor and major pair (by
calling the Unix
.code makedev
function).  This device number is suitable as an
argument to the
.codee mknod
function (see below). Device numbers also appear the
.code :dev
property returned by the
.code stat
function.

The
.code minor
and
.code major
functions extract the minor and major device number
from a combined device number.

.coNP Function @ mknod
.synb
.mets (mknod < path < mode <> [ dev ])
.syne
.desc
The
.code mknod
function tries to create an entry in the filesystem: a file,
FIFO, or a device special file, under the name
.metn path .
If it is successful,
it returns
.codn t ,
otherwise it throws an exception of type
.codn file-error .

The
.meta mode
argument is a bitwise or combination of the requested permissions,
and the type of object to create: one of the constants
.codn s-ifreg ,
.codn s-ififo ,
.codn s-ifchr ,
.code s-ifblk
or
.codn s-ifsock .
The permissions are subject to the system
.codn umask .

If a block or character special device
.cod2 ( s-ifchr
or
.codn s-ifblk )
is being
created, then the
.meta dev
argument specifies the major and minor numbers
of the device. A suitable value can be constructed from a major and minor
pair using the
.code makedev
function.

.TP* Example:

.cblk
   ;; make a character device (8, 3) called /dev/foo
   ;; requesting rwx------ permissions

   (mknod "dev/foo" (logior #o700 s-ifchr) (makedev 8 3))
.cble

.coNP Functions @ symlink and @ link
.synb
.mets (symlink < target << path )
.mets (link < target << path )
.syne
.desc
The
.code symlink
function creates a symbolic link called
.meta path
whose contents
are the absolute or relative path
.metn target .
.meta target
does not actually have to exist.

The link function creates a hard link. The object at
.meta target
is installed
into the filesystem at
.meta path
also.

If these functions succeed, they return
.codn t .
Otherwise they throw an exception
of type
.codn file-error .

.coNP Function @ readlink
.synb
.mets (readlink << path )
.syne
.desc
If
.meta path
names a filesystem object which is a symbolic link, the
.code readlink
function reads the contents of that symbolic link and returns it
as a string.  Otherwise, it fails by throwing an exception of type
.codn file-error .

.coNP Functions @ sh and @ run
.synb
.mets (sh << system-command )
.mets (run < program <> [ argument-list ])
.syne
.desc
The
.code sh
function executes
.meta system-command
using the system command interpreter.
The run function spawns a
.metn program ,
searching for it using the
system PATH.  Using either method, the executed process receives environment
variables from the parent.

\*(TX blocks until the process finishes executing. If the program terminates
normally, then its integer exit status is returned. The value zero indicates
successful termination.

The return value
.code nil
indicates an abnormal termination, or the inability
to run the process at all.

In the case of the
.code run
function, if the child process is created successfully
but the program cannot be executed, then the exit status will be an
.code errno
value from the failed
.code exec
attempt.

.SS* Unix Signal Handling

On platforms where certain advanced features of POSIX signal handling are
available at the C API level, \*(TX exposes signal-handling functionality.

A \*(TX program can install a \*(TL function (such as an anonymous.
.codn lambda ,
or the function object associated with a named function) as the handler for
a signal.

When that signal is delivered, \*(TX will intercept it with its own safe,
internal handler, mark the signal as deferred (in a \*(TX sense) and then
dispatch the registered function at a convenient time.

Handlers currently are not permitted to interrupt the execution of most
\*(TX internal code.  Immediate, asynchronous execution of handlers is
currently enabled only while \*(TX is blocked on I/O operations or sleeping.
Additionally, the
.code sig-check
function can be used to dispatch and clear deferred
signals. These handlers are then safely called if they were subroutines of
.codn sig-check ,
and not asynchronous interrupts.

.coNP Special variables @, sig-hup @, sig-int @, sig-quit @, sig-ill @, sig-trap @, sig-abrt @, sig-bus @, sig-fpe @, sig-kill @, sig-usr1 @, sig-segv @, sig-usr2 @, sig-pipe @, sig-alrm @, sig-term @, sig-chld @, sig-cont @, sig-stop @, sig-tstp @, sig-ttin @, sig-ttou @, sig-urg @, sig-xcpu @, sig-xfsz @, sig-vtalrm @, sig-prof @, sig-poll @, sig-sys @, sig-winch @, sig-iot @, sig-stkflt @, sig-io @ sig-lost and @ sig-pwr
.desc
These variables correspond to the C signal constants
.codn SIGHUP ,
.code SIGINT
and so forth.
The variables
.codn sig-winch ,
.codn sig-iot ,
.codn sig-stk flt,
.codn sig-io ,
.code sig-lost
and
.code sig-pwr
may not be available since a system may lack the corresponding signal
constants. See notes for the function
.codn log-authpriv .

The highest signal number is 31.

.coNP Functions @ set-sig-handler and @ get-sig-handler
.synb
.mets (set-sig-handler < signal-number << handling-spec )
.mets (get-sig-handler << signal-number )
.syne
.desc
The
.code set-sig-handler
function is used to specify the handling for a signal, such
as the installation of a handler function. It updates the signal handling for
a signal whose number is
.meta signal-number
(usually one of the constants like
.codn sig-hup ,
.code sig-int
and so forth), and returns the previous value.  The
.code get-sig-handler
function returns the current value.

The
.meta signal-number
must be an integer the range 1 to 31.

Initially, all 31 signal handling specifications are set to the value
.codn t .

The
.meta handling-spec
parameter may be a function. If a function is specified,
then the signal is enabled and connected to that function until another
call to
.code set-sig-handler
changes the handling for that signal.

If
.meta handling-spec
is the symbol
.codn nil ,
then the function previously associated
with the signal, if any, is removed, and the signal is disabled. For a signal
to be disabled means that the signal is set to the
.code SIG_IGN
disposition (refer to the C API).

If
.meta handling-spec
is the symbol
.codn t ,
then the function previously associated
with the signal, if any, is removed, and the signal is set to its default
disposition. This means that it is set to
.code SIG_DFL
(refer to the C API).
Some signals terminate the process if they are generated while the
handling is configured to the default disposition.

Note that the certain signals like
.code sig-quit
and
.code sig-kill
cannot be ignored or handled.
Please observe the signal documentation in the IEEE POSIX standard, and your
platform.

A signal handling function must take two arguments. It is of the form:

.cblk
.mets (lambda >> ( signal << async-p ) ...)
.cble

The
.meta signal
argument is an integer indicating the signal number for which the
handler is being invoked. The
.meta asyncp-p
argument is a boolean value.
If it is
.codn t ,
it indicates that the handler is being invoked
asynchronously\(emdirectly in a signal handling context.  If it is
.codn nil ,
then it
is a deferred call.  Handlers may do more things in a deferred call, such
as terminate by throwing exceptions, and perform I/O.

The return value of a handler is normally ignored. However if it invoked
asynchronously (the
.meta async-p
argument is true), then if the handler returns
a
.cod2 non- nil
value, it is understood that the handler
requesting that it be deferred. This means that the signal will be marked
as deferred, and the handler will be called again at some later
time in a deferred context, whereby
.meta async-p
is
.codn nil .
This is not guaranteed, however;
it's possible that another signal will arrive before that happens,
possibly resulting in another async call, so the handler must
be prepared to deal with an async call at any time.

If a handler is invoked synchronously, then its return value is ignored.

In the current implementation, signals do not queue. If a signal is delivered
to the process again, while it is marked as deferred, it simply stays deferred;
there is no counter associated with a signal, only a boolean flag.

.coNP The @ sig-check function
.synb
  (sig-check)
.syne
.desc
The
.code sig-check
function tests whether any signals are deferred, and for each
deferred signal in turn, it executes the corresponding handler.  For a signal to
be deferred means that the signal was caught by an internal handler in
\*(TX and the event was recorded by a flag.  If a handler function is removed
while a signal is deferred, the deferred flag is cleared for that signal.

Calls to the
.code sig-check
function may be inserted into CPU-intensive code that
has no opportunity to be interrupted by signals, because it doesn't invoke any
I/O functions.

.coNP The @ kill function
.synb
.mets (kill < process-id <> [ signal ])
.syne
.desc
The
.code kill
function is used for sending a signal to a process group or process.
It is a wrapper for the POSIX
.code kill
function.

If the
.meta signal
argument is omitted, it defaults to the same value as
.codn sig-term .

.SS* Unix Itimers
Itimers ("interval timers") can be used in combination with signal handling to
execute asynchronous actions. Itimers deliver delayed, one-time signals,
and also periodically recurring signals. For more information, consult the
POSIX specification.

.coNP Variables @, itimer-real @, itimer-virtual and @ itimer-prof
.desc
These variables correspond to the POSIX constants
.codn ITIMER_REAL ,
.code ITIMER_VIRTUAL
and
.codn ITIMER_PROF .
Their values are suitable as the
.meta timer
argument of the
.code getitimer
and
.code setitimer
functions.

.coNP Functions @ getitimer and @ setitimer
.synb
.mets (getitimer << timer )
.mets (setitimer < timer < interval << value )
.syne
.desc
The
.code getitimer
function returns the current value of the specified timer,
which must be
.codn itimer-real ,
.code itimer-virtual
or
.codn itimer-prof .

The current value consists of a list of two integer values, which
represents microseconds. The first value is the timer interval,
and the second value is the timer's current value.

Like
.codn getitimer ,
the
.code setitimer
function also retrieves the specified timer.
In addition, it stores a new value in the timer,
which is given by the two arguments, expressed in microseconds.

.SS* Unix Syslog

On platforms where a Unix-like syslog API is available, \*(TX exports this
interface. \*(TX programs can configure logging via the
.code openlog
function,
control the logging mask via
.code setlogmask
and generate logs via
.codn syslog ,
or using special syslog streams.

.coNP Special variables @, log-pid @, log-cons @, log-ndelay @, log-odelay @ log-nowait and @ log-perror
.desc
These variables take on the values of the corresponding C preprocessor
constants from the
.code <syslog.h>
header:
.codn LOG_PID ,
.codn LOG_CON S, etc.
These integer values represent logging options used in the option argument to
the
.code openlog
function.

Note:
.code LOG_PERROR
is not in POSIX, and so
.code log-perror
might not be available.
See notes about
.code LOG_AUTHPRIV
in the documentation for
.codn log-authpriv .

.coNP Special variables @, log-user @, log-daemon @ log-auth and @ log-authpriv
.desc
These variables take on the values of the corresponding C preprocessor
constants from the
.code <syslog.h>
header:
.codn LOG_USER ,
.codn LOG_DAEMON ,
.code LOG_AUTH
and
.codn LOG_AUTHPRIV .
These are the integer facility codes specified in the
.code openlog
function.

Note:
.code LOG_AUTHPRIV
is not in POSIX, and so
.code log-authpriv
might not be available.
For portability use code like
.code (or (symbol-value 'log-authpriv) 0)
to evaluate to 0 if
.code log-authpriv
doesn't exist, or else check for its existence
using
.codn (boundp 'log-authpriv) .

.coNP Special variables @, log-emerg @, log-alert @, log-crit @, log-err @, log-warning @, log-notice @ log-info and @ log-debug

These variables take on the values of the corresponding C preprocessor
constants from the
.code <syslog.h>
header:
.codn LOG_EMERG ,
.codn LOG_ALERT ,
etc.
These are the integer priority codes specified in the
.code syslog
function.

.coNP The @ *stdlog* special variable
.desc
The
.code *stdlog*
variable holds a special kind of stream: a syslog stream.  Each
newline-terminated line of text sent to this stream becomes a log message.

The stream internally maintains a priority value that is applied
when it generates messages. By default, this value is that of
.codn log-info .
The stream holds the priority as the value of the
.code :prio
stream property, which may be changed with the
.code stream-set-prop
function.

The latest priority value which has been configured on the stream is used
at the time the newline character is processed and the log message
is generated, not necessarily the value which was in effect at the time the
accumulation of a line began to take place.

Messages sent to
.code *stdlog*
are delimited by newline characters. That is to say, each line of
text written to the stream is a new log.

.coNP The @ openlog function
.synb
.mets (openlog < id-string >> [ options <> [ facility ]])
.syne
.desc
The
.code openlog
function is a wrapper for the
.code openlog
C function, and the
arguments have the same semantics. It is not necessary to use
.code openlog
in order
to call the
.code syslog
function or to write data to
.codn *stdlog* .
The call is necessary in order to override the default identifying string, to
set options, such as having the PID (process ID) recorded in log messages, and
to specify the facility.

The
.meta id-string
argument is mandatory.

The
.meta option
argument is a bitwise mask (see the logior function) of option
values such as
.code log-pid
and
.codn log-cons .
If it is missing, then a value of 0 is
used, specifying the absence of any options.

The
.meta facility
argument is one of the values
.codn log-user ,
.code log-daemon
or
.codn log-auth .
If it is missing, then
.code log-user
is assumed.

.coNP The @ closelog function
.synb
  (closelog)
.syne
.desc
The
.code closelog
function is a wrapper for the C function
.codn closelog .

.coNP The @ setlogmask function
.synb
.mets (setlogmask << bitmask-integer )
.syne
.desc
The
.code setlogmask
function interfaces to the corresponding C function, and has the
same argument and return value semantics. The
.meta bitmask-integer
argument is a mask of priority
values to enable. The return value is the prior value. Note that if the
argument is zero, then the function doesn't set the mask to zero; it only
returns the current value of the mask.

Note that the priority values like
.code log-emerg
and
.code log-debug
are integer
enumerations, not bitmasks. These values cannot be combined directly to create
a bitmask. Rather, the
.code mask
function should be used on these values.

.TP* Example:

.cblk
  ;; Enable LOG_EMERG and LOG_ALERT messages,
  ;; suppressing all others
  (setlogmask (mask log-emerg log-alert))
.cble

.coNP The @ syslog function
.synb
.mets (syslog < priority < format << format-arg *)
.syne
.desc
This function is the interface to the
.code syslog
C function. The
.code printf
formatting capabilities of the function are not used;
the
.meta format
argument follows the conventions of the \*(TL 
.code format
function instead. Note in particular that
the
.code %m
convention for interpolating the value of strerror(errno) which is
available in some versions of the
.code syslog
C function is currently not supported.

Note that syslog messages are not newline-terminated.

.SS* Unix Path Globbing

On platforms where the POSIX
.code glob
function is available \*(TX provides this functionality in
the form of a like-named function, and some numeric constants.

.coNP Special variables @, glob-err @, glob-mark @, glob-nosort @, glob-nocheck @, glob-noescape @, glob-period @, glob-altdirfunc @, glob-brace @, glob-nomagic @, glob-tilde @ glob-tilde-check and @ glob-onlydir

These variables take on the values of the corresponding C preprocessor
constants from the
.code <glob.h>
header:
.codn GLOB_ERR ,
.codn GLOB_MARK ,
.codn GLOB_NOSORT ,
etc.

These values are passed as the optional second argument of the
.code glob
function. They are bitmasks and so multiple values can be combined
using the
.code logior
function.

Note that the
.codn glob-period ,
.codn glob-altdirfunc ,
.codn glob-brace ,
.codn glob-nomagic ,
.codn glob-tilde ,
.code glob-tilde-check
and
.code glob-onlydir
variables may not be avaiable. They are extensions in the GNU C library
implementation of
.codn glob .

.coNP Function @ glob
.synb
.mets (glob < pattern >> [ flags <> [ error-func ]])
.syne
.desc
The
.code glob
function is a interface to the Unix function of the same name.
The
.meta pattern
argument must be a string, which holds a glob pattern: a pattern which
matches zero or more path names, similar to a regular expression.
The function tries to expand the pattern and return a list of strings
representing the matching path names in the file system.

If there are no matches, then an empty list is returned.

The optional
.meta flags
argument defaults to zero. If given, it may be a bitwise combination of the
values of the variables
.codn glob-err ,
.codn glob-mark ,
.code glob-nosort
and others.

If the
.meta error-func
argument is specified, it gives a callback function which is invoked
when
.code glob
encounters errors accessing paths. The function takes two arguments:
the pathname and the
.code errno
value which occurred for that pathname. The function's return value is
boolean. If the function returns true, then
.code glob
will terminate.

Details of the semantics of the
.code glob
function, and the meaning of all the
.meta flags
arguments are given in the documentation for the C function.

.SS* Web Programming Support

.coNP Functions @ url-encode and @ url-decode
.synb
.mets (url-encode < string <> [ space-plus-p ])
.mets (url-decode < string <> [ space-plus-p ])
.syne
.desc
These functions convert character strings to and from a form which is suitable 
for embedding into the request portions of URL syntax.

Encoding a string for URL use means identifying in it certain characters that
might have a special meaning in the URL syntax and representing it using
"percent encoding": the percent character, followed by the ASCII value of the
character.  Spaces and control characters are also encoded, as are all byte
values greater than or equal to 127 (7F hex).  The printable ASCII characters
which are percent-encoded consist of this set:

.cblk
  :/?#[]@!$&'()*+,;=%
.cble

More generally, strings can consists of Unicode characters, but the URL
encoding consists only of printable ASCII characters. Unicode characters in the
original string are encoded by expanding into UTF-8, and applying
percent-encoding the UTF-8 bytes, which are all in the range
.codn \exx80-\exxFF .

Decoding is the reverse process: reconstituting the UTF-8 byte sequence
specified by the URL-encoding, and then decoding the UTF-8 sequence into the
string of Unicode characters.

There is an additional complication: whether or not to encode spaces as plus,
and to decode plus characters to spaces. In encoding, if spaces are not encoded
to the plus character, then they are encoded as
.codn %20 ,
since spaces are reserved
characters that must be encoded. In decoding, if plus characters are not
decoded to spaces, then they are left alone: they become plus characters in the
decoded string.

The
.code url-encode
function performs the encoding process. If the
.code space-plus-p
argument is omitted or specified as
.codn nil ,
then spaces are encoded as
.codn %20 .
If the argument is a value other than
.codn nil ,
then spaces are encoded as the
character
.code +
.codn (plus) .

The
.code url-decode
function performs the decoding process. If the
.code space-plus-p
argument is omitted or specified as
.codn nil ,
then
.code +
.code (plus)
characters in the
encoded data are retained as
.code +
characters in the decoded strings. Otherwise,
plus characters are converted to spaces.

.coNP Functions @ html-encode and @ html-decode
.synb
.mets (html-encode << text-string )
.mets (html-decode << html-string )
.syne
.desc
The
.code html-encode
and
.code html-decode
functions convert between an HTML and raw
representation of of text.

The
.code html-encode
function returns a string which is based on the content of
.metn text-string ,
but in which all characters which have special meaning in HTML
have been replaced by HTML codes for representing those characters literally.
The returned string is the HTML-encoded verbatim representation of
.metn text-string .

The
.code html-decode
function converts
.metn html-string ,
which may contain HTML
character encodings, into a string which contains the actual characters
represented by those encodings.

The function composition
.code (html-decode (html-encode text))
returns a string which is equal to
.codn text .

The reverse composition
.code (html-encode (html-decode html))
does not necessarily return a string equal to
.codn html .

For instance if html is the string
.strn "<p>Hello, world&#33;</p>" ,
then
.code html-decode
produces
.strn "<p>Hello, world!</p>" .
From this,
.code html-encode
produces
.strn "&lt;p&gt;Hello, world!&lt;/p&gt;" .

.SS* Filter Module
The filter module provides a trie (pronounced "try") data structure,
which is suitable for representing dictionaries for efficient filtering.
Dictionaries are unordered collections of keys, which are strings, which
have associated values, which are also strings.  A trie can be used to filter
text, such that keys appearing in the text are replaced by the corresponding
values. A trie supports this filtering operation by providing an efficient
prefix-based lookup method which only looks at each input character ones, and
which does not require knowledge of the length of the key in advance.

.coNP Function @ make-trie
.synb
  (make-trie)
.syne
.desc
The
.code make-trie
function creates an empty trie. There is no special data type for
a trie; a trie is some existing type such as a hash table.

.coNP Function @ trie-add
.synb
.mets (trie-add < trie < key << value )
.syne
.desc
The
.code trie-add
function adds the string
.meta key
to the trie, associating
it with
.metn value .
If
.meta key
already exists in
.metn trie ,
then the value is updated with
.metn value .

The
.meta trie
must not have been compressed with
.metn trie-compress .

A trie can contain keys which are prefixes of other keys. For instance
it can contain
.str dog
and
.strn dogma .
When a trie is used for matching
and substitution, the longest match is used. If the input presents
the text
.strn doggy ,
then the match is
.strn dog .
If the input is
.strn dogmatic ,
then
.str dogma
matches.

.coNP Function @ trie-compress
.synb
.mets (trie-compress << trie )
.syne
.desc
The
.code trie-compress
function changes the representation of
.meta trie
to a representation which occupies less space and supports faster lookups.
The new representation is returned.

The compressed representation of a trie does not support the
.code trie-add
function.

This function destructively manipulates
.metn trie ,
and may return an object
that is the same object as
.codn trie ,
or it may return a different object,
while at the same time still modifying the internals of
.metn trie .
Consequently, the program should not retain the input object
.codn trie ,
but use the returned object in its place.

.coNP Function @ trie-lookup-begin
.synb
.mets (trie-lookup-begin << trie )
.syne
.desc
The
.code trie-lookup-begin
function returns a context object for performing
an open-coded lookup traversal of a trie. The
.meta tri
argument
is expected to be a trie that was created by the
.code make-trie
function.

.coNP Function @ trie-lookup-feed-char
.synb
.mets (trie-lookup-feed-char < trie-context << char )
.syne
.desc
The
.code trie-lookup-feed-char
function performs a one character step in a trie
lookup. The
.meta trie-context
argument must be a trie contxt returned
by
.metn trie-lookup-begin ,
or by some previous call to
.codn trie-lookup-feed-char .
The
.meta char
argument is the next character to match.

If the lookup is successful (the match through the trie can continue
with the given character) then a new trie context object is returned.
The old trie context remains valid.

If the lookup is unsuccessful,
.code nil
is returned.

Note: determining whether a given string is stored in a trie can be
performed looking up every character of the string successively
with
.codn trie-lookup-feed-char ,
using the newly returned context
for each successive operation. If every character is found, it means
that either that exact string is found in the trie, or a prefix.
The ambiguity can be resolved by testing whether the trie has a value
at the last node using
.codn tree-value-at .
For instance, if
.str catalog
is inserted into an empty trie with value
.strn foo ,
then
.str cat
will look up successfully, being a prefix of
.strn catalog ;
however, the value at
.str cat
is
.codn nil ,
indicating that
.str cat
is only a prefix of one or more entries in the trie.

.coNP Function tree-value-at
.synb
.mets (trie-value-at << trie-context )
.syne
.desc
The
.code trie-value-at
function returns the value stored at the node in
in the trie given by
.metn trie-context .
Nodes which have not been given
a value hold the value
.codn nil .

.coNP Function @ filter-string-tree
.synb
.mets (filter-string-tree < filter << obj )
.syne
.desc
The
.code filter-string-tree
a tree structure similar to
.metn obj ,
in which all of the
string atoms have been filtered through
.metn filter .

The
.meta obj
argument is a string tree structure: either the symbol
.codn nil ,
denoting an empty structure; a string; or a list of tree structures. If
.meta obj
is
.codn nil ,
then
.code filter-string-tree
returns
.codn nil .

The
.meta filter
argument is a filter: it is either a trie, a function, or nil.
If
.meta filter
is
.codn nil ,
then
.code filter-string-trie
just returns
.metn obj .

If
.meta filter
is a function, it must be a function that can be called
with one argument. The strings of the string tree are filtered by passing
each one into the function and substituting the return value into the
corresponding place in the returned structure.

Otherwise if
.meta filter
is a trie, then this trie is used for filtering,
the string elements similarly to a function. For each string, a new
string is returned in which occurrences of the keys in the trie are
replaced by the values in the trie.

.coNP Function @ filter-equal
.synb
.mets (filter-equal < filter-1 < filter-2 < obj-1 << obj-2 )
.syne
.desc
The
.code filter-equal
function tests whether two string trees are equal
under the given filters.

The precise semantics can be given by this expression:

.cblk
.mets (equal (filter-string-tree < filter-1 << obj-1 )
.mets \ \ \ \ \ \  (filter-string-tree < filter-2 << obj-2 ))
.cble

The string tree
.meta obj-1
is filtered through
.metn filter-1 ,
as if by the
.code filter-string-tree
function, and similarly,
.meta obj-2
is
filtered through
.metn filter-2 .
The resulting structures are compared
using
.codn equal ,
and the result of that is returned.

.SS* Access To TXR Pattern Language From Lisp

It is useful to be able to invoke the abilities of the \*(TX pattern Language
from \*(TL. An interface for doing this provided in the form of the
.code match-fun
function, which is used for invoking a \*(TX pattern function.

The
.code match-fun
function has a cumbersome interface which requires the \*(TL program to
explicitly deal with the variable bindings emerging from the pattern match
in the form of an association list.

To make it the interface easier to use, \*(TX provides a library of macros:
the macros
.codn txr-if ,
.codn txr-when
and
.codn txr-case .

These macros are not in the \*(TX image; they must be included from the
.code stdlib
directory.

.coNP Function match-fun
.synb
.mets (match-fun < name < args < input << files )
.syne
.desc
The
.code match-fun
function invokes a \*(TX pattern function whose name is
given by
.metn name ,
which must be a symbol.

The
.meta args
argument is a list of expressions. The expressions may be symbols
which will be interpreted as pattern variables, and may be bound or unbound.
If they are not symbols, then they are treated as expressions (of the
pattern language, not \*(TL) and evaluated accordingly.

The
.meta input
argument is a list of strings, which may be lazy. It represents the
lines of the text stream to be processed.

The
.meta file
argument is a list of filename specifications, which follow
the same conventions as files given on the \*(TX command line. If the pattern
function uses the
.code @(next)
directive, it can process these additional files.

The
.code match-fun
function's return value falls into three cases. If there is a
match failure, it returns
.codn nil .
Otherwise it returns a cons cell. The
.code car
field
of the cons cell holds the list of captured bindings. The
.code cdr
of the cons cell is one of two values. If the entire input was processed, the
cdr field holds the symbol
.codn t .
Otherwise it holds another cons cell whose
.code car
is the remainder of the list of lines which were not matched, and whose
.code cdr
is the line number.

.TP* Example:

.cblk
  @(define foo (x y))
  @x:@y
  @line
  @(end)
  @(do
     (format t "~s\en"
               (match-fun 'foo '(a b)
                          '("alpha:beta" "gamma" "omega") nil)))

  Output:
  (((a . "alpha") (b . "beta")) ("omega") . 3)
.cble

In the above example, the pattern function
.code foo
is called with arguments
.codn (a b) .
These are unbound variables, so they correspond to parameters
.code x
and
.code y
of the function. If
.code x
and
.code y
get bound, those values propagate to
.code a
and
.codn b .
The data being matched consists of the lines
.strn alpha:beta ,
.str gamma
and
.strn omega .
Inside
.codn foo,
.code x
and
.code y
bind to
.str alpha
and
.strn beta ,
and then the line variable binds to
.strn gamma .
The input stream is left with
.strn omega .

Hence, the return value consists of the bindings of
.code x
and
.code y
transferred to
.code a
and
.codn b ,
and the second cons cell which gives information about the rest of the
stream: it is the part starting at
.strn omega ,
which is line 3. Note that the binding for the
.code line
variable does not propagate
out of the pattern function
.codn foo ;
it is local inside it.

.coNP Macro txr-if
.reqb
.mets @(include `@stdlib/txr-case`)
.reqe
.synb
.mets (txr-if < name <> ( argument *) < input < then-expr <> [ else-expr ])
.syne
.desc
The
.code txr-if
macro invokes the \*(TX pattern matching function
.metn name
on some input given by the
.meta input
parameter, which is a list of strings, or a single string.

If
.meta name
succeeds, then
.meta then-expr
is evaluated, and if it fails,
.meta else-expr
is evaluated instead.

In the successful case,
.meta then-expr
is evaluated in a scope in which the bindings emerging from the
.meta name
function are turned into \*(TL variables.
The result of
.code txr-if
is that of
.metn then-expr .

In the failed case,
.meta else-expr
is evaluated in a scope which does not have any new bindings.
The result of
.code txr-if
is that of
.metn else-expr .
If
.meta else-expr
is missing, the result is
.codn nil .

The
.meta argument
forms supply arguments to the pattern function
.metn name .
There must be as many of these arguments as the function
has parameters.

Any argument which is a symbol is treated, for the purposes
of calling the pattern function, as an unbound pattern variable.
The function may or may not produce a binding for that variable.
Also, every argument which is a symbol also denotes a local variable
that is established around
.meta then-expr
if the function suceeds. For any such pattern variable for which the function
produces a binding, the corresponding local variable will be initialized
with the value of that pattern variable. For any such pattern variable
which is left unbound by the function, the corresponding local variable
will be set to
.codn nil .

Any
.meta argument
can be a form other than a symbol. In this situation, the argument is
evaluated, and will be passed to the pattern function as the value of
the binding for the corresponding argument.

.TP* Example:

.cblk
  @(include `@stdlib/txr-case`)
  @(define date (year month day))
  @{year /\ed\ed\ed\ed/}-@{month /\ed\ed/}-@{day /\ed\ed/}
  @(end)
  @(do
     (each ((date '("09-10-20" "2009-10-20" "July-15-2014" "foo")))
       (txr-if date (y m d) date
         (put-line `match: year @y, month @m, day @d`)
         (put-line `no match for @date`))))

  Output:

  no match for 09-10-20
  match: year 2009, month 10, day 20
  no match for July-15-2014
  no match for foo
.cble

.coNP Macro @ txr-when
.reqb
.mets @(include `@stdlib/txr-case`)
.reqe
.synb
.mets (txr-when < name <> ( argument *) < input << form *)
.syne
.desc
The
.code txr-when
macro is based on
.codn txr-if .
It is equivalent to
.code

.cblk
.meti \ \ (txr-if < name <> ( argument *) < input (progn << form *))
.cble

If the pattern function
.meta name
produces a match, then each
.meta form
is evaluated in the scope of the variables established by the
.meta argument
expressions. The result of the
.code txr-when
form is that of the last
.metn form .

If the pattern function fails then the forms are not evaluated,
adn the result value is
.codn nil .

.coNP Macro @ txr-case
.reqb
.mets @(include `@stdlib/txr-case`)
.reqe
.synb
.mets (txr-case < input-form
.mets \ \  >> {( name <> ( argument *) << form *)}*
.mets \ \  >> [( t << form *)])
.syne
.desc
The
.code txr-case
macro evaluates
.meta input-form
and then uses the value as an input to zero or more test clauses.
Each test clause invokes the pattern function named by that clause's
.meta name
argument.

If the function succeeds, then each
.meta form
is evaluated, and the value of the last
.meta form
is taken to be the result value of
.codn txr-case ,
which terminates. If there are no forms, then
.code txr-case
terminates with a
.code nil
result.

The forms are evaluated in an environment in which variables are bound
based on the
.meta argument
forms, with values depending on the result of the
invocation of the
.meta name
pattern function, in the same manner as documented in detail for the
.code txr-if
macro.

If the function fails, then the forms are not evaluated, and control passes to
the next clause.

A clause which begins with the symbol
.code t
executes unconditionally and causes
.code txr-case
to terminate. If it has no forms, then
.code txr-case
yields
.codn nil ,
otherwise the forms are evaluated in order and the value of the last
one specifies the result of
.codn txr-case .

.SS* Quote/Quasiquote Operator Syntax
.coNP Operator @ quote
.synb
.mets (quote << form )
.syne
.desc
The
.code quote
operator, when evaluated, suppresses the evaluation of
.metn form ,
and instead returns
.meta form
itself as an object. For example, if
.meta form
is a symbol, then
.meta form
is not evaluated to the symbol's value; rather
the symbol itself is returned.

Note: the quote syntax
.cblk
.meti >> ' <form>
.cble
is translated to
.cblk
.meti (quote << form ).
.cble

.TP* Example:

.cblk
  (quote a) ;; yields a

  (quote (+ 2 2)) ;; yields (+ 2 2), not 4.
.cble

.coNP Macro @ qquote
.synb
.mets (qquote << form )
.syne
.desc
The
.code qquote
(quasi-quote) macro operator implements a notation for convenient
list construction.  If
.meta form
is an atom, or a list structure which
does not contain any
.code unquote
or
.code splice
operators, then
.cblk
.meti (qquote << form )
.cble
is equivalent to
.cblk
.meti (qquote << form ).
.cble

If
.metn form ,
however, is a list structure which contains
.code unquote
or
.code splice
operators, then the substitutions implied by those operators are performed
on
.metn form ,
and the
.code qquote
operator returns the resulting structure.

Note: how the qquote operator actually works is that it is compiled into
code. It becomes a Lisp expression which, when evaluated, computes the
resulting structure.

A
.code qquote
can contain another
.codn qquote .
If an
.code unquote
or
.code splice
operator occurs
within a nested
.codn qquote ,
it belongs to that
.codn qquote ,
and not to the outer one.

However, an unquote operator which occurs inside another one belongs one level
higher. For instance in

.cblk
  (qquote (qquote (unquote (unquote x))))
.cble

the leftmost
.code qquote
belongs with the rightmost unquote, and the inner
.code qquote
and
.code unquote
belong together. When the outer
.code qquote
is evaluated,
it will insert the value of
.codn x ,
resulting in the object
.codn (qquote (unquote [value-of-x])) .
If this resulting qquote value is evaluated again as Lisp syntax, then it will
yield
.codn [value-of-value-of-x] ,
the value of
.code [value-of-x]
when treated as a Lisp expresion and evaluated.

.TP* Examples:

.cblk
  (qquote a) -> a

  (qquote (a b c)) -> (a b c)

  (qquote (1 2 3 (unquote (+ 2 2)) (+ 2 3))) -> (1 2 3 4 (+ 2 3))

  (qquote (unquote (+ 2 2))) -> 4
.cble

In the second-to-last example, the
.code 1 2 3
and the
.code (+ 2 3)
are quoted verbatim.
Whereas the
.code (unquote (+ 2 2))
operator caused the evaluation of
.code (+ 2 2)
and the substitution of the resulting value.

The last example shows that
.meta form
can itself (the entire argument of
.codn qquote )
can be an unquote operator.
However, note:
.code (quote (splice form))
is not valid.

Note: a way to understand the nesting behavior is a via a possible model of
quasi-quote expansion which recursively compiles any nested quasi quotes first,
and then treats the result of their expansion. For instance, in the processing
of

.cblk
  (qquote (qquote (unquote (unquote x))))
.cble

the
.code qquote
operator first encounters the
embedded
.code (qquote ...)
and compiles it to code. During that recursive
compilation, the syntax
.code (unquote (unquote x))
is encountered.  The inner quote
processes the outer unquote which belongs to it, and the inner
.code (unquote x)
becomes material that is embedded verbatim in the compilation, which will then
be found when the recursion pops back to the outer quasiquote, which will
then traverse the result of the inner compilation and find the
.codn (unquote x) .

.TP* "Dialect note:"

In Lisp dialects which have a published quasiquoting operator syntax, there is
the expectation that the quasiquote read syntax corresponds to it. That is to
say, that for instance the read syntax
.code ^(a b ,c)
is expected translated to
.codn (qquote b (unquote c)) .

In \*(TL, this is not true! Although
.code ^(b b ,c)
is translated to a
quasiquoting macro, it is an internal one, not based on the public
.codn qquote ,
.code unquote
and
.code splice
symbols being documented here.

This idea exists for hygiene. The quasiquote read syntax is not confused
by the presence of the symbols
.codn qquote ,
.code unquote
or
.code splice
in the template, since it doesn't treat them specially.

This also allows programmers to use the quasiquote read syntax to construct
quasiquote macros. For instance

.cblk
  ^(qquote (unquote ,x)) ;; does not mean ^^,x 
.cble

To the quasiquote reader, the
.code qquote
and
.code unquote
symbols mean nothing special,
and so this syntax simply means that if the value of
.code x
is
.codn foo ,
the result will be
.codn (qquote (unquote foo)) .

The form's expansion is actually this:

.cblk
  (sys:qquote (qquote (unquote (sys:unquote x))))
.cble

the
.code sys:qquote
macro recognizes
.code sys:unquote
embedded in the form, and
the other symbols not in the
.code sys:
package are just static template material.

The
.code sys:quote
macro and its associated
.code sys:unquote
and
.code sys:splice
operators work exactly like their ordinary counterparts. So in effect, \*(TX has
two nearly identical, independent quasi-quote implementations, one of which is
tied to the read syntax, and one of which isn't. This is useful for writing
quasiquotes which write quasiquotes.

.coNP Operator @ unquote
.synb
.mets (qquote (... (unquote << form ) ...))
.mets (qquote (unquote << form ))
.syne
.desc
The
.code unquote
operator is not an operator
.I per
.IR se .
The
.code unquote
symbol has no
binding in the global environment. It is a special syntax that is recognized
within a
.code qquote
form, to indicate forms within the quasiquote which are to be
evaluated and inserted into the resulting structure.

The syntax
.cblk
.meti (qquote (unquote << form ))
.cblk
is equivalent to
.metn form :
the
.code qquote
and
.code unquote
"cancel out".

.coNP Operator splice
.synb
.mets (qquote (... (splice << form ) ...))
.syne
.desc
The
.code splice
operator is not an operator
.I per
.IR se .
The
.code splice
symbol has no
binding in the global environment. It is a special syntax that is recognized
within a
.code qquote
form, to indicate forms within the quasiquote which are to be
evaluated and inserted into the resulting structure.

The syntax
.cblk
.meti (qquote (splice << form ))
.cble
is not permitted and raises an exception if evaluated. The
.code splice
syntax must occur within a list, and not in the dotted position.

The
.code splice
form differs from unquote in that
.cblk
.meti (splice << form )
.cble
requires that
.meta form
must evaluate to a list. That list is
integrated into the surrounding list.

.SS* Macros
\*(TL supports structural macros. \*(TX's model of macroexpansion is that
\*(TL code is processed in two phases: the expansion phase and the
evaluation phase. The expansion phase is invoked on Lisp code early during the
processing of source code. For instance when a \*(TX file containing a
.code @(do ...)
directive
is loaded, expansion of the Lisp forms are its arguments takes place during the
parsing of the entire source file, and is complete before any of the code in
that file is executed. If the
.code @(do ...)
form is later executed,
the expanded forms are then evaluated.

\*(TL also supports symbol macros, which are symbolic forms that stand
for forms, with which they are replaced at macro expansion time.

When Lisp data is processed as code by the
.code eval
function, it is first expanded,
and so processed in its entirety in the expansion phase. Then it is processed
in the evaluation phase.

.coNP Macro parameter lists

\*(TX macros support destructuring, similarly to Common Lisp macros.
This means that macro parameter lists are like function argument lists,
but support nesting. A macro parameter list can specify a nested parameter
list in every place where a function argument list allows only a parameter
name. For instance, consider this macro parameter list:

.cblk
  ((a (b c)) : (c frm) ((d e) frm2 de-p) . g)
.cble

The top level of this list has four elements: the mandatory parameter
.codn (a (b c)) ,
the optional parameter
.code c
(with default init form
.codn frm ),
the optional parameter
.code (d e)
(with default init form
.code frm2
and presence-indicating variable
.codn de-p ),
and the dot-position parameter
.code g
which captures trailing arguments.

Note that some of the parameters are compounds:
.code (a (b c))
and
.codn (d e) .
These compounds express nested macro parameter lists.

Macro parameter lists match a similar tree structure to their own.
For instance a mandatory parameter
.code (a (b c))
matches a structure like
.codn (1 (2 3)) ,
such that the parameters
.codn a ,
.code b
and
.code c
will end up bound
to
.codn 1 ,
.code 2
and
.codn 3 ,
respectively.

The binding strictness is relaxed for optional parameters. If
.code (a (b c))
is optional, and the argument is, say,
.codn (1) ,
then
.code a
gets
.codn 1 ,
and
.code b
and
.code c
receive
.codn nil .

Macro parameter lists also support two special keywords, namely
.code :env
and
.codn :whole .

The parameter list
.code (:whole x :env y)
will bind parameter
.code x
to the entire
macro form, and bind parameter
.code y
to the macro environment.
The
.code :whole
and
.code :env
notation can occur anywhere in a macro parameter list.

.coNP Operator @ macro-time
.synb
.mets (macro-time << form *)
.syne
.desc
The
.code macro-time
operator has a syntax similar to the
.code progn
operator. Each
.meta form
is evaluated from left to right, and the resulting value is that of the last
form.

The special behavior of
.code macro-time
is that the evaluation takes place during
the expansion phase, rather than during the evaluation phase.

During the expansion phase, all
.code macro-time
expressions which occur in a context
that calls for evaluation are evaluated, and replaced by their quoted values.
For instance
.code (macro-time (list 1 2 3))
evaluates
.code (list 1 2 3)
to the object
.code (1 2 3)
and the entire
.code macro-time
form is replaced by that value, quoted:
.codn '(1 2 3) .
If the form is evaluated again at evaluation-time, the resulting value will be
that of the quote, in this case
.codn (1 2 3) .

.code macro-time
forms do not see the surrounding lexical environment; the see only
global function and variable bindings and macros.

Note 1:
.code macro-time
is intended for defining helper functions and variables that
are used by macros. A macro cannot "see" a
.code defun
function or
.code defvar
variable
because
.code defun
and
.code defvar
forms are evaluated at evaluation time, which occurs
after expansion time.  The macro-time operator hoists the evaluation of these
forms to macro-expansion time.

Note 2:
.code defmacro
forms are implicitly in macro-time; they do not have to
be wrapped with the
.code macro-time
operator. The
.code macro-time
operator doesn't have
to be used in programs whose macros do not make references to variables
or functions.

.coNP Operator @ defmacro
.synb
.mets (defmacro < name <> ( param * [: << opt-param * ] [. < rest-param ])
.mets \ \  << body-form *)
.syne
.desc
The
.code defmacro
operator is evaluated at expansion time. It defines a
macro-expander function under the name
.metn name ,
effectively creating a new operator.

Note that the parameter list is a macro parameter list, and not a
function parameter list. This means that each
.meta param
and
.meta opt-param
can be not only a symbol, but it can itself be a parameter list.
The corresponding argument is then treated as a structure which
matches that parameter list.  This nesting of parameter lists
can be carried to an arbitrary depth.

A macro is called like any other operator, and resembles a function.  Unlike in
a function call, the macro receives the argument expressions themselves, rather
than their values.  Therefore it operates on syntax rather than on values.
Also, unlike a function call, a macro call occurs in the expansion phase,
rather than the evaluation phase.

The return value of the macro is the macro expansion. It is substituted in
place of the entire macro call form. That form is then expanded again;
it may itself be another macro call, or contain more macro calls.

.TP* Examples:

.cblk
  ;; A macro to swap two places, such as variables.
  ;; (swap x y) will now exchange the contents
  ;; of x and y.

  (defmacro swap (a b)
    (let ((temp (gensym)))
      ^(let ((,temp ,a))
         (set ,a ,b)
         (set ,b ,temp))))

  ;; dolist macro similar to Common Lisp's:
  ;;
  ;; The following will print 1, 2 and 3
  ;; on separate lines:
  ;; and return 42.
  ;;
  ;; (dolist (x '(1 2 3) 42)
  ;;   (format t "~s\en"))

  (defmacro dolist ((var list : result) . body)
    (let ((i (my-gensym)))
      ^(for ((i ,list)) (i ,result) ((set i (cdr i)))
         (let ((,var (car i)))
           ,*body))))
.cble

.coNP Operator @ macrolet
.synb
.mets (macrolet >> ({( name < macro-style-params << macro-body-form *)}*)
.mets \ \  << body-form *)
.syne
.desc
The
.code macrolet
binding operator extends the macro-time lexical environment
by making zero or more new local macros visible.

The
.code macrolet
symbol is followed by a list of macro definitions.
Each definition is a form which begins with a
.metn name ,
followed by
.meta macro-style-params
which is a macro parameter list, and zero or more
.metn macro-body-form s.
These macro definitions are similar
to those globally defined by the
.code defmacro
operator, except that they
are in a local environment.

The macro definitions are followed by optional
.metn body-forms .
The macros specified in the definitions are visible to these
forms.

Forms inside the macro definitions such as the
.metn macro-body-form s,
and initializer forms appearing in the
.meta macro-style-params
are subject
to macro-expansion in a scope in which none of the new macros being
defined are yet visible. Once the macro definitions are themselves
macro-expanded, they are placed into a new macro environment, which
is then used for macro expanding the
.metn body-form s.

A
.code macrolet
form is fully processed in the expansion phase of a form, and is
effectively replaced by
.code progn
form which contains expanded versions of
.metn body-form s.
This expanded structure shows no evidence that any
macrolet forms ever existed in it. Therefore, it is impossible for the code
evaluated in the bodies and parameter lists of
.code macrolet
macros to have any visibility to any surrounding lexical variable bindings,
which are only instantiated in the evaluation phase, after expansion is done
and macros no longer exist.

.coNP Function @ macro-form-p
.synb
.mets (macro-form-p < obj <> [ env ])
.syne
.desc
The
.code macro-form-p
function returns
.code t
if
.meta obj
represents the syntax of
a form which is a macro form: either a compound macro or a symbol macro.
Otherwise it returns
.codn nil .

A macro form is one that will transform under
.code macroexpand-1
or
.codn macroexpand ;
an object which isn't a macro form will not undergo expansion.

The optional
.meta env
parameter is a macroexpansion environment.
A macroexpansion environment is passed down to macros and can be received
via their special
.code :env
parameter.

.meta env
is used by
.code macro-form-p
to determine whether
.meta obj
is a macro in a lexical macro environment.

If
.meta env
is not specified or is
.codn nil ,
then
.code macro-form-p
only recognizes global macros.

.TP* Example:

.cblk
  ;; macro which translates to 'yes if its
  ;; argument is a macro from, or otherwise
  ;; transforms to the form 'no.

  (defmacro ismacro (:env menv form)
    (if (macro-form-p form menv)
     ''yes ''no))

  (macrolet ((local ()))
    (ismacro (local)))    ;; yields yes

  (ismacro (local))       ;; yields no

  (ismacro (ismacro foo)) ;; yields yes
.cble

During macro expansion, the global macro
.code ismacro
is handed the macro-expansion environment 
via
.codn :env menv .

When the macro is invoked within the macrolet,
this environment includes the macro-time lexical scope in which the
.code local
macro is defined. So when global checks whether the argument form
.code (local)
is a macro, the conclusion is yes: the (local) form is a macro
call in that environment:
.code macro-form-p
yields
.codn t .

When
.code (global (local))
is invoked outside of the macrolet, no local macro is visible is
there, and so
.code macro-form-p
yields
.codn nil .

.coNP Functions @ macroexpand-1 and @ macroexpand
.synb
.mets (macroexpand-1 < obj <> [ env ])
.mets (macroexpand < obj <> [ env ])
.syne
.desc
If
.meta obj
is a macro form (an object for which
.code macro-form-p
returns
.codn t ),
these functions expand the macro form and return the expanded form.
Otherwise, they return
.metn obj .

.code macroexpand-1
performs a single expansion, expanding just the macro
that is referenced by the symbol in the first position of
.metn obj ,
and returns the expansion. That expansion may itself be a macro form.

.code macroexpand
performs an expansion similar to
.codn macroexpand-1 .
If the result is
a macro form, then it expands that form, and keeps repeating this process
until the expansion yields a non-macro-form. That non-macro-form is then
returned.

The optional
.meta env
parameter is a macroexpansion environment.
A macroexpansion environment is passed down to macros and can be received
via their special
.code :env
parameter. The environment they receive is their
lexically apparent macro-time environment in which local macros may be
visible.  A macro can use this environment to "manually" expand some
form in the context of that environment.

.TP* Example:

.cblk
  ;; (foo x) expands x, and if x begins with a number,
  ;; it removes  the number and returns the resulting
  ;; form. Otherwise, it returns the entire form.

  (defmacro rem-num (:env menv some-form)
    (let ((expanded (macroexpand some-form menv)))
      (if (numberp (car expanded))
        (cdr expanded)
        some-form)))

  ;; The following yields (42 a).

  (macrolet ((foo () '(1 list 42))
             (bar () '(list 'a)))
    (list (rem-num (foo)) (rem-num (bar)))))
.cble

The
.code rem-num
macro is able to expand the
.code (foo)
and
.code (bar)
forms it receives as
the
.code some-form
argument, even though these forms use local macro that are only
visible in their local scope. This is thanks to the macro
environment passed to
.codn rem-num .
It is correctly able to work with the
expansions
.code (1 list 42)
and
.code (list 'a)
to produce
.code (list 42)
and
.code (list 'a)
which evaluate to
.code 42
and
.code a
respectively.

.coNP Functions @ lexical-var-p and @ lexical-fun-p
.synb
.mets (lexical-var-p < env << form)
.mets (lexical-fun-p < env << form)
.syne
.desc
These two functions are useful to macro writers. They are intended
to be called from the bodies of macro expanders, such as the bodies of
.code defmacro
or
.code macrolet
forms.  The
.meta env
argument is a macro-time environment, which is available to macros
via the special
.code :env
parameter. Using these functions, a macro can enquire whether
a given
.meta form
is a symbol which has a variable binding or a function binding
in the lexical environment.
This information is known during macro expansion. The macro expander
recognizes lexical function and variable bindings, because these
bindings can shadow macros.

.TP* Example:

.cblk
  ;;
  ;; this macro replaces itself with :lexical-var if its
  ;; argument is a lexical variable, :lexical-fun if
  ;; its argument is a lexical function, or with
  ;; :not-lex-fun-var if neither is the case.
  ;;
  (defmacro classify (sym :env e)
    (cond
      ((lexical-var-p e expr) :lexical-var)
      ((lexical-fun-p e expr) :lexical-fun)
      (t :not-lex-fun-var)))

  ;;
  ;; This returns:
  ;;
  ;;   (:lexical-var :not-lex-fun-var :lexical-fun)
  ;;
  (let ((x 1) (y 2))
    (symacrolet ((y x))
      (flet ((f () (+ 2 2)))
        (list (classify x) (classify y) (classify z)))))
.cble

.TP* Note:

These functions do not call
.code macroexpand
on the form. In most cases, it is necessary for the macro writers
to do so. Not that in the above example,  symbol
.code y
is classified as neither a lexical function nor variable.
However, it can be macro-expanded to
.code x
which is a lexical variable.

.coNP Function @ lexical-lisp1-binding
.synb
.mets (lexical-lisp1-binding < env << symbol)
.syne
.desc
The
.code lexical-lisp1-binding
function inspects the macro-time environment
.meta env
to determine what kind of binding, if any, does
.meta symbol
have in that environment, from a Lisp-1 perspective.

That is to say, it considers function bindings, variable bindings
and symbol macro bindings to be in a single name space and finds
the innermost binding of one of these types for
.metn symbol .

If such a binding is found, then the function returns one of
the three keyword symbols
.codn :var ,
.codn :fun ,
or
.codn :symacro .

If no such lexical binding is found, then the function
returns
.codn nil .

Note that a
.code nil
return doesn't mean that the symbol doesn't have a lexical binding.  It could
have an operator macro lexical binding (a macro binding in the function
namespace established by
.codn macrolet ).

.coNP Operator @ defsymacro
.synb
.mets (defsymacro < sym << form )
.syne
.desc

A
.code defsymacro
form introduces a symbol macro: a symbol macro is a parameterless
substitution keyed to a symbol.  In contexts where a symbol macro
definition of
.meta sym
is visible, if the form
.meta sym
appears such that its evaluation is called for, it is subject
to replacement by
.metn form .
After replacement takes place,
.meta form
itself is then processed for further replacement of macros and
symbol macros.

Symbol macros are also recognized in contexts
where
.meta sym
denotes a place which is the target of an assignment operation
like
.code set
and similar.

A
.code defsymacro
form is implicitly executed at expansion time, and thus need
not be wrapped in a
.code macro-time
form, just like
.codn defmacro .

Note: if a symbol macro expands to itself directly, expansion stops. However,
if a symbol macro expands to itself through a chain of expansions,
an infinite expansion time loop results.

.coNP Operator @ symacrolet
.synb
.mets (symacrolet >> ({( sym << form )}*) << body-form *)
.syne
.desc
The
.code symacrolet
operator binds local, lexically scoped macros that are
similar to the global symbol macros introduced by
.codn defsymacro .

Each
.meta sym
in the bindings list is bound to its corresponding form, creating a
new extension of the expansion-time lexical macro environment.

Each
.meta body-form
is subsequently macro-expanded in this new environment
in which the new symbol macros are visible.

Note: ordinary lexical bindings such as those introduced by let or by
function parameters lists shadow symbol macros. If a symbol
.code x
is bound by nested instances of
.code macrolet
and a
.codn let ,
then the scope enclosed by both
constructs will see whichever of the two bindings is more inner,
even though the bindings are active in completely separate phases of
processing.

From the perspective of the arguments of a
.code dwim
form, lexical function bindings also shadow symbol macros.
This is consistent with the Lisp-1-style name resolution which
applies inside a
.code dwim
form. Of course, lexical operator macros do not shadow
symbol macros under any circumstances.

.coNP Operator @ tree-bind
.synb
.mets (tree-bind < macro-style-params < expr << form *)
.syne
.desc
The
.code tree-bind
operator evaluates
.codn expr ,
and then uses the
resulting value as a counterpart to a macro-style parameter list.
If the value has a tree structure which matches the parameters,
then those parameters are established as bindings, and the
.metn form s,
if any, are evaluated in the scope of those bindings.  The value
of the last
.meta form
is returned. If there are no forms,
.code nil
is returned.

Note: this operator throws an exception if there is a
structural mismatch between the parameters and the value of
.codn expr .

One way to avoid this exception is to use
.codn tree-case .

.coNP Operator @ tree-case
.synb
.mets (tree-case < expr >> {( macro-style-params  << form *)}*)
.syne
.desc
The
.code tree-case
operator evaluates
.meta expr
and matches it against a succession
of zero or more cases. Each case defines a pattern match, expressed as a macro
style parameter list
.metn macro-style-params .

If the object produced by
.meta expr
matches
.metn macro-style-params ,
then the parameters are bound, becoming local variables, and the
.metn form s,
if any, are evaluated in order in the environment in which those variables are
visible.  If there are forms, the value of the last
.meta form
becomes the result
value of the case, otherwise the result value of the case is nil.

If the result value of a case is the object
.code :
(the colon symbol), then processing continues with the next case. Otherwise the
evaluation of
.code tree-case
terminates, returning the result value.

If the value of
.meta expr
does not match the
.meta macro-style-params
parameter list of a case, processing continues with the next case.

If no cases match, then
.code tree-case
terminates, returning
.codn nil .

.TP* Example:

.cblk
  ;; reverse function implemented using tree-case

  (defun tb-reverse (obj)
    (tree-case obj
      (() ())      ;; the empty list is just returned
      ((a) obj)    ;; one-element list returned (unnecessary case)
      ((a . b) ^(,*(tb-reverse b) ,a)) ;; car/cdr recursion
      (a a)))     ;; atom is just returned
.cble

Note that in this example, the atom case is placed last, because an
argument list which consists of a symbol is a "catch all" match
that matches any object. We know that it matches an atom, because
the previous
.code (a . b)
case matches conses. In general, the order of the cases in
.code tree-case
is important: even more so than the order of cases in a
.code cond
or
.codn caseql .

.coNP Macro @ tb
.synb
.mets (tb < macro-style-params << form *)
.syne
.desc
The
.code tb
macro is similar to the
.code lambda
operator but its argument binding is based on a macro-style parameter list.
The name is an abbreviation of
.codn tree-bind .

A
.code tb
form evaluates to a function which takes a variable number of
arguments.

When that function is called, those arguments are taken as a list object which
is matched against
.meta macro-style-params
as if by
.metn tree-bind .
If the match is successful, then the parameters are bound to the
corresponding elements from the argument structure and each successive
.meta form
is evaluated an environment in which those bindings are visible.
The value of the last
.meta form
is the return value of the function. If there are no forms,
the function's return value is
.codn nil .

The following equivalence holds, where
.code args
should be understood to be a globally unique symbol:

.cblk
  (tb pattern body ...) <--> (lambda (. args)
                               (tree-bind pattern args body ...))
.cble

.coNP Macro @ tc
.synb
.mets (tc >> {( macro-style-params  << form *)}*)
.syne
.desc
The
.code tc
macro produces an anonymous function whose behavior is closely
based on the
.code tree-case
operator. Its name is an abbreviation of
.codn tree-case .

The anonymous function takes a variable number of arguments.
Its argument list is taken to be the value macro is tested
against the multiple pattern clauses of an implicit
.codn tree-bind .
The return value of the function is that of the implied
.codn tree-bind .

The following equivalence holds, where
.code args
should be understood to be a globally unique symbol:

.cblk
  (tc clause1 clause2 ...) <--> (lambda (. args)
                                   (tree-bind args
                                      clause1 clause2 ...))
.cble

.SS* Debugging Functions
.coNP Functions source-loc and source-loc-str
.synb
.mets (source-loc << form )
.mets (source-loc-str << form )
.syne
.desc
These functions map an expression in a \*(TX program to the file name and
line number of the source code where that form came from.

The
.code source-loc
function returns the raw information as a cons cell
whose
.cod3 car / cdr
consist of the line number, and file name.

The
.code source-loc-str
function formats the information as a string.

If
.meta form
is not a piece of the program source code that was constructed by the
\*(TX parser, then
.code source-loc
returns
.codn nil ,
and
.code source-loc-str
returns a string whose text says that source location is not available.

.coNP Function @ rlcp
.synb
.mets (rlcp < dest-form << source-form )
.syne
.desc
The
.code rlcp
function copies the source code location info ("rl" means "read location")
from the
.meta source-form
object to the
.meta dest-form
object. These objects
are pieces of list-based syntax.

Note: the function is intended to be used in macros. If a macro transforms
.meta source-form
to
.metn dest-form ,
this function can be used to propagate the
source code location info also, so that when the \*(TL evaluator
encounters errors in transformed code, it can give diagnostics which refer
to the original untransformed source code.

.SS* Profiling
.coNP Operator @ prof
.synb
.mets (prof << form *)
.syne
.desc
The
.code prof
operator evaluates the enclosed forms from left to right similarly
to
.codn progn ,
while determining the memory allocation requests and time
consumed by the evaluation of the forms.

If there are no forms, the prof operator measures the smallest measurable
operation of evaluating nothing and producing
.codn nil .

If the evaluation terminates normally (not abruptly by a non-local
control transfer), then
.code prof
yields a list consisting of:

.cblk
.mets >> ( value < malloc-bytes < gc-bytes << milliseconds )
.cble

where
.meta value
is the value returned by the rightmost
.metn form ,
or
.code nil
if there are no forms,
.meta malloc-bytes
is the total number of bytes of all memroy allocation
requests (or at least those known to the \*(TX runtime, such as those of all
internal objects),
.meta gc-bytes
is the total number of bytes drawn from the
garbage-collected heaps, and
.meta milliseconds
is the total processor time
consumed over the execution of those forms.

Notes:

The bytes allocated by the garbage collector from the C function
.code malloc
to create
heap areas are not counted as
.metn malloc-bytes .
.meta malloc-bytes
includes storage
such as the space used for dynamic strings, vectors and bignums (in addition to
their gc-heap allocated nodes), and the various structures used by the
.code cobj
type objects such as streams and hashes. Objects in external libraries that use
uninstrumented allocators are not counted: for instance the C
.code FILE *
streams.

.coNP Macro @ pprof
.synb
.mets (pprof << form *)
.syne
.desc
The
.code pprof
(pretty-printing
.codn prof )
macro is similar to
.codn progn .
It evaluates
.metn form s,
and returns the rightmost one, or
.code nil
if there are no forms.

Over the evaluation of
.metn form s,
it counts memory allocations, and measures
CPU time. If
.metn form s
terminate normally, then just prior to returning,
.code pprof
prints these statistics in a concise report on the
.codn *std-output* .

The
.code pprof
macro relies on the
.code prof
operator.

.SS* Garbage Collection
.coNP Function @ sys:gc
.synb
  (sys:gc)
.syne
.desc
The
.code gc
function triggers garbage collection.  Garbage collection means
that unreachable objects are identified and reclaimed, so that their
storage can be re-used.

.coNP Function @ sys:gc-set-delta
.synb
.mets (sys:gc-set-delta << bytes )
.syne
.desc
The
.code gc-set-delta
function sets the GC delta parameter.

Note: This function may disappear in a future release of \*(TX or suffer
a backward-incompatible change in its syntax or behavior.

When the amount of new dynamic memory allocated since the last garbage
collection equals or exceeds the GC delta, a garbage collection pass is
triggered. From that point, a new delta begins to be accumulated.

Dynamic memory is used for allocating heaps of small garbage-collected objects
such as cons cells, as well as the satellite data attached to some objects:
like the storage arrays of vectors, strings or bignum integers. Most garbage
collector behaviors are based on counting objects in the heaps.

Sometimes a program works with a small number of objects which are very large,
frequently allocating new, large objects and turning old ones into garbage.
For instance a single large integer could be many megabytes long.  In such a
situation, a small number of heap objects therefore control a large amount of
memory.  This requires garbage collection to be triggered much more often than
when working with small objects, such as conses, to prevent runaway allocation
of memory. It is for this reason that the garbage collector uses the GC delta.

There is a default GC delta of 64 megabytes. This may be overridden in
special builds of \*(TX for small systems.

.coNP Function @ finalize
.synb
.mets (finalize < object << function )
.syne

.desc
The
.code finalize
function registers
.meta function
to be invoked in the situation when
.meta object
is identified by the garbage collector as unreachable.
This function is called a finalizer.

If and when this situation occurs, the finalizer
.meta function
will be called with
.meta object
as its only argument.

Finalizers are called in the same order in which they are registered:
newer registrations are called after older registrations.

If
.meta object
is registered multiple times by multiple calls to
.codn finalize ,
then if those finalizers are called, they are all called, in the order
of registration.

After a finalization call takes place, its registration is removed;
.meta object
and
.meta function
are no longer associated.  However, neither
.meta object
nor
.meta function
are reclaimed immediately; they are treated as if they were reachable objects
until at least the next garbage collection pass.
It is therefore safe for
.meta function
to store somewhere a persistent reference to
.meta object
or to itself, thereby reinstanting these objects as reachable.

.meta function
is itself permitted to call
.code finalize
to register the original
.code object
or any other object for finalization. Such registrations made during
finalization execution are not eligible for the current phase of finalization
processing; they will be processed in a later garbage collection pass.

.SS* Modularization
.coNP Special variable @ *self-path*
.desc
This variable holds the invocation path name of the \*(TX program.

.coNP Special variable @ stdlib
The
.code stdlib
variable expands to the directory where the \*(TX standard library
is installed. It can be referenced in
.code @(load)
and
.code @(include)
directives via quasiliteral substitution, as in, for example:

.cblk
  @(include `@stdlib/extract`)
.cble

.SS* Debugger
\*(TX has a simple, crude, built-in debugger. The debugger is invoked by adding
the
.code -d
command line option to an invocation of \*(TX.
In this debugger it is possible to step through code, set breakpoints,
and examine the variable binding environment.

Prior to executing any code, the debugger waits at the
.code txr>
prompt, allowing for the opportunity to set breakpoints.

Help can be obtained with the
.code h
or
.code ?
command.

Whenever the program stops at the debugger, it prints the Lisp-ified
piece of syntax tree that is about to be interpreted.
It also shows the context of the input being matched.

The s command can be used to step into a form; n to step over.
Sometimes the behavior seems counter-intuitive. For instance stepping
over a
.code @(next)
directive actually means skipping everything which follows
it. This is because the query material after a
.code @(next)
is actually child
nodes in the abstract syntax tree node of the
.code next
directive, whereas the surface syntax appears flat.

.coNP Sample Session

Here is an example of the debugger being applied to a web scraping program
which connects to a US NAVY clock server to retrieve a dynamically-generated
web page, from which the current time is extracted, in various time zones.
The handling of the web request is done by the wget command; the
\*(TX query opens a wget command as and scans the body of the HTTP response
containing
HTML. This is the code, saved in a file called navytime.txr:

.cblk
  @(next `!wget -c http://tycho.usno.navy.mil/cgi-bin/timer.pl -O - 2> /dev/null`)
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final"//EN>
  <html>
  <body>
  <TITLE>What time is it?</TITLE>
  <H2> US Naval Observatory Master Clock Time</H2> <H3><PRE>
  @(collect :vars (MO DD HH MM SS (PM "  ") TZ TZNAME))
  <BR>@MO. @DD, @HH:@MM:@SS @(maybe)@{PM /PM/} @(end)@TZ@/\et+/@TZNAME
  @  (until)
  </PRE>@/.*/
  @(end)
  </PRE></H3><P><A HREF="http://www.usno.navy.mil"> US Naval Observatory</A>

  </body></html>
  @(output)
  @  (repeat)
  @MO-@DD @HH:@MM:@SS @PM @TZ
  @  (end)
  @(end)
.cble

This is the debug session:

.cblk
  $ txr -d navytime.txr
  stopped at line 1 of navytime.txr
  form: (next (sys:quasi "!wget -c http://tycho.usno.navy.mil/cgi-bin/timer.pl -O - 2> /dev/null"))
  depth: 1
  data (nil):
  nil
.cble

The user types
.code s
to step into the
.code (next ...)
form.

.cblk
  txr> s
  stopped at line 2 of navytime.txr
  form: (sys:text "<!DOCTYPE" (#<sys:regex: 95e4590> 1+ #\espace) "HTML" (#<sys:regex: 95e4618> 1+ #\espace) "PUBLIC" (#<sys:regex: 95e46a8> 1+ #\espace) "\e"-//W3C//DTD" (#<sys:regex: 95e4750> 1+ #\espace) "HTML" (#<sys:regex: 95e47d8> 1+ #\espace) "3.2" (#<sys:regex: 95e4860> 1+ #\espace) "Final\e"//EN>")
  depth: 2
  data (1):
  "<!DOCTYPE HTML PUBLIC \e"-//W3C//DTD HTML 3.2 Final\e"//EN>"
  txr> s
.cble

The current form now is a
.code sys:text
form which is an internal representation of
a block of horizontal material. The pattern matching is in vertical mode at
this point, and so the line of data is printed without an indication of
character position.

.cblk
  stopped at line 2 of navytime.txr
  form: (sys:text "<!DOCTYPE" (#<sys:regex: 95e4590> 1+ #\espace) "HTML" (#<sys:regex: 95e4618> 1+ #\espace) "PUBLIC" (#<sys:regex: 95e46a8> 1+ #\espace) "\e"-//W3C//DTD" (#<sys:regex: 95e4750> 1+ #\espace) "HTML" (#<sys:regex: 95e47d8> 1+ #\espace) "3.2" (#<sys:regex: 95e4860> 1+ #\espace) "Final\e"//EN>")
  depth: 3
  data (1:0):
  "" . "<!DOCTYPE HTML PUBLIC \e"-//W3C//DTD HTML 3.2 Final\e"//EN>"
.cble

The user types
.code s
to step in.

.cblk
  txr> s
  stopped at line 2 of navytime.txr
  form: "<!DOCTYPE"
  depth: 4
  data (1:0):
  "" . "<!DOCTYPE HTML PUBLIC \e"-//W3C//DTD HTML 3.2 Final\e"//EN>"
.cble

Now, the form about to be processed is the first item of the
.codn (sys:text ...) ,
the string
.strn <!DOCTYPE .

The input is shown broken into two quoted strings with a dot in between.
The dot indicates the current position. The left string is empty, meaning
that this is the leftmost position. The programmer steps:

.cblk
  txr> s
  stopped at line 2 of navytime.txr
  form: (#<sys:regex: 95e4590> 1+ #\espace)
  depth: 4
  data (1:9):
  "<!DOCTYPE" . " HTML PUBLIC \e"-//W3C//DTD HTML 3.2 Final\e"//EN>"
.cble

Control has now passed to the second element of the
.codn (sys:text ...) ,
a regular expression which matches one or more spaces, generated by
a single space in the source code according to the language rules.

The input context shows that
.str <!DOCTYPE
was matched in the input, and the position moved past it.

.cblk
  txr> s
  stopped at line 2 of navytime.txr
  form: "HTML"
  depth: 4
  data (1:10):
  "<!DOCTYPE " . "HTML PUBLIC \e"-//W3C//DTD HTML 3.2 Final\e"//EN>"
.cble

Now, the regular expression has matched and moved the position past
the space; the facing input is now
.strn "HTML ..." .

The programmer then repeats the
.code s
command by hitting Enter.

.cblk
  txr>
  stopped at line 2 of navytime.txr
  form: (#<sys:regex: 95e4618> 1+ #\espace)
  depth: 4
  data (1:14):
  "<!DOCTYPE HTML" . " PUBLIC \e"-//W3C//DTD HTML 3.2 Final\e"//EN>"
  txr>
  stopped at line 2 of navytime.txr
  form: "PUBLIC"
  depth: 4
  data (1:15):
  "<!DOCTYPE HTML " . "PUBLIC \e"-//W3C//DTD HTML 3.2 Final\e"//EN>"
  txr>
  stopped at line 2 of navytime.txr
  form: (#<sys:regex: 95e46a8> 1+ #\espace)
  depth: 4
  data (1:21):
  "<!DOCTYPE HTML PUBLIC" . " \e"-//W3C//DTD HTML 3.2 Final\e"//EN>"
  txr>
  stopped at line 2 of navytime.txr
  form: "\e"-//W3C//DTD"
  depth: 4
  data (1:22):
  "<!DOCTYPE HTML PUBLIC " . "\e"-//W3C//DTD HTML 3.2 Final\e"//EN>"
  txr>
  stopped at line 2 of navytime.txr
  form: (#<sys:regex: 95e4750> 1+ #\espace)
  depth: 4
  data (1:34):
  "<!DOCTYPE HTML PUBLIC \e"-//W3C//DTD" . " HTML 3.2 Final\e"//EN>"
.cble

It is not evident from the session transcript, but during interactive use,
the input context appears to be animated. Whenever the programmer hits
Enter, the new context is printed and the dot appears to advance.

Eventually the programmer becomes bored and place a breakpoint on line 15,
where the
.code @(output)
block begins, and invokes the
.code c
command to continue the execution:

.cblk
  txr> b 15
  txr> c
  stopped at line 15 of navytime.txr
  form: (output (((repeat nil (((sys:var MO nil nil) "-" (sys:var DD nil nil) " " (sys:var HH nil nil) ":" (sys:var MM nil nil) ":" (sys:var SS nil nil) " " (sys:var PM nil nil) " " (sys:var TZ nil nil))) nil nil nil nil nil nil))))
  depth: 2
  data (16):
  ""
.cble

The programmer issues a
.code v
command to take a look at the variable bindings,
which indicate that the
.code @(collect)
has produced some lists:

.cblk
  txr> v
  bindings:
  0: ((PM "  " "PM" "PM" "PM" "PM" "PM" "PM") (TZNAME "Universal Time" "Eastern Time" "Central Time" "Mountain Time" "Pacific Time" "Alaska Time" "Hawaii-Aleutian Time") (TZ "UTC" "EDT" "CDT" "MDT" "PDT" "AKDT" "HAST") (SS "35" "35" "35" "35" "35" "35" "35") (MM "32" "32" "32" "32" "32" "32" "32") (HH "23" "07" "06" "05" "04" "03" "01") (DD "30" "30" "30" "30" "30" "30" "30") (MO "Mar" "Mar" "Mar" "Mar" "Mar" "Mar" "Mar"))
.cble

Then a continue command, which finishes the program, whose output appears:

.cblk
  txr> c
  Mar-30 23:22:52    UTC
  Mar-30 07:22:52 PM EDT
  Mar-30 06:22:52 PM CDT
  Mar-30 05:22:52 PM MDT
  Mar-30 04:22:52 PM PDT
  Mar-30 03:22:52 PM AKDT
  Mar-30 01:22:52 PM HAST
.cble

.SS* Compatibility

New \*(TX versions are usually intended to be backward-compatible with prior
releases in the sense that documented features will continue to work in
the same way. Due to new features, new versions of \*(TX will supply new
behaviors where old versions of \*(TX would have produced an error, such as a
syntax error. Though, strictly speaking, this means that something is working
differently in a new version, replacing an error situation with functionality
is usually not considered a deviation from backward-compatibility.

When a change is introduced which is not backward compatible, \*(TX's 
.code -C
option can be used to request emulation of old behavior.

The option was introduced in \*(TX 98, and so the oldest \*(TX version which
can be emulated is \*(TX 97.

Here are values which have a special meaning as arguments to the
.code -C
option, along with a description of what behaviors are affected:
.IP 102
Up to \*(TX 102, the
.code get-string
function did not close the stream. This old behavior is emulated.

.IP 101
Up to \*(TX 101, the
.code make-like
function incorrectly returned
.code nil
when converting the empty list
.code nil
to string type. This affects numerous generic sequence functions,
causing their result to be
.code nil
instead of an empty string.

.IP 100
Up to \*(TX 100, the
.code split-str
function had an undocumented behavior. When the
.code sep
argument was an empty string, it split the string into
individual characters as if by calling
.codn list-str .
This behavior changed to the currently
documented behavior starting in \*(TX 101.
Also, the arguments of the
.code where
function, which introduced in \*(TX 91, were reversed starting
in \*(TX 101.

.IP 99
Up to \*(TX 99, the substitution of TXR Lisp expressions in
.code @(output)
directives and in the quasistrings of the pattern language
exhibited the buggy behavior that if the TXR Lisp expression
produced a list, the list was rendered as a parenthesized
representation, or the text
.code nil
in the empty list case. Moreover, in the
.code @(output)
case, the value of TXR Lisp expressions was not subject to filtering. 
Starting with \*(TX 100, these issues
are fixed, making the the behavior is consistent with
the behavior of TXR Lisp quasiliterals.
.IP 97
Up to \*(TX 97, the error exception symbols such as
.code file-error
were named with underscores, as in
.codn file_error .
These error symbols existed:
.codn type_error ,
.codn internal_error ,
.codn numeric_error ,
.codn range_error ,
.codn query_error ,
.code file_error
and
.codn process_error .

.SH* Appendix
.SS* A. NOTES ON EXOTIC REGULAR EXPRESSIONS
Users familiar with regular expressions may not be familiar with the complement
and intersection operators, which are often absent from text processing tools
that support regular expressions.  The following remarks are offered in hope
that they are of some use.

.TP* "Equivalence to Sets"
Regexp intersection is not essential; it may be obtained from complement and
union as follows, since De Morgan's law applies to regular expression algebra:
.code (R1)&(R2)
=
.codn ~(~(R1)|~(R2)) .
(The complement of the union of the complements of
R1 and R2 constitutes the intersection.) This law works because the regular
expression operators denote set operations in a straightforward way. A regular
expression denotes a set of strings (a potentially infinite one) in a condensed
way. The union of two regular expressions
.code R1|R2
denotes the union of the set
of texts denoted by
.code R1
and that denoted by
.codn R2 .
Similarly
.code R1&R2
denotes a set intersection, and
.code ~R
denotes a set complement. Thus algebraic laws
that apply to set operations apply to regular expressions. It's useful to keep
in mind this relationship between regular expressions and sets in understanding
intersection and complement.

Given a finite set of strings, like the set
.cblk
{ "abc", "def" }
.cble
which corresponds to the regular expression
.codn (abc|def) ,
the complement is the set which contains
an infinite number of strings: it consists of all possible strings except
.str abc
and
.strn def .
It includes the empty string, all strings of length 1, all strings
of length 2, all strings of length 3 other than
.str abc
and
.strn def ,
all strings of
length 4, etc.  This means that a "harmless looking" expression like
.code ~(abc|def)
can actually match arbitrarily long inputs.

.TP* "Set Difference"
How about matching only three-character-long strings other than
.str abc
or
.strn def ?
To express this, regex intersection can be used: these strings are the
intersection of the set of all three-character strings, and the set of all
strings which are not
.str abc
or
.strn def .
The straightforward set-based reasoning
leads us to this:
.codn ...&~(abc|def) .
This
.code A&~B
idiom is also called set
difference, sometimes notated with a minus sign:
.code A-B
(which is not supported in \*(TX regular expression syntax).  Elements which
are in the set
.codn A ,
but not
.codn B ,
are those elements which are in the intersection of
.code A
with the complement of
.codn B .
This is similar to the arithmetic rule
.codn A - B = A + -B :
subtraction is
equivalent to addition of the additive inverse. Set difference is a useful
tool: it enables us to write a positive match which captures a more general set
than what is intended (but one whose regular expression is far simpler
than a positive match for the exact set we want), then we can
intersect this over-generalized set with the complemented set of
another regular expression which matches the particulars that we wish excluded.

.TP* "Expressivity versus Power"

It turns out that regular expressions which do not make use of the
complement or intersection operators are just as powerful as expressions
that do. That is to say, with or without these operators, regular expressions
can match the same sets of strings (all regular languages).  This means that
for a given regular expression which uses intersection and complement, it is
possible to find a regular expression which doesn't use these operators, yet
matches the same set of strings.  But, though they exist, such equivalent
regular expressions are often much more complicated, which makes them difficult
to design.  Such expressions do not necessarily
. B express
what it is they match; they merely capture the equivalent set. They
perform a job, without making it obvious what it is they do.  The use of
complement and intersection leads to natural ways of expressing many kinds of
matching sets, which not only demonstrate the power to carry out an operation,
but also easily express the concept.

.TP* "Example: Matching C Language Comments"

For instance, using complement, we can write a straightforward regular
expression which matches C language comments. A C language
comment is the digraph
.codn /* ,
followed by any string which does not contain the
closing sequence
.codn */ ,
followed by that closing sequence.
Examples of valid comments are
.codn /**/ ,
.code /* abc */
or
.codn /***/ .
But C
comments do not nest (cannot contain comments), so that 
.code /* /* nested */ */
actually consists of the comment
.codn /* /* nested */ ,
which is followed by the trailing junk
.codn */ .
Our simple characterization of interior part of a C comment as a string
which does not contain the terminating digraph makes use of the
complement, and can be expressed using the complemented regular expression like
this:
.codn (~.*[*][/].*) .
That is to say, strings which contain
.code */
are matched by
the expression
.codn .*[*][/].* :
zero or more arbitrary characters, followed by
.codn */ ,
followed by zero or more arbitrary characters. Therefore, the complement of
this expression matches all other strings: those which do not contain
.codn */ .
These strings make up the inside of a C comment between the
.code /*
and
.codn */ .

The equivalent simple regex is quite a bit more complicated.
Without complement, we must somehow write a positive match for all strings such
that we avoid matching
.codn */ .
Obviously, sequences of characters other than
.code *
are included:
.codn [^*]* .
Occurrences of
.code *
are also allowed, but only if followed
by something other than a slash, so let's include this via union:

.cblk
  ([^*]|[*][^/])*.
.cble

Alas, already, we have a bug in this expression. The
subexpression
.code [*][^/]
can match
.codn ** ,
since a
.code *
is not a
.codn / .
If the next character in the input is
.codn / ,
we missed a comment close.  To fix the problem we
revise to this:

.cblk
  ([^*]|[*][^*/])*
.cble

(The interior of a C language comment is a any
mixture of zero or more non-asterisks, or digraphs consisting of an asterisk
followed by something other than a slash or another asterisk).  Oops, now we
have a problem again. What if two asterisks occur in a comment?  They are not
matched by
.codn [^*] ,
and they are not matched by
.codn [*][^*/] .
Actually, our regex must not simply match asterisk-non-asterisk digraphs, but
rather sequences of one or more asterisks followed by a non-asterisk: 

.cblk
  ([^*]|[*]*[^*/])*
.cble

This is still not right, because, for instance, it fails to match the interior
of a comment which is terminated by asterisks, including the simple test cases
where the comment interior is nothing but asterisks.  We have no provision in
our expression for this case; the expression requires all runs of asterisks to
be followed by something which is not a slash or asterisk. The way to fix this
is to add on a subexpression which optionally matches a run of zero or more
interior asterisks before the comment close:

.cblk
  ([^*]|[*]*[^*/])*[*]*
.cble

Thus our the semi-final regular expression is

.cblk
  [/][*]([^*]|[*]*[^*/])*[*]*[*][/]
.cble

(Interpretation: a C comment is an interior string enclosed in
.codn /* */ ,
where this interior part
consists of a mixture of non-asterisk characters, as well as runs of asterisk
characters which are terminated by a character other than a slash, except for
possibly one rightmost run of asterisks which extends to the end of the
interior, touching the comment close. Phew!) One final simplification is
possible: the tail part
.code [*]*[*][/]
can be reduced to
.code [*]+[/]
such that the
final run of asterisks is regarded as part of an extended comment terminator
which consists of one or more asterisks followed by a slash.   The regular
expression works, but it's cryptic; to someone who has not developed it, it
isn't obvious what it is intended to match.  Working out complemented matching
without complement support from the language is not impossible, but it may be
difficult and error-prone, possibly requiring multiple iterations of
trial-and-error development involving numerous test cases, resulting in an
expression that doesn't have a straightforward relationship to the original
idea.

.TP* "The Non-Greedy Operator"

The non-greedy operator
.code %
is actually defined in terms of a set difference,
which is in turn based on intersection and complement. The uninteresting case
.code (R%)
where the right operand is empty reduces to 
.codn (R*) :
if there is no trailing
context, the non-greedy operator matches
.code R
as far as possible, possibly to the
end of the input, exactly like the greedy operator.  The interesting case
.code (R%T)
is defined as a "syntactic sugar" which expands to the expression
.code ((R*)&(~.*(T&.+).*))T
which means: match the longest string which is matched
by
.codn R* ,
but which does not contain a non-empty match for
.codn T ;
then, match
.codn T .
This is a useful and expressive notation. With it, we can write the regular
expression for matching C language comments simply like this:
.code [/][*].%[*][/]
(match the opening sequence
.codn /* ,
then match a sequence of zero or more
characters non-greedily, and then the closing sequence
.codn */ .
With the non-greedy
operator, we don't have to think about the interior of the comment as set of
strings which excludes
.codn */ .
Though the non-greedy operator appears expressive,
its apparent simplicity may be deceptive.  It looks as if it works "magically"
by itself; "somehow" this
.code .%
part "knows" only to consume enough characters so that
it doesn't swallow an occurrence of the trailing context. Care must be taken
that the trailing context passed to the operator really is the correct text
that should be excluded by the non-greedy match. For instance, take the
expression
.codn .%abc .
If you intend the trailing context to be merely
.codn a ,
you must be careful to write
.codn (.%a)bc .
Otherwise, the trailing context is
.codn abc ,
and this means that the
.code .%
match will consume the longest string that does not contain
.codn abc ,
when in fact what was intended was to consume the longest string that
does not contain
.codn a .
The change in behavior of the
.code %
operator upon modifying the
trailing context is not as intuitive as that of the * operator, because the
trailing context is deeply involved in its logic.

On a related note, for single-character trailing contexts, it may be a good
idea to use a complemented character class instead. That is to say, rather than
.codn (.%a)bc ,
consider
.codn [^a]*abc .
The set of strings which don't contain the
character a is adequately expressed by
.codn [^a]* .