File: newlisp_manual.html

package info (click to toggle)
newlisp 10.7.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 6,248 kB
  • sloc: ansic: 33,280; lisp: 4,181; sh: 609; makefile: 215
file content (30419 lines) | stat: -rw-r--r-- 955,491 bytes parent folder | download
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
27540
27541
27542
27543
27544
27545
27546
27547
27548
27549
27550
27551
27552
27553
27554
27555
27556
27557
27558
27559
27560
27561
27562
27563
27564
27565
27566
27567
27568
27569
27570
27571
27572
27573
27574
27575
27576
27577
27578
27579
27580
27581
27582
27583
27584
27585
27586
27587
27588
27589
27590
27591
27592
27593
27594
27595
27596
27597
27598
27599
27600
27601
27602
27603
27604
27605
27606
27607
27608
27609
27610
27611
27612
27613
27614
27615
27616
27617
27618
27619
27620
27621
27622
27623
27624
27625
27626
27627
27628
27629
27630
27631
27632
27633
27634
27635
27636
27637
27638
27639
27640
27641
27642
27643
27644
27645
27646
27647
27648
27649
27650
27651
27652
27653
27654
27655
27656
27657
27658
27659
27660
27661
27662
27663
27664
27665
27666
27667
27668
27669
27670
27671
27672
27673
27674
27675
27676
27677
27678
27679
27680
27681
27682
27683
27684
27685
27686
27687
27688
27689
27690
27691
27692
27693
27694
27695
27696
27697
27698
27699
27700
27701
27702
27703
27704
27705
27706
27707
27708
27709
27710
27711
27712
27713
27714
27715
27716
27717
27718
27719
27720
27721
27722
27723
27724
27725
27726
27727
27728
27729
27730
27731
27732
27733
27734
27735
27736
27737
27738
27739
27740
27741
27742
27743
27744
27745
27746
27747
27748
27749
27750
27751
27752
27753
27754
27755
27756
27757
27758
27759
27760
27761
27762
27763
27764
27765
27766
27767
27768
27769
27770
27771
27772
27773
27774
27775
27776
27777
27778
27779
27780
27781
27782
27783
27784
27785
27786
27787
27788
27789
27790
27791
27792
27793
27794
27795
27796
27797
27798
27799
27800
27801
27802
27803
27804
27805
27806
27807
27808
27809
27810
27811
27812
27813
27814
27815
27816
27817
27818
27819
27820
27821
27822
27823
27824
27825
27826
27827
27828
27829
27830
27831
27832
27833
27834
27835
27836
27837
27838
27839
27840
27841
27842
27843
27844
27845
27846
27847
27848
27849
27850
27851
27852
27853
27854
27855
27856
27857
27858
27859
27860
27861
27862
27863
27864
27865
27866
27867
27868
27869
27870
27871
27872
27873
27874
27875
27876
27877
27878
27879
27880
27881
27882
27883
27884
27885
27886
27887
27888
27889
27890
27891
27892
27893
27894
27895
27896
27897
27898
27899
27900
27901
27902
27903
27904
27905
27906
27907
27908
27909
27910
27911
27912
27913
27914
27915
27916
27917
27918
27919
27920
27921
27922
27923
27924
27925
27926
27927
27928
27929
27930
27931
27932
27933
27934
27935
27936
27937
27938
27939
27940
27941
27942
27943
27944
27945
27946
27947
27948
27949
27950
27951
27952
27953
27954
27955
27956
27957
27958
27959
27960
27961
27962
27963
27964
27965
27966
27967
27968
27969
27970
27971
27972
27973
27974
27975
27976
27977
27978
27979
27980
27981
27982
27983
27984
27985
27986
27987
27988
27989
27990
27991
27992
27993
27994
27995
27996
27997
27998
27999
28000
28001
28002
28003
28004
28005
28006
28007
28008
28009
28010
28011
28012
28013
28014
28015
28016
28017
28018
28019
28020
28021
28022
28023
28024
28025
28026
28027
28028
28029
28030
28031
28032
28033
28034
28035
28036
28037
28038
28039
28040
28041
28042
28043
28044
28045
28046
28047
28048
28049
28050
28051
28052
28053
28054
28055
28056
28057
28058
28059
28060
28061
28062
28063
28064
28065
28066
28067
28068
28069
28070
28071
28072
28073
28074
28075
28076
28077
28078
28079
28080
28081
28082
28083
28084
28085
28086
28087
28088
28089
28090
28091
28092
28093
28094
28095
28096
28097
28098
28099
28100
28101
28102
28103
28104
28105
28106
28107
28108
28109
28110
28111
28112
28113
28114
28115
28116
28117
28118
28119
28120
28121
28122
28123
28124
28125
28126
28127
28128
28129
28130
28131
28132
28133
28134
28135
28136
28137
28138
28139
28140
28141
28142
28143
28144
28145
28146
28147
28148
28149
28150
28151
28152
28153
28154
28155
28156
28157
28158
28159
28160
28161
28162
28163
28164
28165
28166
28167
28168
28169
28170
28171
28172
28173
28174
28175
28176
28177
28178
28179
28180
28181
28182
28183
28184
28185
28186
28187
28188
28189
28190
28191
28192
28193
28194
28195
28196
28197
28198
28199
28200
28201
28202
28203
28204
28205
28206
28207
28208
28209
28210
28211
28212
28213
28214
28215
28216
28217
28218
28219
28220
28221
28222
28223
28224
28225
28226
28227
28228
28229
28230
28231
28232
28233
28234
28235
28236
28237
28238
28239
28240
28241
28242
28243
28244
28245
28246
28247
28248
28249
28250
28251
28252
28253
28254
28255
28256
28257
28258
28259
28260
28261
28262
28263
28264
28265
28266
28267
28268
28269
28270
28271
28272
28273
28274
28275
28276
28277
28278
28279
28280
28281
28282
28283
28284
28285
28286
28287
28288
28289
28290
28291
28292
28293
28294
28295
28296
28297
28298
28299
28300
28301
28302
28303
28304
28305
28306
28307
28308
28309
28310
28311
28312
28313
28314
28315
28316
28317
28318
28319
28320
28321
28322
28323
28324
28325
28326
28327
28328
28329
28330
28331
28332
28333
28334
28335
28336
28337
28338
28339
28340
28341
28342
28343
28344
28345
28346
28347
28348
28349
28350
28351
28352
28353
28354
28355
28356
28357
28358
28359
28360
28361
28362
28363
28364
28365
28366
28367
28368
28369
28370
28371
28372
28373
28374
28375
28376
28377
28378
28379
28380
28381
28382
28383
28384
28385
28386
28387
28388
28389
28390
28391
28392
28393
28394
28395
28396
28397
28398
28399
28400
28401
28402
28403
28404
28405
28406
28407
28408
28409
28410
28411
28412
28413
28414
28415
28416
28417
28418
28419
28420
28421
28422
28423
28424
28425
28426
28427
28428
28429
28430
28431
28432
28433
28434
28435
28436
28437
28438
28439
28440
28441
28442
28443
28444
28445
28446
28447
28448
28449
28450
28451
28452
28453
28454
28455
28456
28457
28458
28459
28460
28461
28462
28463
28464
28465
28466
28467
28468
28469
28470
28471
28472
28473
28474
28475
28476
28477
28478
28479
28480
28481
28482
28483
28484
28485
28486
28487
28488
28489
28490
28491
28492
28493
28494
28495
28496
28497
28498
28499
28500
28501
28502
28503
28504
28505
28506
28507
28508
28509
28510
28511
28512
28513
28514
28515
28516
28517
28518
28519
28520
28521
28522
28523
28524
28525
28526
28527
28528
28529
28530
28531
28532
28533
28534
28535
28536
28537
28538
28539
28540
28541
28542
28543
28544
28545
28546
28547
28548
28549
28550
28551
28552
28553
28554
28555
28556
28557
28558
28559
28560
28561
28562
28563
28564
28565
28566
28567
28568
28569
28570
28571
28572
28573
28574
28575
28576
28577
28578
28579
28580
28581
28582
28583
28584
28585
28586
28587
28588
28589
28590
28591
28592
28593
28594
28595
28596
28597
28598
28599
28600
28601
28602
28603
28604
28605
28606
28607
28608
28609
28610
28611
28612
28613
28614
28615
28616
28617
28618
28619
28620
28621
28622
28623
28624
28625
28626
28627
28628
28629
28630
28631
28632
28633
28634
28635
28636
28637
28638
28639
28640
28641
28642
28643
28644
28645
28646
28647
28648
28649
28650
28651
28652
28653
28654
28655
28656
28657
28658
28659
28660
28661
28662
28663
28664
28665
28666
28667
28668
28669
28670
28671
28672
28673
28674
28675
28676
28677
28678
28679
28680
28681
28682
28683
28684
28685
28686
28687
28688
28689
28690
28691
28692
28693
28694
28695
28696
28697
28698
28699
28700
28701
28702
28703
28704
28705
28706
28707
28708
28709
28710
28711
28712
28713
28714
28715
28716
28717
28718
28719
28720
28721
28722
28723
28724
28725
28726
28727
28728
28729
28730
28731
28732
28733
28734
28735
28736
28737
28738
28739
28740
28741
28742
28743
28744
28745
28746
28747
28748
28749
28750
28751
28752
28753
28754
28755
28756
28757
28758
28759
28760
28761
28762
28763
28764
28765
28766
28767
28768
28769
28770
28771
28772
28773
28774
28775
28776
28777
28778
28779
28780
28781
28782
28783
28784
28785
28786
28787
28788
28789
28790
28791
28792
28793
28794
28795
28796
28797
28798
28799
28800
28801
28802
28803
28804
28805
28806
28807
28808
28809
28810
28811
28812
28813
28814
28815
28816
28817
28818
28819
28820
28821
28822
28823
28824
28825
28826
28827
28828
28829
28830
28831
28832
28833
28834
28835
28836
28837
28838
28839
28840
28841
28842
28843
28844
28845
28846
28847
28848
28849
28850
28851
28852
28853
28854
28855
28856
28857
28858
28859
28860
28861
28862
28863
28864
28865
28866
28867
28868
28869
28870
28871
28872
28873
28874
28875
28876
28877
28878
28879
28880
28881
28882
28883
28884
28885
28886
28887
28888
28889
28890
28891
28892
28893
28894
28895
28896
28897
28898
28899
28900
28901
28902
28903
28904
28905
28906
28907
28908
28909
28910
28911
28912
28913
28914
28915
28916
28917
28918
28919
28920
28921
28922
28923
28924
28925
28926
28927
28928
28929
28930
28931
28932
28933
28934
28935
28936
28937
28938
28939
28940
28941
28942
28943
28944
28945
28946
28947
28948
28949
28950
28951
28952
28953
28954
28955
28956
28957
28958
28959
28960
28961
28962
28963
28964
28965
28966
28967
28968
28969
28970
28971
28972
28973
28974
28975
28976
28977
28978
28979
28980
28981
28982
28983
28984
28985
28986
28987
28988
28989
28990
28991
28992
28993
28994
28995
28996
28997
28998
28999
29000
29001
29002
29003
29004
29005
29006
29007
29008
29009
29010
29011
29012
29013
29014
29015
29016
29017
29018
29019
29020
29021
29022
29023
29024
29025
29026
29027
29028
29029
29030
29031
29032
29033
29034
29035
29036
29037
29038
29039
29040
29041
29042
29043
29044
29045
29046
29047
29048
29049
29050
29051
29052
29053
29054
29055
29056
29057
29058
29059
29060
29061
29062
29063
29064
29065
29066
29067
29068
29069
29070
29071
29072
29073
29074
29075
29076
29077
29078
29079
29080
29081
29082
29083
29084
29085
29086
29087
29088
29089
29090
29091
29092
29093
29094
29095
29096
29097
29098
29099
29100
29101
29102
29103
29104
29105
29106
29107
29108
29109
29110
29111
29112
29113
29114
29115
29116
29117
29118
29119
29120
29121
29122
29123
29124
29125
29126
29127
29128
29129
29130
29131
29132
29133
29134
29135
29136
29137
29138
29139
29140
29141
29142
29143
29144
29145
29146
29147
29148
29149
29150
29151
29152
29153
29154
29155
29156
29157
29158
29159
29160
29161
29162
29163
29164
29165
29166
29167
29168
29169
29170
29171
29172
29173
29174
29175
29176
29177
29178
29179
29180
29181
29182
29183
29184
29185
29186
29187
29188
29189
29190
29191
29192
29193
29194
29195
29196
29197
29198
29199
29200
29201
29202
29203
29204
29205
29206
29207
29208
29209
29210
29211
29212
29213
29214
29215
29216
29217
29218
29219
29220
29221
29222
29223
29224
29225
29226
29227
29228
29229
29230
29231
29232
29233
29234
29235
29236
29237
29238
29239
29240
29241
29242
29243
29244
29245
29246
29247
29248
29249
29250
29251
29252
29253
29254
29255
29256
29257
29258
29259
29260
29261
29262
29263
29264
29265
29266
29267
29268
29269
29270
29271
29272
29273
29274
29275
29276
29277
29278
29279
29280
29281
29282
29283
29284
29285
29286
29287
29288
29289
29290
29291
29292
29293
29294
29295
29296
29297
29298
29299
29300
29301
29302
29303
29304
29305
29306
29307
29308
29309
29310
29311
29312
29313
29314
29315
29316
29317
29318
29319
29320
29321
29322
29323
29324
29325
29326
29327
29328
29329
29330
29331
29332
29333
29334
29335
29336
29337
29338
29339
29340
29341
29342
29343
29344
29345
29346
29347
29348
29349
29350
29351
29352
29353
29354
29355
29356
29357
29358
29359
29360
29361
29362
29363
29364
29365
29366
29367
29368
29369
29370
29371
29372
29373
29374
29375
29376
29377
29378
29379
29380
29381
29382
29383
29384
29385
29386
29387
29388
29389
29390
29391
29392
29393
29394
29395
29396
29397
29398
29399
29400
29401
29402
29403
29404
29405
29406
29407
29408
29409
29410
29411
29412
29413
29414
29415
29416
29417
29418
29419
29420
29421
29422
29423
29424
29425
29426
29427
29428
29429
29430
29431
29432
29433
29434
29435
29436
29437
29438
29439
29440
29441
29442
29443
29444
29445
29446
29447
29448
29449
29450
29451
29452
29453
29454
29455
29456
29457
29458
29459
29460
29461
29462
29463
29464
29465
29466
29467
29468
29469
29470
29471
29472
29473
29474
29475
29476
29477
29478
29479
29480
29481
29482
29483
29484
29485
29486
29487
29488
29489
29490
29491
29492
29493
29494
29495
29496
29497
29498
29499
29500
29501
29502
29503
29504
29505
29506
29507
29508
29509
29510
29511
29512
29513
29514
29515
29516
29517
29518
29519
29520
29521
29522
29523
29524
29525
29526
29527
29528
29529
29530
29531
29532
29533
29534
29535
29536
29537
29538
29539
29540
29541
29542
29543
29544
29545
29546
29547
29548
29549
29550
29551
29552
29553
29554
29555
29556
29557
29558
29559
29560
29561
29562
29563
29564
29565
29566
29567
29568
29569
29570
29571
29572
29573
29574
29575
29576
29577
29578
29579
29580
29581
29582
29583
29584
29585
29586
29587
29588
29589
29590
29591
29592
29593
29594
29595
29596
29597
29598
29599
29600
29601
29602
29603
29604
29605
29606
29607
29608
29609
29610
29611
29612
29613
29614
29615
29616
29617
29618
29619
29620
29621
29622
29623
29624
29625
29626
29627
29628
29629
29630
29631
29632
29633
29634
29635
29636
29637
29638
29639
29640
29641
29642
29643
29644
29645
29646
29647
29648
29649
29650
29651
29652
29653
29654
29655
29656
29657
29658
29659
29660
29661
29662
29663
29664
29665
29666
29667
29668
29669
29670
29671
29672
29673
29674
29675
29676
29677
29678
29679
29680
29681
29682
29683
29684
29685
29686
29687
29688
29689
29690
29691
29692
29693
29694
29695
29696
29697
29698
29699
29700
29701
29702
29703
29704
29705
29706
29707
29708
29709
29710
29711
29712
29713
29714
29715
29716
29717
29718
29719
29720
29721
29722
29723
29724
29725
29726
29727
29728
29729
29730
29731
29732
29733
29734
29735
29736
29737
29738
29739
29740
29741
29742
29743
29744
29745
29746
29747
29748
29749
29750
29751
29752
29753
29754
29755
29756
29757
29758
29759
29760
29761
29762
29763
29764
29765
29766
29767
29768
29769
29770
29771
29772
29773
29774
29775
29776
29777
29778
29779
29780
29781
29782
29783
29784
29785
29786
29787
29788
29789
29790
29791
29792
29793
29794
29795
29796
29797
29798
29799
29800
29801
29802
29803
29804
29805
29806
29807
29808
29809
29810
29811
29812
29813
29814
29815
29816
29817
29818
29819
29820
29821
29822
29823
29824
29825
29826
29827
29828
29829
29830
29831
29832
29833
29834
29835
29836
29837
29838
29839
29840
29841
29842
29843
29844
29845
29846
29847
29848
29849
29850
29851
29852
29853
29854
29855
29856
29857
29858
29859
29860
29861
29862
29863
29864
29865
29866
29867
29868
29869
29870
29871
29872
29873
29874
29875
29876
29877
29878
29879
29880
29881
29882
29883
29884
29885
29886
29887
29888
29889
29890
29891
29892
29893
29894
29895
29896
29897
29898
29899
29900
29901
29902
29903
29904
29905
29906
29907
29908
29909
29910
29911
29912
29913
29914
29915
29916
29917
29918
29919
29920
29921
29922
29923
29924
29925
29926
29927
29928
29929
29930
29931
29932
29933
29934
29935
29936
29937
29938
29939
29940
29941
29942
29943
29944
29945
29946
29947
29948
29949
29950
29951
29952
29953
29954
29955
29956
29957
29958
29959
29960
29961
29962
29963
29964
29965
29966
29967
29968
29969
29970
29971
29972
29973
29974
29975
29976
29977
29978
29979
29980
29981
29982
29983
29984
29985
29986
29987
29988
29989
29990
29991
29992
29993
29994
29995
29996
29997
29998
29999
30000
30001
30002
30003
30004
30005
30006
30007
30008
30009
30010
30011
30012
30013
30014
30015
30016
30017
30018
30019
30020
30021
30022
30023
30024
30025
30026
30027
30028
30029
30030
30031
30032
30033
30034
30035
30036
30037
30038
30039
30040
30041
30042
30043
30044
30045
30046
30047
30048
30049
30050
30051
30052
30053
30054
30055
30056
30057
30058
30059
30060
30061
30062
30063
30064
30065
30066
30067
30068
30069
30070
30071
30072
30073
30074
30075
30076
30077
30078
30079
30080
30081
30082
30083
30084
30085
30086
30087
30088
30089
30090
30091
30092
30093
30094
30095
30096
30097
30098
30099
30100
30101
30102
30103
30104
30105
30106
30107
30108
30109
30110
30111
30112
30113
30114
30115
30116
30117
30118
30119
30120
30121
30122
30123
30124
30125
30126
30127
30128
30129
30130
30131
30132
30133
30134
30135
30136
30137
30138
30139
30140
30141
30142
30143
30144
30145
30146
30147
30148
30149
30150
30151
30152
30153
30154
30155
30156
30157
30158
30159
30160
30161
30162
30163
30164
30165
30166
30167
30168
30169
30170
30171
30172
30173
30174
30175
30176
30177
30178
30179
30180
30181
30182
30183
30184
30185
30186
30187
30188
30189
30190
30191
30192
30193
30194
30195
30196
30197
30198
30199
30200
30201
30202
30203
30204
30205
30206
30207
30208
30209
30210
30211
30212
30213
30214
30215
30216
30217
30218
30219
30220
30221
30222
30223
30224
30225
30226
30227
30228
30229
30230
30231
30232
30233
30234
30235
30236
30237
30238
30239
30240
30241
30242
30243
30244
30245
30246
30247
30248
30249
30250
30251
30252
30253
30254
30255
30256
30257
30258
30259
30260
30261
30262
30263
30264
30265
30266
30267
30268
30269
30270
30271
30272
30273
30274
30275
30276
30277
30278
30279
30280
30281
30282
30283
30284
30285
30286
30287
30288
30289
30290
30291
30292
30293
30294
30295
30296
30297
30298
30299
30300
30301
30302
30303
30304
30305
30306
30307
30308
30309
30310
30311
30312
30313
30314
30315
30316
30317
30318
30319
30320
30321
30322
30323
30324
30325
30326
30327
30328
30329
30330
30331
30332
30333
30334
30335
30336
30337
30338
30339
30340
30341
30342
30343
30344
30345
30346
30347
30348
30349
30350
30351
30352
30353
30354
30355
30356
30357
30358
30359
30360
30361
30362
30363
30364
30365
30366
30367
30368
30369
30370
30371
30372
30373
30374
30375
30376
30377
30378
30379
30380
30381
30382
30383
30384
30385
30386
30387
30388
30389
30390
30391
30392
30393
30394
30395
30396
30397
30398
30399
30400
30401
30402
30403
30404
30405
30406
30407
30408
30409
30410
30411
30412
30413
30414
30415
30416
30417
30418
30419
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
   <meta name="author" content="Lutz Mueller"/>
   <meta name="keywords" content="newLISP Lisp SCHEME programming language 
   manual reference Artificial Intelligence AI NUEVATEC"/>
   <meta name="description" content="newLISP User Manual and Reference"/>
   <title>newLISP v.10.7.5 Manual and Reference</title>

<style type="text/css" media="screen">

.divider {
	margin-top: 2em; 
	margin-bottom: 1em;
	font-family: Times New Roman, Times, serif;
	color: #ffAA28;
	}

.title {
	font-family:Optima, Georgia, Times New Roman, Times, serif; 
	font-size: 500%;
	color: #404040;
	}

span.arw {
	color:#303030;
	font-size: 100%;
	font-weight: bold;
	}
	
span.err {
	color:#cc0000;
	}

span.function {
	font-family: Verdana, Lucida Sans, Helvetica,  sans-serif;
	color:#dd0000;
	}

span.gnu {
	font-family: Verdana, Lucida Sans, Helvetica,  sans-serif;
	color:#dd0000;
	}

h4	{
	font-family: Verdana, Lucida Sans, Helvetica,  sans-serif;
    color: #404040;
	}

b	{
	font-family: Verdana, Lucida Sans, Helvetica,  sans-serif;
    color: #606060;
	font-weight: 600;
	}


body, h1, h2, h3 {
	font-family: Helvetica, sans-serif;
	color: #404040;
	line-height: 120%;
 	}

h1, h2, h3 {
	font-family: Helvetica,  sans-serif;
	color: #101010;
	line-height: 120%;
    font-weight: 100;
 	}

p {
	font-family: Helvetica Neue, Verdana, Lucida Sans, sans-serif;
	color: #404040;
	line-height: 120%;
 	}

table {
	margin: 0px;
	margin-left: 10px;
	border-style: solid;
	border-color: #888888;
	border-width: 0px;
	padding: 0px;
	background: #f8ffff;
	font-size: 95%;
    }
    
th {
	border-style: solid;
	border-width: 1px;
	border-color: #888888;
	padding: 3px;
	background: #eeeeee;
    font-size: 100%;
    }
    
td {
	border-style: solid;
	border-width: 1px;
	border-color: #888888;
	padding: 3px;
	background: #f8ffff;
    font-size: 100%;
    }
    

pre {
	margin: 0px;
	margin-left: 10px;
	margin-right: 10px;
	border-style: dashed;
	border-width: 1px;
	border-color: #888888;
	padding: 4px;
    font-family: Andale Mono, "Bitstream Vera Sans Mono", Monaco, "Courier New";
    font-size: 90%;
	background: #f8ffff;
    }


tt {
	font-family: Andale Mono, "Bitstream Vera Sans Mono", Monaco, "Courier New";
	font-size: 100%;
	}

.license {
	margin: 30px;
	}
	
</style>

</head>
<body text="#000000" bgcolor="#FFFFFF" link="#376590" vlink="#551A8B" alink="#ffAA28">

<br/><br/><br/><br/><br/><br/><br/><br/><br/>


<center>
<span class="title">newLISP<font size='3'>&#174;</font></span>
</center>

<br/>

<center>
<b>For macOS, GNU Linux, Unix and Windows</b>
</center>

<center>
<h2>User Manual and Reference v.10.7.5</h2>
</center>

<br/><br/><br/><br/>
<center>
<span style="line-height:80%;">
<font size='1'>
<br/>Copyright &copy; 2019 Lutz Mueller&nbsp;<a href="http://www.nuevatec.com">www.nuevatec.com</a>. 
All rights reserved.<br/><br/>
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License,<br/> Version 1.2 or any later version 
published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts,<br/> 
and no Back-Cover Texts. A copy of the license is included in the section entitled 
<a href="#GNUFDL">GNU Free Documentation License</a>.<br/>
The accompanying software is protected by the 
<a href="#GNUGPL">GNU General Public License</a> V.3, June 2007.<br/>
newLISP is a registered trademark of Lutz Mueller.

</font>
</span>
</center>
<br/><br/><br/>

<center><h1>Contents</h1></center>

<h3><a href="#users_manual">User Manual</a></h3>

<ol>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#deprecated">Deprecated functions and future changes</a></li>
<li><a href="#REPL">Interactive Lisp mode</a></li>
<li><a href="#options">Command line options</a>
  <ul>
  <li><a href="#cmd_help">Command line help summary</a></li>
  <li><a href="#url_files">Specifying files as URLs</a></li>
  <li><a href="#no_init">No loading of init.lsp</a></li>
  <li><a href="#stack_size">Stack size</a></li>
  <li><a href="#max_mem">Maximum memory usage</a></li>
  <li><a href="#direct_exec">Direct execution mode</a></li>
  <li><a href="#logging">Logging I/O</a></li>
  <li><a href="#working_dir">Specifying the working directory</a></li>
  <li><a href="#tcpip_server">newLISP as a TCP/IP server</a></li>
  <li><a href="#daemon">TCP/IP daemon mode</a></li>
  <li><a href="#prompt">Suppressing the prompt and HTTP processing</a></li>
  <li><a href="#forcing_prompt">Forcing prompts in pipe I/O mode</a></li>
  <li><a href="#http_mode">HTTP only server mode</a></li>
  <li><a href="#local_domain_server">Local domain Unix socket server</a></li>
  <li><a href="#conn_timeout">Connection timeout</a></li>
  <li><a href="#inetd_daemon"><tt>inetd</tt> daemon mode</a></li>
  <li><a href="#link">Linking a source file with newLISP for a new executable</a></li>
  </ul>
</li>

<li><a href="#startup">Startup, directories, environment</a>
  <ul>
  <li><a href="#environment">Environment variable NEWLISPDIR</a></li>
  <li><a href="#initialization">The initialization file <tt>init.lsp</tt></a></li>
  <li><a href="#directories_unix">Directories on Linux, BSD, macOS</a></li>
  <li><a href="#directories_win">Directories on MS Windows</a></li>
  </ul>
</li>

<li><a href="#shared-lib">Extending newLISP with shared libraries</a></li>
<li><a href="#newlisp-lib">newLISP as a shared library</a></li>
  <ul>
  <li><a href="#newlisp-lib">newLISP as a C library</a></li>
  <li><a href="#newlisp-js-lib">newLISP as a JavaScript library</a></li>
  </ul>
<li><a href="#expressions">Evaluating newLISP expressions</a>
  <ul>
  <li><a href="#multiline">Interactive multiline expressions</a></li>
  <li><a href="#int_float">Integer, floating point data and operators</a></li>
  <li><a href="#big_int">Big integer, unlimited precision arithmetic</a></li>
  <li><a href="#eval_rules">Evaluation rules and data types</a></li>
  </ul>
</li>

<li><a href="#lambda_expressions">Lambda expressions in newLISP</a></li>
<li><a href="#nil_and_true"><tt>nil</tt>, <tt>true</tt>, <tt>cons</tt> and <tt>()</tt> in newLISP</a></li>
<li><a href="#arrays">Arrays</a></li>
<li><a href="#indexing">Indexing elements of strings, lists and arrays</a>
  <ul>
  <li><a href="#implicit_indexing">Implicit indexing for <tt>nth</tt></a></li>
  <li><a href="#implicit_default">Implicit indexing and the default functor</a></li>
  <li><a href="#implicit_rest_slice">Implicit indexing for <tt>rest</tt> and <tt>slice</tt></a></li>
  <li><a href="#implicit_modify">Modify references in lists, arrays and strings</a></li>
  </ul>
</li>  

<li><a href="#destructive">Destructive versus non-destructive functions</a>
<ul>
	<li><a href="#make_nondestructive">Make a destructive function non-destructive</a></li>
</ul>
</li>

<li><a href="#return">Early return from functions, loops, blocks</a>
  <ul>
  <li><a href="#flow_catch_throw">Using <tt>catch</tt> and <tt>throw</tt></a></li>
  <li><a href="#flow_and_or">Using <tt>and</tt> and <tt>or</tt></a></li>
  </ul>
</li>

<li><a href="#scoping">Dynamic and lexical scoping</a></li>

<li><a href="#contexts">Contexts</a>
  <ul>
  <li><a href="#context_rules">Symbol creation in contexts</a></li>
  <li><a href="#creating_contexts">Creating contexts</a></li>
  <li><a href="#scope_global">Global scope</a></li>
  <li><a href="#protection">Symbol protection</a></li>
  <li><a href="#overwrite">Overwriting global symbols and built-ins</a></li>
  <li><a href="#context_vars">Variables holding contexts</a></li>
  <li><a href="#sequence_creating">Sequence of creating contexts</a></li>
  <li><a href="#context_modules">Contexts as programming modules</a></li>
  <li><a href="#context_data">Contexts as data containers</a></li>
  <li><a href="#loading_contexts">Loading and declaring contexts</a></li>
  <li><a href="#serializing">Serializing context objects</a></li>
  </ul>
</li>

<li><a href="#default_function">The context default functor</a>
  <ul>
  <li><a href="#func_memory">Functions with memory</a></li>
  <li><a href="#hash">Hash functions and dictionaries</a></li>
  <li><a href="#pass_big">Passing data by reference</a></li>
  </ul>
</li>

<li><a href="#foop">Functional object-oriented programming</a>
  <ul>
  <li><a href="#newlisp_classes">FOOP classes and constructors</a></li>
  <li><a href="#newlisp_objects">Objects</a></li>
  <li><a href="#colon_operator">The colon <tt>:</tt> operator and polymorphism</a></li>
  <li><a href="#structure_foop">Structuring a larger FOOP program</a></li>
  </ul>
</li>

<li><a href="#multi_processing">Concurrent processing and distributed computing</a>
	<ul>
	<li><a href="#cilk">The Cilk API</a></li>
	<li><a href="#distributed">Distributed network computing</a></li>
	</ul>
</li>

<li><a href="#JSON_XML">JSON, XML, SXML and XML-RPC</a></li>
<li><a href="#internationalization">Customization, localization and UTF-8</a>
  <ul>
  <li><a href="#naming">Customizing function names</a></li>
  <li><a href="#switching">Switching the locale</a></li>
  <li><a href="#decimal_point">Decimal point and decimal comma</a></li>
  <li><a href="#unicode_utf8">Unicode and UTF-8 encoding</a></li>
  <li><a href="#utf8_capable">Functions working on UTF-8 characters</a></li>
  <li><a href="#utf8_version">Functions only available on UTF-8 enabled versions</a></li>
  </ul>
</li>

<li><a href="#commas">Commas in parameter lists</a></li>
</ol>

<h3><a href="#function_ref">Function Reference</a></h3>
<ol>
<li><a href="#symbol_names">Syntax of symbol variables and numbers</a></li>
<li><a href="#type_ids">Data types and names in the reference</a></li>
<li><a href="#functions">Functions in groups</a>
  <ul>
  <li><a href="#list_processing">List processing, flow control, and integer arithmetic</a></li>
  <li><a href="#string_operators">String and conversion functions</a></li>
  <li><a href="#floating_point">Floating point math and special functions</a></li>
  <li><a href="#matrices">Matrix functions</a></li>
  <li><a href="#array-funcs">Array functions</a></li>
  <li><a href="#bit_operators">Bit operators</a></li>
  <li><a href="#predicates">Predicates</a></li>
  <li><a href="#timedate">Date and time functions</a></li>
  <li><a href="#montecarlo">Statistics, simulation and modeling functions</a></li>
  <li><a href="#pattern">Pattern matching</a></li>
  <li><a href="#financial">Financial math functions</a></li>
  <li><a href="#input_output">File and I/O operations</a></li>
  <li><a href="#processes">Processes and the Cilk API</a></li>
  <li><a href="#directory_management">File and directory management</a></li>
  <li><a href="#http_api">HTTP networking API</a></li>
  <li><a href="#socket_tcpip">Socket TCP/IP, UDP and ICMP network API</a></li>
  <li><a href="#JS">API for newLISP in a web browser</a></li>
  <li><a href="#reflection">Reflection and customization</a></li>
  <li><a href="#system_functions">System functions</a></li>
  <li><a href="#importing_libraries">Importing libraries</a></li>
  <li><a href="#internals">newLISP internals API</a></li>
  </ul>
</li>

<li><a href="#functions_alphabetical">Functions in alphabetical order</a>
<p>
<b>
<a href="newlisp_manual.html#shell">!</a>&nbsp;
<a href="newlisp_manual.html#arithmetic">+-*/%</a>&nbsp;
<a href="newlisp_manual.html#abort">Ab</a>&nbsp;
<a href="newlisp_manual.html#append">Ap</a>&nbsp;
<a href="newlisp_manual.html#asin">As</a>&nbsp;
<a href="newlisp_manual.html#base64-dec">Ba</a>&nbsp;
<a href="newlisp_manual.html#callback">Ca</a>&nbsp;
<a href="newlisp_manual.html#clean">Cl</a>&nbsp;
<a href="newlisp_manual.html#command-event">Co</a>&nbsp;
<a href="newlisp_manual.html#current-line">Cu</a>&nbsp;
<a href="newlisp_manual.html#dec">De</a>&nbsp;
<a href="newlisp_manual.html#difference">Di</a>&nbsp;
<a href="newlisp_manual.html#do-until">Do</a>&nbsp;
<a href="newlisp_manual.html#encrypt">En</a>&nbsp;
<br/>
<a href="newlisp_manual.html#exec">Ex</a>&nbsp;
<a href="newlisp_manual.html#file-info">Fi</a>&nbsp;
<a href="newlisp_manual.html#flat">Fl</a>&nbsp;
<a href="newlisp_manual.html#gammaln">Ga</a>&nbsp;
<a href="newlisp_manual.html#global">Gl</a>&nbsp;
<a href="newlisp_manual.html#inc">In</a>&nbsp;
<a href="newlisp_manual.html#lambdap">La</a>&nbsp;
<a href="newlisp_manual.html#listp">Li</a>&nbsp;
<a href="newlisp_manual.html#macrop">Ma</a>&nbsp;
<a href="newlisp_manual.html#mul">Mu</a>&nbsp;
<a href="newlisp_manual.html#net-accept">Net</a>&nbsp;
<a href="newlisp_manual.html#new">New</a>&nbsp;
<a href="newlisp_manual.html#nth">Nt</a>&nbsp;
<a href="newlisp_manual.html#pack">Pa</a>&nbsp;
<br/>
<a href="newlisp_manual.html#pretty-print">Pr</a>&nbsp;
<a href="newlisp_manual.html#randomize">Ra</a>&nbsp;
<a href="newlisp_manual.html#read">Rea</a>&nbsp;
<a href="newlisp_manual.html#regex">Reg</a>&nbsp;
<a href="newlisp_manual.html#search">Sea</a>&nbsp;
<a href="newlisp_manual.html#sequence">Seq</a>&nbsp;
<a href="newlisp_manual.html#sleep">Sl</a>&nbsp;
<a href="newlisp_manual.html#starts-with">St</a>&nbsp;
<a href="newlisp_manual.html#sync">Sy</a>&nbsp;
<a href="newlisp_manual.html#time-of-day">Ti</a>&nbsp;
<a href="newlisp_manual.html#truep">Tr</a>&nbsp;
<a href="newlisp_manual.html#utf8">Ut</a>&nbsp;
<a href="newlisp_manual.html#write-file">Wr</a>&nbsp;
</b>
</p>
</li>  
</ol>

<h3><a href="#appendix">Appendix</a></h3>

<ul>
<li><a href="#error_codes">Error Codes</a></li>
<li><a href="#system_symbols">System Symbols</a></li>
<li><a href="#GNUFDL">GNU Free Documentation License</a></li>
<li><a href="#GNUGPL">GNU General Public License</a></li>
</ul>

<br/>
<a name="introduction"></a>

<center style="font-size: 150%">
<span class="divider">(&nbsp;<font color="#7ba9d4">&part;</font>&nbsp;)</span>
</center>

<br/><br/>

<a name="users_manual"></a>
<center><h1>newLISP User Manual</h1></center>


<h2>1. Introduction</h2>

<p>
newLISP focuses on the core components of Lisp: <em>lists</em>, <em>symbols</em>, 
and <em>lambda expressions</em>. To these, newLISP adds <em>arrays</em>,
<em>implicit indexing</em> on lists and arrays, and <em>dynamic</em> and 
<em>lexical scoping</em>. Lexical scoping is implemented using separate namespaces 
called <em>contexts</em>.</p>

<p>The result is an easier-to-learn Lisp that is even smaller than most Scheme 
implementations, but which still has about 350 built-in functions.
Not much over 200k in size on BSD systems, newLISP is built for high portability
using only the most common Unix system C-libraries. It loads quickly and has 
a small memory footprint. newLISP is as fast or faster than other popular 
scripting languages and uses very few resources.</p>

<p>Both built-in and user-defined functions, along with variables, share the 
same global symbol tree and are manipulated by the same functions. Lambda expressions 
and user-defined functions can be handled like any other list expression.</p>

<p>newLISP is dynamically scoped inside lexically separated contexts (namespaces). 
Contexts in newLISP are used for multiple purposes. They allow (1) partitioning of 
programs into modules, (2) the definition of <em>Classes</em> in FOOP 
(Functional Object Oriented Programming), (3) the definition of functions with 
state and (4) the creation of Hash trees for associative key &rarr; value storage.</p>

<p>newLISP's efficient <em>red-black</em> tree implementation can handle millions 
of symbols in namespaces or hashes without degrading performance.</p>

<p>newLISP allocates and reclaims memory automatically, without using traditional 
asynchronous garbage collection.
All objects &mdash; except for contexts, built-in primitives, and symbols &mdash;
are passed by value and are referenced only once. Upon creation objects are scheduled
for delayed deletion and Lisp cells are recycled for newly created objects.
This results in predictable processing times without the pauses found in traditional 
garbage collection. newLISP's unique automatic memory management makes it the fastest 
interactive Lisp available. More than any other Lisp, it implements the 
<em>data equals program</em> paradigm and full self reflection.</p>

<p>Many of newLISP's built-in functions are polymorphic and accept a variety 
of data types and optional parameters. This greatly reduces the number of 
functions and syntactic forms necessary to learn and implement.
High-level functions are available for string and list processing, financial math, 
statistics, and Artificial Intelligence applications.</p>

<p>newLISP has functions to modify, insert, or delete elements inside 
complex <em>nested</em> lists or <em>multi-dimensional</em> array structures.</p>

<p>Because strings can contain null characters in newLISP, they can be used to 
process binary data with most string manipulating functions.</p>

<p>newLISP can also be extended with a shared library interface
to import functions that access data in foreign binary data structures.
The distribution contains modules for importing popular C-library APIs.</p>

<p>newLISP's HTTP, TCP/IP, and UDP socket interfaces make it easy to write 
distributed networked applications. Its built-in XML interface, along with 
its text-processing features &mdash; Perl Compatible Regular Expressions (PCRE) 
and text-parsing functions &mdash; make newLISP a useful tool for CGI processing.
The source distribution includes examples of HTML forms processing.
newLISP can be run a as a CGI capable web server using its built-in http mode option.</p>

<p>newLISP has built-in support for distributed processing on networks and parallel,
concurrent processing on the same CPU with one or more processing cores.</p>

<p>The source distribution can be compiled for Linux, macOS/Darwin, BSDs, many 
other Unix flavors and MS Windows. newLISP can be compiled as a 64-bit LP64 application 
for full 64-bit memory addressing.</p>

<p>Since version 10.5.7, newLISP also can be compiled to JavaScript and run in
a <a href="http://www.newlisp.org/newlisp-js/">web browser</a>.</p>

<br/>

<h3>Licensing</h3>

<p>newLISP are licensed under version 3
of the <a href="#GNUGPL">GPL (General Public License)</a>.
The newLISP documentation as well as other documentation packaged with newLISP 
are licensed under the <a href="#GNUFDL">GNU Free Documentation License</a>. </p>

<br/>

<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>

<br/>

<a name="deprecated"></a>
<h2>2. Deprecated functions since version 10.3.0</h2>

<p>Since version 10.3.0 newLISP can switch between IPv4 and IPv6 modes during
run-time using the new <a href="#net-ipv">net-ipv</a> function. The 
<tt>-6</tt> commandline option can be used to start newLISP in IPv6 mode. 
After transition to IPv6 the <tt>-6</tt>
commandline switch will be changed to <tt>-4</tt> for starting up in IPv4
mode.</p>

<p>The old writing <tt>parse-date</tt> of <a href="#date-parse">date-parse</a>
is still recognized but deprecated since version 10.3.0. The old writing will 
be removed in a future version.</p>

<p>Since version 10.4.2 <tt>if-not</tt> is deprecated and will be removed in a
future version.</p>

<p>Since version 10.4.6 newLISP has a built-in function <a href="#json-parse">
json-parse</a> for translating JSON data into S-expressions. The module
file <tt>json.lsp</tt> is removed from the distribution.</p>

<p>Since version 10.4.8 newLISP has built-in support for unlimited precision
integers. This makes the GNU GMP module <tt>gmp.lsp</tt> obsolete.</p>

<br/>

<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>

<br/>

<a name="REPL"></a>
<h2>3. Interactive Lisp mode</h2>
<p>The best way to experience Lisp and experiment with it, is using interactive 
mode in a terminal window or operating system command shell. Since version 10.3, 
newLISP's read-eval-print-loop (REPL) accepts multi-line statements.</p>

<p>To enter a multi-line statement hit the [enter] key on an empty line after
the system prompt. To exit multi-line mode, hit the [enter] key again on an empty
line. In the following example computer output is shown in bold letters:</p>

<pre>
<b>></b> 
(define (foo x y)
    (+ x y))

<b>(lambda (x y) (+ x y))
></b> (foo 3 4)
<b>7
></b> 
</pre>

<p>Note, that multi-line mode is only possible in an OS command terminal window
or command shell.</p>

<p>Interactive Lisp mode can accept operating system shell commands. To hit
an OS command enter the '<tt>!</tt>' character right after the prompt, immediately
followed by the shell command:</p>

<pre>
<b>> </b>!ls *.html
<b>CodePatterns.html		MemoryManagement.html	newLISPdoc.html
ExpressionEvaluation.html	manual_frame.html		newlisp_index.html
License.html			newLISP-10.3-Release.html	newlisp_manual.html
> </b>
</pre>

<p>In the example a <tt>ls</tt> shell command is entered to show HTML files
in the current directory. On MS Windows a <tt>dir</tt> command could be used
in the same fashion.</p>

<p>The mode can also be used to call an editor or any other program:</p>

<pre>
<b>> </b>!vi foo.lsp
</pre>

<p>The Vi editor will open to edit the program "foo.lsp". After leaving
the editor the program could be run using a load statement:</p>

<pre>
<b>> </b>(load "foo.lsp")
</pre>

<p>The program <tt>foo.lsp</tt> is now run.</p>

<p>When using a Unix terminal or command shell, tab-expansion for built-in newLISP
functions can be used:</p>

<pre>
<b>> </b>(pri
<b>print       println     primitive?  
> (pri</b>
</pre>

<p>After entering the characters <tt> (pri </tt>  hit the [tab] key once to
show all the built-in functions starting with the same characters. When hitting
[tab] twice before a function name has started, all built-in function names will
be displayed.</p>

<p>On most Unix, parenthesis matching can be enabled on the commandline by
including the following line in the file <tt>.inputrc</tt> in the home
directory:</p>

<pre>
set blink-matching-paren on
</pre>

<p>Not all systems have a version of <tt>libreadline</tt> advanced enough for 
this to work.</p>


<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="options"></a>
<h2>4. Command-line options, startup and directories</h2>

<a name="cmd_help"></a>
<h3>Command line help summary</h3>

<p>When starting newLISP from the command-line several switches and options and 
source files can be specified. Executing:</p>

<pre>
newlisp -h
</pre>

<p>in a command shell will produce the following summary of options and switches:</p>

<pre>
 -h this help (no init.lsp)
 -n no init.lsp (must be first)
 -x &lt;source&gt; &lt;target&gt; link (no init.lsp)
 -v version
 -s &lt;stacksize&gt;
 -m &lt;max-mem-MB&gt; cell memory
 -e &lt;quoted lisp expression&gt;
 -l &lt;path-file&gt; log connections
 -L &lt;path-file&gt; log all
 -w &lt;working dir&gt;
 -c no prompts, HTTP
 -C force prompts
 -t &lt;usec-server-timeout&gt;
 -p &lt;port-no&gt;
 -d &lt;port-no&gt; demon mode
 -http only
 -http-safe safe mode
 -6 IPv6 mode
</pre>

<p>Before or after the command-line switches, files to load and execute can 
be specified. If a newLISP executable program is followed by parameters,
the program must finish with and <tt>(exit)</tt> statement, else newLISP 
will take command-line parameters as additional newLISP scripts to be 
loaded and executed.</p>

<p>On Linux and other Unix systems, a <tt>newlisp</tt> <em>man page</em> 
can be found:</p>

<pre>
man newlisp
</pre>		

<p>This will display a man page in the Linux/Unix shell.</p>

<br/>

<a name="url_files"></a>
<h3>Specifying files as URLs</h3>

<p>newLISP will load and execute files specified on the command-line. Files are 
specified with either their pathname or a <tt>file://</tt> URL on the local file 
system or with a <tt>http://</tt> URL on remote file systems running an HTTP
server. That HTTP server can be newLISP running in HTTP server mode.</p>

<pre>
newlisp aprog.lsp bprog.lsp prog.lsp
newlisp http://newlisp.org/example.lsp
newlisp file:///usr/home/newlisp/demo.lsp
</pre>

<br/>

<a name="no_init"></a>
<h3>No loading of init.lsp</h3>

<p>This option suppresses loading of any present initialization file <tt>init.lsp</tt> 
or <tt>.init.lsp</tt>. In order to work, this must be the first option specified:</p>

<pre>
newlisp -n
</pre>

<p>More about <a href="#initialization">initialization files.</a></p>

<br/>

<a name="stack_size"></a>
<h3>Stack size</h3>

<pre>
newlisp -s 4000
newlisp -s 100000 aprog bprog
newlisp -s 6000 myprog
newlisp -s 6000 http://asite.com/example.lsp
</pre>		

<p>The above examples show starting newLISP with different stack sizes using 
the <tt>-s</tt> option, as well as loading one or more newLISP source files
and loading files specified by an URL. When no stack size is specified, 
the stack defaults to 2048. Per stack position about 80 bytes of memory are
preallocated.</p>

<br/>

<a name="max_mem"></a>
<h3>Maximum memory usage</h3>

<pre>
newlisp -m 128
</pre>		

<p>This example limits newLISP cell memory to 128 megabytes. In 32-bit newLISP, 
each Lisp cell consumes 16 bytes, so the argument <tt>128</tt> would 
represent a maximum of 8,388,608 newLISP cells. This information is returned 
by <a href="#sys-info">sys-info</a> as the list's second element. Although 
Lisp cell memory is not the only memory consumed by newLISP, it is a good 
estimate of overall dynamic memory usage.</p>

<br/>

<a name="direct_exec"></a>
<h3>Direct execution mode</h3>

<p>Small pieces of newLISP code can be executed directly from the command-line:</p>

<pre>
newlisp -e "(+ 3 4)"  <span class="arw">&rarr;</span> 7 ; On MS Windows and Unix

newlisp -e '(append "abc" "def")'  <span class="arw">&rarr;</span> "abcdef" ; On Unix
</pre>		

<p>The expression enclosed in quotation marks is evaluated, and the result is 
printed to standard out (STDOUT). In most Unix system shells, single quotes 
can also be used as command string delimiters. Note that there is a space between 
<tt>-e</tt> and the quoted command string.</p>

<br/>

<a name="logging"></a>
<h3>Logging I/O</h3>

<p>In any mode, newLISP can write a log when started with the <tt>-l</tt> or <tt>-L</tt> 
option. Depending on the mode newLISP is running, different output is written to the log 
file. Both options always must specify the  path of a log-file. The path may be a relative 
path and can be either attached or detached to the <tt>-l</tt> or <tt>-L</tt> option. 
If the file does not exist, it is created when the first logging output is written.</p>

<pre>
newlisp -l./logfile.txt -c

newlisp -L /usr/home/www/log.txt -http -w /usr/home/www/htpdocs
</pre>

<p>The following table shows the items logged in different situations:</p>

<table  width="98%" summary="logging formats">
<tr align="left"><th>logging mode</th><th>command-line and net-eval with 
<tt>-c</tt></th><th>HTTP server with <tt>-http</tt></th></tr>
<tr><td><tt>newlisp -l</tt></td>
    <td>log only input and network connections</td>
    <td>log only network connections</td></tr>
<tr><td><tt>newlisp -L</tt></td>
    <td>log also newLISP output (w/o prompts)</td>
    <td>log also HTTP requests</td></tr>
</table><br/>

<p>All logging output is written to the file specified after the <tt>-l</tt> 
or <tt>-L</tt> option.</p>

<br/>

<a name="working_dir"></a>
<h3>Specifying the working directory</h3>

<p>The <tt>-w</tt> option specifies the initial working directory for newLISP 
after startup:</p>

<pre>
newlisp -w /usr/home/newlisp
</pre>

<p>All file requests without a directory path will now be directed to the path 
specified with the <tt>-w</tt> option.</p>

<br/>

<a name="prompt"></a>
<h3>Suppressing the prompt and HTTP processing</h3>

<p>The command-line prompt and initial copyright banner can be suppressed:</p>

<pre>
newlisp -c
</pre>

<p>Listen and connection messages are suppressed if logging is not enabled. 
The <tt>-c</tt> option is useful when controlling newLISP 
from other programs; it is mandatory when setting it up 
as a <a href="#net-eval">net-eval</a> server.</p>

<p>The <tt>-c</tt> option also enables newLISP server nodes to answer
<tt>HTTP GET</tt>, <tt>PUT</tt>, <tt>POST</tt> and <tt>DELETE</tt> requests, 
as well as perform CGI processing. Using the <tt>-c</tt> option,
together with the <tt>-w</tt> and <tt>-d</tt> options,
newLISP can serve as a standalone <tt>httpd</tt> webserver:</p>

<pre>
newlisp -c -d 8080 -w /usr/home/www
</pre>

<p>When running newLISP as a <tt>inetd</tt> or <tt>xinetd</tt> enabled
server on Unix machines, use:</p>

<pre>
newlisp -c -w /usr/home/www
</pre>

<p>
In <tt>-c</tt> mode, newLISP processes command-line requests as well as
HTTP and <a href="#net-eval">net-eval</a> requests. Running
newLISP in this mode is only recommended on a machine behind
a firewall. This mode should not be run on machines open and accessible
through the Internet. To suppress the processing of 
<a href="#net-eval">net-eval</a> and command-line&ndash;like requests, use 
the safer <tt>-http</tt> option.</p>

<br/>

<a name="forcing_prompt"></a>
<h3>Forcing prompts in pipe I/O mode</h3>

<p>A capital <tt>C</tt> forces prompts when running newLISP in pipe I/O mode
inside the Emacs editor:</p>

<pre>
newlisp -C
</pre>

<p>
To suppress console output from return values from evaluations, 
use <a href="#silent">silent</a>.</p>

<br/>

<a name="tcpip_server"></a>
<h3>newLISP as a TCP/IP server</h3>

<pre>
newlisp some.lsp -p 9090
</pre>		

<p>
This example shows how newLISP can listen for commands on a TCP/IP socket 
connection. In this case, standard I/O is redirected to the port specified with
the <tt>-p</tt> option. <tt>some.lsp</tt> is an optional file loaded during 
startup, before listening for a connection begins.</p>

<p>
The <tt>-p</tt> option is mainly used to control newLISP from another 
application, such as a newLISP GUI front-end or a program written in another 
language. As soon as the controlling client closes the connection, newLISP 
will exit.</p>

<p>
A telnet application can be used to test running newLISP as a server. First 
enter:</p>

<pre>
newlisp -p 4711 &amp;
</pre>		

<p>
The <tt>&amp;</tt> indicates to a Unix shell to run the process in the 
background. On Windows, start the server process without the <tt>&amp;</tt> 
in the foreground and open a second command window for the telnet application.
Now connect with a telnet:</p>

<pre>
telnet localhost 4711
</pre>	

<p>
If connected, the newLISP sign-on banner and prompt appear. Instead of 
<tt>4711</tt>, any other port number could be used.</p>

<p>
When the client application closes the connection, newLISP will exit, too.
</p>

<br/>

<a name="daemon"></a>
<h3>TCP/IP daemon mode</h3>

<p>When the connection to the client is closed in <tt>-p</tt> mode, newLISP 
exits. To avoid this, use the <tt>-d</tt> option instead of the <tt>-p</tt> 
option:</p>

<pre>
newlisp -d 4711 &amp;
</pre>		

<p>
This works like the <tt>-p</tt> option, but newLISP does not exit after a 
connection closes. Instead, it stays in memory, listening for a new connection 
and preserving its state. An <a href="#exit">exit</a> issued from a client 
application closes the network connection, and the newLISP daemon remains 
resident, waiting for a new connection. Any port number could be used in place 
of <tt>4711</tt>. </p>

<p>After each transaction, when a connection closes, newLISP will go through a 
reset process, reinitialize stack and signals and go to the <tt>MAIN</tt> 
context. Only the contents of program and variable symbols will be preserved 
when running a stateful server.</p>

<p>
When running in <tt>-p</tt> or <tt>-d</tt> mode, the opening and closing tags 
<tt>[cmd]</tt> and <tt>[/cmd]</tt> must be used to enclose multiline 
statements.  They must each appear on separate lines. This makes it possible 
to transfer larger portions of code from controlling applications. </p>

<p>The following variant of the <tt>-d</tt> mode is frequently used in a 
distributed computing environment, together with 
<a href="#net-eval">net-eval</a> on the client side:</p>

<pre>
newlisp -c -d 4711 &amp;
</pre>		

<p>The <tt>-c</tt> spec suppresses prompts, making this mode suitable 
for receiving requests from the <a href="#net-eval">net-eval</a> function.</p>

<p>newLISP server nodes running will also answer <tt>HTTP GET</tt>, 
<tt>PUT</tt> and <tt>DELETE</tt> requests. This can be used to retrieve and 
store files with <a href="#get-url">get-url</a>, <a href="#put-url">put-url</a>,
<a href="#delete-url">delete-url</a>, <a href="#read-file">read-file</a>, 
<a href="#write-file">write-file</a> and <a href="#append-file">append-file</a>,
or to load and save programs using <a href="#load">load</a> and 
<a href="#save">save</a> from and to remote server nodes. See the chapters for 
the <tt>-c</tt> and <tt>-http</tt> options for more details.</p>

<br/>

<a name="http_mode"></a>
<h3>HTTP-only server mode</h3>
<p> newLISP can be limited to HTTP processing using the <tt>-http</tt> option.
With this mode, a secure <tt>httpd</tt> web server daemon can be configured:</p>

<pre>
newlisp -http -d 8080 -w /usr/home/www
</pre>

<p> When running newLISP as an <tt>inetd</tt> or <tt>xinetd</tt>-enabled
server on Unix machines, use:</p>

<pre>
newlisp -http -w /usr/home/www
</pre>

<p>To further enhance security and HTTP processing, load a program during 
startup when using this mode:</p>

<pre>
newlisp httpd-conf.lsp -http -w /usr/home/www
</pre>

<p>The file <tt>httpd-conf.lsp</tt> contains a <a href="#command-event">command-event</a>
function configuring a user-defined function to analyze, filter and translate requests.
See the reference for this function for a working example.</p>

<p>In the HTTP modes enabled by either <tt>-c</tt> or <tt>-http</tt>, the 
following file types are recognized, and a correctly formatted 
<tt>Content-Type:</tt> header is sent back:</p>

<table  summary="media types">
<tr align="left"><th>file extension</th><th>media type</th></tr>
<tr><td>.avi</td><td>video/x-msvideo</td></tr>
<tr><td>.css</td><td>text/css</td></tr>
<tr><td>.gif</td><td>image/gif</td></tr>
<tr><td>.htm</td><td>text/htm</td></tr>
<tr><td>.html</td><td>text/html</td></tr>
<tr><td>.jpg</td><td>image/jpg</td></tr>
<tr><td>.js</td><td>application/javascript</td></tr>
<tr><td>.mov</td><td>video/quicktime</td></tr>
<tr><td>.mp3</td><td>audio/mpeg</td></tr>
<tr><td>.mpg</td><td>video/mpeg</td></tr>
<tr><td>.pdf</td><td>application/pdf</td></tr>
<tr><td>.png</td><td>image/png</td></tr>
<tr><td>.wav</td><td>audio/x-wav</td></tr>
<tr><td>.zip</td><td>application/zip</td></tr>
<tr><td><em>any other</em></td><td>text/plain</td></tr>
</table><br/>

<p>To serve CGI, HTTP server mode needs a <tt>/tmp</tt> directory on Unix-like
platforms or a <tt>C:\tmp</tt> directory on MS Windows. newLISP can process GET, PUT,
POST and DELETE requests and create custom response headers. CGI files must have
the extension <tt>.cgi</tt> and have executable permission on Unix. More 
information about CGI processing for newLISP server modes can be found in the 
document <a href="http://www.newlisp.org/CodePatterns.html">
Code Patterns in newLISP</a>. </p>

<p>In both server modes <tt>-c</tt> and <tt>-http</tt> the environment 
variables DOCUMENT_ROOT, HTTP_HOST, REMOTE_ADDR, REQUEST_METHOD, REQUEST_URI, 
SERVER_SOFTWARE and QUERY_STRING are set. The variables CONTENT_TYPE, 
CONTENT_LENGTH, HTTP_HOST, HTTP_USER_AGENT and HTTP_COOKIE are also set, if 
present in the HTTP header sent by the client. Environment variables can be
read using the <a href="#env">env</a> function.</p>

<br/>
		
<a name="local_domain_server"></a>
<h3>Local domain Unix socket server</h3>

<p>Instead of a port, a local domain Unix socket path can be specified in
the <tt>-d</tt> or <tt>-p</tt> server modes.</p>

<pre>
newlisp -c -d /tmp/mysocket &amp;
</pre>	

<p>Test the server using another newLISP process:</p>

<pre>
newlisp -e '(net-eval "/tmp/mysocket" 0 "(symbols)")'
</pre>	

<p>A list of all built-in symbols will be printed to the terminal</p>

<p>This mode will work together with local domain socket modes of
<a href="#net-connect">net-connect</a>, <a href="#net-listen">net-listen</a>,
and <a href="#net-eval">net-eval</a>. Local domain sockets opened with 
<tt>net-connect</tt> and <tt>net-listen</tt> can be served using
<a href="#net-accept">net-accept</a>, <a href="#net-receive">net-receive</a>,
and <a href="#net-send">net-send</a>. Local domain socket connections
can be monitored using <a href="#net-peek">net-peek</a> and 
<a href="#net-select">net-select</a>.</p>

<p>Local domain socket connections are much faster than normal TCP/IP network
connections and preferred for communications between processes on
the same local file system in distributed applications. This mode is not
available on MS Windows.</p>

<br/>

<a name="conn_timeout"></a>
<h3>Connection timeout</h3>

<p>Specifies  a  connection timeout when running in <tt>-p</tt> or <tt>-d</tt> 
demon mode. A newLISP Server will disconnect when no further input is read 
after accepting a client connection. The timeout is specified in micro 
seconds:</p>

<pre>
newlisp -c -t 3000000 -d 4711 &amp;
</pre>

<p>The example specifies a timeout of three seconds.</p>

<br/>

<a name="inetd_daemon"></a>
<h3><tt>inetd</tt>	 daemon mode</h3>

<p>
The <tt>inetd</tt> server running on virtually all Linux/Unix OSes can function 
as a proxy for newLISP. The server accepts TCP/IP or UDP connections and passes 
on requests via standard I/O to newLISP. <tt>inetd</tt> starts a newLISP 
process for each client connection. When a client disconnects, the connection 
is closed and the newLISP process exits.</p>

<p>
<tt>inetd</tt> and newLISP together can handle multiple connections efficiently 
because of newLISP's small memory footprint, fast executable, and short program 
load times. When working with <a href="#net-eval">net-eval</a>, this mode is 
preferred for efficiently handling multiple requests in a distributed computing 
environment.</p>

<p>
Two files must be configured: <tt>services</tt> and <tt>inetd.conf</tt>. 
Both are ASCII-editable and can usually be found at <tt>/etc/services</tt> and 
<tt>/etc/inetd.conf</tt>.
</p>

<p>
Put one of the following lines into <tt>inetd.conf:</tt>
</p>

<pre>
net-eval  stream  tcp  nowait  root  /usr/local/bin/newlisp -c
											 
# as an alternative, a program can also be preloaded
											 
net-eval  stream  tcp  nowait  root  /usr/local/bin/newlisp -c myprog.lsp
</pre>

<p>
Instead of <tt>root</tt>, another user and optional group can be specified. 
For details, see the Unix man page for <tt>inetd</tt>.
</p>

<p>
The following line is put into the <tt>services</tt> file:
</p>

<pre>
net-eval        4711/tcp     # newLISP net-eval requests
</pre>		

<p>
On macOS and some Unix systems, <tt>xinetd</tt> can be used instead of 
<tt>inetd</tt>. Save the following to a file named <tt>net-eval</tt> in the 
<tt>/etc/xinetd.d/</tt> directory:
</p>

<pre>
service net-eval
{
    socket_type = stream
    wait = no
    user = root
    server = /usr/local/bin/newlisp
    port = 4711
    server_args = -c
    only_from = localhost
}
</pre>		
<p>
For security reasons, <tt>root</tt> should be changed to a different user
and file permissions of the www document directory adjusted accordingly.
The <tt>only_from</tt> spec can be left out to permit remote access.
</p>

<p>
See the man pages for <tt>xinetd</tt> and <tt>xinetd.conf</tt> 
for other configuration options.
</p>

<p>
After configuring the daemon, <tt>inetd</tt> or 
<tt>xinetd</tt> must be restarted to 
allow the new or changed configuration files to be read:
</p>

<pre>
kill -HUP &lt;pid&gt;
</pre>	
<p>
Replace <tt>&lt;pid&gt;</tt> with the process ID of the 
running <tt>xinetd</tt> process.
</p>

<p>A number or network protocol other than 4711 or TCP can be specified.</p>

<p>
newLISP handles everything as if the input were being entered 
on a newLISP command-line without a prompt. To test the 
<tt>inetd</tt> setup, the <tt>telnet</tt> program can be used:
</p>

<pre>
telnet localhost 4711
</pre>		

<p>
newLISP expressions can now be entered, and <tt>inetd</tt> will 
automatically handle the startup and communications of a newLISP 
process. Multiline expressions can be entered by bracketing them 
with <tt>[cmd]</tt> and <tt>[/cmd]</tt> tags, each on separate lines.
</p>

<p>newLISP server nodes answer <tt>HTTP GET</tt> and <tt>PUT</tt> requests. 
This can be used to retrieve and store files 
with <a href="#get-url">get-url</a>, <a href="#put-url">put-url</a>,
<a href="#read-file">read-file</a>, <a href="#write-file">write-file</a>
and <a href="#append-file">append-file</a>,
or to load and save programs using <a href="#load">load</a> 
and <a href="#save">save</a> from and to remote server nodes.</p>
		
<br/>

<a name="link"></a>
<h3>Linking a source file with newLISP for a new executable</h3>

<p>Source code and the newLISP executable can be linked together to build a 
self-contained application by using the <tt>-x</tt> command line flag.</p>

<pre>
;; uppercase.lsp - Link example
(println (upper-case (main-args 1)))
(exit)
</pre>		

<p>The program <tt>uppercase.lsp</tt> takes the first word on the command-line 
and converts it to uppercase.</p>

<p>To build this program as a self-contained executable,
follow these steps:</p>

<pre>
# on OSX, Linux and other UNIX

newlisp -x uppercase.lsp uppercase

chmod 755 uppercase # give executable permission

# on Windows the target needs .exe extension

newlisp -x uppercase.lsp uppercase.exe
</pre>		

<p>newLISP will find a newLISP executable in the execution path of the
environment and link a copy of the source code.</p>

<pre>
uppercase "convert me to uppercase"
</pre>	

<p>On Linux and other UNIX, if the current directory is not in the 
executable path:</p>

<pre>
./uppercase "convert me to uppercase"
</pre>	


<p>The console should print:</p>

<pre>
CONVERT ME TO UPPERCASE
</pre>		

<p>Note that neither one of the initialization files <tt>init.lsp</tt> nor 
<tt>.init.lsp</tt> is loaded during startup of linked programs.</p>

<br/>


<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>


<a name="startup"></a>
<h2>5. Startup, directories, environment</h2>

<a name="environment"></a>
<h3>Environment variable <tt>NEWLISPDIR</tt></h3>
<p>During startup, newLISP sets the environment variable <tt>NEWLISPDIR</tt>, 
if it is not set already. On Linux, BSDs, macOS and other Unixes the 
variable is set to <tt>/usr/local/share/newlisp</tt>. On MS Windows the variable is set 
to <tt>%PROGRAMFILES%/newlisp</tt>. On most MS Windows systems <tt>%PROGRAMFILES%</tt> evaluates to the <tt>C:\Program Files (x86)\</tt> directory.</p>

<p>The environment variable <tt>NEWLISPDIR</tt> is useful when loading files 
installed with newLISP:</p>

<pre>
(load (append (env "NEWLISPDIR") "/modules/mysql.lsp"))
</pre>

<p>A predefined function <tt>module</tt> can be used to shorten
the second statement loading from the <tt>modules/</tt>
directory:</p> 

<pre>
(module "mysql.lsp")
</pre>

<br/>

<a name="initialization"></a>
<h3> The initialization file <tt>init.lsp</tt></h3>
<p>Before loading any files specified on the command-line, and before the 
banner and prompt are shown. newLISP tries to load a file <tt>.init.lsp</tt> 
from the home directory of the user starting newLISP. On macOS, Linux and 
other Unix the home directory is found in the <tt>HOME</tt> environment 
variable.  On MS Windows the directory name is contained in the <tt>USERPROFILE</tt>
or <tt>DOCUMENT_ROOT</tt> environment variable.</p>

<p>If a <tt>.init.lsp</tt> cannot be found in the home directory newLISP tries
to load the file <tt>init.lsp</tt> from the directory found in the 
environment variable <tt>NEWLISPDIR</tt>.</p>

<p>When newLISP is run as a shared library, an initialization file is looked 
for in the environment variable <tt>NEWLISPLIB_INIT</tt>. The full path-name 
of the initialization file must be specified. If <tt>NEWLISPLIB_INIT</tt> is 
not defined, no initialization file will be loaded by the library module.</p>

<p> Although newLISP does not require <tt>init.lsp</tt> to run, it is 
convenient for defining functions and system-wide variables.</p>

<p>Note that neither one of the initialization files <tt>init.lsp</tt> nor 
<tt>.init.lsp</tt> is loaded during startup of linked programs or
when one of the options <tt>-n</tt>, <tt>-h</tt>, <tt>-x</tt> is
specified.</p>

<br/>

<a name="directories_unix"></a>
<h3> Directories on Linux, BSD, macOS and other Unix </h3>
<p>
The directory <tt>/usr/local/share/newlisp/modules</tt> contains modules with useful 
functions POP3 mail, etc. The directory <tt>/usr/local/share/doc/newlisp/</tt> 
contains documentation in HTML format.</p>

<br/>

<a name="directories_win"></a>
<h3>Directories on MS Windows</h3>

<p>
On MS Windows systems, all files are installed in the default directory 
<tt>%PROGRAMFILES%\newlisp</tt>.  <tt>PROGRAMFILES</tt> is a MS Windows environment 
variable that resolves to <tt>C:\Program files\newlisp\</tt> in English 
language installations. The subdirectory <tt>%PROGRAMFILES%\newlisp\modules</tt> 
contains modules for interfacing to external libraries and sample programs.</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="shared-lib"></a>
<h2>6. Extending newLISP with shared libraries</h2>
<p>Many shared libraries on Unix and MS Windows systems can be used to
extend newLISP's functionality. Examples are libraries for writing graphical
user interfaces, libraries for encryption or decryption and libraries for 
accessing databases.</p>

<p>The function <a href="#import">import</a> is used to import functions from 
external libraries. The function <a href="#callback">callback</a> is used to 
register callback functions in external libraries. 
Other functions like <a href="#pack">pack</a>,
<a href="#unpack">unpack</a>, <a href="#get-char">get-char</a>, <a href="#get-string">get-string</a>,
<a href="#get-int">get-int</a> and <a href="#get-long">get-long</a> exist
to facilitate formatting input and output to and from imported library 
functions. The fucntion <a href="#cpymem">cpymem</a> allows direct memory-to-memory
copy specifying addresses.</p>

<p>Most of the functions used when writing APIs for share libraries can cause
newLISP to segfault when not used correctly. The reference documentation marks
these functions with a <a href="#shared-lib"><font size="+1">&#x26A0;</font></a> character linking
to this chapter.</p>

<p>See also the chapter 
<a href="http://www.newlisp.org/downloads/CodePatterns.html#toc-23">
23. Extending newLISP</a> in the
<a href="http://www.newlisp.org/downloads/CodePatterns.html">
Code Patterns in newLISP</a> document.</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="newlisp-lib"></a>
<h2>7. newLISP as a shared library</h2>

<h3>newLISP as C library</h3>

<p>newLISP can be compiled as a shared C library. On Linux, BSDs and other Unix 
flavors the library is called <tt>newlisp.so</tt>. On Windows it is called 
<tt>newlisp.dll</tt> and <tt>newlisp.dylib</tt> on macOS. A newLISP shared 
library is used like any other shared library. A newLISP shared library is
only required for importing newLISP functionality into other programming
languages.</p>

<p>The main function to import is <tt>newlispEvalStr</tt>.  Like 
<a href="#eval-string">eval-string</a>, this function takes a string containing 
a newLISP expression and stores the result in a string address. The result can 
be retrieved using <a href="#get-string">get-string</a>.  The returned string 
is formatted like output from a command-line session. It contains terminating 
line-feed characters, but not the prompt string.</p>

<p>When calling <tt>newlispEvalStr</tt>, output normally directed to the 
console (e.g. return values or <a href="#print">print</a> statements) is 
returned in the form of an integer string pointer. The output can be accessed 
by passing this pointer to the <tt>get-string</tt> function. To silence the 
output from return values, use the <a href="#silent">silent</a> function.</p>

<p>To enable <em>stdio</em> on the console, import the function <tt>newlispLibConsole</tt>
and call it with a parameter of <tt>1</tt> for enabling I/O on the console
with <em>stdin</em> and <em>stdout</em>.</p>

<p>Since v.10.3.3 callbacks can also be registered using 
<tt>newlispCallback</tt>.  For more information read the chapter 
<a href="http://www.newlisp.org/downloads/CodePatterns.html#toc-24">
24. newLISP compiled as a shared library</a> in the 
<a href="http://www.newlisp.org/downloads/CodePatterns.html">
Code Patterns in newLISP</a> document.</p>


<a name="newlisp-js-lib"></a>
<h3>newLISP as a JavaScript library</h3>

<p>Since version 10.5.7, newLISP can be compiled to JavaScript using the
<a href="https://github.com/kripken/emscripten/wiki">Emscripten</a>
toolset. The library can be used to run newLISP clientr-side in a web 
browser, just like JavaScript or HTML. An HTML page can host both,
newLISP code and JavaScript code together. Both languages can call
each other. For more information see the <tt>newlisp-js-x.x.x.zip</tt>
distribution package which contains the library <tt>newlisp-js-lib.js</tt>,
documentaion and example applications. A small newLISP development
environment hosted in a browser can also be accessed here:
<a href="http://www.newlisp.org/newlisp-js/">newlisp-js</a>
The application contains links to another example application,
documentation and a download link for the whole package.</p>

<p>newLISP compiled as a JavaScript library adds new functions linked 
from <a href="newlisp_manual.html#JS">API for newLISP in a web browser</a>.
</p>
 

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="expressions"></a>
<h2>8. Evaluating newLISP expressions</h2>

<p>The following is a short introduction to newLISP statement evaluation 
and the role of integer and floating point arithmetic in newLISP.</p>

<p>Top-level expressions are evaluated when using the 
<a href="#load"> load</a> function or when entering expressions in console 
mode on the command-line.</p>

<br/>

<a name="multiline"></a>
<h3>Interactive multiline expressions</h3>

<p>Multiline expressions can be entered by entering an empty line first. 
Once in multiline mode, another empty line returns from entry mode 
and evaluates the statement(s) entered (ouput in boldface):</p>

<pre>
&gt;
(define (foo x y)
    (+ x y))

<b>(lambda (x y) (+ x y))</b>
&gt; (foo 3 4)
<b>7</b>
&gt; _
</pre>		

<p>Entering multiline mode by hitting the enter key on an empty line 
suppresses the prompt. Entering another empty line will leave the multiline 
mode and evaluate expressions.</p>

<p>As an alternativo to entering empty lines, the <tt>[cmd]</tt> and 
<tt>[/cmd]</tt> tags are used, each entered on separate lines. This mode is 
used by some interactive IDEs controlling newLISP and internally by the 
<a href="#net-eval">net-eval</a> function.</p>

<br/>

<a name="int_float"></a>
<h3>Integer, floating point data and operators</h3>

<p>newLISP functions and operators accept integer and floating point numbers, 
converting them into the needed format. For example, a bit-manipulating 
operator converts a floating point number into an integer by omitting the 
fractional part. In the same fashion, a trigonometric function will 
internally convert an integer into a floating point number before performing 
its calculation.</p>

<p>The symbol operators 
(<tt>+</tt> <tt>-</tt> <tt>*</tt> <tt>/</tt> <tt>%</tt> 
<tt>$</tt> <tt>~</tt> <tt>|</tt> <tt>^</tt> <tt>&lt;&lt;</tt> 
<tt>&gt;&gt;</tt>) return values of type integer. Functions and operators named 
with a word instead of a symbol (e.g., <tt>add</tt> rather than <tt>+</tt>) 
return floating point numbers. Integer operators truncate floating point 
numbers to integers, discarding the fractional parts.</p>

<p>newLISP has two types of basic arithmetic operators: integer (<tt>+</tt> 
<tt>-</tt> <tt>*</tt> <tt>/</tt>) and floating point (<tt>add</tt> <tt>sub</tt> 
<tt>mul</tt> <tt>div</tt>).  The arithmetic functions convert their arguments into types compatible 
with the function's own type: integer function arguments into integers, 
floating point function arguments into floating points. To make newLISP 
behave more like other scripting languages, the integer operators 
<tt>+</tt>, <tt>-</tt>, <tt>*</tt>, and <tt>/</tt> can be redefined to 
perform the floating point operators <tt>add</tt>, <tt>sub</tt>, 
<tt>mul</tt>, and <tt>div</tt>:</p>

<pre>
(constant '+ add)
(constant '- sub)
(constant '* mul)
(constant '/ div)
 
;; or all 4 operators at once
(constant '+ add '- sub '* mul '/ div)
</pre>		

<p>
Now the common arithmetic operators <tt>+</tt>, <tt>-</tt>, <tt>*</tt>, 
and <tt>/</tt> accept both integer and floating point numbers and return 
floating point results. </p>

<p>Care must be taken when <a href="#import">importing</a> from libraries 
that use functions expecting integers. After redefining <tt>+, -, *</tt>, 
and <tt>/</tt>, a double floating point number may be unintentionally passed 
to an imported function instead of an integer. In this case, floating point 
numbers can be converted into integers by using the function 
<a href="#int">int</a>. Likewise, integers can be transformed into 
floating point numbers using the <a href="#float">float</a> function:</p>

<pre>
(import "mylib.dll" "foo")  ; importing int foo(int x) from C
(foo (int x))               ; passed argument as integer
(import "mylib.dll" "bar")  ; importing C int bar(double y)
(bar (float y))             ; force double float
</pre>		

<p>Some of the modules shipping with newLISP are written assuming the 
default implementations of <tt>+</tt>, <tt>-</tt>, <tt>*</tt>, and <tt>/</tt>. 
This gives imported library functions maximum speed when performing address 
calculations.</p>

<p>The newLISP preference is to leave <tt>+</tt>, <tt>-</tt>, <tt>*</tt>, and 
<tt>/</tt> defined as integer operators and use <tt>add</tt>, <tt>sub</tt>, 
<tt>mul</tt>, and <tt>div</tt> when explicitly required. Since version 8.9.7, 
integer operations in newLISP are 64 bit operations, whereas 64 bit double 
floating point numbers offer only 52 bits of resolution in the integer part 
of the number.</p>
		
<br/>
<a name="big_int"></a>
<h3>Big integer, multiple precision arithmetic</h3>

<p>The following operators, functions and predicates work on big integers:</p>

<table  summary="functions working on big integers">
<tr align="left"><th>function</th><th>description</th></tr>

<tr>
<td width="16%"><a href="#arithmetic">+ - * / ++ -- %</a></td>
<td width="80%">arithmetic operators</td>
</tr>

<tr>
<td><a href="#logical">&lt; &gt; = &lt;= &gt;= !=</a></td>
<td>logical operators</td>
</tr>

<tr>
<td><a href="#abs">abs</a></td>
<td>returns the absolute value of a number</td>
</tr>

<tr>
<td><a href="#gcd">gcd</a></td>
<td>calculates the greatest common divisor of a group of integers</td>
</tr>

<tr>
<td><a href="#evenp">even?</a></td>
<td>checks the parity of an integer number</td>
</tr>

<tr>
<td><a href="#oddp">odd?</a></td>
<td>checks the parity of an integer number</td>
</tr>

<tr>
<td><a href="#numberp">number?</a></td>
<td>checks if an expression is a float or an integer</td>
</tr>

<tr>
<td><a href="#zerop">zero?</a></td>
<td>checks if an expression is 0 or 0.0</td>
</tr>
</table>

<p>If the first argument in any of these operators and functions is a big 
integer, the calculation performed will be in big integer mode. In the
<a href="#function_ref">Function Reference</a> section of this manual
these are marked with a <a href="#big_int"><font size="-1">bigint</font></a>
suffix.</p> 

<p>Literal integer values greater than 9223372036854775807
or smaller than -9223372036854775808, or integers with an appended letter L,
will be converted and processed in big integer mode. The function 
<a href="#bigint">bigint</a>  can be used to convert from integer, float or string 
format to big integer. The predicate <a href="#bigintp">bigint?</a> checks for 
big integer type.</p>

<pre>
; first argument triggers big integer mode because it's big enough

(+ 123456789012345678901234567890 12345) <span class='arw'>&rarr;</span> 123456789012345678901234580235L

; first small literal put in big integer format by 
; appending L to guarantee big integer mode

(+ 12345L 123456789012345678901234567890) <span class='arw'>&rarr;</span> 123456789012345678901234580235L

(setq x 1234567890123456789012345)
(* x x) <span class='arw'>&rarr;</span> 1524157875323883675049533479957338669120562399025L

; conversion from bigint to float introduces rounding errors

(bigint (float (* x x))) <span class='arw'>&rarr;</span> 1524157875323883725344000000000000000000000000000L

; sequence itself does not take big integers, before using
; apply, the sequence is converted with bigint

(apply * (map bigint (sequence 1 100))) ; calculate 100!
<span class='arw'>&rarr;</span> 93326215443944152681699238856266700490715968264381
  62146859296389521759999322991560894146397615651828
  62536979208272237582511852109168640000000000000000
  00000000L

; only the first operand needs to be bigint for apply
; to work. The following gives the same result

(apply * (cons 1L (sequence 2 100)))

; length on big integers returns the number of decimal digits
(length (apply * (map bigint (sequence 1 100)))) 
<span class='arw'>&rarr;</span> 158 ; decimal digits

; all fibonacci numbers up to 200, only the first number 
; needs to be formatted as big integer, the rest follows
; automatically - when executed from the command line in 
; a 120 char wide terminal, this shows a beautiful pattern

(let (x 1L) (series x (fn (y) (+ x (swap y x))) 200))

</pre>

<p> When doing mixed integer / big integer arithmetic, the first
argument should be a big integer to avoid erratic behaviour.</p>

<pre>
; because the first argument is 64-bit, no big integer arithmetic 
; will be done, although the second argument is big integer 

(+ 123 12345L)
<span class='arw'>&rarr;</span> 12468

; the second argument is recognized as a big integer
; and overflows the capacity of a 64-bit integer

(+ 123 123453456735645634565463563546)
<span class='arw'>&rarr;</span> <span class="err">ERR: number overflows in function +</span>

; now the first argument converts to big integer and the
; whole expression evaluates in big integer mode

(+ 123L 123453456735645634565463563546)
<span class='arw'>&rarr;</span> 123453456735645634565463563669L
</pre>

<p>Under most circumstances mixing float, integers and big integers is 
transparent. Functions automatically do conversions when needed on the 
second argument. The overflow behavior when using normal integers and 
floats only, has not changed from newLISP versions previous to 10.5.0.</p>

<br/>
<a name="eval_rules"></a>
<h3>Evaluation rules and data types</h3>

<p>Evaluate expressions by entering and editing them on the command-line. 
More complicated programs can be entered using editors like Emacs and VI, 
which have modes to show matching parentheses while typing. Load a saved 
file back into a console session by using the <a href="#load">load</a> function.
</p>
<p>
A line comment begins with a <tt>;</tt> (semicolon) or a <tt>#</tt> (number sign) 
and extends to the end of the line. newLISP ignores this line during evaluation. 
The <tt>#</tt> is useful when using newLISP as a scripting language in 
Linux/Unix environments, where the <tt>#</tt> is commonly used as a line comment 
in scripts and shells.</p>

<p>When evaluation occurs from the command-line, the result is printed to the 
console window.</p>

<p>The following examples can be entered on the command-line by typing the code 
to the left of the &nbsp;&nbsp;<span class='arw'>&rarr;</span> symbol. The 
result that appears on the next line should match the code to the right of the 
&nbsp;&nbsp;<span class='arw'>&rarr;</span> symbol.</p>

<p><b>nil</b> and <b>true</b> are Boolean data types that 
evaluate to themselves:</p>

<pre>
nil    <span class='arw'>&rarr;</span> nil
true   <span class='arw'>&rarr;</span> true
</pre>		

<p><b>Integers</b>, <b>big integers</b> and <b>floating point</b> numbers evaluate to themselves:</p>

<pre>
123      <span class='arw'>&rarr;</span> 123    ; decimal integer
0xE8     <span class='arw'>&rarr;</span> 232    ; hexadecimal prefixed by 0x
055      <span class='arw'>&rarr;</span> 45     ; octal prefixed by 0 (zero)
0b101010 <span class='arw'>&rarr;</span> 42     ; binary prefixed by 0b
1.23     <span class='arw'>&rarr;</span> 1.23   ; float
123e-3   <span class='arw'>&rarr;</span> 0.123  ; float in scientific notation

123456789012345678901234567890
<span class='arw'>&rarr;</span> 123456789012345678901234567890L ; parses to big integer
</pre>		
<p>
Integers are 64-bit including the sign bit.  Valid integers 
are numbers between -9,223,372,036,854,775,808 and
+9,223,372,036,854,775,807. Larger numbers converted from floating point 
numbers are truncated to one of the two limits. Integers internal to newLISP, 
which are limited to 32-bit numbers, overflow to either +2,147,483,647 or
-2,147,483,648.</p>

<p>Floating point numbers are IEEE 754 64-bit doubles.
Unsigned numbers up to 18,446,744,073,709,551,615 can be displayed
using special formatting characters for <a href="#format">format</a>.</p>

<p>Big integers are of unlimited precision and only limited in size by memory. 
The memory requirement of a big integer is:</p>

<blockquote>
<b><i>bytes = 4 * ceil(digits / 9) + 4.</i></b>
</blockquote>

<p>Where <i>digits</i> are decimal digits, <i>bytes</i> are 8 bits and <i>ceil</i>
is the ceiling function rounding up to the next integer.</p>

<p>
<b>Strings</b> may contain null characters and can have different 
delimiters. They evaluate to themselves.</p>

<pre>
"hello"             <span class='arw'>&rarr;</span>"hello"  
"\032\032\065\032"  <span class='arw'>&rarr;</span>"  A " 
"\x20\x20\x41\x20"  <span class='arw'>&rarr;</span>"  A "
"\t\r\n"            <span class='arw'>&rarr;</span>"\t\r\n" 
"\x09\x0d\x0a"      <span class='arw'>&rarr;</span>"\t\r\n"

;; null characters are legal in strings:
"\000\001\002"       <span class='arw'>&rarr;</span> "\000\001\002"
{this "is" a string} <span class='arw'>&rarr;</span> "this \"is\" a string"
 
;; use [text] tags for text longer than 2047 bytes:
[text]this is a string, too[/text]
<span class='arw'>&rarr;</span> "this is a string, too"
</pre>	
<p>Strings delimited by <tt>"</tt> (double quotes) will also process 
the following characters escaped with a <tt>\</tt> (backslash):</p>

<table  width="98%" summary="special characters in strings">
<tr align="left"><th>character</th><th>description</th></tr>

<tr><td><tt>\"</tt></td>
<td>for a double quote inside a quoted string</td></tr>

<tr><td><tt>\n</tt></td>
<td>for a line-feed character (ASCII 10)</td></tr>

<tr><td><tt>\r</tt></td>
<td>for a return character (ASCII 13)</td></tr>

<tr><td><tt>\b</tt></td>
<td>for a backspace BS character (ASCII 8)</td></tr>

<tr><td><tt>\t</tt></td>
<td>for a TAB character (ASCII 9)</td></tr>

<tr><td><tt>\f</tt></td>
<td>for a formfeed FF character (ASCII 12)</td></tr>

<tr><td><tt>\nnn</tt></td>
<td>for a three-digit ASCII number (nnn format 
between 000 and 255)</td></tr>

<tr><td><tt>\xnn</tt></td>
<td>for a two-digit-hex ASCII number (xnn format between x00 and xff)</td></tr> 

<tr><td><tt>\unnnn</tt></td>
<td>for a unicode character encoded in the four <tt>nnnn</tt> hexadecimal
digits. newLISP will translate this to a UTF8 character in the UTF8 enabled 
versions of newLISP.</td>
</tr>

<tr><td><tt>\\</tt></td><td>for the backslash character (ASCII 92) 
itself</td></tr>
</table><br/>

<p>Quoted strings cannot exceed 2,047 characters. Longer strings should use 
the <tt>[text]</tt> and <tt>[/text]</tt> tag delimiters.  newLISP automatically 
uses these tags for string output longer than 2,047 characters.</p>

<p>The <tt>{</tt> (left curly bracket), <tt>}</tt> (right curly bracket), 
and <tt>[text], [/text]</tt> delimiters do not perform escape character 
processing.</p>

<p><b>Lambda and lambda-macro expressions</b> evaluate to themselves:</p>

<pre>
(lambda (x) (* x x))                   <span class='arw'>&rarr;</span> (lambda (x) (* x x))
(lambda-macro (a b) (set (eval a) b))  <span class='arw'>&rarr;</span> (lambda-macro (a b) (set (eval a) b))
(fn (x) (* x x))                       <span class='arw'>&rarr;</span> (lambda (x) (* x x))  ; an alternative syntax
</pre>		

<p><b>Symbols</b> evaluate to their contents:</p>

<pre>
(set 'something 123)  <span class='arw'>&rarr;</span> 123
something             <span class='arw'>&rarr;</span> 123
</pre>		

<p><b>Contexts</b> evaluate to themselves:</p>

<pre>
(context 'CTX)  <span class='arw'>&rarr;</span> CTX
CTX             <span class='arw'>&rarr;</span> CTX
</pre>		

<p><b>Built-in functions</b> also evaluate to themselves:</p>

<pre>
add                <span class='arw'>&rarr;</span> add &lt;B845770D&gt;
(eval (eval add))  <span class='arw'>&rarr;</span> add &lt;B845770D&gt;
(constant '+ add)  <span class='arw'>&rarr;</span> add &lt;B845770D&gt;
+                  <span class='arw'>&rarr;</span> add &lt;B845770D&gt;
</pre>		

<p>In the above example, the number between the &lt; &gt; (angle brackets) 
is the hexadecimal memory address (machine-dependent) of the 
<tt>add</tt> function. It is displayed when printing a built-in primitive.</p>

<p><b>Quoted expressions</b> lose one ' (single quote) when evaluated:
</p>

<pre>
'something  <span class='arw'>&rarr;</span> something
''''any     <span class='arw'>&rarr;</span> '''any
'(a b c d)  <span class='arw'>&rarr;</span> (a b c d)
</pre>		

<p>A single quote is often used to <em>protect</em> an expression 
from evaluation (e.g., when referring to the symbol itself instead 
of its contents or to a list representing data instead of a function).</p>
		
<p><b>Lists</b> are evaluated by first evaluating the first list element
before the rest of the expression (as in Scheme). The result of the 
evaluation is applied to the remaining elements in the list and must 
be one of the following: a <tt>lambda</tt> expression, <tt>lambda-macro</tt> 
expression, or <tt>primitive</tt> (built-in) function.</p>

<pre>
(+ 1 2 3 4)                  <span class='arw'>&rarr;</span> 10
(define (double x) (+ x x))  <span class='arw'>&rarr;</span> (lambda (x) (+ x x))
</pre>		

<p>or</p>

<pre>
(set 'double (lambda (x) (+ x x)))
(double 20)               <span class='arw'>&rarr;</span> 40
((lambda (x) (* x x)) 5)  <span class='arw'>&rarr;</span> 25
</pre>		

<p>For a user-defined lambda expression, newLISP evaluates the arguments from 
left to right and binds the results to the parameters (also from left to 
right), before using the results in the body of the expression. </p>

<p>Like Scheme, newLISP evaluates the <em>functor</em> (function object) 
part of an expression before applying the result to its arguments. For 
example:</p>

<pre>
((if (&gt; X 10) * +) X Y)
</pre>		

<p>Depending on the value of X, this expression applies the <tt>*</tt> 
(product) or <tt>+</tt> (sum) function to X and Y.
</p>

<p>Because their arguments are not evaluated, <tt>lambda-macro</tt> 
expressions are useful for extending the syntax of the language. Most 
built-in functions evaluate their arguments from left to right (as needed) 
when executed. Some exceptions to this rule are indicated in the reference 
section of this manual. Lisp functions that do not evaluate all or some of 
their arguments are called <em>special forms</em>.</p>

<p><b>Arrays</b> evaluate to themselves:</p>

<pre>
(set 'A (array 2 2 '(1 2 3 4))) <span class='arw'>&rarr;</span> ((1 2) (3 4))
(eval A)                        <span class='arw'>&rarr;</span> ((1 2) (3 4))
</pre>


<p><b>Shell commands</b>: If an <tt>!</tt> (exclamation mark) 
is entered as the first character on the command-line followed by a shell 
command, the command will be executed. For example, <tt>!ls</tt> on Unix or 
<tt>!dir</tt> on MS Windows will display a listing of the present working directory. 
No spaces are permitted between the <tt>!</tt> and the shell command. Symbols 
beginning with an <tt>!</tt> are still allowed inside expressions or  on the 
command-line when preceded by a space. Note: This mode only works when running 
in the shell and does not work when controlling newLISP from another 
application.
</p>

<p>To exit the newLISP shell on Linux/Unix, press <tt>Ctrl-D</tt>; on MS Windows, 
type <tt>(exit)</tt> or <tt>Ctrl-C</tt>, then the x key.
</p>

<p>Use the <a href="#exec">exec</a> function to access shell commands from 
other applications or to pass results back to newLISP.
</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>

<br/>
<a name="lambda_expressions"></a>
<h2>9. Lambda expressions in newLISP</h2>

<p>Lambda expressions in newLISP evaluate to themselves and can be treated
just like regular lists:</p>

<pre>
(set 'double (lambda (x) (+ x x)))
(set 'double (fn (x) (+ x x)))      ; alternative syntax

(last double)  <span class='arw'>&rarr;</span> (+ x x)            ; treat lambda as a list
</pre>		

<p>Note: No <tt>'</tt> is necessary before the lambda expression because 
lambda expressions evaluate to themselves in newLISP.</p>
<p>
The second line uses the keyword <tt>fn</tt>, an alternative syntax first suggested 
by Paul Graham for his Arc language project.
</p>
<p>
A lambda expression is a <em>lambda list</em>, a subtype of <em>list</em>, and its 
arguments can associate from left to right or right to left. When using 
<a href="#append">append</a>, for example, the arguments associate from left to right:
</p>
<pre>
(append (lambda (x)) '((+ x x)))  <span class='arw'>&rarr;</span> (lambda (x) (+ x x))
</pre>		<p>
<a href="#cons">cons</a>, on the other hand, associates the arguments from right to left:
</p>
<pre>
(cons '(x) (lambda (+ x x)))  <span class='arw'>&rarr;</span> (lambda (x) (+ x x))
</pre>		<p>
Note that the <tt>lambda</tt> keyword is not a symbol in a list, but a
designator of a special <em>type</em> of list: the <em>lambda list</em>.
</p>
<pre>
(length (lambda (x) (+ x x)))  <span class='arw'>&rarr;</span> 2
(first (lambda (x) (+ x x)))   <span class='arw'>&rarr;</span> (x)
</pre>		<p>
Lambda expressions can be mapped or applied onto arguments to work as user-defined, anonymous functions:
</p>
<pre>
((lambda (x) (+ x x)) 123)           <span class='arw'>&rarr;</span> 246
(apply (lambda (x) (+ x x)) '(123))  <span class='arw'>&rarr;</span> 246
(map (lambda (x) (+ x x)) '(1 2 3))  <span class='arw'>&rarr;</span> (2 4 6)
</pre>		

<p>A lambda expression can be assigned to a symbol, which in turn can be 
used as a function:</p>

<pre>
(set 'double (lambda (x) (+ x x)))  <span class='arw'>&rarr;</span> (lambda (x) (+ x x))
(double 123)                        <span class='arw'>&rarr;</span> 246
</pre>		

<p>The <a href="#define">define</a> function is just a shorter way of
assigning a lambda expression to a symbol:</p>

<pre>
(define (double x) (+ x x)))  <span class='arw'>&rarr;</span> (lambda (x) (+ x x))
(double 123)                  <span class='arw'>&rarr;</span> 246
</pre>		

<p>In the above example, the expressions inside the lambda list are still 
accessible within <tt>double</tt>:</p>

<pre>
(set 'double (lambda (x) (+ x x)))  <span class='arw'>&rarr;</span> (lambda (x) (+ x x))
(last double)                       <span class='arw'>&rarr;</span> (+ x x)
</pre>		

<p>A lambda list can be manipulated as a first-class object using any function 
that operates on lists:</p>

<pre>
(setf (nth 1 double) '(mul 2 x))     <span class='arw'>&rarr;</span> (lambda (x) (mul 2 x))
double                           <span class='arw'>&rarr;</span> (lambda (x) (mul 2 x))
(double 123)                     <span class='arw'>&rarr;</span> 246
</pre>		

<p>All arguments are optional when applying lambda expressions and default to <tt>nil</tt> 
when not supplied by the user. This makes it possible to write functions with 
multiple parameter signatures.</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="nil_and_true"></a>
<h2>10. <tt>nil</tt>, <tt>true</tt>, <tt>cons</tt>, and <tt>()</tt></h2>

<p>In newLISP, <tt>nil</tt> and <tt>true</tt> represent both the symbols and the 
Boolean values <em>false</em> and <em>true</em>. Depending on their context, 
<tt>nil</tt> and <tt>true</tt> are treated differently. The following examples use 
<tt>nil</tt>, but they can be applied to <tt>true</tt> by simply reversing the logic.
</p>

<p>Evaluation of <tt>nil</tt> yields a Boolean false and is treated as such inside 
flow control  expressions such as <tt>if</tt>, <tt>unless</tt>, <tt>while</tt>, 
<tt>until</tt>, and <tt>not</tt>. Likewise, evaluating <tt>true</tt> yields true.</p>

<pre>
(set 'lst '(nil nil nil))  <span class='arw'>&rarr;</span> (nil nil nil)
(map symbol? lst)          <span class='arw'>&rarr;</span> (true true true)
</pre>		

<p>In the above example, <tt>nil</tt> represents a symbol. In the following example, 
<tt>nil</tt> and <tt>true</tt> are evaluated and represent Boolean values:</p>

<pre>
(if nil "no" "yes")  <span class='arw'>&rarr;</span> "yes"
(if true "yes" "no") <span class='arw'>&rarr;</span> "yes"
(map not lst)        <span class='arw'>&rarr;</span> (true true true)
</pre>		

<p>In newLISP, <tt>nil</tt> and the empty list <tt>()</tt> are not the same as in 
some other Lisps. Only in conditional expressions are they treated as a Boolean 
false, as in <tt>and</tt>, <tt>or</tt>, <tt>if</tt>, <tt>while</tt>, 
<tt>unless</tt>, <tt>until</tt>, and <tt>cond</tt>.</p>

<p>Evaluation of <tt>(cons 'x '())</tt> yields <tt>(x)</tt>, but <tt>(cons 'x nil)</tt> 
yields <tt>(x nil)</tt> because <tt>nil</tt> is treated as a Boolean value when 
evaluated, not as an empty list. The <tt>cons</tt> of two atoms in newLISP 
does not yield a dotted pair, but rather a two-element list. The predicate 
<tt>atom?</tt> is true for <tt>nil</tt>, but false for the empty list. The empty 
list in newLISP is only an empty list and not equal to <tt>nil</tt>.</p>

<p>A list in newLISP is a newLISP cell of type list. It acts like a container for the 
linked list of elements making up the list cell's contents. There is no 
<em>dotted pair</em> in newLISP because the <em>cdr</em> (tail) part of a Lisp 
cell always points to another Lisp cell and never to a basic data type, such as a 
number or a symbol. Only the <em>car</em> (head) part may contain a basic data type. 
Early Lisp implementations used <em>car</em> and <em>cdr</em> for the names 
<em>head</em> and <em>tail</em>.</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="arrays"></a>
<h2>11. Arrays</h2>

<p>newLISP's arrays enable fast element access within large lists. New arrays 
can be constructed and initialized with the contents of an existing list 
using the function <a href="#array">array</a>. Lists can be converted into 
arrays, and vice versa. Most of the same functions used for modifying and 
accessing lists can be applied to arrays, as well. Arrays can hold any type 
of data or combination thereof.</p>

<p>In particular, the following functions can be used for creating, accessing, 
and modifying arrays:</p>

<table  summary="functions using arrays">
<tr align="left"><th>function</th><th>description</th></tr>

<tr>
<td width="16%"><a href="#append">append</a></td>
<td width="80%">appends arrays</td>
</tr>

<tr>
<td><a href="#apply">apply</a></td>
<td>apply a function or operator to a list of arguments.</td>
</tr>

<tr>
<td><a href="#array">array</a></td>
<td>creates and initializes an array with up to 16 dimensions</td>
</tr>

<tr>
<td><a href="#array-list">array-list</a></td>
<td>converts an array into a list</td>
</tr>

<tr>
<td><a href="#arrayp">array?</a></td>
<td>checks if expression is an array</td>
</tr>

<tr>
<td><a href="#corr">corr</a></td>
<td>calculates the <em>product-moment correlation</em> coefficient</td>
</tr>

<tr>
<td><a href="#det">det</a></td>
<td>returns the determinant of a matrix</td>
</tr>

<tr>
<td><a href="#dolist">dolist</a></td>
<td>evaluates once for each element in an array vector</td>
</tr>

<tr>
<td><a href="#first">first</a></td>
<td>returns the first row of an array</td>
</tr>

<tr>
<td><a href="#invert">invert</a></td>
<td>returns the inversion of a matrix</td>
</tr>

<tr>
<td><a href="#last">last</a></td>
<td>returns the last row of an array</td>
</tr>

<tr>
<td><a href="#length">length</a></td>
<td>returns the number of rows in an array or elements in a vector</td>
</tr>

<tr>
<td><a href="#map">map</a></td>
<td>applies a function to vector(s) of arguments
and returns results in a list.</td>
</tr>

<tr>
<td><a href="#mat">mat</a></td>
<td>perform scalar operations on matrices</td>
</tr>

<tr>
<td><a href="#multiply">multiply</a></td>
<td>multiplies two matrices</td>
</tr>

<tr>
<td><a href="#nth">nth</a></td>
<td>returns an element of and array</td>
</tr>

<tr>
<td><a href="#rest">rest</a></td>
<td>returns all but the first row of an array</td>
</tr>

<tr>
<td><a href="#reverse">reverse</a></td>
<td>reverses the elements or rows in an array</td>
</tr>

<tr>
<td><a href="#setf">setf</a></td>
<td>sets contents of an array reference</td>
</tr>

<tr>
<td><a href="#slice">slice</a></td>
<td>returns a slice of an array</td>
</tr>

<tr>
<td><a href="#sort">sort</a></td>
<td>sort the elements in an array</td>
</tr>

<tr>
<td><a href="#stats">stats</a></td>
<td>calculates some basic statistics for a data vector</td>
</tr>

<tr>
<td><a href="#t-test">t-test</a></td>
<td>compares means of data samples using the <em>Student's t</em> statistic</td>
</tr>

<tr>
<td><a href="#transpose">transpose</a></td>
<td>transposes a matrix</td>
</tr>
</table><br/>

<p>
newLISP represents multidimensional arrays with an array of arrays 
(i.e., the elements of the array are themselves arrays).
</p>

<p>When used interactively, newLISP prints and displays arrays as lists, 
with no way of distinguishing between them.</p>

<p>Use the <a href="#source">source</a> or <a href="#save">save</a> 
functions to serialize arrays (or the variables containing them). 
The <a href="#array">array</a> statement is included as part of 
the definition when serializing arrays.</p>

<p>Like lists, negative indices can be used to enumerate the elements 
of an array, starting from the last element.</p>

<p>An out-of-bounds index will cause an error message on an array or list.</p>

<p>Arrays can be non-rectangular, but they are made rectangular 
during serialization when using <a href="#source">source</a> or <a href="#save">save</a>. 
The <a href="#array">array</a> function always constructs arrays in rectangular form.</p>

<p>The matrix functions <a href="#det">det</a>, <a href="#transpose">transpose</a>, 
<a href="#multiply">multiply</a>, and <a href="#invert">invert</a> can be used on 
matrices built with nested lists or arrays built with <a href="#array">array</a>.</p>

<p>For more details, see <a href="#array">array</a>, <a href="#arrayp">array?</a>, 
and <a href="#array-list">array-list</a> in the reference section of this manual.</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="indexing"></a>
<h2>12. Indexing elements of strings, lists, and arrays</h2>

<p>Some functions take array, list, or string elements (characters) 
specified by one or more <em>int-index</em> (integer index). The positive 
indices run <tt>0, 1, &hellip;, N-2, N-1</tt>, where <tt>N</tt> is the 
number of elements in the list. If <em>int-index</em> is negative, the sequence 
is <tt>-N, -N+1, &hellip;, -2, -1</tt>. Adding <tt>N</tt> to the negative 
index of an element yields the positive index. Unless a function does 
otherwise, an index greater than <tt>N-1</tt> or less then -N causes an 
out-of-bounds error in lists and arrays.</p>

<br/>

<a name="implicit_indexing"></a>
<h3>Implicit indexing for <tt>nth</tt></h3>

<p>Implicit indexing can be used instead of <a href="#nth">nth</a> to 
retrieve the elements of a list or array or the characters of a string:</p>

<pre>
(set 'lst '(a b c (d e) (f g)))

(lst 0)    <span class='arw'>&rarr;</span> a      ; same as (nth 0 lst)
(lst 3)    <span class='arw'>&rarr;</span> (d e)
(lst 3 1)  <span class='arw'>&rarr;</span> e      ; same as (nth '(3 1) lst)
(lst -1)   <span class='arw'>&rarr;</span> (f g)

(set 'myarray (array 3 2 (sequence 1 6)))

(myarray 1)     <span class='arw'>&rarr;</span> (3 4)
(myarray 1 0)   <span class='arw'>&rarr;</span> 3
(myarray 0 -1)  <span class='arw'>&rarr;</span> 2

; indexing ASCII strings
("newLISP" 3)   <span class='arw'>&rarr;</span> "L"

; indexing strings in UTF8 enabled versions
 ("我能吞下玻璃而不伤身体。" 3) <span class='arw'>&rarr;</span> "下"
</pre>		

<p>Indices may also be supplied from a list. In this way, implicit 
indexing works together with functions that take or produce index 
vectors, such as <a href="#push">push</a>, <a href="#pop">pop</a>, 
<a href="#ref">ref</a> and <a href="#ref-all">ref-all</a>.</p>

<pre>
(lst '(3 1))                <span class='arw'>&rarr;</span> e
(set 'vec (ref 'e lst))     <span class='arw'>&rarr;</span> (3 1)
(lst vec)                   <span class='arw'>&rarr;</span> e

; an empty index vector yields the original list or array

(lst '())  <span class='arw'>&rarr;</span> (set 'lst '(a b c (d e) (f g)))
</pre>		

<p>Note that implicit indexing is not breaking newLISP
syntax rules but is merely an expansion of existing rules to
other data types in the functor position of an s-expression. 
In original Lisp, the first element in an s-expression list
is applied as a function to the rest elements as arguments. In newLISP, a list 
in the functor position of an s-expression assumes self-indexing functionality 
using the index arguments following it.</p>

<p>Implicit indexing is faster than the explicit forms, but the explicit forms
may be more readable depending on context.</p>

<p>Note that in the UTF-8&ndash;enabled version of newLISP, implicit indexing 
of strings or using the <a href="#nth">nth</a> function work on character rather 
than single-byte boundaries.</p>

<br/>

<a name="implicit_default"></a>
<h3>Implicit indexing and the default functor</h3>

<p>The <em>default functor</em> is a functor inside a context with the same 
name as the context itself. See <a href="#default_function">The context 
default function</a> chapter. A default functor can be used together with 
implicit indexing to serve as a mechanism for referencing lists:</p>

<pre>
(set 'MyList:MyList '(a b c d e f g))

(MyList 0)   <span class='arw'>&rarr;</span> a
(MyList 3)   <span class='arw'>&rarr;</span> d
(MyList -1)  <span class='arw'>&rarr;</span> g

(3 2 MyList) <span class='arw'>&rarr;</span> (d e)
(-3 MyList)  <span class='arw'>&rarr;</span> (e f g)

(set 'aList MyList)

(aList 3)  <span class='arw'>&rarr;</span> d
</pre>		

<p>In this example, <tt>aList</tt> references <tt>MyList:MyList</tt>, 
not a copy of it. For more information about contexts, see 
<a href="#context_vars">Variables holding contexts</a>.</p>

<p>The indexed default functor can also be used with <a href="#setf">setf</a> as shown 
in the following example:</p>

<pre>
(set 'MyList:MyList '(a b c d e f g))

(setf (MyList 3) 999)   <span class='arw'>&rarr;</span> 999
(MyList 3)              <span class='arw'>&rarr;</span> 999

MyList:MyList           <span class='arw'>&rarr;</span> (a b c 999 e f g)
</pre>

<br/>

<a name="implicit_rest_slice"></a>
<h3>Implicit indexing for <tt>rest</tt> and <tt>slice</tt> </h3>

<p> Implicit forms of <a href="#rest">rest</a> and <a href="#slice">slice</a> 
can be created by prepending a list with one or two numbers for offset and length.
If the length is negative it counts from the end of the list or string:</p>

<pre>
(set 'lst '(a b c d e f g))
; or as array
(set 'lst (array 7 '(a b c d e f g)))

(1 lst)      <span class='arw'>&rarr;</span> (b c d e f g)
(2 lst)      <span class='arw'>&rarr;</span> (c d e f g)
(2 3 lst)    <span class='arw'>&rarr;</span> (c d e)
(-3 2 lst)   <span class='arw'>&rarr;</span> (e f)
(2 -2 lst)   <span class='arw'>&rarr;</span> (c d e)

; resting and slicing is always on 8-bit char borders
; even on UTF8 enabled versions

(set 'str "abcdefg")

(1 str)      <span class='arw'>&rarr;</span> "bcdefg"
(2 str)      <span class='arw'>&rarr;</span> "cdefg"
(2 3 str)    <span class='arw'>&rarr;</span> "cde"
(-3 2 str)   <span class='arw'>&rarr;</span> "ef"
(2 -2 str)   <span class='arw'>&rarr;</span> "cde"

</pre>	

<p>The functions <a href="#rest">rest</a>, <a href="#first">first</a>
and <a href="#last">last</a> work on multi-byte character boundaries
in UTF-8 enabled versions of newLISP. But the implicit indexing forms for
slicing and resting will always work on single-byte boundaries and can be used for 
binary content. Offset and length results from the regular expression functions 
<a href="#find">find</a> and <a href="#regex">regex</a> are also in single-byte
counts and can be further processed with <a href="#slice">slice</a> or it's
implicit form.</p>

<br/>

<a name="implicit_modify"></a>
<h3>Modify references in lists, arrays and strings</h3>

<p>Parts in lists, arrays and strings referenced by indices can be modified using
<a href="#setf">setf</a>:</p>

<pre>
; lists

(set 'lst '(a b c d (e f g)))

(lst 1) <span class='arw'>&rarr;</span> b

(setf (lst 1) 'z) <span class='arw'>&rarr;</span> z

lst <span class='arw'>&rarr;</span> (a z c d (e f g))

(setf (lst -1) '(E F G)) <span class='arw'>&rarr;</span> (E F G)

lst <span class='arw'>&rarr;</span> (a z c d (E F G))

; arrays

(set 'myarray (array 2 3 (sequence 1 6))) <span class='arw'>&rarr;</span> ((1 2 3) (4 5 6))

(setf (myarray 1 2) 66) <span class='arw'>&rarr;</span> 66

myarray <span class='arw'>&rarr;</span> ((1 2 3) (4 5 66))

; strings

(set 's "NewLISP")

(setf (s 0) "n") <span class='arw'>&rarr;</span> "n"

s <span class='arw'>&rarr;</span> "newLISP"
</pre>


<p>Note that only full elements or nested lists or arrays can be changed this way.
Slices or rest parts of lists or arrays as used in implicit resting or slicing cannot
be substituted at once using <a href="#setf">setf</a>, but would have to be substituted
element by element. In strings only one character can be replaced at a time, but
that character can be replaced by a multi-character string.</p> 

<br/>

<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>


<a name="destructive"></a>
<h2>13. Destructive versus nondestructive functions</h2>

<p>Most of the primitives in newLISP are nondestructive (no <em>side effects</em>) 
and leave existing objects untouched, although they may create new ones. There 
are a few destructive functions, however, that <em>do</em> change the contents of a 
variable, list, array, or string:</p>

<table  width="98%" summary="destructive functions">
<tr align="left"><th>function</th><th>description</th></tr>

<tr>
<td><a href="#inci">++</a></td>
<td>increments numbers in integer mode</td>
</tr>

<tr>
<td><a href="#deci">--</a></td>
<td>decrements numbers in integer mode</td>
</tr>

<tr>
<td><a href="#bind">bind</a></td>
<td>binds variable associations in a list</td>
</tr>

<tr>
<td><a href="#constant">constant</a></td>
<td>sets the contents of a variable and protects it</td>
</tr>

<tr>
<td><a href="#extend">extend</a></td>
<td>extends a list or string</td>
</tr>

<tr>
<td><a href="#dec">dec</a></td>
<td>decrements a number referenced by a variable, list or array</td>
</tr>

<tr>
<td><a href="#define">define</a></td>
<td>sets the contents of a variable</td>
</tr>

<tr>
<td><a href="#define-macro">define-macro</a></td>
<td>sets the contents of a variable</td>
</tr>

<tr>
<td><a href="#inc">inc</a></td>
<td>increments a number referenced by a variable, list or array</td>
</tr>

<tr>
<td><a href="#let">let</a></td>
<td>declares and initializes local variables</td>
</tr>

<tr>
<td><a href="#letn">letn</a></td>
<td>initializes local variables incrementally, like nested lets</td>
</tr>

<tr>
<td><a href="#letex">letex</a></td>
<td>expands local variables into an expression, then evaluates</td>
</tr>

<tr>
<td><a href="#net-receive">net-receive</a></td>
<td>reads into a buffer variable</td>
</tr>

<tr>
<td><a href="#pop">pop</a></td>
<td>pops an element from a list or string</td>
</tr>

<tr>
<td><a href="#pop-assoc">pop-assoc</a></td>
<td>removes an association from an association list</td>
</tr>

<tr>
<td><a href="#push">push</a></td>
<td>pushes a new element onto a list or string</td>
</tr>

<tr>
<td><a href="#read">read</a></td>
<td>reads into a buffer variable</td>
</tr>

<tr>
<td><a href="#receive">receive</a></td>
<td>receives a message from a parent or child process</td>
</tr>

<tr>
<td><a href="#replace">replace</a></td>
<td>replaces elements in a list or string</td>
</tr>

<tr>
<td><a href="#reverse">reverse</a></td>
<td>reverses a list or string</td>
</tr>

<tr>
<td><a href="#rotate">rotate</a></td>
<td>rotates the elements of a list or characters of a string</td>
</tr>

<tr>
<td><a href="#set">set</a></td>
<td>sets the contents of a variable</td>
</tr>

<tr>
<td><a href="#setf">setf setq</a></td>
<td>sets the contents of a variable, list, array or string</td>
</tr>

<tr>
<td><a href="#set-ref">set-ref</a></td>
<td>searches for an element in a nested list and replaces it</td>
</tr>

<tr>
<td><a href="#set-ref-all">set-ref-all</a></td>
<td>searches for an element in a nested list and replaces all instances</td>
</tr>

<tr>
<td><a href="#sort">sort</a></td>
<td>sorts the elements of a list or array</td>
</tr>

<tr>
<td><a href="#swap">swap</a></td>
<td>swaps two elements inside a list or string</td>
</tr>

<tr>
<td><a href="#write">write</a></td>
<td>write a string to a file or string buffer</td>
</tr>

</table><br/>

<br/>

<a name="make_nondestructive"></a>
<h3>Make a destructive function non-destructive</h3>

<p>Some destructive functions can be made non-destructive by wrapping the target 
object into the <a href="#copy">copy</a> function.</p>

<pre>
(set 'aList '(a b c d e f))

(replace 'c (copy aList)) <span class='arw'>&rarr;</span> (a b d e f)

aList <span class='arw'>&rarr;</span> (a b c d e f)
</pre>

<p>The list in <tt>aList</tt> is left unchanged.</p>

<br/>

<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="return"></a>
<h2>14. Early return from functions, loops, and blocks</h2>

<p>What follows are methods of interrupting the control flow inside both 
loops and the <a href="#begin">begin</a> expression.</p>

<p>The looping functions <a href="#dolist">dolist</a> and <a href="#dotimes">
dotimes</a> can take optional conditional expressions to leave the loop
early. <a href="#catch">catch</a> and <a href="#throw">throw</a> are a more
general form to break out of a loop body and are also applicable to other
forms or statement blocks.</p>

<br/>

<a name="flow_catch_throw"></a>
<h3>Using <tt>catch</tt> and <tt>throw</tt></h3>

<p>Because newLISP is a functional language, it uses no <tt>break</tt> or 
<tt>return</tt> statements to exit functions or iterations. Instead, a 
block or function can be exited at any point using the functions 
<a href="#catch">catch</a> and <a href="#throw">throw</a>:</p>

<pre>
(define (foo x)
    ...
    (if condition (throw 123))
    ...
    456
)
									 
;; if condition is true

(catch (foo p))  <span class='arw'>&rarr;</span> 123
									 
;; if condition is not true
									 
(catch (foo p))  <span class='arw'>&rarr;</span> 456
</pre>		

<p>Breaking out of loops works in a similar way:</p>

<pre>
(catch
    (dotimes (i N)
        (if (= (foo i) 100) (throw i))))

<span class='arw'>&rarr;</span> value of i when foo(i) equals 100
</pre>		

<p>The example shows how an iteration can be exited before executing <tt>N</tt> times.</p>

<p>Multiple points of return can be coded using <a href="#throw">throw</a>:</p>

<pre>
(catch (begin
    (foo1)
    (foo2)
    (if condition-A (throw 'x))
    (foo3)
    (if condition-B (throw 'y))
    (foo4)
    (foo5)))
</pre>		

<p>If <tt>condition-A</tt> is true, <tt>x</tt> will be returned from 
the <tt>catch</tt> expression; if <tt>condition-B</tt> is true, the 
value returned is <tt>y</tt>. Otherwise, the result from <tt>foo5</tt> 
will be used as the return value.</p>

<p>As an alternative to <a href="#catch">catch</a>, the <a href="#error-event">error-event</a> 
function can be used to catch errors caused by faulty code or user-initiated exceptions.</p>

<p>The <a href="#throw-error">throw-error</a> function may be used
to throw user-defined errors.</p>

<br/>

<a name="flow_and_or"></a>
<h3>Using <tt>and</tt> and <tt>or</tt></h3>

<p>Using the logical functions <a href="#and">and</a> and 
<a href="#or">or</a>, blocks of statements can be built 
that are exited depending on the Boolean result of the enclosed functions:</p>

<pre>
(and
    (func-a)
    (func-b)
    (func-c)
    (func-d))
</pre>		

<p>The <a href="#and">and</a> expression will return as soon as one of the 
block's functions returns <tt>nil</tt> or an <tt>()</tt> (empty list). 
If none of the preceding functions causes an exit from the block, the 
result of the last function is returned.</p>

<p><a href="#or">or</a> can be used in a similar fashion:</p>

<pre>
(or
    (func-a)
    (func-b)
    (func-c)
    (func-d))
</pre>		
<p>
The result of the <a href="#or">or</a> expression will be the first function 
that returns a value which is <em>not</em> <tt>nil</tt> or <tt>()</tt>.
</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="scoping"></a>
<h2>15. Dynamic and lexical scoping</h2>

<p>newLISP uses dynamic scoping <em>inside</em> contexts. A context is a lexically 
closed namespace. In this way, parts of a newLISP program can live in different 
namespaces taking advantage of <em>lexical scoping</em>.</p>

<p>
When the parameter symbols of a lambda expression are bound to its arguments, 
the old bindings are pushed onto a stack. newLISP automatically restores the 
original variable bindings when leaving the lambda function. </p>
<p>
The following example illustrates the <em>dynamic scoping</em> mechanism. 
The text in bold is the output from newLISP:</p>

<pre>
&gt; (set 'x 1)
<b>1</b>
&gt; (define (f) x)
<b>(lambda () x)</b>
&gt; (f)
<b>1</b>
&gt; (define (g x) (f))
<b>(lambda (x) (f))</b>
&gt; (g 0)
<b>0</b>
&gt; (f)
<b>1</b> 
&gt; _
</pre>	

<p>The variable <tt>x</tt> is first set to <tt>1</tt>. But when <tt>(g 0)</tt> 
is called, <tt>x</tt> is bound to <tt>0</tt> and <tt>x</tt> is reported 
by <tt>(f)</tt> as <tt>0</tt> during execution of <tt>(g 0)</tt>. After 
execution of <tt>(g 0)</tt>, the call to <tt>(f)</tt> will report <tt>x</tt> as <tt>1</tt> again.</p>

<p>This is different from the <em>lexical scoping</em> mechanisms found in 
languages like C or Java, where the binding of local parameters occurs inside 
the function only. In lexically scoped languages like C, <tt>(f)</tt> would 
always print the global bindings of the symbol <tt>x</tt> with <tt>1</tt>.
</p>

<p>Be aware that passing quoted symbols to a user-defined function causes a 
name clash if the same variable name is used as a function parameter:</p>

<pre>
(define (inc-symbol x y) (inc (eval x) y))
(set 'y 200)
(inc-symbol 'y 123)  <span class='arw'>&rarr;</span> 246
y                    <span class='arw'>&rarr;</span> 200  ; y is still 200
</pre>		
<p>
Because the global <tt>y</tt> shares the same symbol as the function's second parameter, 
<tt>inc-symbol</tt> returns 246 (123 + 123), leaving the global <tt>y</tt> unaffected. 
Dynamic scoping's <em>variable capture</em> can be a disadvantage when passing symbol 
references to user-defined functions. newLISP offers several methods to avoid variable
capture.</p>

<ul>
<li>The function <a href="#args">args</a> can be used when passing symbols.</li>
<li>One or more user-defined functions can be placed in their own namespace called 
a <a href="#contexts">context</a>. A symbol name clash cannot occur when accessing 
symbols and calling functions from <em>outside</em> of the defining context.</li>
</ul>

<p>
Contexts should be used to group related functions when creating interfaces 
or function libraries. This surrounds the functions with a lexical "fence", 
thus avoiding variable name clashes with the calling functions.
</p>
<p>
newLISP uses contexts for different forms of lexical scoping. See the 
chapters <a href="#contexts">Contexts</a> and 
<a href="#default_function">default functors</a> for more information.</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="contexts"></a>
<h2>16. Contexts</h2>

<p>In newLISP, symbols can be separated into namespaces called <em>contexts</em>. 
Each context has a private symbol table separate from all other contexts. Symbols 
known in one context are unknown in others, so the same name may be used 
in different contexts without conflict.</p>

<p>Contexts are used to build modules of isolated variable and function definitions. 
They also can be used to build dictionaries fo key values pairs. Contexts can be 
copied and dynamically assigned to variables or passed as arguments by reference.
Because contexts in newLISP have lexically separated namespaces, they allow programming 
with <em>lexical scoping</em> and software object styles of programming.</p>

<p>Contexts are identified by symbols that are part of the root or <tt>MAIN</tt> 
context. Although context symbols are uppercased in this chapter, lowercase symbols 
may also be used.</p>

<p>In addition to context names, <tt>MAIN</tt> contains the symbols for built-in 
functions and special symbols such as <tt>true</tt> and <tt>nil</tt>. The <tt>MAIN</tt> 
context is created automatically each time newLISP is run. To see all the symbols 
in MAIN, enter the following expression after starting newLISP:</p>

<pre>
(symbols)
</pre>

<p>To see all symbols in <tt>MAIN</tt> pointing to contexts:</p>

<pre>
(filter context? (map eval (symbols)))
</pre>

<p>To seel all context symbols in <tt>MAIN</tt> when <tt>MAIN</tt> is not the
current context:</p>

<pre>
(filter context? (map eval (symbols MAIN)))
</pre>

<br/>

<a name="context_rules"></a>
<h3>Symbol creation in contexts</h3>

<p>The following rules should simplify the process of understanding contexts by 
identifying to which context the created symbols are being assigned.</p>

<ol>
<li>
<p>newLISP first parses and translates each expression starting at the top
level. All symbols are created during this phase. After the expression is 
translated, it gets evaluated.</p>
</li>

<li>
<p>A symbol is created when newLISP first <em>sees</em> it, while calling 
the <a href="#load">load</a>, <a href="#sym">sym</a>, 
or <a href="#eval-string">eval-string</a> functions. When newLISP reads 
a source file, symbols are created <em>before</em> evaluation occurs. The
<a href="#reader-event">reader-event</a> function can be used to inspect
the expression after reading and translating but before evaluation. The
<a href="#read-expr">read-expr</a> function can be used to read and translate
newLISP source without evaluation.</p>
</li>

<li>
<p>When an unknown symbol is encountered during code translation, 
a search for its definition begins inside the current context. 
Failing that, the search continues inside <tt>MAIN</tt> for a 
built-in function, context, or global symbol. If no definition 
is found, the symbol is created locally inside the current context.</p>
</li>

<li>
<p>Once a symbol is created and assigned to a specific context, 
it will belong to that context permanently or until it is deleted
using the <a href="#delete">delete</a> function.</p>
</li>

<li>
<p>When a user-defined function is evaluated, the context is switched 
to the name-space which owns that symbol.</p>
</li>

<li><p>A context switch only influences symbol creation during 
<a href="#load">load</a>, <a href="#sym">sym</a>,
or <a href="#eval-string">eval-string</a>. 
<a href="#load">load</a> by default loads into MAIN except
 when context switches occur on the top level of the file loaded. 
For better style, the context should always be specified when the functions 
<a href="#sym">sym</a> and <a href="#eval-string">eval-string</a> 
are used. A context switch should normally only be made on the top level of 
a program, never inside a function.</p>
</li>
</ol>

<br/>

<a name="creating_contexts"></a>
<h3>Creating contexts</h3>

<p>Contexts can be created either by using the <a href="#context">context</a>
function or via implicit creation. The first method is used when writing larger 
portions of code belonging to the same context:</p>

<pre>
(context 'FOO)

(set 'var 123)

(define (func x y z)
    ... )

(context MAIN)
</pre>		

<p>If the context does not exist yet, the context symbol must be quoted.
If the symbol is not quoted, newLISP assumes the symbol is a variable
holding the symbol of the context to create. Because a context evaluates
to itself, already existing contexts like MAIN do not require quoting.</p>

<p>When newLISP reads the above code, it will read, then evaluate the first
statement: <tt>(context 'FOO)</tt>. This causes newLISP to switch the namespace
to FOO and the following symbols <tt>var</tt>, <tt>x</tt>, <tt>y</tt> and <tt>z</tt>
will all be created in the FOO context when reading and evaluating the remaining
expressions.</p>

<p>A context symbol is protected against change. Once a symbol refers to a 
context, it cannot be used for any other purpose, except when using 
<a href="#delete">delete</a>.</p>

<p>To refer to <tt>var</tt> or <tt>func</tt> from anywhere else outside the 
FOO namespace, they need to be prefixed with the context name:</p>

<pre>
FOO:var <span class='arw'>&rarr;</span> 123

(FOO:func p q r)
</pre>	

<p>Note, that in the above example only <tt>func</tt> belongs to the <tt>FOO</tt>
name space the symbols <tt>p q r</tt> all are part of the current context
from which the <tt>FOO:func</tt> call is made.</p>	

<p>The <a href="#symbols">symbols</a> function is used to show all symbols
belonging to a context:</p>

<pre>
(symbols FOO) <span class='arw'>&rarr;</span> (FOO:func FOO:var FOO:x FOO:y FOO:z)

; or from inside the context symbols are shown without context prefix
(context FOO) <span class='arw'>&rarr;</span> (func x y z)
(sumbols)
</pre>		

<br/>

<b>Implicitly creating contexts</b>

<p>A context is implicitly created when referring to one that does not yet exist. 
Unlike the <tt>context</tt> function, the context is not switched. The following 
statements are all executed inside the <tt>MAIN</tt> context:</p>

<pre>
&gt; (set 'ACTX:var "hello")
<b>"hello"</b>
&gt; ACTX:var
<b>"hello"</b>
&gt; _
</pre>		

<p>Note that only the symbols prefixed with their context name will be part
of the context:</p>

<pre>
(define (ACTX:foo x y) 
    (+ x y))
</pre>		

<p>When above code is loaded in MAIN only <tt>foo</tt> will be part of 
<tt>ACTX</tt>. The symbols <tt>x</tt> and <tt>y</tt> will still be part
of <tt>MAIN</tt>. To make all locals of <tt>ACTX:foo</tt> members of
the <tt>ACTX</tt> context, they would either have to be prefixed with
<tt>ACTX</tt>, or the whole funtion must be preceded by a context
switch satement at the top level:</p>

<pre>
(context 'ACTX)
(define (foo x y)
    (+ x y)
(context MAIN

;; above same as

(define (ACTX:foo ACTX:x ACTX:y)
    (+ ACTX:x ACTX:y))
</pre>

<br/>

<b>Loading module files</b>
<br/>

<p>When loading source files on the command-line with <a href="#load">load</a>, 
or when executing the functions <a href="#eval-string">eval-string</a> or 
<a href="#sym">sym</a>, the <tt>context</tt> function tells the newLISP source
code reader in which namespace to put all of the symbols and definitions:</p>

<pre>
;;; file MY_PROG.LSP
;;
;; everything from here on goes into GRAPH
(context 'GRAPH)
				 
(define (draw-triangle x y z)
    (&hellip;))

(define (draw-circle)
    (&hellip;))
									 
;; show the runtime context, which is GRAPH
(define (foo)
    (context))
									 
;; switch back to MAIN
(context 'MAIN)
				 
;; end of file					
</pre>		

<p>The <tt>draw-triangle</tt> and <tt>draw-circle</tt> functions &mdash; along 
with their <tt>x</tt>, <tt>y</tt>, and <tt>z</tt> parameters &mdash; are now 
part of the <tt>GRAPH</tt> context. These symbols are known only to <tt>GRAPH</tt>. 
To call these functions from another context, prefix them with <tt>GRAPH:</tt></p>

<pre>
(GRAPH:draw-triangle 1 2 3)
(GRAPH:foo)  <span class='arw'>&rarr;</span> GRAPH										
</pre>		

<p>The last statement shows how the runtime context has changed to 
<tt>GRAPH</tt> (function <tt>foo</tt>'s context).</p>

<p>A symbol's name and context are used when comparing symbols from different 
contexts. The <a href="#term">term</a> function can be used to extract the term 
part from a fully qualified symbol.</p>

<pre>
;; same symbol name, but in different context
(= 'A:val 'B:val)                    <span class='arw'>&rarr;</span> nil
(= (term 'A:val) (term 'B:val))      <span class='arw'>&rarr;</span> true
(= (prefix 'A:val) (prefix 'B:val))  <span class='arw'>&rarr;</span> nil
</pre>		

<p>Note: The symbols in above example are quoted with a <tt>'</tt> (single quote) 
because we are interested in the symbol itself, not in the contents of the symbol.</p>

<br/>

<a name="scope_global"></a>
<h3>Global scope</h3>

<p>By default, only built-in functions and symbols like <tt>nil</tt> and
<tt>true</tt> are visible inside contexts other than <tt>MAIN</tt>. To make a symbol 
visible to every context, use the <a href="#global">global</a> function:</p>

<pre>
(set 'aVar 123) <span class='arw'>&rarr;</span> 123
(global 'aVar)  <span class='arw'>&rarr;</span> aVar

(context 'FOO)  <span class='arw'>&rarr;</span> FOO

aVar            <span class='arw'>&rarr;</span> 123
</pre>		

<p>Without the <tt>global</tt> statement, the second <tt>aVar</tt> would have 
returned <tt>nil</tt> instead of <tt>123</tt>. If <tt>FOO</tt> had a previously 
defined symbol (<tt>aVar</tt> in this example) <em>that</em> symbol's value 
&mdash; and not the global's &mdash; would be returned instead. Note that only 
symbols from the <tt>MAIN</tt> context can be made global.</p>

<p>Once it is made visible to contexts through the <a href="#global">global</a> function, 
a symbol cannot be hidden from them again.</p>

<br/>

<a name="protection"></a>
<h3>Symbol protection</h3>

<p>By using the <a href="#constant">constant</a> function, symbols can be both set 
and protected from change at the same time:</p>

<pre>
&gt; (constant 'aVar 123)  <span class='arw'>&rarr;</span> 123
&gt; (set 'aVar 999)
<span class='err'>ERR: symbol is protected in function set : aVar</span>
&gt;_
</pre>		<p>
	A symbol needing to be both a constant and a global can be defined simultaneously:
</p>
<pre>
(constant (global 'aVar) 123)
</pre>		

<p>In the current context, symbols protected by <tt>constant</tt> can be overwritten 
by using the <tt>constant</tt> function again. This protects the symbols from 
being overwritten by code in other contexts.</p>

<br/>

<a name="overwrite"></a>
<h3>Overwriting global symbols and built-ins</h3>

<p>Global and built-in function symbols can be overwritten inside a
context by prefixing them with their <em>own</em> context symbol:</p>

<pre>
(context 'Account)

(define (Account:new &hellip;)
    (&hellip;))

(context 'MAIN)
</pre>		

<p>In this example, the built-in function <a href="#new">new</a> is overwritten by 
<tt>Account:new</tt>, a different function that is private to the <tt>Account</tt> context.
</p>

<br/>

<a name="context_vars"></a>
<h3>Variables containing contexts</h3>

<p>Variables can be used to refer to contexts:</p>

<pre>
(set 'FOO:x 123)

(set 'ctx FOO)    <span class='arw'>&rarr;</span> FOO

ctx:x             <span class='arw'>&rarr;</span> 123

(set 'ctx:x 999)  <span class='arw'>&rarr;</span> 999

FOO:x             <span class='arw'>&rarr;</span> 999
</pre>		

<p>
Context variables are useful when writing functions, which need to refer to
different contexts during runtime or use contexts which do not exist during
definition:</p>

<pre>
(define (update ctx val)
    (set 'ctx:sum val)
    (ctx:func 999)
)

(context 'FOO)
(define (func x)
    (println "=&gt;" x))
(context MAIN)
</pre>		

<p>The following shows a terminal session using above definitions. The program
output is shown in bold-face:</p>

<pre>
<b>&gt;</b> (update FOO 123)
<b>=&gt; 999</b>

<b>&gt;</b> FOO:sum
<b>123</b>
<b>&gt;</b>
</pre>		

<p>The same one function <tt>update</tt> can display different behavior depending
on the context passed as first parameter. </p>

<br/>

<a name="sequence_creating"></a>
<h3>Sequence of creating or loading contexts</h3>

<p>The sequence in which contexts are created or loaded can lead to unexpected 
results. Enter the following code into a file called <tt>demo</tt>:</p>

<pre>
;; demo - file for loading contexts
(context 'FOO)
(set 'ABC 123)
(context MAIN)

(context 'ABC)
(set 'FOO 456)
(context 'MAIN)
</pre>		

<p>Now load the file into the newlisp shell:</p>

<pre>
&gt; (load "demo")
<span class='err'>ERR: symbol is protected in function set : FOO</span>
&gt; _
</pre>		

<p>Loading the file causes an error message for <tt>FOO</tt>, but not 
for <tt>ABC</tt>. When the first context <tt>FOO</tt> is loaded, the 
context <tt>ABC</tt> does not exist yet, so a local variable <tt>FOO:ABC</tt> 
gets created. When <tt>ABC</tt> loads, <tt>FOO</tt> already exists as a global 
protected symbol and will be correctly flagged as protected.</p>

<p><tt>FOO</tt> could still be used as a local variable in the <tt>ABC</tt> 
context by explicitly prefixing it, as in <tt>ABC:FOO</tt>.</p>

<br/>

<a name="context_modules"></a>
<h3>Contexts as programming modules</h3>

<p>Contexts in newLISP are mainly used for partitioning source into 
modules. Because each module lives in a different namespace, modules
are lexically separated and the names of symbols cannot clash with
identical names in other modules.</p>

<p>The <a href="http://newlisp.org/code/modules/">modules</a>, which are
part of the newLISP distribution, are a good example of how to put related
 functions into a module file, and how to document modules using
 the <a href="http://newlisp.org/newLISPdoc.html">newLISPdoc</a> utility.</p> 

<p>For best programming practice, a file should only contain one module and 
the filename should be similar if not identical to the context name used:</p>

<pre>
;; file db.lsp, commonly used database functions

(context 'db)

;; Variables used throughout this namespace

(define db:handle)
(define db:host "http://localhost")

;; Constants

(constant 'Max_N 1000000)
(constant 'Path "/usr/data/")

;; Functions

(define (db:open ... )
    ... )

(define (db:close ... )
    ... )

(define (db:update ... )
    ... )
</pre>


<p>The example shows a good practice of predefining variables, which are global
inside the namespace, and defining as constants the variables that will not change.</p>

<p>If a file must contain more than one context, then the end of the context
should be marked with a switch back to <tt>MAIN</tt>:</p>

<pre>
;; Multi context file multi.lsp

(context 'A-ctx)
...
(context MAIN)

(context 'B-ctx)
...
(context MAIN)

(context 'C-ctx)
...
(context MAIN)
</pre>

<p>In any case <a href="#load">load</a> will always switch back to the context
from where it was called.</p>

<br/>

<a name="context_data"></a>
<h3>Contexts as data containers</h3>

<p>Contexts are frequently uses as data containers, e.g. for configuration data:</p>

<pre>
;; Config.lsp - configuration setup

(context 'Config)

(set 'user-name "admin")
(set 'password "secret")
(set 'db-name "/usr/data/db.lsp")
...

;; eof
</pre>

<p>Loading the <tt>Config</tt> namespace will now load a whole variable set into
memory at once:</p>

<pre>
(load "Config.lsp")

(set 'file (open Config:db-name "read"))
...
...
</pre>

<p>In a similar fashion a whole data set can be saved:</p>

<pre>
(save "Config.lsp" 'Config)
</pre>

<p>Read more about this in the section <a href="#serializing">Serializing contexts</a>.</p>

<br/>

<a name="loading_contexts"></a>
<h3>Loading and declaring contexts</h3>

<p>Module files are loaded using the <a href="#load">load</a> function.
If a programming project contains numerous modules that refer
to each other, they can be pre-declared to avoid problems due to context forward 
references that can occur before the loading of that context.</p>

<pre>
;; pre-declaring contexts, finish with Main to return
(map context '(Utilities Config Acquisition Analysis SysLog MAIN))

;; loading context module files
(load "Utilities.lsp" "Acquisition.lsp")
(load "http://192.168.1.34/Config.lsp") ; load module from remote location
(load "Analysis.lsp" "SysLog.lsp")

(define (run)
    ... )

(run)

;; end of file </pre>

<p>When pre-declaring and loading modules as shown in the example, the sequence
of declaration or loading can be neglected. All forward references to variables
and definitions in modules not loaded yet will be translated correctly. Wrong
usage of a context symbol will result in an error message before that context
is loaded.</p>

<p>Modules not starting with a context switch are always loaded into <tt>MAIN</tt>
except when the <a href="#load">load</a> statement specifies a target context
as the last parameter. The <a href="#load">load</a> function can take <tt>URL</tt>s
to load modules from remote locations, via <tt>HTTP</tt>.</p>

<p>The current context after the <a href="#load">load</a> statement will always be 
the same as before the <a href="#load">load</a>.</p>

<br/>

<a name="serializing"></a>
<h3>Serializing contexts</h3>

<p>Serialization makes a software object <em>persistent</em>
by converting it into a character stream,
which is then saved to a file or string in memory.
In newLISP, anything referenced by a symbol can be serialized to a file
by using the <a href="#save">save</a> function.
Like other symbols, contexts are saved just by using their names:
</p>

<pre>
(save "mycontext.lsp" 'MyCtx)              ; save MyCtx to mycontext.lsp

(load "mycontext.lsp")                     ; loads MyCtx into memory

(save "mycontexts.lsp" 'Ctx1 'Ctx2 'Ctx3)  ; save multiple contexts at once
</pre>		

<p>
For details, see the functions <a href="#save">save</a> (mentioned above)
and <a href="#source">source</a> (for serializing to a newLISP string).
</p>

<br/>		
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>

<br/>

<a name="default_function"></a>
<h2>17. The context default functor</h2>

<p>A <em>default functor</em> or <em>default function</em>
is a symbol or user-defined function or macro
with the same name as its namespace. When the context is used
as the name of a function or in the functor position of an s-expression,
newLISP executes the default function.</p>

<pre>
;; the default function

(define (Foo:Foo a b c) (+ a b c))

(Foo 1 2 3)  <span class='arw'>&rarr;</span> 6
</pre>

<p>If a default function is called from a context other than <tt>MAIN</tt>,
the context must already exist or be declared with a <em>forward declaration</em>,
which creates the context and the function symbol:</p>

<pre>
;; forward declaration of a default function
(define Fubar:Fubar)    

(context 'Foo)
(define (Foo:Foo a b c)
    &hellip;
    (Fubar a b)         ; forward reference
    (&hellip;))         ; to default function

(context MAIN)

;; definition of previously declared default function

(context 'Fubar)
(define (Fubar:Fubar x y)
    (&hellip;))

(context MAIN)
</pre>		

<p>Default functions work like global functions,
but they are lexically separate from the context in which they are called.</p>

<p>Like a lambda or lambda-macro function, default functions can be used 
with <a href="#map">map</a> or <a href="#apply">apply</a>.
</p>

<br/>

<a name="func_memory"></a>
<h3>Functions with memory</h3>

<p>A default function  can update the lexically isolated static variables
contained inside its namespace:</p>

<pre>
;; a function with memory

(define (Gen:Gen x)
    (if Gen:acc
        (inc Gen:acc x)
        (setq Gen:acc x)))

(Gen 1)  <span class='arw'>&rarr;</span> 1
(Gen 1)  <span class='arw'>&rarr;</span> 2
(Gen 2)  <span class='arw'>&rarr;</span> 4
(Gen 3)  <span class='arw'>&rarr;</span> 7

gen:acc  <span class='arw'>&rarr;</span> 7
</pre>		

<p>The first time the <tt>Gen</tt> function is called,
its accumulator is set to the value of the argument.
Each successive call increments <tt>Gen</tt>'s accumulator
by the argument's value.</p>

<p>The definition of <tt>Gen:Gen</tt> shows, how a function is put in its own namespace 
without using the surrounding <tt>(context 'Gen)</tt> and <tt>(context MAIN)</tt> 
statements. In that case only symbols qualified by the namespace prefix will 
end up in the <tt>Gen</tt> context. In the above example the variable 
<tt>x</tt> is still part of <tt>MAIN</tt>.</p>

<br/>

<a name="hash"></a>
<h3>Hash functions and dictionaries</h3>

<p>There are several functions that can be used to place symbols into namespace contexts. 
When using dictionaries as simple hash-like collections of variable &rarr; value pairs, use the 
uninitialized <a href="#default_function">default functor</a>:</p>

<pre>
(define Myhash:Myhash) ; create namespace and default functor

; or as a safer alternative

(new Tree 'Myhash) ; create from built-in template
</pre>		

<p>Either method can be used to make the  <tt>MyHash</tt> dictionary space and default
functor. The second method is safer, as it will protect the default functor 
<tt>MyHash:MyHash</tt> from change. The <em>default functor</em> in a namespace must 
contain <tt>nil</tt> to be used as a dictionary. The string used for the symbol name
is limited to 1022 characters and internally an underscore is prepended to the symbol
name used in the context. Creating key-value pairs and retrieving 
a value is easy:</p>

<pre>
(Myhash "var" 123) ; create and set variable/value pair

(Myhash "var")  <span class='arw'>&rarr;</span> 123 ; retrieve value

; keys can be integers and will be converted to strings internally

(Myhash 456 "hello")

(Myhash 456)    <span class='arw'>&rarr;</span> "hello"

; internally an underscore is prepended to the symbol name

(symbols Myhash)  <span class='arw'>&rarr;</span> (Myhash:Myhash Myhash:_456 Myhash:_var)
</pre>		

<p>Symbol variables created this way can contain spaces or other characters
normally not allowed in newLISP symbol names:</p>

<pre>
(define Foo:Foo)
; or to protect the default functor from change
; (new Tree 'Foo)

(Foo "John Doe" 123)         <span class='arw'>&rarr;</span> 123
(Foo "#1234" "hello world")  <span class='arw'>&rarr;</span> "hello world"
(Foo "var" '(a b c d))       <span class='arw'>&rarr;</span> (a b c d)

(Foo "John Doe")  <span class='arw'>&rarr;</span> 123
(Foo "#1234")     <span class='arw'>&rarr;</span> "hello world"
(Foo "var")     <span class='arw'>&rarr;</span> (a b c d)
</pre>		

<p>An entry which doesn't exist will return <tt>nil</tt>:</p>

<pre>
(Foo "bar")    <span class='arw'>&rarr;</span> nil
</pre>		

<p>Setting an entry to <tt>nil</tt> will effectively delete it from
the namespace.</p>

<p>An association list can be generated from the contents of the namespace:</p>

<pre>
(Foo) <span class='arw'>&rarr;</span> (("#1234" "hello world") ("John Doe" 123) ("var" (a b c d)))
</pre>		

<p>Entries in the dictionary can also be created from a list:</p>

<pre>
(Foo '(("#1234" "hello world") ("John Doe" 123) ("var" (a b c d))) <span class='arw'>&rarr;</span> Foo
</pre>		

<p>The list can also be used to iterate through the sorted key -&gt; value pairs:</p>

<pre>
(dolist (item (Foo)) (println (item 0) " -&gt; " (item 1)))

<b>#1234 -&gt; hello world
John Doe -&gt; 123
var -&gt; (a b c d)</b>
</pre>

<p>Like many built-in functions hash expressions return a reference to their content which 
can be modified directly:</p>

<pre>
(pop (Foo "var")) <span class='arw'>&rarr;</span> a

(Foo "var") <span class='arw'>&rarr;</span> (b c d)

(push 'z (Foo "var")) <span class='arw'>&rarr;</span> (z b c d)

(Foo "var") <span class='arw'>&rarr;</span> (z b c d)
</pre>

<p>When setting hash values the anaphoric system variable <tt>$it</tt>
can be used to refer to the old value when setting the new:</p>

<pre>
(Foo "bar" "hello world")

(Foo "bar" (upper-case $it))

(Foo "bar") <span class='arw'>&rarr;</span> "HELLO WORLD"
</pre>

<p>Hash values also can be modified using <a href="#setf">setf</a>:</p>

<pre>
(Foo "bar" 123)        <span class='arw'>&rarr;</span> 123

(setf (Foo "bar") 456) <span class='arw'>&rarr;</span> 456

(Foo "bar")            <span class='arw'>&rarr;</span> 456
</pre>

<p>But supplying the value as a second parameter to the hash functions 
is shorter to write and faster.</p>

<p>Dictionaries can easily be saved to a file and reloaded later:</p>

<pre>
; save dictionary
(save "Foo.lsp" 'Foo)

; load dictionary
(load "Foo.lsp")
</pre>	

<p>Internally the key strings are created and stored as symbols in the
hash context. All key strings are prepended with an <tt>_</tt> 
underscore character.  This protects against overwriting the default symbol and
symbols like <tt>set</tt> and <tt>sym</tt>, which are needed when loading
a hash namespace from disk or over <tt>HTTP</tt>. Note the following
difference:</p>

<pre>
(Foo) <span class='arw'>&rarr;</span> (("#1234" "hello world") ("John Doe" 123) ("var" (a b c d)))

(symbols Foo) <span class='arw'>&rarr;</span> (Foo:Foo Foo:_#1234 Foo:_John Doe Foo:_var)
</pre>	

<p>In the first line hash symbols are shown as strings without the preceding
underscore characters. The second line shows the internal form of the symbols with
prepended underscore characters.</p>


<p>For a more detailed introduction to <em>namespaces</em>, see the chapter on
<a href="#contexts">Contexts</a>.
</p>

<br/>

<a name="pass_big"></a>
<h3>Passing data by reference</h3>

<p>A <a href="#default_function">default functor</a> can also be used to hold data. 
If this data contains a list or string, the context name can be used as a reference to 
the data:</p>

<pre>
;; the default functor for holding data

(define Mylist:Mylist '(a b c d e f g))

(Mylist 3) <span class='arw'>&rarr;</span> d 

(setf (Mylist 3) 'D) <span class='arw'>&rarr;</span> D

Mylist:Mylist <span class='arw'>&rarr;</span> (a b c D e f g)

;; access list or string data from a default functor

(first Mylist) <span class='arw'>&rarr;</span> a

(reverse Mylist) <span class='arw'>&rarr;</span> (g f e D c b a)

(set 'Str:Str "acdefghijklmnop") 

(upper-case Str) <span class='arw'>&rarr;</span> "ACDEFGHIJKLMNOP"
</pre>

<p>Most of the time, newLISP passes parameters by <em>value copy</em>.
This poses a potential problem when passing large lists or strings
to user-defined functions or macros. Strings and lists, which are packed 
in a namespace using default functors, are passed automatically by reference:
</p>

<pre>
;; use a default functor to hold a list

(set 'Mydb:Mydb (sequence 1 100000))

(define (change-db obj idx value)
    (setf (obj idx) value))

; pass by context reference
(change-db Mydb 1234 "abcdefg")

(Mydb 1234)  <span class='arw'>&rarr;</span> "abcdefg"
</pre>		

<p> Any argument of a built-in function calling for either a list or a string 
&mdash; but no other data type &mdash; can receive data passed by reference. 
Any user-defined function can take either normal variables, or can take a context 
name for passing a reference to the default functor containing a list or string.</p>

<p>Note that on lists with less than about 100 elements or strings of less than 
about 50000 characters, the speed difference between reference and value passing is 
negligible. But on bigger data objects, differences in both speed and memory usage 
between reference and value passing can be significant.</p>

<p>Built-in and user-defined functions are suitable for <u>both</u> types of arguments, 
but when passing context names, data will be passed by reference.</p>

<p>Quoted symbols can also be used to pass data by reference, but this method
has disadvantages:</p>

<pre>
(define (change-list aList) (push 999 (eval aList)))

(set 'data '(1 2 3 4 5))

; note the quote ' in front of data
(change-list 'data)  <span class='arw'>&rarr;</span> (999 1 2 3 4 5)

data  <span class='arw'>&rarr;</span>  (999 1 2 3 4 5)
</pre>

<p>Although this method is simple to understand and use, it poses the potential 
problem of <em>variable capture</em> when passing the same symbol as used  
as a function parameter:</p>

<pre>
;; pass data by symbol reference

&gt; (set 'aList '(a b c d))
(a b c d)
&gt; (change-list 'aList)

<span class='err'>ERR: list or string expected : (eval aList)
called from user defined function change-list</span>
&gt; 
</pre>

<p>At the beginning of the chapter it was shown how to package data 
in a name-space using a default functor. Not only the default 
functor but any symbol in  context can be used to hold data. The 
disadvantage is that the calling function must have knowledge about
the symbol being used:</p>

<pre>
;; pass data by context reference

(set 'Mydb:data (sequence 1 100000))

(define (change-db obj idx value)
    (setf (obj:data idx) value))

(change-db Mydb 1234 "abcdefg")

(nth 1234 Mydb:data)   <span class='arw'>&rarr;</span> "abcdefg"
; or
(Mydb:data 1234)   <span class='arw'>&rarr;</span> "abcdefg"
</pre>

<p>
The function receives the namespace in the variable <tt>obj</tt>,
but it must have the knowledge that the list to access is contained
in the <tt>data</tt> symbol of that namespace (context).</p>

<br/>

<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>

<br/>

<a name="foop"></a>
<h2>18. Functional object-oriented programming</h2>

<p>Functional-object oriented programming (FOOP) is based on the following 
five principles:</p>

<ul>
<li><p>Class attributes and methods are stored in the namespace of the object class.</p></li>
<li><p>The namespace default functor holds the object constructor method.</p></li>
<li><p>An object is constructed using a list, the first element of which is the 
context symbol describing the class of the object.</p></li>
<li><p>Polymorphism is implemented using the <a href="#colon"><tt>:</tt> (colon)</a>
operator, which selects the appropriate class from the object.</p></li>
<li><p>A target object inside a class-method function is accessed via the <a href="#self">self</a>
function.</p></li>
</ul>

<p>The following paragraphs are a short introduction to FOOP as designed by 
<em>Michael Michaels</em> from <a href="http://neglook.com">neglook.com</a>.</p>


<br/>

<a name="newlisp_classes"></a>
<h3>FOOP classes and constructors</h3>

<p>Class attributes and methods are stored in the namespace of the object class. 
No object instance data is stored in this namespace/context. Data variables in
the class namespace only describe the class of objects as a whole but don't contain
any object specific information. A generic FOOP object constructor can be used
as a template for specific object constructors when creating new object classes
with <tt>new</tt>:</p>

<pre>
; built-in generic FOOP object constructor
(define (Class:Class) 
    (cons (context) (args)))

; create some new classes

(new Class 'Rectangle)   <span class='arw'>&rarr;</span> Rectangle
(new Class 'Circle)      <span class='arw'>&rarr;</span> Circle

; create some objects using the default constructor

(set 'rect (Rectangle 10 20))   <span class='arw'>&rarr;</span> (Rectangle 10 20)
(set 'circ (Circle 10 10 20))   <span class='arw'>&rarr;</span> (Circle 10 10 20)

; create a list of objects
; building the list using the list function instead of assigning
; a quoted list ensures that the object constructors are executed

(set 'shapes (list (Circle 5 8 12) (Rectangle 4 8) (Circle 7 7 15)))
<span class='arw'>&rarr;</span> ((Circle 5 8 12) (Rectangle 4 8) (Circle 7 7 15))
</pre>

<p>The generic FOOP constructor is already pre-defined, and FOOP
code can start with <tt>(new Class ...)</tt> statements right away.</p>

<p>As a matter of style, new classes should only be created in the MAIN context. 
If creating a new class while in a different namespace, the new class name 
must be prefixed with MAIN and the statement should be on the top-level:</p>

<pre>
(context 'Geometry)

(new Class 'MAIN:Rectangle)
(new Class 'MAIN:Circle)

...
</pre>

<p>Creating the namespace classes using <a href="#new">new</a> reserves the class 
name as a context in newLISP and facilitates forward references. At the same time, 
a simple constructor is defined for the new class for instantiating new objects. 
As a convention, it is recommended to start class names in upper-case to signal that 
the name stands for a namespace.</p>

<p>In some cases, it may be useful to overwrite the simple constructor, that was 
created during class creation, with <tt>new</tt>:</p>

<pre>
; overwrite simple constructor 
(define (Circle:Circle x y radius)
    (list Circle x y radius))
</pre>

<p>A constructor can also specify defaults:</p>

<pre>
; constructor with defaults
(define (Circle:Circle (x 10) (y 10) (radius 3))
    (list Circle x y radius))

(Circle) <span class='arw'>&rarr;</span> (Circle 10 10 3)
</pre>

<p>In many cases the constructor as created when using <tt>new</tt> is sufficient and overwriting
it is not necessary.</p>

<br/>

<a name="newlisp_objects"></a>
<h3>Objects and associations</h3>

<p>FOOP represents objects as lists. The first element of the list indicates the 
object's kind or class, while the remaining elements contain the data. The following 
statements define two <em>objects</em> using any of the constructors defined previously:</p>

<pre>
(set 'myrect (Rectangle 5 5 10 20)) <span class='arw'>&rarr;</span> (Rectangle 5 5 10 20)
(set 'mycircle (Circle 1 2 10)) <span class='arw'>&rarr;</span> (Circle 1 2 10)
</pre>

<p>An object created is identical to the function necessary to create it (hence FOOP). 
Nested objects can be created in a similar manner:</p>

<pre>
; create classes
(new Class 'Person)
(new Class 'Address)
(new Class 'City)
(new Class 'Street)

; create an object containing other objects
(set 'JohnDoe (Person (Address (City "Boston") (Street 123 "Main Street"))))
<span class='arw'>&rarr;</span> (Person (Address (City "Boston") (Street 123 "Main Street")))
</pre>

<p>Objects in FOOP not only resemble functions they also resemble associations. The
<a href="#assoc">assoc</a> function can be used to access object data by name:</p>

<pre>
(assoc Address JohnDoe) <span class='arw'>&rarr;</span> (Address (City "Boston") (Street 123 "Main Street"))

(assoc (list Address Street) JohnDoe) <span class='arw'>&rarr;</span> (Street 123 "Main Street")
</pre>

<p>In a similar manner <a href="#setf">setf</a> together with <a href="#assoc">assoc</a> 
can be used to modify object data:</p>

<pre>
(setf (assoc (list Address Street) JohnDoe) '(Street 456 "Main Street"))
<span class='arw'>&rarr;</span> (Street 456 "Main Street")
</pre>

<p>The street number has been changed from <tt>123</tt> to <tt>456</tt>.</p>

<p>Note that in none of the <tt>assoc</tt> statements <tt>Address</tt> and <tt>Street</tt>
need to carry quotes. The same is true in the set statement: 
<tt>(set 'JohnDoe (Person ...))</tt> for the data part assigned. In both cases we do not 
deal with symbols or lists of symbols but rather with contexts and FOOP objects which 
evaluate to themselves. Quoting would not make a difference.</p>

<br/>

<a name="colon_operator"></a>
<h3>The colon <tt>:</tt> operator and polymorphism</h3>

<p>In newLISP, the colon character <tt>:</tt> is primarily used to
connect the context symbol with the symbol it is qualifying. 
Secondly, the colon function is used in FOOP to resolve a function's 
application <em>polymorphously</em>.</p>

<p>The following code defines two functions called <tt>area</tt>, 
each belonging to a different namespace / class. Both functions could
have been defined in different modules for better separation, but in 
this case they are defined in the same file and without bracketing 
<a href="#context">context</a> statements. Here, only
the symbols <tt>rectangle:area</tt> and <tt>circle:area</tt> belong
to different namespaces. The local parameters <tt>p</tt>, <tt>c</tt>, 
<tt>dx</tt>, and <tt>dy</tt> are all part of <tt>MAIN</tt>,
but this is of no concern.</p>

<pre>
;; class methods for rectangles

(define (Rectangle:area)
    (mul (self 3) (self 4)))

(define (Rectangle:move dx dy)
    (inc (self 1) dx) 
    (inc (self 2) dy))

;; class methods for circles

(define (Circle:area)
    (mul (pow (self 3) 2) (acos 0) 2))

(define (Circle:move dx dy)
    (inc (self 1) dx) 
    (inc (self 2) dy))
</pre>

<p>By prefixing the <tt>area</tt> or <tt>move</tt> symbol with the
<a href="#colon"><tt>:</tt> (colon)</a>, 
we can call these functions for each class of object. Although there is no space 
between the colon and the symbol following it, newLISP parses them as distinct entities. 
The colon works as a function that processes parameters:</p> 

<pre>
(:area myrect) <span class='arw'>&rarr;</span> 200 ; same as (: area myrect)
(:area mycircle) <span class='arw'>&rarr;</span> 314.1592654 ; same as (: area mycircle)

;; map class methods uses curry to enclose the colon operator and class function

(map (curry :area) (list myrect mycircle)) <span class='arw'>&rarr;</span> (200 314.1592654)

(map (curry :area) '((Rectangle 5 5 10 20) (Circle 1 2 10))) <span class='arw'>&rarr;</span> (200 314.1592654) 


;; objects are mutable (since v10.1.8)

(:move myrect 2 3)
(:move mycircle 4 5) 

myrect    <span class='arw'>&rarr;</span> (Rectangle 7 8 10 20)
mycircle  <span class='arw'>&rarr;</span> (Circle 5 7 10)
</pre>

<p>In this example, the correct qualified symbol (<tt>rectangle:area</tt> or 
<tt>circle:area</tt>) is constructed and applied to the object data based on 
the symbol following the colon and the context name (the first element of the object list).</p>

<p>Note, that although the caller specifies the called target object of the call,
the method definition does not include the object as a parameter. When writing 
functions to modify FOOP objects, instead the function <a href="#self">self</a>
is used to access and index the object.</p>

<br/>
<a name="structure_foop"></a>
<h3>Structuring a larger FOOP program</h3>

<p>In all the previous examples, class function methods where directly
written into the MAIN context namespace. This works and is adequate 
for smaller programs written by just one programmer. When writing larger 
systems, all the methods for one class should be surrounded by 
<a href="#context">context</a> statements to provide better isolation 
of parameter variables used and to create an isolated location for potential
class variables.</p>

<p>Class variables could be used in this example as a container for
lists of objects, counters or other information specific to a class
but not to a specific object. The following code segment rewrites the 
example from above in this fashion.</p>

<p>Each context / namespace could go into an extra file with the same
name as the class contained. Class creation, startup code and the main
control code is in a file <tt>MAIN.lsp</tt>:</p>

<pre>
; file MAIN.lsp - declare all classes used in MAIN

(new Class 'Rectangle)
(new Class 'Circle)

; start up code

(load "Rectangle.lsp")
(load "Circle.lsp")

; main control code

; end of file
</pre>

<p>Each class is in a separate file:</p>

<pre>
; file Rectangle.lsp - class methods for rectangles

(context Rectangle)

(define (Rectangle:area)
(mul (self 3) (self 4)))

(define (Rectangle:move dx dy)
(inc (self 1) dx) 
(inc (self 2) dy))

; end of file
</pre>

<p>And the <tt>Circle</tt> class file follows:</p>

<pre>
; file Circle.lsp - class methods for circles

(context Circle)

(define (Circle:area)
    (mul (pow (self 3) 2) (acos 0) 2))

(define (Circle:move dx dy)
    (inc (self 1) dx) 
    (inc (self 2) dy))

; end of file
</pre>

<p>All sets of class functions are now lexically separated
from each other.</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>


<a name="multi_processing"></a>
<h2>19. Concurrent processing and distributed computing</h2>

<p>newLISP has high-level APIs to control multiple processes on the same
CPU or distributed onto different computer nodes on a TCP/IP network.</p>

<br/>

<a name="cilk"></a>
<h3>Cilk API</h3>

<p>newLISP implements a <a href="http://supertech.csail.mit.edu/cilk/">Cilk</a>-
like API to launch and control concurrent processes. The API can take advantage of 
multi-core computer architectures. Only three functions, <a href="#spawn">spawn</a>, 
<a href="#sync">sync</a> and <a href="#abort">abort</a>, are necessary to start
multiple processes and collect the results in a synchronized fashion. The underlying
operating system distributes processes onto different cores inside the CPU or
executes them on the same core in parallel if there are not enough cores present.
Note that newLISP only implements the API; optimized scheduling
of spawned procedures is not performed as in Cilk. Functions are started in the order
they appear in <tt>spawn</tt> statements and are distributed and scheduled onto 
different cores in the CPU by the operating system.</p>

<p>When multiple cores are present, this can increase overall processing speed
by evaluating functions in parallel. But even when running on single core CPUs,
the Cilk API makes concurrent processing much easier for the programmer and
may speed up processing if subtasks include waiting for I/O or sleeping.</p>

<p>Since version 10.1 <a href="#send">send</a> and <a href="#receive">receive</a>
message functions are available for communications between parent and child 
processes. The functions can be used in blocking and non blocking communications 
and can transfer any kind of newLISP data or expressions. Transmitted expressions 
can be evaluated in the recipients environment.</p>

<p>Internally, newLISP uses the lower level <a href="#fork">fork</a>,
<a href="#wait-pid">wait-pid</a>, <a href="#destroy">destroy</a>, and 
<a href="#share">share</a> functionalities to control processes and synchronize
the passing of computed results via a shared memory interface.</p>

<p>Only on macOS and other Unixes will the Cilk API parallelize tasks.
On MS Windows, the API is not available.</p>

<br/>

<a name="distributed"></a>
<h3>Distributed network computing</h3>

<p>With only one function, <a href="#net-eval">net-eval</a>, newLISP implements
distributed computing. Using <tt>net-eval</tt>, different tasks can be mapped
and evaluated on different nodes running on a TCP/IP network or local domain Unix sockets
network when running on the same computer. <tt>net-eval</tt> does all the housekeeping 
required to connect to remote nodes, transfer functions to execute, and
collect the results. <tt>net-eval</tt> can also use a call-back function to
further structure consolidation of incoming results from remote nodes.</p>

<p>The functions <a href="#read-file">read-file</a>, <a href="#write-file">write-file</a>,  
<a href="#append-file">append-file</a> and <a href="#delete-file">delete-file</a> all can 
take URLs instead of path-file names. Server side newLISP running in demon mode or an other 
HTTP server like Apache, receive standard HTTP requests and translate them into the 
corresponding actions on files.</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>

<br/>
<a name="JSON_XML"></a>
<h2>20. JSON, XML, S-XML, and XML-RPC</h2> 
<b>JSON support</b>
<p>JSON-encoded data can be parsed into S-expressions using the 
<a href="#json-parse">json-parse</a> function. Error information for
failed JSON translations can be retrieved using <a href="#json-error">json-error</a>.</p>

<p>For a description of the JSON format (<u>J</u>ava<u>S</u>cript <u>O</u>bject <u>N</u>otation)
consult <a href="http://json.org">json.org</a>.
Examples for correct formatted JSON text can be seen at 
<a href="http://json.org/examples.html">json.org/examples.html</a>.</p>

<p>To retrieve data in nested lists resulting from JSON translation, use the
<a href="#assoc">assoc</a>, <a href="#lookup">lookup</a> and <a href="#ref">ref</a>
functions.</p>

<p>See the description of <a href="#json-parse">json-parse</a> for a complete example
of parsing and processing JSON data.</p>

<b>XML support</b>
<p>newLISP's built-in support for XML-encoded data or documents
comprises three functions:
<a href="#xml-parse">xml-parse</a>,
<a href="#xml-type-tags">xml-type-tags</a>, and <a href="#xml-error">xml-error</a>.
</p>

<p>
Use the <a href="#xml-parse">xml-parse</a> function
to parse XML-encoded strings.
When <tt>xml-parse</tt> encounters an error,
<tt>nil</tt> is returned.
To diagnose syntax errors caused by incorrectly formatted XML,
use the function <a href="#xml-error">xml-error</a>.
The <a href="#xml-type-tags">xml-type-tags</a> function can be used
to control or suppress the appearance of XML type tags.
These tags classify XML into one of four categories:
text, raw string data, comments, and element data.</p>


<b>XML source:</b>
<pre>
&lt;?xml version="1.0"?&gt;
&lt;DATABASE name="example.xml"&gt;
&lt;!--This is a database of fruits--&gt;
&lt;FRUIT&gt;
&lt;NAME&gt;apple&lt;/NAME&gt;
&lt;COLOR&gt;red&lt;/COLOR&gt;
&lt;PRICE&gt;0.80&lt;/PRICE&gt;
&lt;/FRUIT&gt;
&lt;/DATABASE&gt;
</pre>		<br/>
<b>Parsing without options:</b>
<pre>
(xml-parse (read-file "example.xml"))
<span class='arw'>&rarr;</span>  (("ELEMENT" "DATABASE" (("name" "example.xml")) (("TEXT" "\r\n")
("COMMENT" "This is a database of fruits")
("TEXT" "\r\n        ")
("ELEMENT" "FRUIT" () (
	("TEXT" "\r\n\t        ")
	("ELEMENT" "NAME" () (("TEXT" "apple")))
	("TEXT" "\r\n\t\t")
	("ELEMENT" "COLOR" () (("TEXT" "red")))
	("TEXT" "\r\n\t\t")
	("ELEMENT" "PRICE" () (("TEXT" "0.80")))
	("TEXT" "\r\n\t")))
("TEXT" "\r\n"))))
</pre>		<p>
	S-XML can be generated directly from XML
	using <a href="#xml-type-tags">xml-type-tags</a>
	and the special option parameters
	of the <a href="#xml-parse">xml-parse</a> function:
</p>
<br/>
<b>S-XML generation using all options:</b>
<pre>
(xml-type-tags nil nil nil nil)
(xml-parse (read-file "example.xml") (+ 1 2 4 8 16))
<span class='arw'>&rarr;</span>  ((DATABASE (@ (name "example.xml"))
  (FRUIT (NAME "apple")
	  (COLOR "red")
	  (PRICE "0.80"))))
	
</pre>
<p>S-XML is XML reformatted as newLISP <em>S-expressions</em>.
The <tt>@</tt> (at symbol) denotes an XML attribute specification.</p>

<p>To retrieve data in nested lists resulting from S-XML translation, use the
<a href="#assoc">assoc</a>, <a href="#lookup">lookup</a> and <a href="#ref">ref</a>
functions.</p>

<p>See <a href="#xml-parse">xml-parse</a> in the reference section of the manual
for details on parsing and option numbers, as well as for a longer example.</p>

<br/>
<b>XML-RPC</b>
<br/>
<p>
	The remote procedure calling protocol XML-RPC uses
	HTTP post requests as a transport and
	XML for the encoding of method names, parameters, and parameter types.
	XML-RPC client libraries and servers have been implemented
	for most popular compiled and scripting languages.
</p>
<p>
	For more information about XML,
	visit <a href="http://www.xmlrpc.com/">www.xmlrpc.com</a>.
</p>
<p>
	XML-RPC clients and servers are easy to write
	using newLISP's built-in network and XML support.
	A stateless XML-RPC server implemented as a CGI service
	can be found in the file <tt>examples/xmlrpc.cgi</tt>. This
	script can be used together with a web server, like Apache.
	This XML-RPC service script implements
	the following methods:
</p>

<table  width="98%" summary="XMPRPC methods for newLISP server">
<tr align="left"><th>method</th><th>description</th></tr>
<tr>
<td><tt>system.listMethods</tt></td>
<td>Returns a list of all method names</td>
</tr>
<tr>
<td><tt>system.methodHelp</tt></td>
<td>Returns help for a specific method</td>
</tr>
<tr>
<td><tt>system.methodSignature</tt></td>
<td>Returns a list of return/calling signatures for a specific method </td>
</tr>
<tr>
<td><tt>newLISP.evalString</tt></td>
<td>Evaluates a Base64 newLISP expression string</td></tr>
</table><br/>

<p>
The first three methods are <em>discovery</em> methods implemented by most XML-RPC servers.
The last one is specific to the newLISP XML-RPC server script and
implements remote evaluation of a Base64-encoded string of newLISP source code.
newLISP's <a href="#base64-enc">base64-enc</a> and <a href="#base64-dec">base64-dec</a> functions
can be used to encode and decode Base64-encoded information.
</p>

<p>
In the <tt>modules</tt> directory of the source distribution,
the file <tt>xmlrpc-client.lsp</tt> implements a specific client interface for
all of the above methods.</p>

<pre>
(load "xmlrpc-client.lsp")  ; load XML-RPC client routines						 

(XMLRPC:newLISP.evalString
"http://localhost:8080/xmlrpc.cgi"
"(+ 3 4)")  <span class='arw'>&rarr;</span> "7"
</pre>		<p>
	In a similar fashion,
	standard <tt>system.xxx</tt> calls can be issued.
</p>
<p>
All functions return either a result if successful, or <tt>nil</tt> if a request fails.
In case of failure, the expression <tt>(XMLRPC:error)</tt> can be evaluated
to return an error message.</p>

<p>
For more information, please consult the header of the file <tt>modules/xmlrpc-client.lsp</tt>.
</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="internationalization"></a>
<h2>21. Customization, localization, and UTF-8</h2>

<a name="naming"></a>
<h3>Customizing function names</h3>

<p>All built-in primitives in newLISP can be easily renamed:</p>

<pre>
(constant 'plus +)
</pre>		<p>
	Now, <tt>plus</tt> is functionally equivalent to <tt>+</tt>
	and runs at the same speed.
</p>
<p>
	The <a href="#constant">constant</a> function,
	rather than the <tt>set</tt> function,
	must be used to rename built-in primitive symbols.
	By default, all built-in function symbols
	are protected against accidental overwriting.
</p>

<p>
It is possible to redefine all integer arithmetic operators to their floating
point equivalents:
</p>

<pre>
(constant '+ add)
(constant '- sub)
(constant '* mul)
(constant '/ div)
</pre>		

<p>All operations using <tt>+</tt>, <tt>-</tt>, <tt>*</tt>, and <tt>/</tt>
are now performed as floating point operations.</p>

<p>Using the same mechanism, the names of built-in functions
can be translated into languages other than English:</p>

<pre>
(constant 'wurzel sqrt)    ; German for 'square-root'

; make the new symbol global at the same time
(constant (global 'imprime) print)  ; Spanish for 'print'
&hellip;
</pre>		

<p>The new symbol can be made global at the same time using <a href="#global">global</a>.</p>

<br/>

<a name="switching"></a>
<h3>Switching the locale</h3>

<p>newLISP can switch locales based on the platform and operating system.
On startup, non-UTF-8 enabled newLISP attempts to set the ISO C standard 
default POSIX locale, available for most platforms and locales.  On UTF-8
enabled newLISP the default locale for the platform is set. The 
<a href="#set-locale">set-locale</a> function can also be used to switch 
to the default locale:</p>
<pre>
(set-locale "")
</pre>		
<p>
This switches to the default locale used on your platform/operating system
and ensures character handling (e.g., <a href="#upper-case">upper-case</a>)
works correctly.</p>

<p>Many Unix systems have
a variety of locales available. To find out which ones are available on
a particular Linux/Unix/BSD system, execute the following command
in a system shell:</p>

<pre>
locale -a
</pre>		

<p>
This command prints a list of all the locales available on your system.
Any of these may be used as arguments to <a href="#set-locale">set-locale</a>:</p>

<pre>
(set-locale "es_US")
</pre>		<p>
	This would switch to a U.S. Spanish locale.
	Accents or other characters
	used in a U.S. Spanish environment
	would be correctly converted.
</p>
<p>
	See the manual description for more details
	on the usage of <a href="#set-locale">set-locale</a>.
</p>

<br/>

<a name="decimal_point"></a>
<h3>Decimal point and decimal comma</h3>

<p>Many countries use a comma instead of a period
as a decimal separator in numbers.
newLISP correctly parses numbers
depending on the locale set:</p>

<pre>
; switch to German locale on a Linux  or OSX system
(set-locale "de_DE") <span class='arw'>&rarr;</span> ("de_DE" ",")

; newLISP source and output use a decimal comma
(div 1,2 3)  <span class='arw'>&rarr;</span> 0,4
</pre>		

<p>The default POSIX C locale, which is set when newLISP starts up,
uses a period as a decimal separator.</p>

<p>The following countries use a <b>period as a decimal separator</b>:</p>

<blockquote>Australia, Botswana, Canada (English-speaking), China, Costa Rica, 
Dominican Republic, El Salvador, Guatemala, Honduras, Hong Kong, India, Ireland, 
Israel, Japan, Korea (both North and South), Malaysia, Mexico, Nicaragua, 
New Zealand, Panama, Philippines, Puerto Rico, Saudi Arabia, Singapore, Switzerland, 
Thailand, United Kingdom, and United States.</blockquote>

<p>The following countries use a <b>comma as a decimal separator</b>:</p>

<blockquote>Albania, Andorra, Argentina, Austria, Belarus, Belgium, Bolivia, 
Brazil, Bulgaria, Canada (French-speaking), Croatia, Cuba, Chile, Colombia, 
Czech Republic, Denmark, Ecuador, Estonia, Faroes, Finland, France, Germany, 
Greece, Greenland, Hungary, Indonesia, Iceland, Italy, Latvia, Lithuania, 
Luxembourg, Macedonia, Moldova, Netherlands, Norway, Paraguay, Peru, Poland, 
Portugal, Romania, Russia, Serbia, Slovakia, Slovenia, Spain, South Africa, 
Sweden, Ukraine, Uruguay, Venezuela, and Zimbabwe.</blockquote>


<br/>
<a name="unicode_utf8"></a>
<h3>Unicode and UTF-8 encoding</h3>

<p>Note that for many European languages,
the <a href="#set-locale">set-locale</a> mechanism
is sufficient to display non-ASCII character sets,
as long as each character is presented as <em>one</em> byte internally.
UTF-8 encoding is only necessary for multi-byte character sets as described 
in this chapter.</p>

<p>
	newLISP can be compiled
	as a UTF-8&ndash;enabled application.
	UTF-8 is a multi-byte encoding
	of the international Unicode character set.
	A UTF-8&ndash;enabled newLISP
	running on an operating system with UTF-8 enabled
	can handle any character of the installed locale.
</p>
<p>
	The following steps
	make UTF-8 work with newLISP
	on a specific operating system and platform:
</p>
<p>
	<tt>(1)</tt> Use one of the makefiles
	ending in <tt>utf8</tt>
	to compile newLISP as
	a UTF-8 application.
	If no UTF-8 makefile
	is available for your platform,
	the normal makefile
	for your operating system
	contains instructions
	on how to change it
	for UTF-8.
</p>
<p>
	The macOS binary installer contains
	a UTF-8&ndash;enabled version by default.
</p>
<p>
	<tt>(2)</tt> Enable the UTF-8 locale
	on your operating system.
	Check and set a UTF-8 locale
	on Unix and Unix-like OSes
	by using the <tt>locale</tt> command
	or the <tt>set-locale</tt> function within newLISP.
	On Linux, the locale can be changed by setting
	the appropriate environment variable.
	The following example uses <tt>bash</tt>
	to set the U.S. locale:
</p>
<pre>
export LC_CTYPE=en_US.UTF-8
</pre>		
<p><tt>(3)</tt> The UTF-8&ndash;enabled newLISP automatically switches to the locale found
on the operating system.  Make sure the command shell
is UTF-8&ndash;enabled.  The U.S. version of WinXP's <tt>notepad.exe</tt>
can display Unicode UTF-8&ndash;encoded characters, but the command shell cannot.
On Linux and other Unixes, the Xterm shell can be used
when started as follows:</p>

<pre>
LC_CTYPE=en_US.UTF-8 xterm
</pre>		<p>
	The following procedure can now be used
	to check for UTF-8 support.
	After starting newLISP, type:
</p>
<pre>
(println (char 937))               ; displays Greek uppercase omega
(println (lower-case (char 937)))  ; displays lowercase omega
</pre>		<p>
	While the uppercase omega (&Omega;) looks
	like a big O on two tiny legs,
	the lowercase omega (&omega;) has
	a shape similar to a small <tt>w</tt>
	in the Latin alphabet.
</p>
<p>
	Note: Only the output of <tt>println</tt>
	will be displayed as a character;
	<tt>println</tt>'s return value
	will appear on the console
	as a multi-byte ASCII character.
</p>
<p>
	When UTF-8&ndash;enabled newLISP
	is used on a non-UTF-8&ndash;enabled display,
	both the output and the return value
	will be two characters.
	These are the two bytes necessary
	to encode the omega character.
</p>

<br/>

<a name="utf8_capable"></a>
<h3>Functions working on UTF-8 characters</h3>

<p>When UTF-8&ndash;enabled newLISP is used, the following string functions work
on one- or multi-byte characters rather than one 8-bit byte boundaries:</p>

<table  width="98%" summary="functions working on character boundaries in UTF-8">
<tr align="left"><th>function</th><th>description</th></tr>

<tr>
<td><a href="#char">char</a></td>
<td>translates between characters and ASCII/Unicode</td>
</tr>

<tr>
<td><a href="#chop">chop</a></td>
<td>chops characters from the end of a string</td>
</tr>

<tr>
<td><a href="#date">date</a></td>
<td>converts date number to string (when used with the third argument)</td>
</tr>

<tr>
<td><a href="#dostring">dostring</a></td>
<td>evaluates once for each character in a string</td>
</tr>

<tr>
<td><a href="#explode">explode</a></td>
<td>transforms a string into a list of characters</td>
</tr>

<tr>
<td><a href="#first">first</a></td>
<td>gets first element in a list (car, head) or string</td>
</tr>

<tr>

<td><a href="#last">last</a></td>
<td>returns the last element of a list or string</td>
</tr>


<tr>
<td><a href="#lower-case">lower-case</a></td>
<td>converts a string to lowercase characters</td>
</tr>

<tr>
<td><a href="#nth">nth</a></td>
<td>gets the <em>nth</em> element of a list or string</td>
</tr>

<tr>
<td><a href="#pop">pop</a></td>
<td>deletes an element from a list or string</td>
</tr>

<tr>
<td><a href="#push">push</a></td>
<td>inserts a new element in a list or string</td>
</tr>

<tr>
<td><a href="#rest">rest</a></td>
<td>gets all but the first element of a list (cdr, tail) or string</td>
</tr>

<tr>
<td><a href="#select">select</a></td>
<td>selects and permutes elements from a list or string</td>
</tr>

<tr>
<td><a href="#title-case">title-case</a></td>
<td>converts the first character of a string to uppercase</td>
</tr>

<tr>
<td><a href="#trim">trim</a></td>
<td>trims a string from both sides</td>
</tr>

<tr>
<td><a href="#upper-case">upper-case</a></td>
<td>converts a string to uppercase characters</td>
</tr>

</table><br/>

<p>All other string functions work on 8-bit bytes.  When positions are returned,
as in <a href="#find">find</a> or <a href="#regex">regex</a>,
they are single 8-bit byte positions rather than character positions which
may be multi-byte.
The <a href="#get-char">get-char</a> and <a href="#slice">slice</a> functions
do not take multi-byte character offsets, but single-byte offsets, even
in UTF-8 enabled versions of newLISP.
The <a href="#reverse">reverse</a> function reverses
a byte vector, not a character vector. The last three functions can still 
be used to manipulate binary non-textual data in the UTF-8&ndash;enabled 
version of newLISP. To make <a href="#slice">slice</a> and <a href="#reverse">reverse</a>
work with UTF-8 strings, combine them with <a href="#explode">explode</a> and
<a href="#join">join</a>.</p>

<p>To enable UTF-8 in Perl Compatible Regular Expressions (PCRE)
&mdash; used by <a href="#directory">directory</a>, <a href="#find">find</a>, 
<a href="#member">member</a>, <a href="#parse">parse</a>, <a href="#regex">regex</a>, 
<a href="#regex-comp">regex-comp</a> and <a href="#replace">replace</a> &mdash;
set the option number accordingly (2048). Note that offset and lengths in 
<a href="#regex">regex</a> results are always in single byte counts.
See the <a href="#regex">regex</a> documentation for details.</p>

<p>Use <a href="#explode">explode</a> to obtain an array
of UTF-8 characters and to manipulate characters rather than bytes
when a UTF-8&ndash;enabled function is unavailable:</p>

<pre>
(join (reverse (explode str)))  ; reverse UTF-8 characters
</pre>		

<p>The above string functions (often used to manipulate non-textual binary data)
now work on character, rather than byte, boundaries,
so care must be exercised when using the UTF-8&ndash;enabled version.
The size of the first 127 ASCII characters &mdash;
along with the characters in popular code pages such as ISO 8859 &mdash;
is one byte long.  When working exclusively within these code pages,
UTF-8&ndash;enabled newLISP is not required. 
The <a href="#set-locale">set-locale</a> function alone
is sufficient for localized behavior.</p>

<br/>

<a name="utf8_version"></a>
<h3>Functions only available on UTF-8 enabled versions</h3>

<table  width="98%" summary="functions only available on UTF-8 version">
<tr align="left"><th>function</th><th>description</th></tr>

<tr>
<td><a href="#unicode">unicode</a></td>
<td>converts UTF-8 or ASCII strings into USC-4 Unicode</td>
</tr>

<tr>
<td><a href="#utf8">utf8</a></td>
<td>converts UCS-4 Unicode strings to UTF-8</td>
</tr>

<tr>
<td><a href="#utf8len">utf8len</a></td>
<td>returns the number of UTF-8 characters in a string</td>
</tr>

</table><br/>

<p>The first two functions are rarely used in practice,
as most Unicode text files are already UTF-8&ndash;encoded
(rather than UCS-4, which uses four-byte integer characters).
Unicode can be displayed directly when using the 
<tt>"%ls"</tt> <a href="#format">format</a> specifier.</p>

<p>For further details on UTF-8 and Unicode,
consult <a href="http://www.cl.cam.ac.uk/~mgk25/unicode.html"><em>UTF-8 and Unicode FAQ 
for Unix/Linux</em></a> by <em>Markus Kuhn</em>.</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>

<br/>

<a name="commas"></a>
<h2>22. Commas in parameter lists</h2>

<p>
	Some of the example programs contain functions
	that use a comma to separate the parameters into two groups.
	This is not a special syntax of newLISP,
	but rather a visual trick.
	The comma is a symbol just like any other symbol.
	The parameters after the comma are not required
	when calling the function;
	they simply declare local variables in a convenient way.
	This is possible in newLISP because parameter variables in lambda expressions
	are local and arguments are optional:
</p>
<pre>
(define (my-func a b c , x y z)
    (set 'x &hellip;)
(&hellip;))
</pre>		
<p>
When calling this function, only <tt>a, b</tt>, and <tt>c</tt> are used as parameters.
The others (the comma symbol, <tt>x</tt>, <tt> y</tt>, and <tt>z</tt>) are initialized 
to <tt>nil</tt> and are local to the function.  After execution, the function's contents 
are forgotten and the environment's symbols are restored to their previous values.
</p>
<p>
	For other ways of declaring and initializing local variables,
	see <a href="#let">let</a>, <a href="#letex">letex</a> and
	<a href="#letn">letn</a>.
</p>

<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>


<br/><br/><br/>


<center style="font-size: 150%">
<span class="divider">(&nbsp;<font color="#7ba9d4">&part;</font>&nbsp;)</span>
</center>

<a name="function_ref"></a>
<center><h1>newLISP Function Reference</h1></center>

<br/><br/>

<a name="symbol_names"></a>
<h2>1. Syntax of symbol variables and numbers</h2>

<p>Source code in newLISP is parsed according to the rules outlined here. 
When in doubt, verify the behavior of newLISP's internal parser
by calling <a href="#parse">parse</a> without optional arguments.</p>

<br/>

<h3>Symbols for variable names</h3>
<p>
The following rules apply to the naming of symbols 
used as variables or functions:
</p>


<ol>
<li>Variable symbols should not start with any of the following characters:<br/>
<tt># ; " ' ( )  { } . , 0 1 2 3 4 5 6 7 8 9</tt><br/><br/></li>

<li>Variable symbols starting with a <tt>+</tt> or <tt>-</tt> cannot have a 
number as the second character.<br/><br/></li>

<li>Any character is allowed inside a variable name, except for:<br/>
<tt>" ' ( ) : ,</tt> and the space character. These mark the end of a variable symbol.<br/><br/></li>

<li>A symbol name starting with <tt>[</tt> (left square bracket) and ending with 
<tt>]</tt> (right square bracket) may contain any character except the right square 
bracket.<br/><br/></li>

<li>A symbol name starting with <tt>$</tt> (dollar sign) is global. There are several of these
symbols already <a href="#system_symbols">built into newLISP</a> and set and changed 
internally. This type of global symbol can also be created by the user.
</li>

</ol>


<p>
All of the following symbols are legal variable names in newLISP:
</p>

<!-- example -->

<pre>
myvar
A-name
X34-zz
[* 7 5 ()};]
*111*
</pre>

<p>
Sometimes it is useful to create hash-like <a href="#hash">lookup dictionaries</a>
with keys containing characters that are illegal in newLISP variables. 
The functions <a href="#sym">sym</a> and <a href="#context">context</a> 
can be used to create symbols containing these characters:
</p>

<pre>
(set (sym "(#:L*") 456)  <span class='arw'>&rarr;</span> 456 ; the symbol '(#:L*'

(eval (sym "(#:L*"))  <span class='arw'>&rarr;</span> 456

(set (sym 1) 123)  <span class='arw'>&rarr;</span> 123

(eval (sym 1))  <span class='arw'>&rarr;</span> 123

1        <span class='arw'>&rarr;</span> 1
(+ 1 2)  <span class='arw'>&rarr;</span> 3
</pre>


<p>
The last example creates the symbol <tt>1</tt> 
containing the value <tt>123</tt>. 
Also note that creating such a symbol does not alter newLISP's normal operations, 
since <tt>1</tt> is still parsed as the number one.
</p>

<br/>

<h3>Numbers</h3>

<p>When parsing binary, hex, decimal, float and integer numbers, up to
1000 digits are parsed when present. The rest will be read as new token(s).
Note that IEEE 754 64-bit doubles distinguish only up to 16 significant
digits. If more than 308 digits are present before the decimal point, the
number will convert to <tt>inf</tt> (infinity). For big integers the 1000 
limitation exists only when parsing source. There is no limit when a result 
of big integers math exceeds 1000 digits.</p>

<p>
newLISP recognizes the following number formats:
</p>

<p>
<b>Integers</b> are one or more digits long, 
optionally preceded by a <tt>+</tt> or <tt>-</tt> sign. 
Any other character marks the end of the integer 
or may be part of the sequence 
if parsed as a float (see float syntax below).
</p>

<!-- example -->

<pre>
123
+4567
-999
</pre>

<p>
<b>Big integers</b> can be of unlimited precision and are processed
differently from normal 64-bi integers internally.</p>

<!-- example -->
<pre>
123456789012345678901234567890 ; will automatically be converted to big int
-123L                          ; appended L forces conversion
0L
</pre>

<p>when parsing the command line or programming source, newLISP will
recognise, integers bigger than 64-bit and convert the to big integers.
Smaller numbers can be forced to big integer format by appending the
letter L.</p>


<p>
<b>Hexadecimals</b> start with a <tt>0x</tt> (or <tt>0X</tt>),  
followed by any combination of the hexadecimal digits: 
<tt>0123456789abcdefABCDEF</tt>. 
Any other character ends the hexadecimal number. Only up to 16 hexadecimal digits
are valid and any more digits are ignored.
</p>

<!-- example -->

<pre>
0xFF    <span class='arw'>&rarr;</span>  255
0x10ab  <span class='arw'>&rarr;</span> 4267
0X10CC  <span class='arw'>&rarr;</span> 4300
</pre>

<p>
<b>Binaries</b> start with a <tt>0b</tt> (or <tt>0B</tt>),
followed by up to 64 bits coded with 1's or 0s. Any other character ends the binary number.
Only up to 64 bits are valid and any more bits are ignored.
</p>

<!-- example -->

<pre>
0b101010   <span class='arw'>&rarr;</span>  42
</pre>



<p>
<b>Octals</b> start with an optional <tt>+</tt> (plus) or <tt>-</tt> (minus) sign and a <tt>0</tt> (zero), 
followed by any combination of the octal digits: <tt>01234567</tt>. 
Any other character ends the octal number. Only up to 21 octal digits are valid
and any more digits are ignored.
</p>

<!-- example -->

<pre>
012   <span class='arw'>&rarr;</span>  10
010   <span class='arw'>&rarr;</span>   8
077   <span class='arw'>&rarr;</span>  63
-077  <span class='arw'>&rarr;</span> -63
</pre>



<p>
<b>Floating point</b> numbers can start 
with an optional <tt>+</tt> (plus) or <tt>-</tt> (minus) sign, 
but they cannot be followed by a <tt>0</tt> (zero); 
this would make them octal numbers instead of floating points. 
A single <tt>.</tt> (decimal point) can appear anywhere within
a floating point number, including at the beginning.
</p>

<p>Only 16 digits are siginificant and any more digits are ignored.</p>

<!-- example -->

<pre>
1.23     <span class='arw'>&rarr;</span>  1.23
-1.23    <span class='arw'>&rarr;</span> -1.23
+2.3456  <span class='arw'>&rarr;</span>  2.3456
.506     <span class='arw'>&rarr;</span>  0.506
</pre>


<p>
As described below, <b>scientific notation</b> 
starts with a floating point number
called the <em>significand</em> (or <em>mantissa</em>),
followed by the letter <tt>e</tt> or <tt>E</tt> 
and an integer <em>exponent</em>.
</p>

<!-- example -->

<pre>
1.23e3    <span class='arw'>&rarr;</span>  1230
-1.23E3   <span class='arw'>&rarr;</span> -1230
+2.34e-2  <span class='arw'>&rarr;</span>  0.0234
.506E3    <span class='arw'>&rarr;</span>  506
</pre>



<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="type_ids"></a>
<h2>2. Data types and names in the reference</h2>

<p>To describe the types and names of a function's parameters, 
the following naming convention is used throughout the reference section:</p>


<b>syntax&#058; (format <em>str-format</em> <em>exp-data-1</em> [<em>exp-data-i</em> ... ])</b>



<p>Arguments are represented by symbols formed by the argument's type and name, 
separated by a <tt>-</tt> (hyphen). Here, <em>str-format</em> (a string) and 
<em>exp-data-1</em> (an expression) are named "format" and "data-1", respectively.</p>

<p>Arguments enclosed in brackets <tt>[</tt> and <tt>]</tt> are optional. When
arguments are separated by a vertical <tt>|</tt> then one of them must be chosen.</p>

<h3>array</h3>

<p>An array (constructed with the <a href="#array">array</a> function).</p>

<h3>body</h3>

<p>One or more expressions for evaluation. The expressions are evaluated sequentially 
if there is more than one.</p>


<pre>
1 7.8
nil
(+ 3 4)
"Hi" (+ a b)(print result)
(do-this)(do-that) 123
</pre>


<h3>bool</h3>

<p><tt>true</tt>, <tt>nil</tt>, or an expression evaluating to one of these two.
</p>


<pre>
true, nil, (&lt;= X 10)
</pre>


<h3>context</h3>

<p>An expression evaluating to a context (namespace) or a variable symbol 
holding a context.</p>


<pre>
MyContext, aCtx, TheCTX
</pre>


<h3>exp</h3>

<p>Any data type described in this chapter.</p>

<h3>func</h3>

<p>
	A symbol or an expression evaluating to 
	an operator symbol or lambda expression.
</p>


<pre>
+, add, (first '(add sub)), (lambda (x) (+ x x))
</pre>


<h3>int</h3>

<p>
An integer or an expression evaluating to an integer. 
Generally, if a floating point number is used 
when an int is expected, 
the value is truncated to an integer.
</p>


<pre>
123, 5, (* X 5)
</pre>


<h3>list</h3>
<p>
	A list of elements (any type) 
	or an expression evaluating to a list.
</p>

<pre>
(a b c "hello" (+ 3 4))
</pre>


<h3>num</h3>
<p>
An integer, a floating point number, 
or an expression evaluating to one of these two. 
If an integer is passed, 
it is converted to a floating point number.
</p>

<pre>
1.234, (div 10 3), (sin 1)
</pre>


<h3>matrix</h3>
<p>A list in which each row element is itself a list 
or an array in which each row element is itself an array.
All element lists or arrays (rows) are of the same length.
Any data type can be element of a matrix, but when
using specific matrix operations like <a href="#det">det</a>, 
<A href="#multiply">multiply</A>, or <A href="#invert">invert</A>, 
all numbers must be floats or integers.
</p>

<p>
The dimensions of a matrix are defined 
by indicating the number of rows
and the number of column elements per row. 
Functions working on matrices 
ignore superfluous columns in a row. 
For missing row elements, 
<tt>0.0</tt> is assumed by the functions 
<a href="#det">det</a>, <a href="#multiply">multiply</a>, 
and <a href="#invert">invert</a>, 
while <a href="#transpose">transpose</a> assumes <tt>nil</tt>.
Special rules apply for <a href="#transpose">transpose</a> 
when a whole row is not a list or an array, 
but some other data type.
</p>


<pre>
((1  2  3  4)
(5  6  7  8)
(9 10 11 12))        ; 3 rows 4 columns
		   
((1 2) (3 4) (5 6))  ; 3 rows 2 columns
</pre>


<h3>place</h3>

<p>A place referenced by a symbol or a place defined in a list, array 
or string by indexing with <a href="#nth">nth</a> or <a href="#indexing">implicit indexing</a>
or a place referenced by functions like <a href="#first">first</a>, <a href="#last">last</a>,
<a href="#assoc">assoc</a> or <a href="#lookup">lookup</a>.</p>


<h3>str</h3>

<p>A string or an expression that evaluates to a string.</p>

<p>Depending on the length and processing of special characters, strings are delimited
by either quotes <tt>""</tt>, braces <tt>{}</tt> or <tt>[text][/text]</tt> tags.</p>

<p>Strings limited by either quotes <tt>""</tt> or braces <tt>{}</tt> must not exceed
2047 characters. Longer strings should be limited by <tt>[text][/text]</tt> tags for
unlimited text length.</p>

<pre>
"Hello", (append first-name  " Miller")
</pre>


<p>
Special characters can be included in quoted strings 
by placing a <tt>\</tt> (backslash) before the character or 
digits to escape them:</p>

<table width="98%"  summary="escaping special characters in strings">
<tr align="left" valign="bottom"><th>character</th><th>description</th></tr>
<tr><td><tt>\"</tt></td><td>for a double quote inside a quoted string</td></tr>
<tr><td><tt>\n</tt></td> <td>the line-feed character (ASCII 10)</td></tr>
<tr> <td><tt>\r</tt></td> <td>the carriage return character (ASCII 13)</td> </tr>
<tr><td><tt>\b</tt></td><td>for a backspace BS character (ASCII 8)</td></tr>
<tr><td><tt>\t</tt></td><td>for a TAB character (ASCII 9)</td></tr>
<tr><td><tt>\f</tt></td><td>for a formfeed FF character (ASCII 12)</td></tr>
<tr><td><tt>\nnn</tt></td> <td>a decimal ASCII code where nnn is between 000 and 255</td></tr>
<tr><td><tt>\xnn</tt></td> <td>a hexadecimal code where nn is between 00 and FF</td></tr>
<tr><td><tt>\unnnn</tt></td><td>a unicode character encoded in the four <tt>nnnn</tt> hexadecimal
digits. When reading a quoted string, newLISP will translate 
this to a UTF8 character in the UTF8 enabled versions of newLISP.</td></tr>
<tr><td><tt>\\</tt></td> <td>the backslash character itself</td></tr>
</table><br/>

<p>Decimals start with a digit. Hexadecimals start with <tt>x</tt>:</p>

<pre>
"\065\066\067" <span class='arw'>&rarr;</span> "ABC"
"\x41\x42\x43" <span class='arw'>&rarr;</span> "ABC"
</pre>


<p>Instead of a <tt>"</tt> (double quote), a <tt>{</tt> (left curly bracket) 
and <tt>}</tt> (right curly bracket) can be used to delimit strings.
This is useful when quotation marks need to occur inside strings. 
Quoting with the curly brackets suppresses the backslash escape effect 
for special characters. Balanced nested curly brackets may be used within 
a string.  This aids in writing regular expressions or short sections of 
HTML.</p>


<pre>
(print "&lt;A href=\"http://mysite.com\"&gt;" ) ; the cryptic way

(print {&lt;A href="http://mysite.com"&gt;} )   ; the readable way


; path names on MS Windows

(set 'path "C:\\MyDir\\example.lsp")

; no escaping when using braces

(set 'path {C:\MyDir\example.lsp})

; on MS Windows the forward slash can be used in path names

(set 'path "C:/MyDir/example.lsp")

; inner braces are balanced
(regex {abc{1,2}} line) 

(print [text]
  this could be
  a very long (&gt; 2048 characters) text,
  i.e. HTML.
[/text])
</pre>


<p>
	The tags <tt>[text]</tt> and <tt>[/text]</tt> 
	can be used to delimit long strings 
	and suppress escape character translation. 
	This is useful for delimiting long HTML passages 
	in CGI files written in newLISP 
	or for situations where character translation 
	should be completely suppressed. 
	Always use the <tt>[text]</tt> tags 
	for strings longer than 2048 characters.
</p>

<h3>sym</h3>

<p>
	A symbol or expression evaluating to a symbol.
</p>


<pre>
'xyz, (first '(+ - /)), '*, '- , someSymbol,
</pre>



<p>Most of the context symbols in this manual start with an uppercase letter 
to distinguish them from other symbols.</p>

<h3>sym-context</h3>
<p>
A symbol, an existing context, or an expression evaluating to a symbol 
from which a context will be created.  If a context does not already exist, 
many functions implicitly create them 
(e.g., <a href="#bayes-train">bayes-train</a>, <a Href="#context">context</a>, 
<a href="#eval-string">eval-string</a>, 
<a href="#load">load</a>, <a href="#sym">sym</a>, and <a href="#xml-parse">xml-parse</a>).
The context must be specified when these functions are used 
on an existing context.  Even if a context already exists, 
some functions may continue to take quoted symbols (e.g., <a href="#context">context</a>).
For other functions, such as <a href="#contextp">context?</a>, the distinction is critical.
</p>


<br/>



<br/>
<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>
<br/>

<a name="functions"></a>
<h2>3. Functions in groups</h2>

<p>Some functions appear in more than one group.</p>

<a name="list_processing"></a>
<h3>List processing, flow control, and integer arithmetic</h3>

<table border="0" cellpadding="1" width="95%" align="center" summary="List processing, flow control and integer
arithmetic">

<tr>
<td WIDTH="16%"><a href="#arithmetic">+, -, *, /, %</a></td>
<td WIDTH="80%">integer arithmetic</td>
</tr>

<tr>
<td><a href="#inci">++</a></td>
<td>increment integer numbers</td>
</tr>

<tr>
<td><a href="#deci">--</a></td>
<td>decrement integer numbers</td>
</tr>

<tr>
<td><a href="#logical">&lt;, &gt;, =</a></td>
<td>compares any data type: less, greater, equal</td>
</tr>

<tr>
<td><a href="#logical">&lt;=, &gt;=, !=</a></td>
<td>compares any data type: less-equal, greater-equal, not-equal</td>
</tr>

<tr>
<td><a href="#colon">:</a></td>
<td>constructs a context symbol and applies it to an object</td>
</tr>

<tr>
<td><a href="#and">and</a></td>
<td>logical <tt>and</tt></td>
</tr>

<tr>
<td><a href="#append">append</a></td>
<td>appends lists ,arrays or strings to form a new list, array or string</td>
</tr>

<tr>
<td><a href="#apply">apply</a></td>
<td>applies a function or primitive to a list of arguments</td>
</tr>

<tr>
<td><a href="#args">args</a></td>
<td>retrieves the argument list of a function or macro expression</td>
</tr>

<tr>
<td><a href="#assoc">assoc</a></td>
<td>searches for keyword associations in a list</td>
</tr>

<tr>
<td><a href="#begin">begin</a></td>
<td>begins a block of functions</td>
</tr>

<tr>
<td><a href="#bigint">bigint</a></td>
<td>convert a number to big integer format</td>
</tr>

<tr>
<td><a href="#bind">bind</a></td>
<td>binds variable associations in a list</td>
</tr>

<tr>
<td><a href="#case">case</a></td>
<td>branches depending on contents of control variable</td>
</tr>

<tr>
<td><a href="#catch">catch</a></td>
<td>evaluates an expression, possibly catching errors</td>
</tr>

<tr>
<td><a href="#chop">chop</a></td>
<td>chops elements from the end of a list</td>
</tr>

<tr>
<td><a href="#clean">clean</a></td>
<td>cleans elements from a list</td>
</tr>

<tr>
<td><a href="#collect">collect</a></td>
<td>repeat evaluating an expression and collect results in a list</td>
</tr>

<tr>
<td><a href="#cond">cond</a></td>
<td>branches conditionally to expressions</td>
</tr>

<tr>
<td><a href="#cons">cons</a></td>
<td>prepends an element to a list, making a new list</td>
</tr>

<tr>
<td><a href="#constant">constant</a></td>
<td>defines a constant symbol</td>
</tr>

<tr>
<td><a href="#count">count</a></td>
<td>counts elements of one list that occur in another list</td>
</tr>

<tr>
<td><a href="#curry">curry</a></td>
<td>transforms a function f(x, y) into a function fx(y)</td>
</tr>

<tr>
<td><a href="#define">define</a></td>
<td>defines a new function or lambda expression</td>
</tr>

<tr>
<td><a href="#define-macro">define-macro</a></td>
<td>defines a macro or lambda-macro expression</td>
</tr>

<tr>
<td><a href="#def-new">def-new</a></td>
<td>copies a symbol to a different context (namespace)</td>
</tr>

<tr>
<td><a href="#difference">difference</a></td>
<td>returns the difference between two lists</td>
</tr>

<tr>
<td><a href="#doargs">doargs</a></td>
<td>iterates through the arguments of a function</td>
</tr>

<tr>
<td><a href="#dolist">dolist</a></td>
<td>evaluates once for each element in a list</td>
</tr>

<tr>
<td><a href="#dostring">dostring</a></td>
<td>evaluates once for each character in a string</td>
</tr>

<tr>
<td><a href="#dotimes">dotimes</a></td>
<td>evaluates once for each number in a range</td>
</tr>

<tr>
<td><a href="#dotree">dotree</a></td>
<td>iterates through the symbols of a context</td>
</tr>

<tr>
<td><a href="#do-until">do-until</a></td>
<td>repeats evaluation of an expression until the condition is met</td>
</tr>

<tr>
<td><a href="#do-while">do-while</a></td>
<td>repeats evaluation of an expression while the condition is true</td>
</tr>

<tr>
<td><a href="#dup">dup</a></td>
<td>duplicates a list or string a specified number of times</td>
</tr>

<tr>
<td><a href="#ends-with">ends-with</a></td>
<td>checks the end of a string or list against a key of the same type</td>
</tr>

<tr>
<td><a href="#eval">eval</a></td>
<td>evaluates an expression</td>

</tr>
<tr>
<td><a href="#exists">exists</a></td>
<td>checks for the existence of a condition in a list</td>
</tr>

<tr>
<td><a href="#expand">expand</a></td>
<td>replaces a symbol in a nested list</td>
</tr>

<tr>
<td><a href="#explode">explode</a></td>
<td>explodes a list or string</td>
</tr>

<tr>
<td><a href="#extend">extend</a></td>
<td>extends a list or string</td>
</tr>

<tr>
<td><a href="#first">first</a></td>
<td>gets the first element of a list or string</td>
</tr>

<tr>
<td><a href="#filter">filter</a></td>
<td>filters a list</td>
</tr>

<tr>
<td><a href="#find">find</a></td>
<td>searches for an element in a list or string</td>
</tr>

<tr>
<td><a href="#flat">flat</a></td>
<td>returns the flattened list</td>
</tr>

<tr>
<td><a href="#define">fn</a></td>
<td>defines a new function or lambda expression</td>
</tr>

<tr>
<td><a href="#for">for</a></td>
<td>evaluates once for each number in a range</td>
</tr>

<tr>
<td><a href="#for-all">for-all</a></td>
<td>checks if all elements in a list meet a condition</td>
</tr>

<tr>
<td><a href="#if">if</a></td>
<td>evaluates an expression conditionally</td>
</tr>

<tr>
<td><a href="#index">index</a></td>
<td>filters elements from a list and returns their indices</td>
</tr>


<tr>
<td><a href="#intersect">intersect</a></td>
<td>returns the intersection of two lists</td>
</tr>

<tr>
<td><a href="#define">lambda</a></td>
<td>defines a new function or lambda expression</td>
</tr>

<tr>
<td><a href="#last">last</a></td>
<td>returns the last element of a list or string</td>
</tr>

<tr>
<td><a href="#length">length</a></td>
<td>calculates the length of a list or string</td>
</tr>

<tr>
<td><a href="#let">let</a></td>
<td>declares and initializes local variables</td>
</tr>

<tr>
<td><a href="#letex">letex</a></td>
<td>expands local variables into an expression, then evaluates</td>
</tr>

<tr>
<td><a href="#letn">letn</a></td>
<td>initializes local variables incrementally, like nested lets</td>
</tr>

<tr>
<td><a href="#list">list</a></td>
<td>makes a list</td>
</tr>

<tr>
<td><a href="#local">local</a></td>
<td>declares local variables</td>
</tr>

<tr>
<td><a href="#lookup">lookup</a></td>
<td>looks up members in an association list</td>
</tr>

<tr>
<td><a href="#map">map</a></td>
<td>maps a function over members of a list, collecting the results</td>
</tr>

<tr>
<td><a href="#match">match</a></td>
<td>matches patterns against lists; for matching against strings, see 
<a href="#find">find</a> and <a href="#regex">regex</a></td>
</tr>

<tr>
<td><a href="#member">member</a></td>
<td>finds a member of a list or string</td>
</tr>

<tr>
<td><a href="#not">not</a></td>
<td>logical <tt>not</tt></td>
</tr>

<tr>
<td><a href="#nth">nth</a></td>
<td>gets the <em>nth</em> element of a list or string</td>
</tr>

<tr>
<td><a href="#or">or</a></td>
<td>logical <tt>or</tt></td>
</tr>

<tr>
<td><a href="#pop">pop</a></td>
<td>deletes and returns an element from a list or string</td>
</tr>

<tr>
<td><a href="#pop-assoc">pop-assoc</a></td>
<td>removes an association from an association list</td>
</tr>

<tr>
<td><a href="#push">push</a></td>
<td>inserts a new element into a list or string</td>
</tr>

<tr>
<td><a href="#quote">quote</a></td>
<td>quotes an expression</td>
</tr>

<tr>
<td><a href="#ref">ref</a></td>
<td>returns the position of an element inside a nested list</td>
</tr>

<tr>
<td><a href="#ref-all">ref-all</a></td>
<td>returns a list of index vectors of elements inside a nested list</td>
</tr>

<tr>
<td><a href="#rest">rest</a></td>
<td>returns all but the first element of a list or string</td>
</tr>

<tr>
<td><a href="#replace">replace</a></td>
<td>replaces elements inside a list or string</td>
</tr>

<tr>
<td><a href="#reverse">reverse</a></td>
<td>reverses a list or string</td>
</tr>

<tr>
<td><a href="#rotate">rotate</a></td>
<td>rotates a list or string</td>
</tr>

<tr>
<td><a href="#select">select</a></td>
<td>selects and permutes elements from a list or string</td>
</tr>

<tr>
<td><a href="#self">self</a></td>
<td>Accesses the target object inside a FOOP method</td>
</tr>

<tr>
<td><a href="#set">set</a></td>
<td>sets the binding or contents of a symbol</td>
</tr>

<tr>
<td><a href="#setf">setf setq</a></td>
<td>sets contents of a symbol or list, array or string reference</td>
</tr>

<tr>
<td><a href="#set-ref">set-ref</a></td>
<td>searches for an element in a nested list and replaces it</td>
</tr>

<tr>
<td><a href="#set-ref-all">set-ref-all</a></td>
<td>searches for an element in a nested list and replaces all instances</td>
</tr>

<tr>
<td><a href="#silent">silent</a></td>
<td>works like <a href="#begin">begin</a> but suppresses console output of the return value</td>
</tr>

<tr>
<td><a href="#slice">slice</a></td>
<td>extracts a sublist or substring</td>
</tr>

<tr>
<td><a href="#sort">sort</a></td>
<td>sorts the members of a list</td>
</tr>

<tr>
<td><a href="#starts-with">starts-with</a></td>
<td>checks the beginning of a string or list against a key of the same type</td>
</tr>

<tr>
<td><a href="#swap">swap</a></td>
<td>swaps two elements inside a list or string</td>
</tr>

<tr>
<td><a href="#unify">unify</a></td>
<td>unifies two expressions</td>
</tr>

<tr>
<td><a href="#unique">unique</a></td>
<td>returns a list without duplicates</td>
</tr>

<tr>
<td><a href="#union">union</a></td>
<td>returns a unique list of elements found in two or more lists.</td>
</tr>

<tr>
<td><a href="#unless">unless</a></td>
<td>evaluates an expression conditionally</td>
</tr>

<tr>
<td><a href="#until">until</a></td>
<td>repeats evaluation of an expression until the condition is met</td>
</tr>

<tr>
<td><a href="#when">when</a></td>
<td>evaluates a block of statements conditionally</td>
</tr>

<tr>
<td><a href="#while">while</a></td>
<td>repeats evaluation of an expression while the condition is true</td>
</tr>
</table><br/>

<a name="string_operators"></a>
<h3>String and conversion functions</h3>

<table border="0" cellpadding="1" width="95%" align="center" summary="String and conversion functions">


<tr>
<td WIDTH="16%"><a href="#address">address</a></td>
<td WIDTH="84%">gets the memory address of a number or string</td>
</tr>

<tr>
<td><a href="#bigint">bigint</a></td>
<td>convert a number to big integer format</td>
</tr>

<tr>
<td><a href="#bits">bits</a></td>
<td>translates a number into binary representation</td>
</tr>

<tr>
<td><a href="#char">char</a></td>
<td>translates between characters and ASCII codes</td>
</tr>

<tr>
<td><a href="#chop">chop</a></td>
<td>chops off characters from the end of a string</td>
</tr>

<tr>
<td><a href="#dostring">dostring</a></td>
<td>evaluates once for each character in a string</td>
</tr>

<tr>
<td><a href="#dup">dup</a></td>
<td>duplicates a list or string a specified number of times</td>
</tr>

<tr>
<td><a href="#ends-with">ends-with</a></td>
<td>checks the end of a string or list against a key of the same type</td>
</tr>

<tr>
<td><a href="#encrypt">encrypt</a></td>
<td>does a one-time&ndash;pad encryption and decryption of a string</td>
</tr>

<tr>
<td><a href="#eval-string">eval-string</a></td>
<td>compiles, then evaluates a string</td>
</tr>

<tr>
<td><a href="#explode">explode</a></td>
<td>transforms a string into a list of characters</td>
</tr>

<tr>
<td><a href="#extend">extend</a></td>
<td>extends a list or string</td>
</tr>

<tr>
<td><a href="#find">find</a></td>
<td>searches for an element in a list or string</td>
</tr>

<tr>
<td><a href="#find-all">find-all</a></td>
<td>returns a list of all pattern matches found in string</td>
</tr>

<tr>
<td><a href="#first">first</a></td>
<td>gets the first element in a list or string</td>
</tr>

<tr>
<td><a href="#float">float</a></td>
<td>translates a string or integer into a floating point number</td>
</tr>

<tr>
<td><a href="#format">format</a></td>
<td>formats numbers and strings as in the C language</td>
</tr>

<tr>
<td><a href="#get-char">get-char</a></td>

<td>gets a character from a memory address</td>
</tr>

<tr>
<td><a href="#get-float">get-float</a></td>
<td>gets a double float from a memory address</td>
</tr>

<tr>
<td><a href="#get-int">get-int</a>&nbsp;&nbsp;</td>
<td>gets a 32-bit integer from a memory address</td>
</tr>

<tr>
<td><a href="#get-long">get-long</a>&nbsp;&nbsp;</td>
<td>gets a long 64-bit integer from a memory address</td>
</tr>

<tr>
<td><a href="#get-string">get-string</a></td>
<td>gets a string from a memory address</td>
</tr>

<tr>
<td><a href="#int">int</a></td>
<td>translates a string or float into an integer</td>
</tr>

<tr>
<td><a href="#join">join</a></td>
<td>joins a list of strings</td>
</tr>

<tr>
<td><a href="#last">last</a></td>
<td>returns the last element of a list or string</td>
</tr>

<tr>
<td><a href="#lower-case">lower-case</a></td>
<td>converts a string to lowercase characters</td>
</tr>

<tr>
<td><a href="#member">member</a></td>
<td>finds a list or string member</td>
</tr>

<tr>
<td><a href="#name">name</a></td>
<td>returns the name of a symbol or its context as a string</td>
</tr>

<tr>
<td><a href="#nth">nth</a></td>
<td>gets the <em>nth</em> element in a list or string</td>
</tr>

<tr>
<td><a href="#pack">pack</a></td>
<td>packs newLISP expressions into a binary structure</td>
</tr>

<tr>
<td><a href="#parse">parse</a></td>
<td>breaks a string into tokens</td>
</tr>

<tr>
<td><a href="#pop">pop</a></td>
<td>pops from a string</td>
</tr>

<tr>
<td><a href="#push">push</a></td>
<td>pushes onto a string</td>
</tr>

<tr>
<td><a href="#regex">regex</a></td>
<td>performs a Perl-compatible regular expression search</td>
</tr>

<tr>
<td><a href="#regex-comp">regex-comp</a></td>
<td>pre-compiles a regular expression pattern</td>
</tr>

<tr>
<td><a href="#replace">replace</a></td>
<td>replaces elements in a list or string</td>

</tr>

<tr>
<td><a href="#rest">rest</a></td>
<td>gets all but the first element of a list or string</td>
</tr>

<tr>
<td><a href="#reverse">reverse</a></td>
<td>reverses a list or string</td>
</tr>

<tr>
<td><a href="#rotate">rotate</a></td>
<td>rotates a list or string</td>
</tr>

<tr>
<td><a href="#select">select</a></td>
<td>selects and permutes elements from a list or string</td>
</tr>

<tr>
<td><a href="#setf">setf setq</a></td>
<td>sets contents of a string reference</td>
</tr>

<tr>
<td><a href="#slice">slice</a></td>
<td>extracts a substring or sublist</td>
</tr>

<tr>
<td><a href="#source">source</a></td>
<td>returns the source required to bind a symbol as a string</td>
</tr>

<tr>
<td><a href="#starts-with">starts-with</a></td>
<td>checks the start of the string or list against a key string or list</td>
</tr>

<tr>
<td><a href="#string">string</a></td>
<td>transforms anything into a string</td>
</tr>

<tr>
<td><a href="#sym">sym</a></td>
<td>translates a string into a symbol</td>
</tr>

<tr>
<td><a href="#title-case">title-case</a></td>

<td>converts the first character of a string to uppercase</td>
</tr>

<tr>
<td><a href="#trim">trim</a></td>
<td>trims a string on one or both sides</td>
</tr>

<tr>
<td><a href="#unicode">unicode</a></td>
<td>converts ASCII or UTF-8 to UCS-4 Unicode</td>
</tr>

<tr>
<td><a href="#utf8">utf8</a></td>
<td>converts UCS-4 Unicode to UTF-8</td>
</tr>

<tr>
<td><a href="#utf8len">utf8len</a></td>
<td>returns length of an UTF-8 string in UTF-8 characters</td>
</tr>


<tr>
<td><a href="#unpack">unpack</a></td>
<td>unpacks a binary structure into newLISP expressions</td>
</tr>

<tr>

<td><a href="#upper-case">upper-case</a></td>
<td>converts a string to uppercase characters</td>
</tr>

</table><br/>

<a name="floating_point"></a>
<h3>Floating point math and special functions</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="Floating point math and special functions">


<tr>
<td WIDTH="16%"><a href="#abs">abs</a></td>
<td WIDTH="84%">returns the absolute value of a number</td>
</tr>

<tr>
<td><a href="#acos">acos</a></td>
<td>calculates the arc-cosine of a number</td>
</tr>

<tr>
<td><a href="#acosh">acosh</a></td>
<td>calculates the inverse hyperbolic cosine of a number</td>
</tr>

<tr>
<td><a href="#add">add</a></td>
<td>adds floating point or integer numbers and returns a floating point number</td>
</tr>

<tr>
<td><a href="#array">array</a></td>
<td>creates an array</td>
</tr>

<tr>
<td><a href="#array-list">array-list</a></td>
<td>returns a list conversion from an array</td>
</tr>

<tr>
<td><a href="#asin">asin</a></td>
<td>calculates the arcsine of a number</td>
</tr>

<tr>
<td><a href="#asinh">asinh</a></td>
<td>calculates the inverse hyperbolic sine of a number</td>
</tr>


<tr>
<td><a href="#atan">atan</a></td>
<td>calculates the arctangent of a number</td>
</tr>

<tr>
<td><a href="#atanh">atanh</a></td>
<td>calculates the inverse hyperbolic tangent of a number</td>
</tr>

<tr>
<td><a href="#atan2">atan2</a></td>
<td>computes the principal value of the arctangent of Y / X in radians</td>
</tr>

<tr>
<td><a href="#beta">beta</a></td>
<td>calculates the beta function</td>
</tr>

<tr>
<td><a href="#betai">betai</a></td>
<td>calculates the incomplete beta function</td>
</tr>

<tr>
<td><a href="#binomial">binomial</a></td>
<td>calculates the binomial function</td>
</tr>

<tr>
<td><a href="#ceil">ceil</a></td>

<td>rounds up to the next integer</td>
</tr>

<tr>
<td><a href="#cos">cos</a></td>
<td>calculates the cosine of a number</td>

</tr>
<tr>
<td><a href="#cosh">cosh</a></td>
<td>calculates the hyperbolic cosine of a number</td>
</tr>

<tr>
<td><a href="#crc32">crc32</a></td>
<td>calculates a 32-bit CRC for a data buffer</td>
</tr>

<tr>
<td><a href="#dec">dec</a></td>
<td>decrements a number in a variable, list or array</td>
</tr>

<tr>
<td><a href="#div">div</a></td>
<td>divides floating point or integer numbers</td>
</tr>

<tr>
<td><a href="#erf">erf</a></td>
<td>calculates the error function of a number</td>
</tr>

<tr>
<td><a href="#exp">exp</a></td>
<td>calculates the exponential <em>e</em> of a number</td>
</tr>

<tr>
<td><a href="#factor">factor</a></td>
<td>factors a number into primes</td>
</tr>

<tr>
<td><a href="#fft">fft</a></td>
<td>performs a fast Fourier transform (FFT)</td>
</tr>

<tr>
<td><a href="#floor">floor</a></td>
<td>rounds down to the next integer</td>
</tr>

<tr>
<td><a href="#flt">flt</a></td>
<td>converts a number to a 32-bit integer representing a float</td>
</tr>

<tr>
<td><a href="#gammai">gammai</a></td>
<td>calculates the incomplete Gamma function</td>
</tr>

<tr>
<td><a href="#gammaln">gammaln</a></td>
<td>calculates the log Gamma function</td>
</tr>

<tr>
<td><a href="#gcd">gcd</a></td>
<td>calculates the greatest common divisor of a group of integers</td>
</tr>

<tr>
<td><a href="#ifft">ifft</a></td>
<td>performs an inverse fast Fourier transform (IFFT)</td>
</tr>

<tr>
<td><a href="#inc">inc</a></td>
<td>increments a number in a variable, list or array</td>
</tr>

<tr>
<td><a href="#infp">inf?</a></td>
<td>checks if a floating point value is infinite</td>
</tr>

<tr>
<td><a href="#log">log</a></td>
<td>calculates the natural or other logarithm of a number</td>
</tr>

<tr>
<td><a href="#min">min</a></td>
<td>finds the smallest value in a series of values</td>
</tr>

<tr>
<td><a href="#max">max</a></td>
<td>finds the largest value in a series of values</td>

</tr>

<tr>
<td><a href="#mod">mod</a></td>
<td>calculates the modulo of two numbers</td>
</tr>

<tr>
<td><a href="#mul">mul</a></td>
<td>multiplies floating point or integer numbers</td>
</tr>

<tr>
<td><a href="#NaNp">NaN?</a></td>
<td>checks if a float is NaN (not a number)</td>
</tr>

<tr>
<td><a href="#round">round</a></td>
<td>rounds a number</td>
</tr>

<tr>
<td><a href="#pow">pow</a></td>
<td>calculates <em>x</em> to the power of <em>y</em></td>
</tr>

<tr>
<td><a href="#sequence">sequence</a></td>
<td>generates a list sequence of numbers</td>
</tr>

<tr>
<td><a href="#series">series</a></td>

<td>creates a geometric sequence of numbers</td>
</tr>

<tr>
<td><a href="#sgn">sgn</a></td>
<td>calculates the signum function of a number</td>
</tr>

<tr>
<td><a href="#sin">sin</a></td>
<td>calculates the sine of a number</td>
</tr>

<tr>
<td><a href="#sinh">sinh</a></td>
<td>calculates the hyperbolic sine of a number</td>
</tr>

<tr>
<td><a href="#sqrt">sqrt</a></td>
<td>calculates the square root of a number</td>
</tr>

<tr>
<td><a href="#ssq">ssq</a></td>
<td>calculates the sum of squares of a vector</td>
</tr>

<tr>
<td><a href="#sub">sub</a></td>
<td>subtracts floating point or integer numbers</td>
</tr>

<tr>
<td><a href="#tan">tan</a></td>
<td>calculates the tangent of a number</td>
</tr>

<tr>
<td><a href="#tanh">tanh</a></td>
<td>calculates the hyperbolic tangent of a number</td>
</tr>

<tr>
<td><a href="#uuid">uuid</a>&nbsp;</td>
<td>returns a UUID (Universal Unique IDentifier)</td>
</tr>

</table><br/>

<a name="matrices"></a>
<h3>Matrix functions</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="Matrix functions">

<tr>
<td  WIDTH="16%"><a href="#det">det</a></td>
<td  WIDTH="84%">returns the determinant of a matrix</td>
</tr>

<tr>
<td><a href="#invert">invert</a></td>
<td>returns the inversion of a matrix</td>
</tr>

<tr>
<td><a href="#mat">mat</a></td>
<td>performs scalar operations on matrices</td>
</tr>

<tr>
<td><a href="#multiply">multiply</a></td>
<td>multiplies two matrices</td>
</tr>

<tr>
<td><a href="#transpose">transpose</a>&nbsp;</td>
<td>returns the transposition of a matrix</td>
</tr>

</table><br/>

<a name="array-funcs"></a>
<h3>Array functions</h3>

<table border="0" cellpadding="1" width="95%" align="center" summary="Array functions">

<tr>
<td width="16%" ><a href="#append">append</a></td>
<td width="84%">appends arrays</td>
</tr>

<tr>
<td><a href="#array">array</a></td>
<td>creates and initializes an array with up to 16 dimensions</td>
</tr>

<tr>
<td><a href="#array-list">array-list</a></td>
<td>converts an array into a list</td>
</tr>

<tr>
<td><a href="#arrayp">array?</a></td>
<td>checks if expression is an array</td>
</tr>

<tr>
<td ><a href="#det">det</a></td>
<td>returns the determinant of a matrix</td>
</tr>

<tr>
<td ><a href="#first">first</a></td>
<td>returns the first row of an array</td>
</tr>

<tr>
<td ><a href="#invert">invert</a></td>
<td>returns the inversion of a matrix</td>
</tr>

<tr>
<td ><a href="#last">last</a></td>
<td>returns the last row of an array</td>
</tr>

<tr>
<td><a href="#mat">mat</a></td>
<td>performs scalar operations on matrices</td>
</tr>


<tr>
<td ><a href="#multiply">multiply</a></td>
<td>multiplies two matrices</td>
</tr>

<tr>
<td ><a href="#nth">nth</a></td>
<td>returns an element of an array</td>
</tr>

<tr>
<td ><a href="#rest">rest</a></td>
<td>returns all but the first row of an array</td>
</tr>

<tr>
<td><a href="#setf">setf</a></td>
<td>sets contents of an array reference</td>
</tr>

<tr>
<td ><a href="#slice">slice</a></td>
<td>returns a slice of an array</td>
</tr>

<tr>
<td ><a href="#transpose">transpose</a></td>
<td>transposes a matrix</td>
</tr>
</table><br/>

<a name="bit_operators"></a>
<h3>Bit operators</h3>

<table border="0" cellpadding="1" width="95%" align="center" summary="bit operators">


<tr>
<td WIDTH="16%"><a href="#bit_shift">&lt;&lt;, &gt;&gt;</a>&nbsp;&nbsp;&nbsp;</td>
<td WIDTH="84%">bit shift left, bit shift right</td>
</tr>

<tr>
<td><a href="#bit_and">&amp;</a></td>
<td>bitwise and</td>
</tr>

<tr>
<td><a href="#bit_inclusive">|</a></td>
<td>bitwise inclusive or</td>

</tr>

<tr>
<td><a href="#bit_exclusive">^</a></td>
<td>bitwise exclusive or</td>
</tr>

<tr>
<td><a href="#bit_not">~</a></td>
<td>bitwise not</td>
</tr>
</table><br/>

<a name="predicates"></a>
<h3>Predicates</h3>

<table border="0" cellpadding="1" width="95%" align="center" summary="Predicates">

<tr>
<td width="16%"><a href="#atomp">atom?</a></td>
<td width="84%">checks if an expression is an atom</td>
</tr>

<tr>
<td><a href="#arrayp">array?</a></td>
<td>checks if an expression is an array</td>
</tr>

<tr>
<td><a href="#bigintp">bigint?</a></td>
<td>checks if a number is a big integer</td>
</tr>

<tr>
<td><a href="#contextp">context?</a></td>
<td>checks if an expression is a context</td>
</tr>

<tr>
<td><a href="#directoryp">directory?</a></td>
<td>checks if a disk node is a directory</td>
</tr>

<tr>
<td><a href="#emptyp">empty?</a></td>
<td>checks if a list or string is empty</td>
</tr>

<tr>
<td><a href="#evenp">even?</a></td>
<td>checks the parity of an integer number</td>
</tr>

<tr>
<td><a href="#filep">file?</a></td>
<td>checks if a file exists</td>
</tr>

<tr>
<td><a href="#floatp">float?</a></td>
<td>checks if an expression is a float</td>
</tr>

<tr>
<td><a href="#globalp">global?</a></td>
<td>checks if a symbol is global</td>
</tr>

<tr>
<td><a href="#infp">inf?</a></td>
<td>checks if a floating point value is infinite</td>
</tr>

<tr>
<td><a href="#integerp">integer?</a></td>
<td>checks if an expression is an integer</td>
</tr>

<tr>
<td><a href="#lambdap">lambda?</a></td>
<td>checks if an expression is a lambda expression</td>
</tr>

<tr>
<td><a href="#legalp">legal?</a></td>
<td>checks if a string contains a legal symbol</td>
</tr>

<tr>
<td><a href="#listp">list?</a></td>
<td>checks if an expression is a list</td>
</tr>

<tr>
<td><a href="#macrop">macro?</a></td>
<td>checks if an expression is a lambda-macro expression</td>
</tr>

<tr>
<td><a href="#NaNp">NaN?</a></td>
<td>checks if a float is NaN (not a number)</td>
</tr>

<tr>
<td><a href="#nilp">nil?</a></td>
<td>checks if an expression is <tt>nil</tt></td>
</tr>

<tr>
<td><a href="#nullp">null?</a></td>
<td>checks if an expression is <tt>nil</tt>, <tt>""</tt>, <tt>()</tt>, <tt>0</tt> or <tt>0.0</tt></td>
</tr>

<tr>
<td><a href="#numberp">number?</a></td>
<td>checks if an expression is a float or an integer</td>
</tr>

<tr>
<td><a href="#oddp">odd?</a></td>
<td>checks the parity of an integer number</td>
</tr>

<tr>
<td><a href="#protectedp">protected?</a></td>
<td>checks if a symbol is protected</td>
</tr>

<tr>
<td><a href="#primitivep">primitive?</a></td>
<td>checks if an expression is a primitive</td>
</tr>

<tr>
<td><a href="#quotep">quote?</a></td>
<td>checks if an expression is quoted</td>
</tr>

<tr>
<td><a href="#stringp">string?</a></td>
<td>checks if an expression is a string</td>
</tr>

<tr>
<td><a href="#symbolp">symbol?</a></td>
<td>checks if an expression is a symbol</td>
</tr>

<tr>
<td><a href="#truep">true?</a></td>
<td>checks if an expression is not <tt>nil</tt></td>
</tr>

<tr>
<td><a href="#zerop">zero?</a></td>
<td>checks if an expression is <tt>0</tt> or <tt>0.0</tt></td>
</tr>

</table><br/>

<a name="timedate"></a>
<h3>Date and time functions</h3>

<table border="0" cellpadding="1" width="95%" align="center" summary="Time and date functions">

<tr>
<td WIDTH="16%"><a href="#date">date</a></td>
<td WIDTH="84%">converts a date-time value to a string</td>
</tr>

<tr>
<td><a href="#date-list">date-list</a></td>
<td>returns a list of year, month, day, hours, minutes, seconds from a time value in seconds</td>
</tr>

<tr>
<td><a href="#date-parse">date-parse</a></td>
<td>parses a date string and returns the number of seconds passed since January 1, 1970, (formerly <tt>parse-date</tt>)</td>
</tr>

<tr>
<td><a href="#date-value">date-value</a></td>
<td>calculates the time in seconds since January 1, 1970 for a date and time</td>
</tr>

<tr>
<td><a href="#now">now</a></td>
<td>returns a list of current date-time information</td>
</tr>

<tr>
<td><a href="#time">time</a></td>
<td>calculates the time it takes to evaluate an expression in milliseconds</td>
</tr>

<tr>
<td><a href="#time-of-day">time-of-day</a></td>
<td>calculates the number of milliseconds elapsed since the day started</td>
</tr>

</table><br/>

<a name="montecarlo"></a>
<h3>Statistics, simulation and modeling functions</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="Statistics, simulation and modelling math functions">


<tr>
<td width="16%"><a href="#amb">amb</a></td>
<td width="84%">randomly selects an argument and evaluates it</td>
</tr>

<tr>
<td width="16%"><a href="#bayes-query">bayes-query</a></td>
<td width="84%">calculates Bayesian probabilities for a data set</td>
</tr>

<tr>
<td width="16%"><a href="#bayes-train">bayes-train</a></td>
<td width="84%">counts items in lists for Bayesian or frequency analysis</td>
</tr>

<tr>
<td><a href="#corr">corr</a></td>
<td>calculates the <em>product-moment correlation</em> coefficient</td>
</tr>

<tr>
<td><a href="#crit-chi2">crit-chi2</a></td>
<td>calculates the <em>Chi&sup2;</em> statistic for a given probability</td>
</tr>

<tr>
<td><a href="#crit-f">crit-f</a></td>
<td>calculates the <em>F</em> statistic for a given probability</td>
</tr>

<tr>
<td><a href="#crit-t">crit-t</a></td>
<td>calculates the <em>Student's t</em> statistic for a given probability</td>
</tr>

<tr>
<td><a href="#crit-z">crit-z</a></td>
<td>calculates the normal distributed <em>Z</em> for a given probability</td>
</tr>

<tr>
<td><a href="#kmeans-query">kmeans-query</a></td>
<td>calculates distances to cluster centroids or other data points</td>
</tr>

<tr>
<td><a href="#kmeans-train">kmeans-train</a></td>
<td>partitions a data set into clusters</td>
</tr>

<tr>
<td><a href="#normal">normal</a></td>
<td>makes a list of normal distributed floating point numbers</td>
</tr>

<tr>
<td><a href="#prob-chi2">prob-chi2</a></td>
<td>calculates the tail probability of a <em>Chi&sup2;</em> distribution value</td>
</tr>

<tr>
<td><a href="#prob-f">prob-f</a></td>
<td>calculates the tail probability of a <em>F</em> distribution value</td>
</tr>

<tr>
<td><a href="#prob-t">prob-t</a></td>
<td>calculates the tail probability of a <em>Student's t</em> distribution value</td>
</tr>

<tr>
<td><a href="#prob-z">prob-z</a></td>
<td>calculates the cumulated probability of a <em>Z</em> distribution value</td>
</tr>

<tr>
<td><a href="#rand">rand</a></td>
<td>generates random numbers in a range</td>
</tr>

<tr>
<td><a href="#random">random</a></td>
<td>generates a list of evenly distributed floats</td>
</tr>

<tr>
<td><a href="#randomize">randomize</a></td>
<td>shuffles all of the elements in a list</td>
</tr>

<tr>
<td><a href="#seed">seed</a></td>
<td>seeds the internal random number generator</td>
</tr>

<tr>
<td><a href="#stats">stats</a></td>
<td>calculates some basic statistics for a data vector</td>
</tr>

<tr>
<td><a href="#t-test">t-test</a></td>
<td>compares means of data samples using the <em>Student's t</em> statistic</td>
</tr>

</table><br/>

<a name="pattern"></a>
<h3>Pattern matching</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="Time and date functions">

<tr>
<td><a href="#ends-with">ends-with</a></td>
<td>tests if a list or string ends with a pattern</td>
</tr>

<tr>
<td WIDTH="16%"><a href="#find">find</a></td>
<td WIDTH="84%">searches for a pattern in a list or string</td>
</tr>

<tr>
<td><a href="#find-all">find-all</a></td>
<td>finds all occurrences of a pattern in a string</td>
</tr>

<tr>
<td><a href="#match">match</a></td>
<td>matches list patterns</td>
</tr>

<tr>
<td><a href="#parse">parse</a></td>
<td>breaks a string along around patterns</td>
</tr>

<tr>
<td><a href="#ref">ref</a></td>
<td>returns the position of an element inside a nested list</td>
</tr>

<tr>
<td><a href="#ref-all">ref-all</a></td>
<td>returns a list of index vectors of elements inside a nested list</td>
</tr>

<tr>
<td><a href="#regex">regex</a></td>
<td>finds patterns in a string</td>
</tr>

<tr>
<td><a href="#replace">replace</a></td>
<td>replaces patterns in a string</td>
</tr>

<tr>
<td><a href="#search">search</a></td>
<td>searches for a pattern in a file</td>
</tr>

<tr>
<td><a href="#starts-with">starts-with</a></td>
<td>tests if a list or string starts with a pattern</td>
</tr>

<tr>
<td><a href="#unify">unify</a></td>
<td>performs a logical unification of patterns</td>
</tr>

</table><br/>

<a name="financial"></a>
<h3>Financial math functions</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="Financial math functions">

<tr>
<td WIDTH="16%"><a href="#fv">fv</a></td>
<td WIDTH="84%">returns the future value of an investment</td>
</tr>

<tr>
<td><a href="#irr">irr</a></td>
<td>calculates the internal rate of return</td>
</tr>

<tr>
<td><a href="#nper">nper</a></td>
<td>calculates the number of periods for an investment</td>
</tr>

<tr>
<td><a href="#npv">npv</a></td>
<td>calculates the net present value of an investment</td>
</tr>

<tr>
<td><a href="#pv">pv</a></td>

<td>calculates the present value of an investment</td>
</tr>

<tr>
<td><a href="#pmt">pmt</a></td>
<td>calculates the payment for a loan</td>
</tr>

</table><br/>

<a name="input_output"></a>
<h3>Input/output and file operations</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="Input/output and file operations">


<tr>
<td WIDTH="16%"><a href="#append-file">append-file</a></td>
<td WIDTH="84%">appends data to a file</td>
</tr>

<tr>
<td><a href="#close">close</a></td>
<td>closes a file</td>
</tr>

<tr>
<td><a href="#current-line">current-line</a></td>
<td>retrieves contents of last read-line buffer</td>
</tr>

<tr>
<td><a href="#device">device</a></td>
<td>sets or inquires about current print device</td>
</tr>

<tr>

<td><a href="#exec">exec</a></td>
<td>launches another program, then reads from or writes to it</td>
</tr>

<tr>
<td><a href="#load">load</a></td>
<td>loads and evaluates a file of newLISP code</td>
</tr>

<tr>
<td><a href="#open">open</a></td>
<td>opens a file for reading or writing</td>

</tr>

<tr>
<td><a href="#peek">peek</a></td>
<td>checks file descriptor for number of bytes ready for reading</td>
</tr>

<tr>
<td><a href="#print">print</a></td>
<td>prints to the console or a device</td>
</tr>

<tr>

<td><a href="#println">println</a></td>
<td>prints to the console or a device with a line-feed</td>
</tr>

<tr>
<td><a href="#read">read</a></td>
<td>reads binary data from a file</td>
</tr>

<tr>
<td><a href="#read-char">read-char</a></td>
<td>reads an 8-bit character from a file</td>
</tr>

<tr>
<td><a href="#read-file">read-file</a></td>
<td>reads a whole file in one operation</td>
</tr>

<tr>
<td><a href="#read-key">read-key</a></td>
<td>reads a keyboard key</td>
</tr>

<tr>
<td><a href="#read-line">read-line</a></td>
<td>reads a line from the console or file</td>
</tr>

<tr>
<td><a href="#read-utf8">read-utf8</a></td>
<td>reads UTF-8 character from a file</td>
</tr>

<tr>
<td><a href="#save">save</a></td>
<td>saves a workspace, context, or symbol to a file</td>
</tr>

<tr>
<td><a href="#search">search</a></td>
<td>searches a file for a string</td>

</tr>

<tr>
<td><a href="#seek">seek</a></td>
<td>sets or reads a file position</td>
</tr>

<tr>
<td><a href="#write">write</a></td>
<td>writes binary data to a file or string</td>
</tr>

<tr>

<td><a href="#write-char">write-char</a></td>
<td>writes a character to a file</td>
</tr>

<tr>
<td><a href="#write-file">write-file</a></td>
<td>writes a file in one operation</td>
</tr>

<tr>
<td><a href="#write-line">write-line</a></td>
<td>writes a line to the console or a file</td>
</tr>

</table><br/>

<a name="processes"></a>
<h3>Processes and the Cilk API</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="Processes and the Cilk API">

<tr>
<td WIDTH="16%"><a href="#shell">!</a></td>
<td WIDTH="84%">shells out to the operating system</td>
</tr>

<tr>
<td><a href="#abort">abort</a></td>
<td>aborts a child process started with <tt>spawn</tt></td>
</tr>

<tr>
<td><a href="#destroy">destroy</a></td>
<td>destroys a process created with <tt>fork</tt> or <tt>process</tt></td>
</tr>

<tr>
<td><a href="#exec">exec</a></td>
<td>runs a process, then reads from or writes to it</td>
</tr>

<tr>
<td><a href="#fork">fork</a></td>
<td>launches a newLISP child process</td>
</tr>

<tr>
<td><a href="#pipe">pipe</a></td>
<td>creates a pipe for interprocess communication</td>
</tr>

<tr>
<td><a href="#process">process</a></td>
<td>launches a child process, remapping standard I/O and standard error</td>
</tr>

<tr>
<td><a href="#receive">receive</a></td>
<td>receive a message from another process</td>
</tr>

<tr>
<td><a href="#semaphore">semaphore</a></td>
<td>creates and controls semaphores</td>
</tr>

<tr>
<td><a href="#send">send</a></td>
<td>send a message to another process</td>
</tr>


<tr>
<td><a href="#share">share</a></td>
<td>shares memory with other processes</td>
</tr>

<tr>
<td><a href="#spawn">spawn</a></td>
<td>launches a child process for Cilk process management</td>
</tr>

<tr>
<td><a href="#sync">sync</a></td>
<td>waits for child processes launched with <tt>spawn</tt> and collects results</td>
</tr>

<tr>
<td><a href="#wait-pid">wait-pid</a></td>
<td>waits for a child process to end</td>
</tr>

</table><br/>

<a name="directory_management"></a>
<h3>File and directory management</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="File and directory management">

<tr>
<td WIDTH="16%"><a href="#change-dir">change-dir</a>&nbsp;</td>
<td WIDTH="84%">changes to a different drive and directory</td>
</tr>

<tr>
<td><a href="#copy-file">copy-file</a></td>
<td>copies a file</td>
</tr>

<tr>
<td><a href="#delete-file">delete-file</a></td>
<td>deletes a file</td>
</tr>

<tr>
<td><a href="#directory">directory</a></td>
<td>returns a list of directory entries</td>
</tr>

<tr>
<td><a href="#file-info">file-info</a></td>
<td>gets file size, date, time, and attributes</td>
</tr>

<tr>
<td><a href="#make-dir">make-dir</a></td>
<td>makes a new directory</td>
</tr>

<tr>
<td><a href="#real-path">real-path</a></td>
<td>returns the full path of the relative file path</td>
</tr>

<tr>
<td><a href="#remove-dir">remove-dir</a></td>
<td>removes an empty directory</td>
</tr>

<tr>
<td><a href="#rename-file">rename-file</a></td>
<td>renames a file or directory</td>
</tr>

</table><br/>

<a name="http_api"></a>
<h3>HTTP networking API</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="HTTP networking API">


<tr>
<td width="16%"><a href="#base64-enc">base64-enc</a></td>
<td width="84%">encodes a string into BASE64 format</td>
</tr>

<tr>
<td><a href="#base64-dec">base64-dec</a></td>

<td>decodes a string from BASE64 format</td>
</tr>

<tr>
<td><a href="#delete-url">delete-url</a></td>
<td>deletes a file or page from the web</td>
</tr>

<tr>
<td><a href="#get-url">get-url</a></td>
<td>reads a file or page from the web</td>
</tr>

<tr>
<td><a href="#json-error">json-error</a></td>
<td>returns error information from a failed JSON translation.</td>
</tr>

<tr>
<td><a href="#json-parse">json-parse</a></td>
<td>parses JSON formatted data</td>
</tr>

<tr>
<td><a href="#post-url">post-url</a></td>
<td>posts info to a URL address</td>
</tr>

<tr>
<td><a href="#put-url">put-url</a></td>
<td>uploads a page to a URL address</td>
</tr>

<tr>
<td><a href="#xfer-event">xfer-event</a></td>
<td>registers an event handler for HTTP byte transfers</td>
</tr>

<tr>
<td><a href="#xml-error">xml-error</a></td>
<td>returns last XML parse error</td>
</tr>

<tr>
<td><a href="#xml-parse">xml-parse</a></td>

<td>parses an XML document</td>
</tr>

<tr>
<td><a href="#xml-type-tags">xml-type-tags</a>&nbsp;</td>
<td>shows or modifies XML type tags</td>
</tr>

</table><br/>

<a name="socket_tcpip"></a>
<h3>Socket TCP/IP, UDP and ICMP network API</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="Socket TCP/IP and UDP network API">


<tr>
<td WIDTH="16%"><a href="#net-accept">net-accept</a></td>
<td WIDTH="84%">accepts a new incoming connection</td>
</tr>

<tr>
<td><a href="#net-close">net-close</a></td>
<td>closes a socket connection</td>
</tr>

<tr>
<td><a href="#net-connect">net-connect</a></td>
<td>connects to a remote host</td>
</tr>

<tr>
<td><a href="#net-error">net-error</a></td>
<td>returns the last error</td>
</tr>

<tr>
<td><a href="#net-eval">net-eval</a></td>
<td>evaluates expressions on multiple remote newLISP servers</td>
</tr>

<tr>
<td><a href="#net-interface">net-interface</a></td>
<td>Sets the default interface IP address on multihomed computers.</td>
</tr>

<tr>
<td><a href="#net-ipv">net-ipv</a></td>
<td>Switches between IPv4 and IPv6 internet protocol versions.</td>
</tr>

<tr>
<td><a href="#net-listen">net-listen</a></td>
<td>listens for connections to a local socket</td>
</tr>

<tr>
<td><a href="#net-local">net-local</a></td>
<td>returns the local IP and port number for a connection</td>
</tr>

<tr>
<td><a href="#net-lookup">net-lookup</a></td>
<td>returns the name for an IP number</td>
</tr>

<tr>
<td><a href="#net-packet">net-packet</a></td>
<td>send a custom configured IP packet over raw sockets</td>
</tr>

<tr>
<td><a href="#net-peek">net-peek</a></td>
<td>returns the number of characters ready to be read from a network socket</td>
</tr>

<tr>
<td><a href="#net-peer">net-peer</a></td>
<td>returns the remote IP and port for a net connect</td>
</tr>


<tr>
<td><a href="#net-ping">net-ping</a></td>
<td>sends a ping packet (ICMP echo request) to one or more addresses</td>
</tr>

<tr>
<td><a href="#net-receive">net-receive</a></td>
<td>reads data on a socket connection</td>
</tr>

<tr>
<td><a href="#net-receive-from">net-receive-from</a>&nbsp;</td>
<td>reads a UDP on an open connection</td>
</tr>

<tr>
<td><a href="#net-receive-udp">net-receive-udp</a></td>
<td>reads a UDP and closes the connection</td>
</tr>

<tr>
<td><a href="#net-select">net-select</a></td>

<td>checks a socket or list of sockets for status</td>
</tr>

<tr>
<td><a href="#net-send">net-send</a></td>
<td>sends data on a socket connection</td>
</tr>

<tr>
<td><a href="#net-send-to">net-send-to</a></td>
<td>sends a UDP on an open connection</td>
</tr>

<tr>
<td><a href="#net-send-udp">net-send-udp</a></td>
<td>sends a UDP and closes the connection</td>
</tr>

<tr>
<td><a href="#net-service">net-service</a></td>
<td>translates a service name into a port number</td>
</tr>

<tr>
<td><a href="#net-sessions">net-sessions</a></td>

<td>returns a list of currently open connections </td>
</tr>

</table><br/>

<a name="JS"></a>
<h3>API for newLISP in a web browser</h3>

<table border="0" cellpadding="1" width="95%" align="center" summary="API for newLISP in a web browser">

<tr>
<td width="16%"><a href="#display-html">display-html</a></td>
<td width="84%">display an HTML page in a web browser</td>
</tr>

<tr>
<td><a href="#eval-string-js">eval-string-js</a></td>
<td>evaluate JavaScript in the current web browser page</td>
</tr>

</table><br/>


<a name="reflection"></a>
<h3>Reflection and customization</h3>

<table border="0" cellpadding="1" width="95%" align="center" summary="Reflection and customization">

<tr>
<td width="16%"><a href="#command-event">command-event</a></td>
<td width="84%">pre-processes the command-line and HTTP requests</td>
</tr>

<tr>
<td><a href="#error-event">error-event</a></td>
<td>defines an error handler</td>
</tr>

<tr>
<td><a href="#history">history</a></td>
<td>returns the call history of a function</td>
</tr>

<tr>
<td><a href="#last-error">last-error</a></td>
<td>report the last error number and text</td>
</tr>

<tr>
<td><a href="#macro">macro</a></td>
<td>create a reader expansion macro</td>
</tr>

<tr>
<td><a href="#ostype">ostype</a></td>
<td>contains a string describing the OS platform</td>
</tr>

<tr>
<td><a href="#prefix">prefix</a></td>
<td>Returns the context prefix of a symbol</td>
</tr>

<tr>
<td><a href="#prompt-event">prompt-event</a></td>
<td>customizes the interactive newLISP shell prompt</td>
</tr>

<tr>
<td><a href="#read-expr">read-expr</a></td>
<td>reads and translates s-expressions from source</td>
</tr>

<tr>
<td><a href="#reader-event">reader-event</a></td>
<td>preprocess expressions before evaluation event-driven</td>
</tr>

<tr>
<td><a href="#set-locale">set-locale</a></td>
<td>switches to a different locale</td>
</tr>

<tr>
<td><a href="#source">source</a></td>
<td>returns the source required to bind a symbol to a string</td>
</tr>

<tr>
<td><a href="#sys-error">sys-error</a></td>
<td>reports OS system error numbers</td>
</tr>

<tr>
<td><a href="#sys-info">sys-info</a></td>
<td>gives information about system resources</td>
</tr>

<tr>
<td><a href="#term">term</a></td>
<td>returns the term part of a symbol or its context as a string</td>
</tr>

</table><br/>

<a name="system_functions"></a>
<h3>System functions</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="System functions">

<tr>
<td width="16%"><a href="#systemsymbol">$</a></td>
<td width="84%">accesses system variables $0 -&gt; $15</td>
</tr>

<tr>
<td><a href="#callback">callback</a></td>
<td>registers a callback function for an imported library</td>
</tr>

<tr>
<td><a href="#catch">catch</a></td>
<td>evaluates an expression, catching errors and early returns</td>
</tr>

<tr>
<td><a href="#context">context</a></td>
<td>creates or switches to a different namespace</td>
</tr>

<tr>
<td><a href="#copy">copy</a></td>
<td>copies the result of an evaluation</td>
</tr>

<tr>
<td><a href="#debug">debug</a></td>
<td>debugs a user-defined function</td>
</tr>

<tr>
<td><a href="#delete">delete</a></td>
<td>deletes symbols from the symbol table</td>
</tr>

<tr>
<td><a href="#default">default</a></td>
<td>returns the contents of a default functor from a context</td>
</tr>

<tr>
<td><a href="#env">env</a></td>
<td>gets or sets the operating system's environment</td>
</tr>

<tr>
<td><a href="#exit">exit</a></td>
<td>exits newLISP, setting the exit value</td>
</tr>

<tr>
<td><a href="#global">global</a></td>
<td>makes a symbol accessible outside MAIN</td>
</tr>

<tr>
<td><a href="#import">import</a></td>
<td>imports a function from a shared library</td>
</tr>

<tr>
<td><a href="#main-args">main-args</a></td>
<td>gets command-line arguments</td>
</tr>

<tr>
<td><a href="#new">new</a></td>
<td>creates a copy of a context</td>
</tr>

<tr>
<td><a href="#pretty-print">pretty-print</a></td>
<td>changes the pretty-printing characteristics</td>
</tr>

<tr>
<td><a href="#read-expr">read-expr</a></td>
<td>translates a string to an s-expression without evaluating it</td>
</tr>

<tr>
<td><a href="#reset">reset</a></td>
<td>goes to the top level</td>
</tr>

<tr>
<td><a href="#signal">signal</a></td>
<td>sets a signal handler</td>
</tr>

<tr>
<td><a href="#sleep">sleep</a></td>
<td>suspends processing for specified milliseconds</td>
</tr>

<tr>
<td><a href="#sym">sym</a></td>
<td>creates a symbol from a string</td>
</tr>

<tr>
<td><a href="#symbols">symbols</a></td>
<td>returns a list of all symbols in the system</td>
</tr>

<tr>
<td><a href="#throw">throw</a></td>
<td>causes a previous <a href="#catch">catch</a> to return</td>
</tr>

<tr>
<td><a href="#throw-error">throw-error</a></td>
<td>throws a user-defined error</td>
</tr>

<tr>
<td><a href="#timer">timer</a></td>
<td>starts a one-shot timer, firing an event</td>
</tr>

<tr>
<td><a href="#trace">trace</a></td>
<td>sets or inquires about trace mode</td>
</tr>

<tr>
<td><a href="#trace-highlight">trace-highlight</a></td>
<td>sets highlighting strings in trace mode</td>
</tr>

</table><br/>

<a name="importing_libraries"></a>
<h3>Importing libraries</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="Importing libraries<">


<tr>
<td WIDTH="16%"><a href="#address">address</a></td>
<td WIDTH="84%">returns the memory address of a number or string</td>

</tr>

<tr>
<td><a href="#callback">callback</a></td>
<td>registers a callback function for an imported library</td>
</tr>

<tr>
<td><a href="#flt">flt</a></td>
<td>converts a number to a 32-bit integer representing a float</td>
</tr>

<tr>
<td><a href="#float">float</a></td>
<td>translates a string or integer into a floating point number</td>
</tr>

<tr>

<td><a href="#get-char">get-char</a></td>
<td>gets a character from a memory address</td>
</tr>

<tr>
<td><a href="#get-float">get-float</a></td>
<td>gets a double float from a memory address</td>
</tr>

<tr>
<td><a href="#get-int">get-int</a>&nbsp;&nbsp;</td>
<td>gets a 32-bit integer from a memory address</td>
</tr>

<tr>
<td><a href="#get-long">get-long</a>&nbsp;&nbsp;</td>
<td>gets a long 64-bit integer from a memory address</td>
</tr>

<tr>
<td><a href="#get-string">get-string</a></td>
<td>gets a string from a memory address</td>
</tr>

<tr>
<td><a href="#import">import</a></td>
<td>imports a function from a shared library</td>
</tr>

<tr>

<td><a href="#int">int</a></td>
<td>translates a string or float into an integer</td>
</tr>

<tr>
<td><a href="#pack">pack</a></td>
<td>packs newLISP expressions into a binary structure</td>
</tr>

<tr>
<td><a href="#struct">struct</a></td>
<td>Defines a data structure with C types</td>
</tr>

<tr>
<td><a href="#unpack">unpack</a></td>
<td>unpacks a binary structure into newLISP expressions</td>

</tr>
</table><br/>

<a name="internals"></a>
<h3>newLISP internals API</h3>


<table border="0" cellpadding="1" width="95%" align="center" summary="newLISP internals API">

<tr>
<td width="16%"><a href="#command-event">command-event</a></td>
<td width="84%">pre-processes the command-line and HTTP requests</td>
</tr>

<tr>
<td><a href="#cpymem">cpymem</a></td>
<td>copies memory between addresses</td>
</tr>

<tr>
<td><a href="#dump">dump</a></td>
<td>shows memory address and contents of newLISP cells</td>
</tr>

<tr>
<td><a href="#prompt-event">prompt-event</a></td>
<td>customizes the interactive newLISP shell prompt</td>
</tr>

<tr>
<td><a href="#read-expr">read-expr</a></td>
<td>reads and translates s-expressions from source</td>
</tr>

<tr>
<td><a href="#reader-event">reader-event</a></td>
<td>preprocess expressions before evaluation event-driven</td>
</tr>


</table><br/>

<br/>

<center>
<span class="divider">(&nbsp;<font color="#7ba9d4">&sect;</font>&nbsp;)</span>
</center>

<br/>

<a name="functions_alphabetical"></a>
<h2>4. Functions in alphabetical order</h2>

<br/>

<a name="shell"></a>
<h2><span class="function">!</span></h2>
<h4>syntax: (! <em>str-shell-command</em> [<em>int-flags</em>])</h4>

<p>Executes the command in <em>str-command</em> by shelling out to the 
operating system and executing.  This function returns a different value
depending on the host operating system.</p>

<!-- example -->

<pre>
(! "vi")  
(! "ls -ltr")
</pre>

<p>Use the <a href="#exec">exec</a> function to execute a shell command 
and capture the standard output or to feed standard input. 
The <a href="#process">process</a> function may be used to launch a 
non-blocking child process and redirect std I/O and std error to pipes.</p>

<p>On Ms Windows the optional <em>int-flags</em> parameter takes process
creation flags as defined for the Windows <tt>CreateProcessA</tt> function
to control various parameters of process creation. The inclusion of this
parameter &ndash; which also can be <tt>0</tt> &ndash; forces a different 
creation of the process without a command shell window. This parameter is 
ignored on Unix.</p>

<pre>
; on MS Windows
; close the console of the currently running newLISP process
(apply (import "kernel32" "FreeConsole")) 

; start another process and wait for it to finish
(! "notepad.exe" 0)

(exit)
</pre>

<p>Without the additional parameter, the <tt>!</tt> call would create a
new command window replacing the closed one.</p>

<p>Note that <tt>!</tt> (exclamation mark) can be also be used as 
a command-line shell operator by omitting the parenthesis and space 
after the <tt>!</tt>:</p>

<!-- example -->

<pre>
<b>&gt;</b> !ls -ltr    ; executed in the newLISP shell window
</pre>


<p>
	Used in this way, 
	the <tt>!</tt> operator 
	is not a newLISP function at all, 
	but rather a special feature of 
	the newLISP command shell. 
	The <tt>!</tt> must be entered 
	as the first character
	on the command-line.
</p>

<br/><br/>

<a name="systemsymbol"></a>
<h2><span class="function">$</span></h2>
<h4>syntax: ($ <em>int-idx</em>)</h4>

<p>
The functions that use regular expressions 	(<a href="#directory">directory</a>,
<a href="#ends-with">ends-with</a>, <a href="#find">find</a>, <a href="#find-all">find-all</a>, 
<a href="#parse">parse</a>, <a href="#regex">regex</a>, <a href="#search">search</a>, 
<a href="#starts-with">starts-with</a> and <a href="#replace">replace</a>) 
all bind their results to the predefined system variables <tt>$0</tt>, <tt>$1</tt>, 
<tt>$2</tt>&ndash;<tt>$15</tt> after or during the function's execution. System variables 
can be treated the same as any other symbol. As an alternative, the contents of these 
variables may also be accessed by using <tt>($ 0)</tt>, <tt>($ 1)</tt>, <tt>($ 2)</tt>, 
etc. This method allows indexed access (i.e., <tt>($ i)</tt>, where <tt>i</tt> is an integer).</p>

<!-- example -->

<pre>
(set 'str  "http://newlisp.org:80")
(find "http://(.*):(.*)" str 0)  <span class='arw'>&rarr;</span> 0
                                 
$0  <span class='arw'>&rarr;</span> "http://newlisp.org:80"
$1  <span class='arw'>&rarr;</span> "newlisp.org"
$2  <span class='arw'>&rarr;</span> "80"
                                 
($ 0)  <span class='arw'>&rarr;</span> "http://newlisp.org:80"
($ 1)  <span class='arw'>&rarr;</span> "newlisp.org"
($ 2)  <span class='arw'>&rarr;</span> "80"
</pre>

<br/><br/>

<a name="arithmetic"></a>
<h2><span class="function">+, -, *, / ,%</span>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (+ <em>int-1</em> [<em>int-2</em> ... ])</h4>

<p>Returns the sum of all numbers in <em>int-1</em> &mdash;.</p>

<h4>syntax: (- <em>int-1</em> [<em>int-2</em> ... ])</h4>

<p>Subtracts <em>int-2</em> from <em>int-1</em>, then the next <em>int-i</em> 
from the previous result. If only one argument is given, 
its sign is reversed. </p>

<h4>syntax: (* <em>int-1</em> [<em>int-2</em> ... ])</h4>

<p>The product is calculated for <em>int-1</em> to <em>int-i</em>.</p>

<h4>syntax: (/ <em>int-1</em> [<em>int-2</em> ... ])</h4>

<p>Each result is divided successively until the end of the list is reached. 
Division by zero causes an error.</p>

<h4>syntax: (% <em>int-1</em> [<em>int-2</em> ... ])</h4>

<p>Each result is divided successively by the next <em>int</em>, 
then the rest (modulo operation) is returned. Division by zero causes an error. 
For floating point numbers, use the <a href="#mod">mod</a> function.</p>

<!-- example -->

<pre>
(+ 1 2 3 4 5)        <span class='arw'>&rarr;</span> 15
(+ 1 2 (- 5 2) 8)    <span class='arw'>&rarr;</span> 14
(- 10 3 2 1)         <span class='arw'>&rarr;</span> 4
(- (* 3 4) 6 1 2)    <span class='arw'>&rarr;</span> 3
(- 123)              <span class='arw'>&rarr;</span> -123
(map - '(10 20 30))  <span class='arw'>&rarr;</span> (-10 -20 -30)
(* 1 2 3)            <span class='arw'>&rarr;</span> 6
(* 10 (- 8 2))       <span class='arw'>&rarr;</span> 60
(/ 12 3)             <span class='arw'>&rarr;</span> 4
(/ 120 3 20 2)       <span class='arw'>&rarr;</span> 1
(% 10 3)             <span class='arw'>&rarr;</span> 1
(% -10 3)            <span class='arw'>&rarr;</span> -1
(+ 1.2 3.9)          <span class='arw'>&rarr;</span> 4
</pre>


<p>Floating point values in arguments to 
<tt>+</tt>, <tt>-</tt>, <tt>*</tt>, <tt>/</tt>, and <tt>%</tt> 
are truncated to the integer value closest to <tt>0</tt> (zero).
</p>

<p>
Floating point values larger or smaller than
the maximum (<tt>9,223,372,036,854,775,807</tt>)
or minimum (<tt>-9,223,372,036,854,775,808</tt>) integer values 
are truncated to those values. This includes the values for
<tt>+Inf</tt> and <tt>-Inf</tt>.
</p>

<p>
Calculations resulting in values 
larger than <tt>9,223,372,036,854,775,807</tt> 
or smaller than <tt>-9,223,372,036,854,775,808</tt> 
wrap around from positive to negative 
or negative to positive.
</p>

<p>
Floating point values that evaluate to <tt>NaN</tt> (Not a Number), 
ar treated as <tt>0</tt> (zero). 
</p>

<br/><br/>

<a name="inci"></a>
<h2><span class="function">++</span>&nbsp;<a href="#destructive">!</a>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (++ <em>place</em> [<em>num</em> ... ])</h4>

<p>The <tt>++</tt> operator works like <a href="#inc">inc</a>, but performs
integer arithmetic. Without the optional argument in <em>num</em>,
<tt>++</tt> increments the number in <em>place</em> by <tt>1</tt>.</p>

<p>If floating point numbers are passed as arguments, their fractional part
gets truncated first.</p>

<p>Calculations resulting in numbers greater than 9,223,372,036,854,775,807 wrap 
around to negative numbers.  Results smaller than -9,223,372,036,854,775,808 
wrap around to positive numbers.</p>

<p><em>place</em> is either a symbol or a place in a list structure holding a
number, or a number returned by an expression.</p>

<!-- example -->

<pre>
(set 'x 1)    
(++ x)        <span class='arw'>&rarr;</span> 2
(set 'x 3.8)
(++ x)        <span class='arw'>&rarr;</span> 4
(++ x 1.3)    <span class='arw'>&rarr;</span> 5
(set 'lst '(1 2 3))
(++ (lst 1) 2))  <span class='arw'>&rarr;</span> 4
lst              <span class='arw'>&rarr;</span> (1 4 3)
</pre>

<p>If the symbol for <em>place</em> contains <tt>nil</tt>, it is treated
as if containing <tt>0</tt>.</p>

<p>See <a href="#deci">--</a> for decrementing numbers in integer mode.
See <a href="#inc">inc</a> for incrementing numbers in floating point mode.</p>

<br/><br/>

<a name="deci"></a>
<h2><span class="function">--</span>&nbsp;<a href="#destructive">!</a>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (-- <em>place</em> [<em>num</em> ... ])</h4>

<p>The <tt>--</tt> operator works like <a href="#inc">dec</a>, but performs
integer arithmetic. Without the optional argument in <em>num-2</em>,
<tt>--</tt> decrements the number in <em>place</em> by <tt>1</tt>.</p>

<p>If floating point numbers are passed as arguments, their fractional part
gets truncated first.</p>

<p>Calculations resulting in numbers greater than 9,223,372,036,854,775,807 wrap 
around to negative numbers.  Results smaller than -9,223,372,036,854,775,808 
wrap around to positive numbers.</p>

<p><em>place</em> is either a symbol or a place in a list structure holding a
number, or a number returned by an expression.</p>

<!-- example -->

<pre>
(set 'x 1)    
(-- x)        <span class='arw'>&rarr;</span> 0
(set 'x 3.8)
(-- x)        <span class='arw'>&rarr;</span> 2
(-- x 1.3)    <span class='arw'>&rarr;</span> 1

(set 'lst '(1 2 3))
(-- (lst 1) 2))  <span class='arw'>&rarr;</span> 0
lst              <span class='arw'>&rarr;</span> (1 0 3)
</pre>

<p>If the symbol for <em>place</em> contains <tt>nil</tt>, it is treated
as if containing <tt>0</tt>.</p>

<p>See <a href="#inci">++</a> for incrementing numbers in integer mode.
See <a href="#dec">dec</a> for decrementing numbers in floating point mode.</p>

<br/><br/>
<a name="logical"></a>
<h2><span class="function">&lt;, &gt;, =, &lt;=, &gt;=, !=</span>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (&lt; <em>exp-1</em> [<em>exp-2</em> ... ])<br/>
syntax: (&gt; <em>exp-1</em> [<em>exp-2</em> ... ])<br/>
syntax: (= <em>exp-1</em> [<em>exp-2</em> ... ])<br/>
syntax: (&lt;= <em>exp-1</em> [<em>exp-2</em> ... ])<br/>
syntax: (&gt;= <em>exp-1</em> [<em>exp-2</em> ... ])<br/>
syntax: (!= <em>exp-1</em> [<em>exp-2</em> ... ])</h4>

<p>
Expressions are evaluated and the results are compared successively. 
As long as the comparisons conform to the comparison operators, 
evaluation and comparison will continue until all arguments are tested 
and the result is <tt>true</tt>. As soon as one comparison fails, 
<tt>nil</tt> is returned.
</p>

<p>If only one argument is supplied, all comparison operators assume <tt>0</tt> (zero)
as a second argument. This can be used to check if a number is negative, positive, zero 
or not zero.</p>


<p>
All types of expressions can be compared: 
	atoms, numbers, symbols, and strings. 
	List expressions can also be compared 
	(list elements are compared recursively).
</p>

<p>
	When comparing lists, 
	elements at the beginning of the list 
	are considered more significant than the elements following 
	(similar to characters in a string). 
	When comparing lists of different lengths but equal elements, 
	the longer list is considered greater (see examples).
</p>

<p>
	In mixed-type expressions, 
	the types are compared from lowest to highest. 
	Floats and integers are compared by first
	converting them to the needed type, 
	then comparing them as numbers.
</p>

<blockquote>
<b>Atoms:</b> nil, true, integer or float, string, symbol, primitive<br/>
<b>Lists:</b> quoted list/expression, list/expression, lambda, lambda-macro
</blockquote>

<!-- example -->
<pre>
(&lt; 3 5 8 9)                     <span class='arw'>&rarr;</span> true
(&gt; 4 2 3 6)                     <span class='arw'>&rarr;</span> nil
(&lt; "a" "c" "d")                 <span class='arw'>&rarr;</span> true
(&gt;= duba aba)                   <span class='arw'>&rarr;</span> true
(&lt; '(3 4) '(1 5))               <span class='arw'>&rarr;</span> nil
(&gt; '(1 2 3) '(1 2))             <span class='arw'>&rarr;</span> true
(= '(5 7 8) '(5 7 8))           <span class='arw'>&rarr;</span> true
(!= 1 4 3 7 3)                  <span class='arw'>&rarr;</span> true
(&lt; 1.2 6 "Hello" 'any '(1 2 3))           <span class='arw'>&rarr;</span> true
(&lt; nil true)                              <span class='arw'>&rarr;</span> true
(&lt; '(((a b))) '(((b c))))                 <span class='arw'>&rarr;</span> true
(&lt; '((a (b c)) '(a (b d)) '(a (b (d)))))  <span class='arw'>&rarr;</span> true

; with single argument compares against 0

(&gt; 1)    <span class='arw'>&rarr;</span> true ; checks for positive
(&gt; -1)   <span class='arw'>&rarr;</span> nil ; checks for negative
(= 123)  <span class='arw'>&rarr;</span> nil ; checks for zero

(map &gt; '(1 3 -4 -3 1 2))   <span class='arw'>&rarr;</span> (true true nil nil true true)
</pre>

<br/><br/>

<a name="bit_shift"></a>
<h2><span class="function">&lt;&lt;, &gt;&gt;</span></h2>
<h4>syntax: (&lt;&lt; <em>int-1</em> <em>int-2</em> [<em>int-3</em> ... ])<br/>

syntax: (&gt;&gt; <em>int-1</em> <em>int-2</em> [<em>int-3</em> ... ])<br/>

syntax: (&lt;&lt; <em>int-1</em>)<br/>
syntax: (&gt;&gt; <em>int-1</em>)</h4>

<p>
	The number <em>int-1</em> is arithmetically shifted 
	to the left or right by the number of bits given as <em>int-2</em>, 
	then shifted by <em>int-3</em> and so on. 
	For example, 64-bit integers may be shifted up to 63 positions. 
	When shifting right, 
	the most significant bit is duplicated 
	(<em>arithmetic shift</em>):
</p>


<pre>
(&gt;&gt; 0x8000000000000000 1)  <span class='arw'>&rarr;</span> 0xC000000000000000  ; not 0x0400000000000000!
</pre>

<br/>
<!-- example -->

<pre>
(&lt;&lt; 1 3)      <span class='arw'>&rarr;</span>  8
(&lt;&lt; 1 2 1)    <span class='arw'>&rarr;</span>  8
(&gt;&gt; 1024 10)  <span class='arw'>&rarr;</span>  1
(&gt;&gt; 160 2 2)  <span class='arw'>&rarr;</span> 10

(&lt;&lt; 3)        <span class='arw'>&rarr;</span>  6
(&gt;&gt; 8)        <span class='arw'>&rarr;</span>  4
</pre>


<p>When <em>int-1</em> is the only argument <tt>&lt;&lt;</tt>
and <tt>&gt;&gt;</tt> shift by one bit.
</p>

<br/><br/>

<a name="bit_and"></a>
<h2><span class="function">&amp;</span></h2>
<h4>syntax: (&amp; <em>int-1</em> <em>int-2</em> [<em>int-3</em> ... ])</h4>

<p>
	A bitwise <tt>and</tt> operation is performed 
	on the number in <em>int-1</em> with the number in <em>int-2</em>, 
	then successively with <em>int-3</em>, etc.
</p>

<!-- example -->

<pre>
(&amp; 0xAABB 0x000F)  <span class='arw'>&rarr;</span> 11  ; which is 0xB
</pre>

<br/><br/>

<a name="bit_inclusive"></a>
<h2><span class="function">|</span></h2>
<h4>syntax: (| <em>int-1</em> <em>int-2</em> [<em>int-3</em> ... ])</h4>

<p>
	A bitwise <tt>or</tt> operation is performed 
	on the number in <em>int-1</em> with the number in <em>int-2</em>, 
	then successively with <em>int-3</em>, etc.
</p>

<!-- example -->

<pre>
(| 0x10 0x80 2 1)  <span class='arw'>&rarr;</span> 147
</pre>

<br/><br/>

<a name="bit_exclusive"></a>
<h2><span class="function">^</span></h2>
<h4>syntax: (^ <em>int-1</em> <em>int-2</em> [<em>int-3</em> ... ])</h4>

<p>
	A bitwise <tt>xor</tt> operation is performed 
	on the number in <em>int-1</em> with the number in <em>int-2</em>, 
	then successively with <em>int-3</em>, etc.
</p>

<!-- example -->

<pre>
(^ 0xAA 0x55)  <span class='arw'>&rarr;</span> 255
</pre>

<br/><br/>

<a name="bit_not"></a>
<h2><span class="function">~</span></h2>
<h4>syntax: (~ <em>int</em>)</h4>

<p>
	A bitwise <tt>not</tt> operation is performed 
	on the number in <em>int</em>,
	reversing all of the bits.
</p>

<!-- example -->

<pre>
(format "%X" (~ 0xFFFFFFAA))  <span class='arw'>&rarr;</span> "55"
(~ 0xFFFFFFFF)                <span class='arw'>&rarr;</span> 0
</pre>

<br/><br/>

<a name="colon"></a>
<h2><span class="function">:</span></h2>
<h4>syntax: (: <em>sym-function</em> <em>list-object</em> [ ... ])</h4>

<p>The colon is used not only as a syntactic separator between
namespace prefix  and the term inside but also as an operator.
When used as an operator, the colon <tt>:</tt> constructs a 
context symbol from the context name in the object list and the 
symbol following the colon. The object list in <em>list-object</em> 
can be followed by other parameters.</p>

<p>The <tt>:</tt> operator implements <em>polymorphism</em> of 
object methods, which are part of different object classes 
represented by contexts (namespaces). In newLISP, an object is 
represented by a list, the first element of which is the 
symbol (name) of its class context.
The class context implements the functions applicable to the object. 
No space is required between the colon and the symbol following it.</p>

<!-- example -->

<pre>
(define (Rectangle:area)
    (mul (self 3) (self 4)))

(define (Circle:area)
    (mul (pow (self 3) 2) (acos 0) 2))

(define (Rectangle:move dx dy)
    (inc (self 1) dx) 
	(inc (self 2) dy)) 

(define (Circle:move p dx dy)
    (inc (self 1) dx) (inc (self 2) dy)) 

(set 'myrect '(Rectangle 5 5 10 20)) ; x y width height
(set 'mycircle '(Circle 1 2 10)) ; x y radius

;; using the : (colon) operator to resolve to a specific context

(:area myrect)     <span class='arw'>&rarr;</span> 200
(:area mycircle)   <span class='arw'>&rarr;</span> 314.1592654

;; map class methods uses curry to enclose the colon operator and class function

(map (curry :area) (list myrect mycircle)) <span class='arw'>&rarr;</span> (200 314.1592654)

(map (curry :area) '((Rectangle 5 5 10 20) (Circle 1 2 10))) <span class='arw'>&rarr;</span> (200 314.1592654) 

;; change object attributes using a function and re-assigning
;; to the objects name

(:move myrect 2 3)       
myrect   <span class='arw'>&rarr;</span> (Rectangle 7 8 10 20)

(:move mycircle 4 5)   
mycircle <span class='arw'>&rarr;</span> (Circle 5 7 10)
</pre>

<p>Inside the FOOP methods the <a href="#self">self</a> function is used to access
the target object of the method.</p>

<br/><br/>

<a name="abort"></a>
<h2><span class="function">abort</span></h2>
<h4>syntax: (abort <em>int-pid</em>)<br/>
syntax: (abort)</h4>

<p>In the first form, <tt>abort</tt> aborts a specific child process of the
current parent process giving the process id in <em>int-pid</em>. The process
must have been started using <a href="#spawn">spawn</a>. For processes
started using <a href="#fork">fork</a>, use <a href="#destroy">destroy</a>
instead.</p>

<p>The function <tt>abort</tt> is not available on Windows.</p>


<!-- example -->

<pre>
(abort 2245)  <span class='arw'>&rarr;</span> true
</pre>


<p>To abort all child processes spawned from the current process use <tt>abort</tt>
without any parameters:</p>


<pre>
(abort)  <span class='arw'>&rarr;</span> true ; abort all
</pre>


<p>The function <tt>abort</tt> is part of the Cilk API for synchronizing
child processes and process parallelization. See the reference for the
function <a href="#spawn">spawn</a> for a full discussion of the Cilk API.</p>

<br/><br/>

<a name="abs"></a>
<h2><span class="function">abs</span>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (abs <em>num</em>)</h4>

<p>
	Returns the absolute value of the number in <em>num</em>.
</p>

<!-- example -->

<pre>
(abs -3.5)  <span class='arw'>&rarr;</span> 3.5
</pre>

<br/><br/>

<a name="acos"></a>
<h2><span class="function">acos</span></h2>
<h4>syntax: (acos <em>num-radians</em>)</h4>

<p>
	The arc-cosine function is calculated 
	from the number in <em>num-radians</em>.
</p>

<!-- example -->

<pre>
(acos 1)  <span class='arw'>&rarr;</span> 0
(cos (acos 1)) <span class='arw'>&rarr;</span> 1
</pre>

<br/><br/>

<a name="acosh"></a>
<h2><span class="function">acosh</span></h2>
<h4>syntax: (acosh <em>num-radians</em>)</h4>

<p>Calculates the inverse hyperbolic cosine of <em>num-radians</em>, 
the value whose hyperbolic cosine is <em>num-radians</em>. 
If <em>num-radians</em> is less than 1, 
<tt>acosh</tt> returns <tt>NaN</tt>.</p>

<!-- example -->

<pre>
(acosh 2)  <span class='arw'>&rarr;</span> 1.316957897
(cosh (acosh 2)) <span class='arw'>&rarr;</span> 2
(acosh 0.5) <span class='arw'>&rarr;</span> NaN
</pre>

<br/><br/>


<a name="add"></a>
<h2><span class="function">add</span></h2>
<h4>syntax: (add <em>num-1</em> [<em>num-2</em> ... ])</h4>

<p>
	All of the numbers in <em>num-1</em>, <em>num-2</em>, and on 
	are summed.
	<tt>add</tt> accepts float or integer operands, 
	but it always returns a floating point number. 
	Any floating point calculation with <tt>NaN</tt> 
	also returns <tt>NaN</tt>.
</p>

<!-- example -->

<pre>
(add 2 3.25 9)   <span class='arw'>&rarr;</span> 14.25
(add 1 2 3 4 5)  <span class='arw'>&rarr;</span> 15
</pre>

<br/><br/>

<a name="address"></a>
<h2><span class="function">address</span></h2>
<h4>syntax: (address <em>int</em>)<br/>

syntax: (address <em>float</em>)<br/>
syntax: (address <em>str</em>)</h4>

<p>
	Returns the memory address of the integer in <em>int</em>, 
	the double floating point number in <em>float</em>, 
	or the string in <em>str</em>. 
	This function is used for passing parameters to library functions 
	that have been imported using the <a href="#import">import</a> function.
</p>

<!-- example -->

<pre>
(set 's "\001\002\003\004")

(get-char (+ (address s) 3))   <span class='arw'>&rarr;</span> 4

(set 'x 12345) ; x is a 64-bit long int

; on a big-endian CPU, i.e. PPC or SPARC 
(get-long (address x))         <span class='arw'>&rarr;</span> 12345
; the 32-bit int is in high 32-bit part of the long int
(get-int (+ (address x) 4))    <span class='arw'>&rarr;</span> 12345

; on a little-endian CPU, i.e. Intel i386
; the 32-bit int is in the low 32-bit part of the long int
(get-int (address x))          <span class='arw'>&rarr;</span> 12345

; on both architectures (integers are 64 bit in newLISP)
(set 'x 1234567890)
(get-long (address x))         <span class='arw'>&rarr;</span>  1234567890

</pre>


<p>
When a string is passed to C library function the address of the string is 
used automatically, and it is not necessary to use the <tt>address</tt> 
function in that case. As the example shows, <tt>address</tt> can be used 
to do pointer arithmetic on the string's address.</p>

<p><tt>address</tt> should only be used on persistent addresses from
data objects referred to by a variable symbol, not from volatile intermediate
expression objects.</p>

<p>
	See also the <a href="#get-char">get-char</a>, <a href="#get-int">get-int</a>, 
	<a href="#get-long">get-long</a> and <a href="#get-float">get-float</a> functions.
</p>

<br/><br/>

<a name="amb"></a>
<h2><span class="function">amb</span></h2>

<h4>syntax: (amb <em>exp-1</em> [<em>exp-2</em> ... ])</h4>

<p>
	One of the expressions <em>exp-1</em> ... <em>n</em> is selected at random, 
	and the evaluation result is returned.
</p>

<!-- example -->

<pre>
(amb 'a 'b 'c 'd 'e)  <span class='arw'>&rarr;</span> one of: a, b, c, d, or e at random

(dotimes (x 10) (print (amb 3 5 7)))  <span class='arw'>&rarr;</span> 35777535755
</pre>


<p>
	Internally, newLISP uses the same function as <a href="#rand">rand</a> to pick a random number. 
	To generate random floating point numbers, 
	use <a href="#random">random</a>, 
	<a href="#randomize">randomize</a>, or <a href="#normal">normal</a>. 
	To initialize the pseudo random number generating process 
	at a specific starting point,
	use the <a href="#seed">seed</a> function.
</p>

<br/><br/>

<a name="and"></a>
<h2><span class="function">and</span></h2>
<h4>syntax: (and <em>exp-1</em> [<em>exp-2</em> ... ])</h4>

<p>
	The expressions <em>exp-1</em>, <em>exp-2</em>, <em>etc.</em> are evaluated in order,
	returning the result of the last expression.
	If any of the expressions yield <tt>nil</tt> or the empty list <tt>()</tt>, 
	evaluation is terminated and <tt>nil</tt> or the empty list <tt>()</tt> is returned.
</p>

<!-- example -->

<pre>
(set 'x 10)                       <span class='arw'>&rarr;</span> 10
(and (&lt; x 100) (&gt; x 2))           <span class='arw'>&rarr;</span> true
(and (&lt; x 100) (&gt; x 2) "passed")  <span class='arw'>&rarr;</span> "passed"
(and '())                         <span class='arw'>&rarr;</span> ()
(and true)                        <span class='arw'>&rarr;</span> true
(and)                             <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="append"></a>
<h2><span class="function">append</span></h2>
<h4>syntax: (append <em>list-1</em> [<em>list-2</em> ... ])<br/>
syntax: (append <em>array-1</em> [<em>array-2</em> ... ])<br/>
syntax: (append <em>str-1</em> [<em>str-2</em> ... ])</h4>

<p>In the first form, <tt>append</tt> works with lists, 
appending <em>list-1</em> through <em>list-n</em> to form a new list. 
The original lists are left unchanged.</p>

<!-- example -->

<pre>
(append '(1 2 3) '(4 5 6) '(a b))  <span class='arw'>&rarr;</span> (1 2 3 4 5 6 a b)

(set 'aList '("hello" "world"))    <span class='arw'>&rarr;</span> ("hello" "world")

(append aList '("here" "I am"))    <span class='arw'>&rarr;</span> ("hello" "world" "here" "I am")
</pre>


<p>In the second form <tt>append</tt> works on arrays:</p>

<!-- example -->

<pre>
(set 'A (array 3 2 (sequence 1 6)))
<span class='arw'>&rarr;</span> ((1 2) (3 4) (5 6))
(set 'B (array 2 2 (sequence 7 10)))
<span class='arw'>&rarr;</span> ((7 8) (9 10))

(append A B)
<span class='arw'>&rarr;</span> ((1 2) (3 4) (5 6) (7 8) (9 10))

(append B B B)
<span class='arw'>&rarr;</span> ((7 8) (9 10) (7 8) (9 10) (7 8) (9 10))

</pre>


<p>
In the third form, <tt>append</tt> works on strings.  The strings in 
<em>str-n</em> are concatenated into a new string and returned.</p>

<!-- example -->

<pre>
(set 'more " how are you")       <span class='arw'>&rarr;</span> " how are you"

(append "Hello " "world," more)  <span class='arw'>&rarr;</span> "Hello world, how are you"
</pre>


<p>
<tt>append</tt> is also suitable for processing binary strings containing zeroes.
The <a href="#string">string</a> function would cut off strings at zero bytes.</p>

<p>
Linkage characters or strings can be specified using the 
<a href="#join">join</a> function. Use the <a href="#string">string</a> 
function to convert arguments to strings and append in one step.</p>

<p>Use the functions <a href="#extend">extend</a> and <a href="#push">push</a>
to append to an existing list or string modifying the target.</p>

<br/><br/>

<a name="append-file"></a>
<h2><span class="function">append-file</span></h2>
<h4>syntax: (append-file <em>str-filename</em> <em>str-buffer</em>)</h4>

<p>
Works similarly to <a href="#write-file">write-file</a>, but the content 
in <em>str-buffer</em> is appended if the file in <em>str-filename</em> exists. 
If the file does not exist, it is created (in this case, <tt>append-file</tt> 
works identically to <a href="#write-file">write-file</a>). This function 
returns the number of bytes written.</p>

<p>On failure the function returns <tt>nil</tt>. For error information, 
use <a href="#sys-error">sys-error</a> when used on files. When used
on URLs <a href="#net-error">net-error</a> gives more error
information.</p>

<!-- example -->

<pre>
(write-file "myfile.txt" "ABC") 
(append-file "myfile.txt" "DEF")

(read-file "myfile.txt")  <span class='arw'>&rarr;</span> "ABCDEF"
</pre>


<p><tt>append-file</tt> can take a <tt>http://</tt> or <tt>file://</tt> URL
in <em>str-file-name</em>. In case of the <tt>http://</tt>  prefix ,
<tt>append-file</tt> works exactly like <a href="#put-url">put-url</a> with 
<tt>"Pragma: append\r\n"</tt> in the header option and can take the same 
additional parameters. The <tt>"Pragma: append\r\n"</tt> option is supplied 
automatically.</p>

<!-- example -->

<pre>
(append-file "http://asite.com/message.txt" "More message text.")
</pre>


<p>The file <tt>message.txt</tt> is appended at a remote
location <tt>http://asite.com</tt> with the contents of 
<em>str-buffer</em>. If the file does not yet exist, it
will be created. In this mode, <tt>append-file</tt> can also be used
to transfer files to remote newLISP server nodes.
</p>



<p>See also <a href="#read-file">read-file</a> and
<a href="#write-file">write-file</a>.
</p>

<br/><br/>

<a name="apply"></a>
<h2><span class="function">apply</span></h2>
<h4>syntax: (apply <em>func</em> <em>list</em> [<em>int-reduce</em>])<br/>
syntax: (apply <em>func</em>)</h4>

<p>Applies the contents of <em>func</em> (primitive, user-defined function, or 
lambda expression)  to the arguments in <em>list</em>. Only functions and
operators with standard evaluation of their arguments can be applied.</p>

<p>In the second syntax <tt>apply</tt> is used on functions without any 
arguments.</p>

<!-- example -->

<pre>
(apply + '(1 2 3 4))                   <span class='arw'>&rarr;</span> 10
(set 'aList '(3 4 5))                  <span class='arw'>&rarr;</span> (3 4 5)
(apply * aList)                        <span class='arw'>&rarr;</span> 60
(apply sqrt '(25))                     <span class='arw'>&rarr;</span> 5
(apply (lambda (x y) (* x y)) '(3 4))  <span class='arw'>&rarr;</span> 12
</pre>



<p>
	The <em>int-reduce</em> parameter can optionally contain 
	the number of arguments taken by the function in <em>func</em>. 
	In this case, 
	<em>func</em> will be repeatedly applied using the previous result 
	as the first argument and taking the other arguments required 
	successively from <em>list</em> 
	(in left-associative order). 
	For example, if <tt>op</tt> takes two arguments, then:
</p>


<pre>
(apply op '(1 2 3 4 5) 2)

;; is equivalent to

(op (op (op (op 1 2) 3) 4) 5)

;; find the greatest common divisor 
;; of two or more integers 
;; note that newLISP already has a gcd function

(define (gcd_ a b)
    (let (r (% b a))
        (if (= r 0) a (gcd_ r a))))

(define-macro (my-gcd)
    (apply gcd_ (map eval (args)) 2))

(my-gcd 12 18 6)    <span class='arw'>&rarr;</span> 6
(my-gcd 12 18 6 4)  <span class='arw'>&rarr;</span> 2
</pre>


<p>The last example shows how <tt>apply</tt>'s <em>reduce</em> functionality 
can be used to convert a two-argument function into one that takes multiple arguments. Note, that a built-in <a href="#gcd">gcd</a> is available.</p>

<p>
<tt>apply</tt> should only be used on functions and operators that evaluate all 
of their arguments, not on <em>special forms</em> like <a href="#dotimes">dotimes</a> 
or <a href="#case">case</a>, which evaluate only some of their arguments. 
Doing so will cause the function to fail.
</p>

<br/><br/>

<a name="args"></a>
<h2><span class="function">args</span></h2>
<h4>syntax: (args)<br/>
syntax: (args <em>int-idx-1</em> [<em>int-idx-2</em> ... ])</h4>

<p>
Accesses a list of all unbound arguments passed to the currently evaluating 
<a href="#define">define</a>, <a href="#define-macro">define-macro</a> 
lambda, or lambda-macro expression. Only the arguments of the current function 
or macro that remain after local variable binding has occurred are available. 
The <tt>args</tt> function is useful for defining functions or macros 
with a variable number of parameters.</p>

<p>
<tt>args</tt> can be used to define hygienic macros that avoid the danger of 
variable capture. See <a href="#define-macro">define-macro</a>.
</p>

<!-- example -->

<pre>
(define-macro (print-line)
    (dolist (x (args))
        (print x "\n")))
                        
(print-line "hello" "World")
</pre>

<p>
	This example prints a line-feed after each argument. 
	The macro mimics the effect of the built-in function 
	<a href="#println">println</a>.
</p>

<p>
	In the second syntax, 
	<tt>args</tt> can take one or more indices (<em>int-idx-n</em>).
</p>

<!-- example -->

<pre>
(define-macro (foo)
    (print (args 2) (args 1) (args 0)))

(foo x y z) 
<b>zyx</b> 

(define (bar)
	(args 0 2 -1))

(bar '(1 2 (3 4)))  <span class='arw'>&rarr;</span> 4
</pre>


<p>
	The function <tt>foo</tt> 
	prints out the arguments in reverse order. 
	The <tt>bar</tt> function 
	shows <tt>args</tt> being used 
	with multiple indices
	to access nested lists.
</p>

<p>
	Remember that <tt>(args)</tt> only contains the arguments 
	not already bound to local variables 
	of the current function or macro:
</p>

<!-- example -->

<pre>
(define (foo a b) (args))
  
(foo 1 2)        <span class='arw'>&rarr;</span> ()
                 
(foo 1 2 3 4 5)  <span class='arw'>&rarr;</span> (3 4 5)
</pre>


<p>
	In the first example, 
	an empty list is returned because 
	the arguments are bound to the 
	two local symbols, <tt>a</tt> and <tt>b</tt>.
	The second example demonstrates that, 
	after the first two arguments are bound 
	(as in the first example), three arguments remain 
	and are then returned by <tt>args</tt>.
</p>

<p>
	<tt>(args)</tt> can be used as an argument 
	to a built-in or user-defined function call, 
	but it should not be used as an argument to another macro, 
	in which case <tt>(args)</tt> would not be evaluated 
	and would therefore have the wrong
	contents in the new macro environment.
</p>

<br/><br/>

<a name="array"></a>
<h2><span class="function">array</span></h2>

<h4>syntax: (array <em>int-n1</em> [<em>int-n2</em> ... ] [<em>list-init</em>])</h4>

<p>Creates an array with <em>int-n1</em> elements, 
optionally initializing it with the contents of <em>list-init</em>. 
Up to sixteen dimensions may be specified for multidimensional arrays.</p>

<p>Internally, newLISP builds multidimensional arrays by using arrays as the 
elements of an array. newLISP arrays should be used whenever random indexing 
into a large list becomes too slow. Not all list functions may be used on arrays. 
For a more detailed discussion, see the chapter on <a href="#arrays">arrays</a>.</p>

<!-- example -->

<pre>
(array 5)                  <span class='arw'>&rarr;</span> (nil nil nil nil nil)

(array 5 (sequence 1  5))  <span class='arw'>&rarr;</span> (1 2 3 4 5)

(array 10 '(1 2))          <span class='arw'>&rarr;</span> (1 2 1 2 1 2 1 2 1 2)
</pre>



<p>Arrays can be initialized with objects of any type. If fewer initializers than 
elements are provided, the list is repeated until all elements of the array are 
initialized.</p>


<pre>

(set 'myarray (array 3 4 (sequence 1 12)))
<span class='arw'>&rarr;</span> ((1 2 3 4) (5 6 7 8) (9 10 11 12))
</pre>


<p>Arrays are modified and accessed using most of the same functions used for 
modifying lists:</p>


<pre>
(setf (myarray 2 3) 99) <span class='arw'>&rarr;</span> 99)
myarray <span class='arw'>&rarr;</span> ((1 2 3 4) (5 6 7 8) (9 10 11 99))

(setf (myarray 1 1) "hello")  <span class='arw'>&rarr;</span> "hello"

myarray <span class='arw'>&rarr;</span> ((1 2 3 4) (5 "hello" 7 8) (9 10 11 99))

(setf (myarray 1) '(a b c d)) <span class='arw'>&rarr;</span> (a b c d)
myarray <span class='arw'>&rarr;</span> ((1 2 3 4) (a b c d) (9 10 11 99))

(nth 1 myarray)     <span class='arw'>&rarr;</span> (a b c d)  ; access a whole row
                    
;; use implicit indexing and slicing on arrays
                    
(myarray 1)     <span class='arw'>&rarr;</span> (a b c d)
                    
(myarray 0 -1)  <span class='arw'>&rarr;</span> 4

(2 myarray)     <span class='arw'>&rarr;</span> ((9 10 11 99)) 

(-3 2 myarray)  <span class='arw'>&rarr;</span> ((1 2 3 4) (a b c d)) 
</pre>


<p>Care must be taken to use an array when replacing a whole row.
</p>

<p>
<a href="#array-list">array-list</a> can be used to convert arrays back into lists:
</p>


<pre>
(array-list myarray)  <span class='arw'>&rarr;</span> ((1 2 3 4) (a b c d) (1 2 3 99))
</pre>


<p>To convert a list back into an array, apply <a href="#flat">flat</a> to the list:
</p>


<pre>
(set 'aList '((1 2) (3 4)))             <span class='arw'>&rarr;</span> ((1 2) (3 4))

(set 'aArray (array 2 2 (flat aList)))  <span class='arw'>&rarr;</span> ((1 2) (3 4))
</pre>


<p>The <a href="#arrayp">array?</a> function 
can be used to check if an expression is an array:
</p>


<pre>
(array? myarray)               <span class='arw'>&rarr;</span> true
                               
(array? (array-list myarray))  <span class='arw'>&rarr;</span> nil
</pre>



<p>
When serializing arrays using the  function <a href="#source">source</a> 
or <a href="#save">save</a>, the generated code includes the <tt>array</tt> 
statement necessary to create them. This way, variables containing arrays are 
correctly serialized when saving with <a href="#save">save</a> or creating 
source strings using <a href="#source">source</a>.</p>



<pre>
(set 'myarray (array 3 4 (sequence 1 12)))

(save "array.lsp" 'myarray)

;; contents of file arraylsp ;;

(set 'myarray (array 3 4 (flat '(
  (1 2 3 4) 
  (5 6 7 8) 
  (9 10 11 12)))))
</pre>

<br/><br/>

<a name="array-list"></a>
<h2><span class="function">array-list</span></h2>
<h4>syntax: (array-list <em>array</em>)</h4>

<p>
	Returns a list conversion from <em>array</em>, 
	leaving the original array unchanged:
</p>

<!-- example -->

<pre>
(set 'myarray (array 3 4 (sequence 1 12)))
<span class='arw'>&rarr;</span> ((1 2 3 4) (5 6 7 8) (9 10 11 12))

(set 'mylist (array-list myarray))
<span class='arw'>&rarr;</span> ((1 2 3 4) (5 6 7 8) (9 10 11 12))

(list (array? myarray) (list? mylist))
<span class='arw'>&rarr;</span> (true true)
</pre>

<br/><br/>

<a name="arrayp"></a>
<h2><span class="function">array?</span></h2>
<h4>syntax: (array? <em>exp</em>)</h4>

<p>
	Checks if <em>exp</em> is an array:
</p>

<!-- example -->

<pre>
(set 'M (array 3 4 (sequence 1 4)))   
<span class='arw'>&rarr;</span> ((1 2 3 4) (1 2 3 4) (1 2 3 4)))


(array? M)               <span class='arw'>&rarr;</span> true

(array? (array-list M))  <span class='arw'>&rarr;</span> nil
</pre>

<br/><br/>

<a name="asin"></a>
<h2><span class="function">asin</span></h2>
<h4>syntax: (asin <em>num-radians</em>)</h4>

<p>
	Calculates the arcsine function from the number in <em>num-radians</em> 
	and returns the result.
</p>

<!-- example -->

<pre>
(asin 1)  <span class='arw'>&rarr;</span> 1.570796327
(sin (asin 1)) <span class='arw'>&rarr;</span> 1
</pre>

<br/><br/>

<a name="asinh"></a>
<h2><span class="function">asinh</span></h2>
<h4>syntax: (asinh <em>num-radians</em>)</h4>

<p>Calculates the inverse hyperbolic sine of <em>num-radians</em>, 
the value whose hyperbolic sine is <em>num-radians</em>.</p>

<!-- example -->

<pre>
(asinh 2)         <span class='arw'>&rarr;</span> 1.443635475
(sinh (asinh 2))  <span class='arw'>&rarr;</span> 2
</pre>

<br/><br/>

<a name="assoc"></a>
<h2><span class="function">assoc</span></h2>
<h4>syntax: (assoc <em>exp-key</em> <em>list-alist</em>)<br/>
syntax: (assoc <em>list-exp-key</em> <em>list-alist</em>)</h4>


<p>
In the first syntax the value of <em>exp-key</em> is used 
to search <em>list-alist</em> for a <em>member-list</em> 
whose first element matches the key value. 
If found, the <em>member-list</em> is returned;
otherwise, the result will be <tt>nil</tt>.
</p>

<!-- example -->

<pre>
(assoc 1 '((3 4) (1 2)))  <span class='arw'>&rarr;</span> (1 2)

(set 'data '((apples 123) (bananas 123 45) (pears 7)))

(assoc 'bananas data)  <span class='arw'>&rarr;</span> (bananas 123 45)
(assoc 'oranges data)  <span class='arw'>&rarr;</span> nil
</pre>

<p>Together with <a href="#setf">setf</a> <tt>assoc</tt> can be used
to change an association.</p>

<pre>
(setf (assoc 'pears data) '(pears 8))

data  <span class='arw'>&rarr;</span> ((apples 123) (bananas 123 45) (pears 8))
</pre>

<p>In the second syntax more then one key expressions can be specified
to search in nested, multilevel association lists:</p>

<!-- example -->

<pre>
(set 'persons '(
    (id001 (name "Anne") (address (country "USA") (city "New York")))
    (id002 (name "Jean") (address (country "France") (city "Paris")))
))

(assoc '(id001 address) persons) <span class='arw'>&rarr;</span> (address (country "USA") (city "New York"))
(assoc '(id001 address city) persons) <span class='arw'>&rarr;</span> (city "New York")
</pre>


<p>The list in <em>list-aList</em> can be a context which will be interpreted
as its <em>default functor</em>. This way very big lists can be passed by reference
for speedier access and less memory usage:</p>


<pre>
(set 'persons:persons '(
    (id001 (name "Anne") (address (country "USA") (city "New York")))
    (id002 (name "Jean") (address (country "France") (city "Paris")))
))

(define (get-city db id)
    (last (assoc (list id 'address 'city) db ))
)

(get-city persons 'id001) <span class='arw'>&rarr;</span> "New York"
</pre>


<p>
For making replacements in association lists, use the 
<a href="#setf">setf</a> together with the <tt>assoc</tt> function.
The <a href="#lookup">lookup</a> function is used to perform association lookup 
and element extraction in one step.</p>

<br/><br/>

<a name="atan"></a>
<h2><span class="function">atan</span></h2>
<h4>syntax: (atan <em>num-radians</em>)</h4>

<p>
	The arctangent of <em>num-radians</em> 
	is calculated and returned.
</p>

<!-- example -->

<pre>
(atan 1)        <span class='arw'>&rarr;</span> 0.7853981634
(tan (atan 1))  <span class='arw'>&rarr;</span> 1
</pre>

<br/><br/>

<a name="atan2"></a>
<h2><span class="function">atan2</span></h2>
<h4>syntax: (atan2 <em>num-Y-radians</em> <em>num-X-radians</em>)</h4>

<p>
	The <tt>atan2</tt> function computes 
	the principal value of 
	the arctangent of Y / X in radians. 
	It uses the signs of both arguments 
	to determine the quadrant of
	the return value. 
	<tt>atan2</tt> is useful for converting 
	Cartesian coordinates 
	into polar coordinates.
</p>

<!-- example -->

<pre>
(atan2 1 1)                       <span class='arw'>&rarr;</span> 0.7853981634
(div (acos 0) (atan2 1 1))        <span class='arw'>&rarr;</span> 2
(atan2 0 -1)                      <span class='arw'>&rarr;</span> 3.141592654
(= (atan2 1 2) (atan (div 1 2)))  <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="atanh"></a>
<h2><span class="function">atanh</span></h2>
<h4>syntax: (atanh <em>num-radians</em>)</h4>

<p>Calculates the inverse hyperbolic tangent of <em>num-radians</em>, 
the value whose hyperbolic tangent is <em>num-radians</em>. If the 
absolute value of <em>num-radians</em> is greater than 1, 
<tt>atanh</tt> returns <tt>NaN</tt>; if it is equal to 1, <tt>atanh</tt> returns infinity.</p>

<!-- example -->

<pre>
(atanh 0.5) <span class='arw'>&rarr;</span> 0.5493061443
(tanh (atanh 0.5)) <span class='arw'>&rarr;</span> 0.5
(atanh 1.1) <span class='arw'>&rarr;</span> NaN
(atanh 1) <span class='arw'>&rarr;</span> inf
</pre>

<br/><br/>

<a name="atomp"></a>
<h2><span class="function">atom?</span></h2>
<h4>syntax: (atom? <em>exp</em>)</h4>

<p>
	Returns <tt>true</tt> if the value of <em>exp</em> is an atom, 
	otherwise <tt>nil</tt>.	
	An expression is an atom if it evaluates to nil, 
	true, an integer, a float, a string, a symbol or a primitive. 
	Lists, lambda or lambda-macro expressions, 
	and quoted expressions are not atoms.
</p>

<!-- example -->

<pre>
(atom? '(1 2 3))      <span class='arw'>&rarr;</span> nil
(and (atom? 123)
     (atom? "hello")
     (atom? 'foo))    <span class='arw'>&rarr;</span> true
(atom? ''foo)         <span class='arw'>&rarr;</span> nil
</pre>

<br/><br/>

<a name="base64-dec"></a>
<h2><span class="function">base64-dec</span></h2>
<h4>syntax: (base64-dec <em>str</em>)</h4>

<p>
	The BASE64 string in <em>str</em> is decoded. 
	Note that <em>str</em> is not verified
	to be a valid BASE64 string. 
	The decoded string is returned.
</p>

<!-- example -->

<pre>
(base64-dec "SGVsbG8gV29ybGQ=")  <span class='arw'>&rarr;</span> "Hello World"
</pre>


<p>
	For encoding,
	use the <a href="#base64-enc">base64-enc</a> function.
</p>
	
<p> 
	newLISP's BASE64 handling is derived from 
	routines found in the Unix <a href="http://curl.haxx.se/">curl</a>
    utility and conforms to the RFC 4648 standard.
</p>

<br/><br/>

<a name="base64-enc"></a>
<h2><span class="function">base64-enc</span></h2>
<h4>syntax: (base64-enc <em>str</em> [<em>bool-flag</em>])</h4>

<p>
The string in <em>str</em> is encoded into BASE64 format. 
This format encodes groups of 3 * 8 = 24 input bits 
into 4 * 8 = 32 output bits, 
where each 8-bit output group 
represents 6 bits from the input string. 
The 6 bits are encoded into 64 possibilities
from the letters A&ndash;Z and a&ndash;z; 
the numbers 0&ndash;9; 
and the characters + (plus sign) and / (slash). 
The = (equals sign) is used as a filler 
in unused 3- to 4-byte translations. 
This function is helpful for converting binary content 
into printable characters.
</p>

<p>Without the optional <em>bool-flag</em> parameter the empty string <tt>""</tt> is
encoded into <tt>"===="</tt>. If <em>bool-flag</em>  evaluates to <tt>true</tt>, 
the empty string <tt>""</tt> is translated into <tt>""</tt>. Both translations
result in <tt>""</tt> when using <a href="base64-dec">base64-dec</a>.</p>

<p>
The encoded string is returned.
</p>

<p>
BASE64 encoding is used with many Internet protocols 
to encode binary data for inclusion in text-based messages
(e.g., XML-RPC).
</p>

<!-- example -->

<pre>
(base64-enc "Hello World")  <span class='arw'>&rarr;</span> "SGVsbG8gV29ybGQ="

(base64-enc "")             <span class='arw'>&rarr;</span> "===="
(base64-enc "" true)        <span class='arw'>&rarr;</span> ""
</pre>


<p>
	Note that <tt>base64-enc</tt> does not insert 
	carriage-return/line-feed pairs in longer BASE64 sequences 
	but instead returns a pure BASE64-encoded string.
</p>


<p>
	For decoding, 
	use the <a href="#base64-dec">base64-dec</a> function.
</p>
	
<p>
	newLISP's BASE64 handling is derived from routines 
	found in the Unix <a href="http://curl.haxx.se/">curl</a> 
    utility and conforms to the RFC 4648 standard.
</p>

<br/><br/>

<a name="bayes-query"></a>
<h2><span class="function">bayes-query</span></h2>
<h4>syntax: (bayes-query <em>list-L</em> <em>context-D</em> [<em>bool-chain</em> [<em>bool-probs</em>]])</h4>

<p>
Takes a list of tokens (<em>list-L</em>) and a trained dictionary (<em>context-D</em>) 
and returns a list of the combined probabilities of the tokens in one category 
(<em>A</em> or <em>Mc</em>) versus a category (<em>B</em>) or
against all other categories (<em>Mi</em>).  All tokens in <em>list-L</em> 
should occur in <em>context-D</em>. 
When using the default <em>R.A. Fisher inverse Chi&sup2;&nbsp;</em> mode, 
nonexistent tokens will skew results toward equal probability in all categories.
</p>

<p>
Non-existing tokens will not have any influence on the result when using the 
true <em>Chain Bayesian</em> mode with <em>bool-chain</em> set to <tt>true</tt>.  
The optional last flag, <em>bool-probs</em>, indicates whether frequencies or 
probability values are used in the data set. The <a href="#bayes-train">bayes-train</a> 
function is typically used to generate a data set's frequencies.
</p>

<p>
Tokens can be strings or symbols.  If strings are used, they are prepended 
with an underscore before being looked up in <em>context-D</em>.  If 
<a href="#bayes-train">bayes-train</a> was used to generate <em>context-D</em>'s 
frequencies, the underscore was automatically prepended during the learning process.
</p>

<p>
	Depending on the flag specified in <em>bool-probs</em>, 
	<a href="#bayes-query">bayes-query</a> employs either the 
	R. A. Fisher inverse Chi&sup2; method of compounding probabilities 
	or the Chain Bayesian method. 
	By default, when no flag or <tt>nil</tt> is specified in <em>bool-probs</em>,
	the inverse Chi&sup2; method of compounding probabilities is used. 
	When specifying <tt>true</tt> in <em>bool-probs</em>, 
	the Chain Bayesian method is used.
</p>

<p>
	If the inverse Chi&sup2; method is used, 
	the total number of tokens 
	in the different training set's categories 
	should be equal or similar. 
	Uneven frequencies in categories 
	will skew the results.
</p>

<p>
	For two categories <em>A</em> and <em>B</em>, 
	<tt>bayes-query</tt> uses the following formula:
</p>


<b><em>p(A|tkn) = p(tkn|A) * p(A) / ( p(tkn|A) * p(A) + p(tkn|B) * p(B) )</em></b>

   
<p>
	For <em>N</em> categories, the formula can be generalized to:
</p>
   

<b><em>p(Mc|tkn) = p(tkn|Mc) * p(Mc) / sum-i-N( p(tkn|Mi) * p(Mi) )</em></b>
 

<p>
	The probabilities (<em>p(Mi)</em> or <em>p(A)</em>, along with <em>p(B)</em>) 
	represent the <em>Bayesian prior probabilities</em>. 
	<em>p(Mc|tkn)</em> and <em>p(A|tkn)</em> are the 
	<em>posterior Bayesian</em> probabilities of a category or model.
    This <i>naive</i> Bayes formula does nor take into account dependencies
    between different categories. 
</p>

<p>
	Priors are handled differently, 
	depending on whether the R.A. Fisher inverse Chi&sup2; 
	or the Chain Bayesian method is used. 
	In Chain Bayesian mode, 
	posteriors from one token calculation get the priors in the next calculation. 
	In the default inverse Chi&sup2; method, 
	priors are not passed on via chaining, 
	but probabilities are compounded using the inverse Chi&sup2; method.
</p>

<p>
	In Chain Bayes mode, 
	tokens with zero frequency in one category 
	will effectively put the probability of that category to 0 (zero). 
	This also causes all posterior priors to be set to 0
	and the category to be completely suppressed in the result. 
	Queries resulting in zero probabilities for all categories 
	yield <em>NaN</em> values.
</p>

<p>
	The default inverse Chi&sup2; method 
	is less sensitive about zero frequencies 
	and still maintains a low probability for that token. 
	This may be an important feature in natural language processing 
	when using <em>Bayesian statistics</em>. 
	Imagine that five different language <em>corpus</em> categories have been trained, 
	but some words occurring in one category are not present in another. 
	When the pure Chain Bayesian method is used, 
	a sentence could never be classified into its correct category
	because the zero-count of just one word token could effectively exclude it 
	from the category to which it belongs.
</p>

<p>
	On the other hand, 
	the Chain Bayesian method offers exact results 
	for specific proportions in the data. 
	When using Chain Bayesian mode for natural language data,
	all zero frequencies should be removed from the trained dictionary first.
</p>

<p>
The return value of <tt>bayes-query</tt> is a list of probability values, 
one for each category. Following are two examples: the first for the 
default inverse Chi&sup2; mode, the second for a data set processed with the 
Chain Bayesian method.
</p>

<br/>
<h3>R.A. Fisher inverse Chi&sup2; method</h3>

<p>
	In the following example, 
	the two data sets are books from Project Gutenberg.
	We assume that different authors 
	use certain words with different frequencies 
	and want to determine if a sentence is more likely to occur in one 
	or the other author's writing.
	A similar method is frequently used to differentiate between spam 
	and legitimate email.
</p>


<pre>
;; from Project Gutenberg: http://www.gutenberg.org/catalog/
;; The Adventures of Sherlock Holmes - Sir Arthur Conan Doyle

(bayes-train (parse (lower-case (read-file "Doyle.txt")) 
                    "[^a-z]+" 0) '() 'DoyleDowson)

;; A Comedy of Masks - Ernest Dowson and Arthur Moore

(bayes-train '() (parse (lower-case (read-file "Dowson.txt")) 
                    "[^a-z]+" 0) 'DoyleDowson)

(save "DoyleDowson.lsp" 'DoyleDowson)
</pre>


<p>
	The two training sets are loaded, split into tokens, 
	and processed by the <a href="#bayes-train">bayes-train</a> function. 
	In the end, the <tt>DoyleDowson</tt> dictionary is saved to a file, 
	which will be used later with the <tt>bayes-query</tt> function.
</p>

<p>
	The following code illustrates how <tt>bayes-query</tt> is used 
	to classify a sentence as <em>Doyle</em> or <em>Dowson</em>:
</p>


<pre>
(load "DoyleDowson.lsp")
(bayes-query (parse "he was putting the last touches to a picture") 
    'DoyleDowson)
<span class='arw'>&rarr;</span> (0.0359554723158327 0.964044527684167) 

(bayes-query (parse "immense faculties and extraordinary powers of observation") 
    'DoyleDowson)
<span class='arw'>&rarr;</span> (0.983569359827141 0.0164306401728594) 
</pre>


<p>
	The queries correctly identify the first sentence as a <em>Dowson</em> sentence,
	and the second one as a <em>Doyle</em> sentence.
</p>
      
<br/>
      
<h3>Chain Bayesian method</h3>

<p>
	The second example is frequently found 
	in introductory literature on Bayesian statistics. 
	It shows the Chain Bayesian method of 
	using <tt>bayes-query</tt> on the data of a previously processed data set:
</p>

<!-- example -->

<pre>
(set 'Data:test-positive '(8 18))
(set 'Data:test-negative '(2 72))
(set 'Data:total '(10 90))
</pre>

   
<p>
	A disease occurs in 10 percent of the population. 
	A blood test developed to detect this disease 
	produces a false positive rate of 20 percent in the healthy population 
	and a false negative rate of 20 percent in the sick. 
	What is the probability of a person carrying 
	the disease after testing positive?</p>

<!-- example -->

<pre>
(bayes-query '(test-positive) Data true)
<span class='arw'>&rarr;</span> (0.3076923077 0.6923076923)

(bayes-query '(test-positive test-positive) Data true)
<span class='arw'>&rarr;</span> (0.64 0.36)

(bayes-query '(test-positive test-positive test-positive) Data true)
<span class='arw'>&rarr;</span> (0.8767123288 0.1232876712)
</pre>


<p>
	Note that the Bayesian formulas used 
	assume statistical independence of events 
	for the <tt>bayes-query</tt> to work correctly.
</p>

<p>
	The example shows that a person must test positive several times 
	before they can be confidently classified as sick.
</p>

<p>
	Calculating the same example using the R.A. Fisher Chi&sup2; method
	will give less-distinguished results.
</p> 

<br/>
<h3>Specifying probabilities instead of counts</h3>

<p>
	Often, data is already available as probability values 
	and would require additional work to reverse them into frequencies. 
	In the last example, the data were originally defined as percentages. 
	The additional optional <em>bool-probs</em> flag 
	allows probabilities to be entered directly 
	and should be used together with the Chain Bayesian mode 
	for maximum performance:
</p>

<!-- example -->

<pre>
(set 'Data:test-positive '(0.8 0.2))
(set 'Data:test-negative '(0.2 0.8))
(set 'Data:total '(0.1 0.9))

(bayes-query '(test-positive) Data true true)
<span class='arw'>&rarr;</span> (0.3076923077 0.6923076923)

(bayes-query '(test-positive test-positive) Data true true)
<span class='arw'>&rarr;</span> (0.64 0.36)

(bayes-query '(test-positive test-positive test-positive) Data true true)
<span class='arw'>&rarr;</span> (0.8767123288 0.1232876712)
</pre>


<p>
	As expected, the results are the same for probabilities 
	as they are for frequencies.
</p>

<br/><br/>

<a name="bayes-train"></a>
<h2><span class="function">bayes-train</span></h2>
<h4>syntax: (bayes-train <em>list-M1</em> [<em>list-M2</em> ... ] <em>sym-context-D</em>)</h4>

<p>
Takes one or more lists of tokens (<em>M1</em>, <em>M2&mdash;</em>) 
from a joint set of tokens. In newLISP, tokens can be symbols or strings 
(other data types are ignored). Tokens are placed in a common dictionary 
in <em>sym-context-D</em>, and the frequency is counted  for each token 
in each category <em>Mi</em>. If the context does not yet exist, 
it must be quoted.
</p>
   
<p> The <em>M</em> categories represent data models for which sequences of 
tokens can be classified  (see <a href="#bayes-query">bayes-query</a>). 
Each token in <em>D</em> is a content-addressable symbol 
containing a list of the frequencies for this token within each category. 
String tokens are prepended with an <tt>_</tt> (underscore) 
before being converted into symbols. A symbol named <tt>total</tt> is created
containing the total of each category. The <tt>total</tt> symbol cannot be part 
of the symbols passed as an <em>Mi</em> category.
</p>

<p>
The function returns a list of token frequencies found in the different categories 
or models.
</p>

<!-- example -->

<pre>
(bayes-train '(A A B C C) '(A B B C C C) 'L)  <span class='arw'>&rarr;</span> (5 6)

L:A      <span class='arw'>&rarr;</span> (2 1)
L:B      <span class='arw'>&rarr;</span> (1 2)
L:C      <span class='arw'>&rarr;</span> (2 3)
L:total  <span class='arw'>&rarr;</span> (5 6)

(bayes-train '("one" "two" "two" "three")
             '("three" "one" "three") 
             '("one" "two" "three") 'S)       
<span class='arw'>&rarr;</span> (4 3 3)

S:_one    <span class='arw'>&rarr;</span> (1 1 1)
S:_two    <span class='arw'>&rarr;</span> (2 0 1)
S:_three  <span class='arw'>&rarr;</span> (1 2 1)
S:total   <span class='arw'>&rarr;</span> (4 3 3)
</pre>


<p>The first example shows training with two lists of symbols. The second example 
illustrates how an <tt>_</tt> is prepended when training with strings.</p>

<p><tt>bayes-train</tt> creates symbols from strings prepending an underscore
character. This is the same way hashes are created and contexts populates with
symbols by <tt>bayes-train</tt> can be used like hashes:</p>


<pre>
; use a bayes-trained context namespace like a hash dictionary

(S "two")   <span class='arw'>&rarr;</span> (2 0 1)
(S "three") <span class='arw'>&rarr;</span> (1 2 1)

(S) <span class='arw'>&rarr;</span> (("one" (1 1 1)) ("three" (1 2 1)) ("two" (2 0 1)))
</pre>


	
<p>
Note that these examples are just for demonstration purposes. In reality, training 
sets may contain thousands or millions of words, especially when training natural 
language models. But small data sets may be used when the frequency of symbols 
just describe already-known proportions. In this case, it may be better to describe 
the model data set explicitly, without the <tt>bayes-train</tt> function:
</p>


<pre>
(set 'Data:tested-positive '(8 18))
(set 'Data:tested-negative '(2 72))
(set 'Data:total '(10 90))
</pre>


<p>
The last data are from a popular example used to describe the 
<a href="#bayes-query">bayes-query</a> function in introductory papers 
and books about <em>bayesian networks</em>.
</p>

<p>
Training can be done in different stages by using <tt>bayes-train</tt> on an 
existing trained context with the same number of categories. The new symbols 
will be added, then counts and totals will be correctly updated.</p>

<p>
Training in multiple batches may be necessary on big text corpora or documents 
that must be tokenized first. These corpora can be tokenized in small portions, 
then fed into <tt>bayes-train</tt> in multiple stages. Categories can also be 
singularly trained by specifying an empty list for the absent corpus:
</p>


<pre>
(bayes-train shakespeare1 '() 'data)
(bayes-train shakespeare2 '() 'data)
(bayes-train '() hemingway1 'data)
(bayes-train '() hemingway2 'data)
(bayes-train shakepeare-rest hemingway-rest 'data)
</pre>


<p>
<tt>bayes-train</tt> will correctly update word counts and totals.</p>

<p>
	Using <tt>bayes-train</tt> inside a context other than <tt>MAIN</tt> 
	requires the training contexts to have been created previously within 
	the <tt>MAIN</tt> context via the <a href="#context">context</a> function.
</p>

<p>
	<tt>bayes-train</tt> is not only useful with the <a href="#bayes-query">bayes-query</a> function, 
	but also as a function for counting in general.
	For instance, the resulting frequencies 
	could be analyzed using <a href="#prob-chi2">prob-chi2</a> 
	against a <em>null hypothesis</em> of proportional distribution 
	of items across categories.
</p>

<br/><br/>

<a name="begin"></a>
<h2><span class="function">begin</span></h2>
<h4>syntax: (begin <em>body</em>)</h4>

<p>
	The <tt>begin</tt> function is used to group a block of expressions. 
	The expressions in <em>body</em> are evaluated in sequence, and 
	the value of the last expression in <em>body</em> is returned.
</p>

<!-- example -->

<pre>
(begin
  (print "This is a block of 2 expressions\n")
  (print "================================"))
</pre>


<p>
	Some built-in functions like <a href="#cond">cond</a>, <a href="#define">define</a>,
	<a href="#doargs">doargs</a>, <a href="#dolist">dolist</a>, <a href="#dostring">dostring</a>, 
    <a href="#dotimes">dotimes</a>, <a href="#when">when</a> and <a href="#while">while</a> 
	already allow multiple expressions in their bodies,
	but <tt>begin</tt> is often used in an <a href="#if">if</a> expression.
</p>

<p>
	The <a href="#silent">silent</a> function works like <tt>begin</tt>, 
	but suppresses console output on return.
</p>

<br/><br/>

<a name="beta"></a>
<h2><span class="function">beta</span></h2>
<h4>syntax: (beta <em>cum-a</em> <em>num-b</em>)</h4>

<p>
	The <em>Beta</em> function, <tt>beta</tt>,  
	is derived from the <em>log Gamma</em> 
	<tt>gammaln</tt> function as follows:
</p>

<p><em><b>
beta = exp(gammaln(a) + gammaln(b) - gammaln(a + b))
</b></em></p>

<!-- example -->

<pre>
(beta 1 2)  <span class='arw'>&rarr;</span> 0.5
</pre>

<br/><br/>

<a name="betai"></a>
<h2><span class="function">betai</span></h2>

<h4>syntax: (betai <em>num-x</em> <em>num-a</em> <em>num-b</em>)</h4>

<p>
	The <em>Incomplete Beta</em> function, <tt>betai</tt>, 
	equals the cumulative probability of the <em>Beta</em> distribution, <tt>betai</tt>, 
	at <em>x</em> in <em>num-x</em>. 
	The cumulative binomial distribution is defined as the probability of an event, <em>pev</em>, 
	with probability <em>p</em> to occur <em>k</em> or more times in <em>N</em> trials:
</p>


<p><em><b> pev = Betai(p, k, N - k + 1) </b></em></p>

<!-- example -->

<pre>
(betai 0.5 3 8)  <span class='arw'>&rarr;</span> 0.9453125
</pre>


<p>
The example calculates the probability for an event 
with a probability of 0.5 to occur 3 or more times in 10 trials (8 = 10 - 3 + 1). 
The incomplete Beta distribution can be used to derive a variety of other functions 
in mathematics and statistics. 
See also the <a href="#binomial">binomial</a> function.
</p>

<br/><br/>


<a name="bigint"></a>
<h2><span class="function">bigint</span></h2>
<h4>syntax: (bigint <em>number</em>)<br/>
syntax: (bigint <em>string</em>)</h4>

<p>A floating point or integer number gets converted to big integer format.
When converting from floating point, rounding errors occur going back and forth
between decimal and binary arithmetic.</p>

<p>A string argument gets parsed to a number and converted to a big integer.</p>

<!-- example -->

<pre>
(bigint 12345)          <span class='arw'>&rarr;</span> 12345L

(bigint 1.234567890e30) <span class='arw'>&rarr;</span> 1234567889999999957361000000000L 

(set 'num 567890)
(bigint num)            <span class='arw'>&rarr;</span> 567890L

(bigint "-54321")       <span class='arw'>&rarr;</span> -54321L
(bigint "123.45")       <span class='arw'>&rarr;</span> 123L
(bigint "123hello")     <span class='arw'>&rarr;</span> 123L
</pre>

<p>See also the manual chapter <a href="#big_int">Big integer, unlimited precision arithmetic</a></p>

<br/><br/>

<a name="bigintp"></a>
<h2><span class="function">bigint?</span></h2>
<h4>syntax: (bigint? <em>number</em>)</h4>

<p>Check if a number is formatted as a big integer.</p>

<!-- example -->

<pre>
(set 'x 12345)
(set 'y 12345L)
(set 'z 123456789012345678901234567890)
(set 'p 1.2345e20)
(set 'q (bigint p))

(bigint? x)  <span class='arw'>&rarr;</span> nil
(bigint? y)  <span class='arw'>&rarr;</span> true
(bigint? z)  <span class='arw'>&rarr;</span> true
(bigint? p)  <span class='arw'>&rarr;</span> nil
(bigint? q)  <span class='arw'>&rarr;</span> true
</pre>

<p>See also the manual chapter <a href="#big_int">Big integer, unlimited precision arithmetic</a></p>

<br/><br/>

<a name="bind"></a>

<h2><span class="function">bind</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (bind <em>list-variable-associations</em> [<em>bool-eval</em>])</h4>

<p><em>list-variable-associations</em> contains an association list of 
symbols and their values. <tt>bind</tt> sets all symbols
to their associated values.</p>

<p>The associated values are evaluated if the <em>bool-eval</em> flag is <tt>true</tt>:</p>

<pre>
(set 'lst '((a (+ 3 4)) (b "hello")))

(bind lst)         <span class='arw'>&rarr;</span> "hello"

a    <span class='arw'>&rarr;</span> (+ 3 4)
b    <span class='arw'>&rarr;</span> "hello"

(bind lst true)    <span class='arw'>&rarr;</span> "hello"

a    <span class='arw'>&rarr;</span> 7
</pre>

<p>The return value of bind is the value of the last association.</p>

<p><tt>bind</tt> is often used to bind association lists returned
by <a href="#unify">unify</a>.</p>

<pre>
(bind (unify '(p X Y a) '(p Y X X)))    <span class='arw'>&rarr;</span> a

X    <span class='arw'>&rarr;</span> a
Y    <span class='arw'>&rarr;</span> a
</pre>

<p>This can be used for de-structuring:</p>

<pre>
(set 'structure '((one "two") 3 (four (x y z))))
(set 'pattern '((A B) C (D E)))
(bind (unify pattern structure))

A <span class='arw'>&rarr;</span> one
B <span class='arw'>&rarr;</span> "two"
C <span class='arw'>&rarr;</span> 3
D <span class='arw'>&rarr;</span> four
E <span class='arw'>&rarr;</span> (x y z)
</pre>


<p><a href="#unify">unify</a>  returns an association list and <tt>bind</tt> binds the 
associations.</p>

<br/><br/>

<a name="binomial"></a>

<h2><span class="function">binomial</span></h2>
<h4>syntax: (binomial <em>int-n</em> <em>int-k</em> <em>float-p</em>)</h4>

<p>
The binomial distribution function is defined as the probability for an event 
to occur <em>int-k</em> times in <em>int-n</em> trials if that event has a 
probability of <em>float-p</em> and all trials are independent of one another:</p>


<em><b>binomial = pow(p, k) * pow(1.0 - p, n - k) * n! / (k! * (n - k)!)</b></em>


<p>
	where <em>x!</em> is the factorial of <em>x</em> 
	and <em>pow(x, y)</em> is <em>x</em> raised to the power of <em>y</em>.
</p>

<br/>

<!-- example -->

<pre>
(binomial 10 3 0.5)  <span class='arw'>&rarr;</span> 0.1171875
</pre>


<p>
	The example calculates the probability for an event 
	with a probability of 0.5 to occur 3 times in 10 trials. 
	For a cumulated distribution, 
	see the <a href="#betai">betai</a> function.
</p>

<br/><br/>

<a name="bits"></a>
<h2><span class="function">bits</span></h2>
<h4>syntax: (bits <em>int</em> [<em>bool</em>])</h4>

<p>Transforms a number in <em>int</em> to a string of 1's and 0's or a 
list, if <em>bool</em> evaluates to anything not <tt>nil</tt>.</p>

<p>In string representation bits are in high to low order. In list
presentation 1's and 0's are represented as <tt>true</tt> and <tt>nil</tt>
and in order from the lowest to the highest bit. This allows direct
indexing and program control switching on the result.</p>

<!-- example -->

<pre>
(bits 1234)      <span class='arw'>&rarr;</span> "10011010010"

(int (bits 1234) 0 2) <span class='arw'>&rarr;</span> 1234

(bits 1234 true)     <span class='arw'>&rarr;</span> (nil true nil nil true nil true true nil nil true)

((bits 1234 true) 0) <span class='arw'>&rarr;</span> nil ; indexing of the result
</pre>


<p><a href="#int">int</a> with a base of 2 is the inverse function to <tt>bits</tt>.</p>

<br/><br/>

<a name="callback"></a>
<h2><span class="function"> callback </span></h2>
<h4>syntax: (callback <em>int-index</em> <em>sym-function</em>)<br/>
syntax: (callback <em>sym-function</em> <em>str-return-type</em> [<em>str_param_type</em> ...])<br/>
syntax: (callback <em>sym-function</em>)</h4>

<p>In the first <b>simple <tt>callback</tt> syntax</b> up to sixteen (0 to 15) <em>callback</em> 
functions for up to eight parameters can be registered with imported libraries. 
The <tt>callback</tt> function returns a procedure address that invokes a 
user-defined function in <em>sym-function</em>. The following example shows 
the usage of callback functions when importing the <a href="http://www.opengl.org">OpenGL</a> 
graphics library:</p>

<p>If more than sixteen callback functions are required, slots must be 
reassigned to a different callback function.</p>

<!-- example -->

<pre>
...
(define (draw)
    (glClear GL_COLOR_BUFFER_BIT )
    (glRotated rotx 0.0 1.0 0.0)
    (glRotated roty 1.0 0.0 0.0)
    (glutWireTeapot 0.5)
    (glutSwapBuffers))

(define (keyboard key x y)
    (if (= (&amp; key 0xFF) 27) (exit)) ; exit program with ESC
    (println "key:" (&amp; key 0xFF) " x:" x  " y:" y))

(define (mouse button state x y)
    (if (= state 0)
        (glutIdleFunc 0) ; stop rotation on button press
        (glutIdleFunc (callback 4 'rotation)))
    (println "button: " button " state:" state " x:" x " y:" y))

(glutDisplayFunc (callback 0 'draw))
(glutKeyboardFunc (callback 1 'keyboard))
(glutMouseFunc (callback 2 'mouse))
...
</pre>


<p>The address returned by <tt>callback</tt> is registered with the 
<a href="http://www.opengl.org/documentation/specs/glut/spec3/spec3.html">Glut</a> library. 
The above code is a snippet from the file <tt>opengl-demo.lsp</tt>, 
in the <tt>examples/</tt> directory of the source distribution of newLISP
and can also be downloaded from 
<a href="http://www.newlisp.org/downloads/OpenGL/">newlisp.org/downloads/OpenGL</a>.</p>

<p>In the second <b>extended <tt>callback</tt> syntax</b> type specifiers are used to 
describe the functions return and parameter value types when the function is called. 
An unlimited number of callback functions can be registered with the second syntax, and 
return values are passed back to the calling function. The symbol in <em>sym-function</em>
contains a newLISP defined function used as a callback function callable from a C program.</p>

<p>In the third syntax <tt>callback</tt> returns a previously returned C-callable
address for that symbol.</p>

<p>While the first simple <tt>callback</tt> syntax only handles integers and pointer
values, <tt>callback</tt> in the expanded syntax can also handle simple and double precision
floating point numbers passed in an out of the <tt>callback</tt> function.</p>

<p>Both the simple and extended syntax can be mixed inside the same program.</p>

<p>The following example shows the <a href="#import">import</a> of the <tt>qsort</tt>
C library function, which takes as one of it's arguments the address of a comparison
function. The comparison function in this case is written in newLISP and called into
by the imported <tt>qsort</tt> function:</p>

<pre>
; C void qsort(...) takes an integer array with number and width
; of array elements and a pointer to the comparison function
(import "libc.dylib" "qsort" "void" "void*" "int" "int" "void*")

(set 'rlist '(2 3 1 2 4 4 3 3 0 3))
; pack the list into an C readable 32-bit integer array
(set 'carray (pack (dup "ld " 10) rlist))

; the comparison callback function receives pointers to integers
(define (cmp a b) 
    (- (get-int a) (get-int b)))

; generate a C callable address for cmp
(set 'func (callback 'cmp "int" "void*" "void*"))

; sort the carray
(qsort carray 10 4 func)

; unpack the sorted array into a LISP list
(unpack (dup "ld" 10) carray)  <span class='arw'>&rarr;</span>  (0 1 2 2 3 3 3 3 4 4) 
</pre>

<p>As type specifiers the same string tags can be used as in the 
<a href="#import">import</a> function. All pointer types are passed as numbers in and
out of the <tt>callback</tt> function. The functions <a href="#get-char">get-char</a>,
<a href="#get-int">get-int</a>, <a href="#get-long">get-long</a> and 
<a href="#get-string">get-string</a> can be used to extract numbers of
different precision from parameters. Use <a href="#pack">pack</a> and 
<a href="#unpack">unpack</a> to extract data from binary buffers and structures.</p>

<p>Note that newLISP as already a fast built-in <a href="#sort">sort</a> function.</p>

<br/><br/>

<a name="case"></a>
<h2><span class="function">case</span></h2>
<h4>syntax: (case <em>exp-switch</em> (<em>exp-1</em> <em>body-1</em>) [(<em>exp-2</em> <em>body-2</em>) ... ])</h4>

<p>The result of evaluating <em>exp-switch</em> 
is compared to each of the <em>unevaluated</em> expressions 
<em>exp-1, exp-2,</em> &mdash;. If a match is found, the 
corresponding expressions in <em>body</em> 
are evaluated.  The result of the last body expression is returned 
as the result for the entire <tt>case</tt> expression. </p>


<!-- example -->

<pre>
(define (translate n)
  (case n
    (1 "one")
    (2 "two")          
    (3 "three")
    (4 "four")
    (true "Can't translate this")))

(translate 3)   <span class='arw'>&rarr;</span> "three"
(translate 10)  <span class='arw'>&rarr;</span> "Can't translate this"
</pre>


<p>
	The example shows how, 
	if no match is found, 
	the last expression in the body of a <tt>case</tt> function
	can be evaluated.
</p>

<br/><br/>

<a name="catch"></a>
<h2><span class="function">catch</span></h2>

<h4>syntax: (catch <em>exp</em>)<br/>
syntax: (catch <em>exp</em> <em>symbol</em>)</h4>

<p>
	In the first syntax, 
	<tt>catch</tt> will return the result of the evaluation of <em>exp</em> 
	or the evaluated argument of a <a href="#throw">throw</a> 
	executed during the evaluation of <em>exp</em>:
</p>

<!-- example -->

<pre>
(catch (dotimes (x 1000) 
  (if (= x 500) (throw x))))  <span class='arw'>&rarr;</span> 500
</pre>


<p>
	This form is useful for breaking out of iteration loops 
	and for forcing an early return 
	from a function or expression block:
</p>


<pre>
(define (foo x)
   &hellip;
  (if condition (throw 123))
    &hellip;
  456)

;; if condition is true

(catch (foo p))  <span class='arw'>&rarr;</span> 123

;; if condition is not true

(catch (foo p))  <span class='arw'>&rarr;</span> 456
</pre>



<p>
	In the second syntax, 
	<tt>catch</tt> evaluates the expression <em>exp</em>, 
	stores the result in <em>symbol</em>, 
	and returns <tt>true</tt>.  
	If an error occurs during evaluation, 
	<tt>catch</tt> returns <tt>nil</tt> 
	and stores the error message in <em>symbol</em>. 
	This form can be useful when errors are expected 
	as a normal potential outcome of a function 
	and are dealt with during program execution.
</p>

<!-- example -->

<pre>
(catch (func 3 4) 'result)  <span class='arw'>&rarr;</span> nil
result  
<span class='arw'>&rarr;</span> <span class='err'>"ERR: invalid function in function catch : (func 3 4)"</span>

(constant 'func +)          <span class='arw'>&rarr;</span> + &lt;4068A6&gt;
(catch (func 3 4) 'result)  <span class='arw'>&rarr;</span> true
result                      <span class='arw'>&rarr;</span> 7
</pre>


<p>
	When a <a href="#throw">throw</a> is executed during the evaluation of <em>exp</em>,
	<tt>catch</tt> will return <tt>true</tt>, 
	and the <tt>throw</tt> argument will be stored in <em>symbol</em>:
</p>


<pre>
(catch (dotimes (x 100) 
  (if (= x 50) (throw "fin"))) 'result)  <span class='arw'>&rarr;</span> true

result  <span class='arw'>&rarr;</span> "fin"
</pre>


<p>
	As well as being used for early returns from functions and 
	for breaking out of iteration loops (as in the first syntax), 
	the second syntax of <tt>catch</tt> can also be used to catch errors. 
	The <a href="#throw-error">throw-error</a> function may be used 
	to throw user-defined errors.
</p>

<br/><br/>

<a name="ceil"></a>
<h2><span class="function">ceil</span></h2>
<h4>syntax: (ceil <em>number</em>)</h4>

<p>
	Returns the next highest integer above <em>number</em> 
	as a floating point.
</p>

<!-- example -->

<pre>
(ceil -1.5)  <span class='arw'>&rarr;</span> -1
(ceil 3.4)   <span class='arw'>&rarr;</span> 4
</pre>


<p>See also the <a href="#floor">floor</a> function.
</p>

<br/><br/>


<a name="change-dir"></a>
<h2><span class="function">change-dir</span></h2>
<h4>syntax: (change-dir <em>str-path</em>)</h4>

<p>
	Changes the current directory to be the one given in <em>str-path</em>.
	If successful, <tt>true</tt> is returned; otherwise <tt>nil</tt> is returned.
</p>

<!-- example -->

<pre>
(change-dir "/etc")
</pre>


<p>
	Makes <tt>/etc</tt> the current directory.
</p>

<br/><br/>

<a name="char"></a>
<h2><span class="function">char</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (char <em>str</em> [<em>int-index</em> [true]])<br/>
syntax: (char <em>int</em>)</h4>

<p>Given a string argument, extracts the character at <em>int-index</em> from <em>str</em>,
returning either the ASCII value of that character or the Unicode value on UTF-8 enabled 
versions of newLISP.</p>

<p>If <em>int-index</em> is omitted, 0 (zero) is assumed. If <em>int-idx</em>
is followed by a boolean <tt>true</tt> value, than the index treats <em>str</em> as an 8-bit  byte
array instead of an array of multi-byte UTF-8 characters.</p>

<p>The empty string returns <tt>nil</tt>. Both <tt>(char 0)</tt> and <tt>(char nil)</tt> will 
return <tt>"\000"</tt>.</p>

<p>
See <a href="#indexing">Indexing elements of strings and lists</a>.
</p>

<p>
Given an integer argument, 
<tt>char</tt> returns a string containing the ASCII character 
with value <em>int</em>.
</p>

<p>
On UTF-8&ndash;enabled versions of newLISP, the value in <em>int</em> 
is taken as Unicode and a UTF-8 character is returned.
</p>

<!-- example -->

<pre>
(char "ABC")          <span class='arw'>&rarr;</span> 65  ; ASCII code for "A"
(char "ABC" 1)        <span class='arw'>&rarr;</span> 66  ; ASCII code for "B"
(char "ABC" -1)       <span class='arw'>&rarr;</span> 67  ; ASCII code for "C"
(char "B")            <span class='arw'>&rarr;</span> 66  ; ASCII code for "B"
(char "Ω")            <span class='arw'>&rarr;</span> 937 ; UTF-8 code for "Ω"
(char "Ω" 1 true)     <span class='arw'>&rarr;</span> 169 ; byte value at offset 1

(char 65)  <span class='arw'>&rarr;</span> "A"
(char 66)  <span class='arw'>&rarr;</span> "B"

(char (char 65))  <span class='arw'>&rarr;</span> 65      ; two inverse applications

(map char (sequence 1 255))  ; returns current character set

; The Zen of UTF-8
(char (&amp; (char "生") (char "死"))) <span class='arw'>&rarr;</span> 愛 ; by @kosh_bot
</pre>

<br/><br/>

<a name="chop"></a>
<h2><span class="function">chop</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (chop <em>str</em> [<em>int-chars</em>])<br/>
syntax: (chop <em>list</em> [<em>int-elements</em>])</h4>

<p>
	If the first argument evaluates to a string, 
	<tt>chop</tt> returns a copy of <em>str</em> 
	with the last <em>int-char</em> characters omitted.
	If the <em>int-char</em> argument is absent, 
	one character is omitted.
	<tt>chop</tt> does not alter <em>str</em>.</p>


<p>
	If the first argument evaluates to a list, 
	a copy of <em>list</em> is returned 
	with <em>int-elements</em> omitted 
	(same as for strings).
</p>

<!-- example -->

<pre>
(set 'str "newLISP")  <span class='arw'>&rarr;</span> "newLISP"
                      
(chop str)    <span class='arw'>&rarr;</span> "newLIS"
(chop str 2)  <span class='arw'>&rarr;</span> "newLI"
                      
str  <span class='arw'>&rarr;</span> "newLISP"

(set 'lst '(a b (c d) e))

(chop lst)    <span class='arw'>&rarr;</span> (a b (c d))
(chop lst 2)  <span class='arw'>&rarr;</span> (a b)
                      
lst  <span class='arw'>&rarr;</span> (a b (c d) e)
</pre>

<br/><br/>

<a name="clean"></a>
<h2><span class="function">clean</span></h2>
<h4>syntax: (clean <em>exp-predicate</em> <em>list</em>)</h4>

<p>
	The predicate <em>exp-predicate</em> is applied 
	to each element of <em>list</em>. 
	In the returned list, 
	all elements for which <em>exp-predicate</em> is <tt>true</tt> 
	are eliminated.
</p>

<p>
	<tt>clean</tt> works like <a href="#filter">filter</a> 
	with a negated predicate.
</p>

<!-- example -->

<pre>
(clean symbol? '(1 2 d 4 f g 5 h))   <span class='arw'>&rarr;</span> (1 2 4 5)

(filter symbol? '(1 2 d 4 f g 5 h))  <span class='arw'>&rarr;</span> (d f g h)

(define (big? x) (&gt; x 5))        <span class='arw'>&rarr;</span> (lambda (x) (&gt; x 5))

(clean big? '(1 10 3 6 4 5 11))  <span class='arw'>&rarr;</span> (1 3 4 5)

(clean &lt;= '(3 4 -6 0 2 -3 0))  <span class='arw'>&rarr;</span> (3 4 2)

(clean (curry match '(a *)) '((a 10) (b 5) (a 3) (c 8) (a 9)))
<span class='arw'>&rarr;</span>  ((b 5) (c 8))
</pre>


<p>
	The predicate may be a built-in predicate 
	or a user-defined function or lambda expression.
</p>

<p>
	For cleaning numbers from one list 
	using numbers from another, 
	use <a href="#difference">difference</a> 
	or <a href="#intersect">intersect</a> 
	(with the list mode option).
</p>

<p>
	See also the related function <a href="#index">index</a>, 
	which returns the indices of the remaining elements, 
	and <a href="#filter">filter</a>, 
	which returns all elements for which a predicate returns true.
</p>

<br/><br/>


<a name="close"></a>
<h2><span class="function">close</span></h2>
<h4>syntax: (close <em>int-file</em>)</h4>

<p>
	Closes the file specified by the file handle in <em>int-file</em>. 
	The handle would have been obtained 
	from a previous <a href="#open">open</a> operation. 
	If successful, <tt>close</tt> returns <tt>true</tt>; otherwise  <tt>nil</tt> is returned.
</p>

<!-- example -->

<pre>
(close (device))  <span class='arw'>&rarr;</span> true
(close 7)         <span class='arw'>&rarr;</span> true
(close aHandle)   <span class='arw'>&rarr;</span> true
</pre>


<p>
	Note that using <tt>close</tt> on <a href="#device">device</a> 
	automatically resets it to 0 (zero, the screen device).
</p>
	
<br/><br/>

<a name="collect"></a>
<h2><span class="function">collect</span></h2>
<h4>syntax: (collect <em>exp</em> [<em>int-max-count</em>])</h4>

<p>Evaluates the expression in <em>exp</em>  and collects the results in a list
until evaluation of <em>exp</em> returns <tt>nil</tt>.</p>

<p>Optionally a maximum count of elements can be specified in <em>int-max-count</em>.</p>

<pre>
; collect results until nil is returned
(set 'x 0)
(collect (if (&lt;= (inc x) 10) x)) <span class='arw'>&rarr;</span> (1 2 3 4 5 6 7 8 9 10)

; collect results until nil is returned or 6 results are collected
(set 'x 0)
(collect (if (&lt;= (inc x) 10) x) 6) <span class='arw'>&rarr;</span> (1 2 3 4 5 6)
</pre>

<br/><br/>

<a name="command-event"></a>
<h2><span class="function">command-event</span></h2>
<h4>syntax: (command-event <em>sym-event-handler</em> | <em>func-event-handler</em>)<br/>
syntax: (command-event nil)</h4>

<p>Specifies a user defined function for pre-processing the newLISP command-line
before it gets evaluated. This can be used to write customized interactive
 newLISP shells and to transform HTTP requests when running in server mode.</p>

<p><tt>command-event</tt> takes either a symbol of a user-defined function or a lambda 
function. The event-handler function must return a string or the command-line will be 
passed untranslated to newLISP.</p>

<p>To only force a prompt and disable command processing, the function should return 
the empty string <tt>""</tt>. To reset <tt>command-event</tt>, use the second syntax.</p>

<p>The following example makes the newLISP shell work like a normal Unix
shell when the command starts with a letter. But starting the line with an open
parenthesis or a space initiates a newLISP evaluation.</p>

<!-- example -->

<pre>
(command-event (fn (s) 
	(if (starts-with s "[a-zA-Z]" 0) (append "!" s) s)))
</pre>


<p>See also the related <a href="#prompt-event">prompt-event</a> which can be used
for further customizing interactive mode by modifying the newLISP prompt.</p>

<p>The following program can be used either stand-alone or included in newLISP's
<tt>init.lsp</tt> startup file:</p>

<pre>
#!/usr/local/bin/newlisp

; set the prompt to the current directory name
(prompt-event (fn (ctx) (append (real-path) "&gt; ")))

; pre-process the command-line
(command-event (fn (s) 
    (if 
        (starts-with s "cd") 
        (string " " (true? (change-dir (last (parse s " ")))))

        (starts-with s "[a-zA-Z]" 0)
        (append "!" s)

        true s)))
</pre> 

<p>In the definition of the command-line translation function the Unix
command <tt>cd</tt> gets a special treatment, to make sure that the directory
is changed for newLISP process too. This way when shelling out with <tt>!</tt> and
coming back, newLISP will maintain the changed directory.</p>

<p>Command lines for newLISP must start either with a space or an opening
parenthesis. Unix commands must start at the beginning of the line.</p>

<p>When newLISP is running in server mode either using the <tt>-c</tt> or
<tt>-http</tt> option, it receives HTTP requests similar to the following:</p>

<pre>
GET /index.html
</pre> 

<p>Or if a query is involved:</p>

<pre>
GET /index.cgi?userid=joe&amp;password=secret
</pre> 

<p>A function specified by <tt>command-event</tt> could filter and transform 
these request lines, e.g.: discovering all queries trying to perform CGI using
a file ending in <tt>.exe</tt>.&nbsp; Such a request would be translated into a
request for an error page:</p>

<pre>
;; httpd-conf.lsp
;;
;; filter and translate HTTP requests for newLISP
;; -c or -http server modes
;; reject query commands using CGI with .exe files

(command-event (fn (s)
    (let (request s)
        (when (find "?" s) ; is this a query
            (set 'request (first (parse s "?")))
            ; discover illegal extension in queries
            (when (ends-with request ".exe")
                (set 'request "GET /errorpage.html")) )
        request)
))
</pre> 

<p>When starting the server mode with <tt>newlisp httpd-conf.lsp -c -d80 -w ./httpdoc</tt>
newLISP will load the definition for <tt>command-event</tt> for filtering incoming
requests, and the query:</p>

<pre>
GET /cmd.exe?dir
</pre> 

<p>Would be translated into:</p>

<pre>
GET /errorpage.html
</pre> 

<p>The example shows a technique frequently used in the past by spammers on MS 
Windows based, bad configured web servers to gain control over servers.</p>

<p><tt>httpd-conf.lsp</tt> files can easily be debugged loading the file into an interactive
newLISP session and entering the HTTP requests manually. newLISP will translate the command
line and dispatch it to the built-in web server. The server output will appear in the shell
window.</p>

<p>Note, that the command line length as well as the line length in HTTP headers is limited to 512 characters for newLISP.</p>

<br/><br/>

<a name="cond"></a>
<h2><span class="function">cond</span></h2>

<h4>syntax: (cond (<em>exp-condition-1</em> <em>body-1</em>) [(<em>exp-condition-2</em> <em>body-2</em>) ... ])</h4>

<p>
	Like <tt>if</tt>, <tt>cond</tt> conditionally evaluates the expressions 
	within its body. 
	The <em>exp-condition</em>s are evaluated in turn, 
	until some <em>exp-condition-i</em> is found 
	that evaluates to anything other than <tt>nil</tt> 
	or an empty list <tt>()</tt>.
	The result of evaluating <em>body-i</em> 
	is then returned as the result of the entire <em>cond-expression</em>. 
	If all conditions evaluate to <tt>nil</tt> 
	or an empty list,
	<em>cond</em> returns the value of the last <em>cond-expression</em>.
</p>

<!-- example -->

<pre>
(define (classify x)
  (cond
    ((&lt; x 0) "negative")
    ((&lt; x 10) "small")
    ((&lt; x 20) "medium")
    ((&gt;= x 30) "big")))

(classify 15)   <span class='arw'>&rarr;</span> "medium"
(classify 22)   <span class='arw'>&rarr;</span> "nil"
(classify 100)  <span class='arw'>&rarr;</span> "big"
(classify -10)  <span class='arw'>&rarr;</span> "negative"
</pre>


<p>
	When a <em>body-n</em> is missing, 
	the value of the last <em>cond-expression</em> evaluated 
	is returned. 
	If no condition evaluates to <tt>true</tt>, 
	the value of the last conditional expression is returned
	(i.e., <tt>nil</tt> or an empty list).
</p>


<pre>
(cond ((+ 3 4)))  <span class='arw'>&rarr;</span> 7
</pre>


<p>
	When used with multiple arguments, 
	the function <a href="#if">if</a> 
	behaves like <tt>cond</tt>, 
	except it does not need extra parentheses 
	to enclose the condition-body pair 
	of expressions.
</p>

<br/><br/>

<a name="cons"></a>
<h2><span class="function">cons</span></h2>
<h4>syntax: (cons <em>exp-1</em> <em>exp-2</em>)</h4>

<p>
	If <em>exp-2</em> evaluates to a list, 
	then a list is returned with the result of evaluating <em>exp-1</em> 
	inserted as the first element. 
	If <em>exp-2 </em>evaluates to anything other than a list, 
	the results of evaluating <em>exp-1</em> and <em>exp-2</em> 
	are returned in a list. 
	Note that there is no <em>dotted pair</em> in newLISP:
	<em>cons</em>ing two atoms constructs a list, not a dotted pair.
</p>

<!-- example -->

<pre>
(cons 'a 'b)            <span class='arw'>&rarr;</span> (a b)
(cons 'a '(b c))        <span class='arw'>&rarr;</span> (a b c)
(cons (+ 3 4) (* 5 5))  <span class='arw'>&rarr;</span> (7 25)
(cons '(1 2) '(3 4))    <span class='arw'>&rarr;</span> ((1 2) 3 4)
(cons nil 1)            <span class='arw'>&rarr;</span> (nil 1)
(cons 1 nil)            <span class='arw'>&rarr;</span> (1 nil)
(cons 1)                <span class='arw'>&rarr;</span> (1)
(cons)			<span class='arw'>&rarr;</span> ()
</pre>


<p>
	Unlike other Lisps that return <tt>(s)</tt>
	as the result of the expression <tt>(cons 's nil)</tt>, 
	newLISP's <tt>cons</tt> returns <tt>(s nil)</tt>. 
	In newLISP, <tt>nil</tt> is a Boolean value 
	and is not equivalent to an empty list, 
	and a newLISP cell holds only one value.
</p>

<p>
	<tt>cons</tt> behaves like the inverse operation of <a href="#first">first</a>
and <a href="#rest">rest</a> 
	(or <a href="#first">first</a> and <a href="#last">last</a> if the list is a pair):
</p>


<pre>
(cons (first '(a b c)) (rest '(a b c)))  <span class='arw'>&rarr;</span> (a b c)

(cons (first '(x y)) (last '(x y)))      <span class='arw'>&rarr;</span> (x y)
</pre>

<br/><br/>

<a name="constant"></a>
<h2><span class="function">constant</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (constant <em>sym-1</em> <em>exp-1</em> [<em>sym-2</em> <em>exp-2</em>] ...)</h4>

<p>
	Identical to <a href="#set">set</a> in functionality,
	<tt>constant</tt> further protects the symbols from subsequent modification. 
	A symbol set with <tt>constant</tt> can only be modified 
	using the <tt>constant</tt> function again.  
	When an attempt is made to modify the contents of a symbol protected with <tt>constant</tt>, 
	newLISP generates an error message. 
	Only symbols from the current context can be used with <tt>constant</tt>. 
	This prevents the overwriting of symbols 
	that have been protected in their home context.
	The last <em>exp-n</em> initializer is always optional.
</p>

<p>
	Symbols initialized with <a href="#set">set</a>, <a href="#define">define</a>, 
	or <a href="#define-macro"> define-macro</a> can still be protected by using 
	the <tt>constant</tt> function:
</p>

<pre>
(constant 'aVar 123)  <span class='arw'>&rarr;</span> 123
(set 'aVar 999) 
<span class='err'>ERR: symbol is protected in function set: aVar</span>

(define (double x) (+ x x))

(constant 'double)

;; equivalent to

(constant 'double (fn (x) (+ x x)))
</pre>


<p>
	The first example defines a constant, <tt>aVar</tt>, 
	which can only be changed by using another <tt>constant</tt> statement. 
	The second example protects <tt>double</tt> from being changed
	(except by <tt>constant</tt>). 
	Because a function definition in newLISP 
	is equivalent to an assignment of a lambda function, 
	both steps can be collapsed into one, 
	as shown in the last statement line. 
	This could be an important technique 
	for avoiding protection errors 
	when a file is loaded multiple times.
</p>

<p>
	The last value to be assigned can be omitted. 
	<tt>constant</tt> returns the contents of
	the last symbol set and protected.
</p>

<p>
	Built-in functions can be assigned to symbols 
	or to the names of other built-in functions, 
	effectively redefining them as different functions.
	There is no performance loss when renaming functions.
</p>


<pre>
(constant 'squareroot sqrt)  <span class='arw'>&rarr;</span> sqrt &lt;406C2E&gt;
(constant '+ add)            <span class='arw'>&rarr;</span> add &lt;4068A6&gt;
</pre>


<p>
	<tt>squareroot</tt> will behave like <tt>sqrt</tt>. 
	The <tt>+</tt> (plus sign) is redefined 
	to use the mixed type floating point mode of <tt>add</tt>. 
	The hexadecimal number displayed in the result 
	is the binary address of the built-in function 
	and varies on different platforms and OSes.
</p>

<br/><br/>

<a name="context"></a>
<h2><span class="function">context</span></h2>
<h4>syntax: (context [<em>sym-context</em>])<br/>
syntax: (context <em>sym-context</em> <em>str | sym</em> [<em>exp-value</em>])</h4>

<p> In the first syntax, <tt>context</tt> is used to switch to a different context namespace. 
Subsequent <a href="#load">load</a>s of newLISP source or functions like 
<a href="#eval-string">eval-string</a> and <a href="#sym">sym</a> will put newly created 
symbols and function definitions in the new context.</p>

<p>If the context still needs to be created, the symbol for the new context should be specified. 
When no argument is passed to <tt>context</tt>, then the symbol for the current context is returned. </p>

<p>Because contexts evaluate to themselves, a quote is not necessary 
to switch to a different context if that context already exists.
</p>

<!-- example -->

<pre>
(context 'GRAPH)          ; create / switch context GRAPH

(define (foo-draw x y z)  ; function resides in GRAPH
  (&hellip;))
                                
(set 'var 12345)
(symbols)  <span class='arw'>&rarr;</span> (foo-draw var)  ; GRAPH has now two symbols

(context MAIN)               ; switch back to MAIN (quote not required)

(print GRAPH:var) <span class='arw'>&rarr;</span> 12345    ; contents of symbol in GRAPH

(GRAPH:foo-draw 10 20 30)    ; execute function in GRAPH
(set 'GRAPH:var 6789)        ; assign to a symbol in GRAPH
</pre>


<p>
	If a context symbol is referred to before the context exists, 
	the context will be created implicitly.
</p>


<pre>
(set 'person:age 0)       ; no need to create context first
(set 'person:address "")  ; useful for quickly defining data structures
</pre>


<p>
	Contexts can be copied:
</p>


<pre>
(new person 'JohnDoe)  <span class='arw'>&rarr;</span>  JohnDoe

(set 'JohnDoe:age 99)
</pre>


<p>
	Contexts can be referred to by a variable:
</p>


<pre>
(set 'human JohnDoe)

human:age  <span class='arw'>&rarr;</span> 99

(set 'human:address "1 Main Street")

JohnDoe:address  <span class='arw'>&rarr;</span> "1 Main Street"
</pre>



<p>An evaluated context (no quote) can be given as an argument:
</p>


<pre>
<b>&gt;</b> (context 'FOO)
<b>FOO</b>
<b>FOO></b> (context MAIN)
<b>MAIN</b>
<b>&gt;</b> (set 'old FOO)
FOO
<b>&gt;</b> (context 'BAR)
<b>BAR</b>
<b>BAR></b> (context MAIN:old)
<b>FOO</b>
<b>FOO></b> 
</pre>



<p>
	If an identifier with the same symbol already exists, 
	it is redefined to be a context.
</p>

<p>
	Symbols within the current context 
	are referred to simply by their names, 
	as are built-in functions and special symbols 
	like <tt>nil</tt> and <tt>true</tt>. 
	Symbols outside the current context 
	are referenced by prefixing the symbol name 
	with the context name and a <tt>:</tt> (colon). 
	To quote a symbol in a different context, 
	prefix the context name with a <tt>'</tt> (single quote).
</p>

<p>
	Within a given context, symbols may be created 
	with the same name as built-in functions 
	or context symbols in MAIN.
	This overwrites the symbols in MAIN 
	when they are prefixed with a context:
</p>


<pre>
(context 'CTX)
(define (CTX:new var)
    (&hellip;))
    
(context 'MAIN)
</pre>


<p><tt>CTX:new</tt> will overwrite new in MAIN.</p>

<p> In the second syntax, <tt>context</tt> can be used to create symbols in a namespace.
Note that this should not be used for creating hashes or dictionaries. For a shorter,
more convenient method to use namespaces as hash-like dictionaries, see the chapter 
<a href="#hash">Hash functions and dictionaries</a>.
</p>


<pre>
;; create a symbol and store data in it
(context 'Ctx "abc" 123)   <span class='arw'>&rarr;</span> 123
(context 'Ctx 'xyz 999)    <span class='arw'>&rarr;</span> 999

;; retrieve contents from  symbol
(context 'Ctx "abc")       <span class='arw'>&rarr;</span> 123
(context 'Ctx 'xyz)        <span class='arw'>&rarr;</span> 999
Ctx:abc                    <span class='arw'>&rarr;</span> 123
Ctx:xyz                    <span class='arw'>&rarr;</span> 999
</pre>


<p>
The first three statements create a symbol and store a value of any data type inside. 
The first statement also creates the context named <tt>Ctx</tt>.
When a symbol is specified for the name, the name is taken
from the symbol and creates a symbol with the same name
in the context <tt>Ctx</tt>.
</p>

<p>
Symbols can contain spaces or any other special characters 
not typically allowed in newLISP symbols being used as variable names. 
This second syntax of <tt>context</tt> only creates the new symbol 
and returns the value contained in it.  It does not switch to the new namespace.
</p>

<br/><br/>

<a name="contextp"></a>
<h2><span class="function">context?</span></h2>

<h4>syntax: (context? <em>exp</em>)<br/>
syntax: (context? <em>exp</em> <em>str-sym</em>)</h4>

<p>
	In the first syntax, 
	<em>context?</em> is a predicate that returns <tt>true</tt> 
	only if <em>exp</em> evaluates to a context; 
	otherwise, it returns <tt>nil</tt>.
</p>

<!-- example -->

<pre>
(context? MAIN)  <span class='arw'>&rarr;</span> true
(set 'x 123)
(context? x)     <span class='arw'>&rarr;</span> nil

(set 'FOO:q "hola")  <span class='arw'>&rarr;</span> "hola"
(set 'ctx FOO)
(context? ctx)       <span class='arw'>&rarr;</span> true  ; ctx contains context foo
</pre>


<p>
	The second syntax checks for the existence of a symbol in a context. 
	The symbol is specified by its name string in <em>str-sym</em>.
</p>


<pre>
(context? FOO "q")  <span class='arw'>&rarr;</span> true
(context? FOO "p")  <span class='arw'>&rarr;</span> nil
</pre>


<p>
	Use <a href="#context">context</a> to change and create namespaces 
	and to create hash symbols in contexts.
</p>

<br/><br/>

<a name="copy"></a>
<h2><span class="function">copy</span></h2>
<h4>syntax: (copy <em>exp</em>)<br/>
syntax: (copy <em>int-addr</em> [<em>bool-flag</em>])</h4>

<p>The first syntax makes a copy from evaluating expression in <em>exp</em>. 
Some built-in functions are <a href="#destructice">destructive</a>, changing 
the original contents of a list, array or string they are working on. 
With <tt>copy</tt> their behavior can be made non-destructive.</p>

<pre>
(set 'aList '(a b c d e f))

(replace 'c (copy aList)) <span class='arw'>&rarr;</span> (a b d e f)

aList <span class='arw'>&rarr;</span> (a b c d e f)

(set 'str "newLISP") <span class='arw'>&rarr;</span> "newLISP"

(rotate (copy str)) <span class='arw'>&rarr;</span> "PnewLIS"

str <span class='arw'>&rarr;</span> "newLISP" 
</pre>

<p>Using <tt>copy</tt> the functions <a href="#replace">replace</a> and
<a href="#rotate">rotate</a> are prevented from changing the data.
A modified version of the data is returned.</p>

<p>The second syntax, marked by the <tt>true</tt> in <em>bool-flag</em>,
copies a newLISP expression from a memory address.The following two
expressions are equivalent:</p>

<pre>
(set 'x "hello world")
(copy x)  <span class='arw'>&rarr;</span> "hello world"
(copy (first (dump x)) true) <span class='arw'>&rarr;</span> "hello world"
</pre>

<p>The second syntax can be useful when interfacing with C-code generating
newLISP expressions.</p>


<br/><br/>

<a name="copy-file"></a>
<h2><span class="function">copy-file</span></h2>
<h4>syntax: (copy-file <em>str-from-name</em> <em>str-to-name</em>)</h4>

<p>
	Copies a file from a path-filename given in <em>str-from-name</em> 
	to a path-filename given in <em>str-to-name</em>. 
	Returns <tt>true</tt> if the copy was successful or <tt>nil</tt>, 
	if the copy was unsuccessful.
</p>

<!-- example -->

<pre>
(copy-file "/home/me/newlisp/data.lsp" "/tmp/data.lsp")
</pre>

<br/><br/>

<a name="corr"></a>
<h2><span class="function">corr</span></h2>
<h4>syntax: (corr <em>list-vector-X</em> <em>list-vector-Y</em>)</h4>

<p>Calculates the <em>Pearson</em> product-moment correlation coefficient as a measure
of the linear relationship between the two variables in  <em>list-vector-X</em>
and  <em>list-vector-Y</em>. Both lists must be of same length.</p>

<p><tt>corr</tt> returns a list containing the following values:</p>

<table>
<tr align="left"><th>name</th><th>description</th></tr>
<tr><td>r</td><td>Correlation coefficient</td></tr>
<tr><td>b0</td><td>Regression coefficient offset</td></tr>
<tr><td>b1</td><td>Regression coefficient slope</td></tr>
<tr><td>t</td><td>t - statistic for significance testing</td></tr>
<tr><td>df</td><td>Degrees of freedom for t</td></tr>
<tr><td>p</td><td>Two tailed probability of t under the null hypothesis</td></tr>
</table>
<br />
<!-- example -->

<pre>
(set 'study-time '(90 100 130 150 180 200 220 300 350 400))
(set 'test-errors '(25 28 20 20 15 12 13 10 8 6))

(corr study-time test-errors) <span class='arw'>&rarr;</span> (-0.926 29.241 -0.064 -6.944 8 0.0001190)
</pre>

<p>The negative correlation of <tt>-0.926</tt> between study time and test errors is 
highly significant with a two-tailed <tt>p</tt> of about <tt>0.0001</tt> under the null hypothesis.</p>

<p>The regression coefficients <tt>b0 = 29.241</tt> and <tt>b1 = -0.064</tt>
can be used to estimate values of the Y variable (test errors) from values in X (study time)
using the equation <tt><em><b>Y = b0 + b1 * X</b></em></tt>.</p>

<br/><br/>

<a name="cos"></a>
<h2><span class="function">cos</span></h2>
<h4>syntax: (cos <em>num-radians</em>)</h4>

<p>
	Calculates the cosine of <em>num-radians</em>
	and returns the result.
</p>

<!-- example -->

<pre>
(cos 1)                     <span class='arw'>&rarr;</span> 0.5403023059
(set 'pi (mul 2 (acos 0)))  <span class='arw'>&rarr;</span> 3.141592654
(cos pi)                    <span class='arw'>&rarr;</span> -1
</pre>

<br/><br/>

<a name="cosh"></a>
<h2><span class="function">cosh</span></h2>
<h4>syntax: (cosh <em>num-radians</em>)</h4>

<p>Calculates the hyperbolic cosine of <em>num-radians</em>. 
The hyperbolic cosine is defined mathematically as: 
<em>(exp (x) + exp (-x)) / 2</em>.
An overflow to <tt>inf</tt> may occur 
if <em>num-radians</em> is too large.</p>

<!-- example -->

<pre>
(cosh 1)     <span class='arw'>&rarr;</span> 1.543080635
(cosh 10)    <span class='arw'>&rarr;</span> 11013.23292
(cosh 1000)  <span class='arw'>&rarr;</span> inf
(= (cosh 1) (div (add (exp 1) (exp -1)) 2))  <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="count"></a>
<h2><span class="function">count</span></h2>
<h4>syntax: (count <em>list-1</em> <em>list-2</em>)</h4>

<p>
	Counts elements of <em>list-1</em> in <em>list-2</em> 
	and returns a list of those counts.
</p>

<!-- example -->

<pre>
(count '(1 2 3) '(3 2 1 4 2 3 1 1 2 2))  <span class='arw'>&rarr;</span> (3 4 2)
(count '(z a) '(z d z b a z y a))        <span class='arw'>&rarr;</span> (3 2)

(set 'lst (explode (read-file "myFile.txt")))
(set 'letter-counts (count (unique lst) lst))
</pre>

<p>
	The second example counts all occurrences 
	of different letters in <tt>myFile.txt</tt>.
</p>

<p>
	The first list in <tt>count</tt>, 
	which specifies the items to be counted in the second list, 
	should be unique. 
	For items that are not unique, 
	only the first instance will carry a count; 
	all other instances will display <tt>0</tt> (zero).
</p>

<br/><br/>

<a name="cpymem"></a>
<h2><span class="function">cpymem</span>&nbsp; 
<a href="#shared-lib"><font size="+2">&#x26A0;</font></a></h2>
<h4>syntax: (cpymem <em>int-from-address</em> <em>int-to-address</em> <em>int-bytes</em>)</h4>

<p>Copies <em>int-bytes</em> of memory from <em>int-from-address</em> 
to <em>int-to-address</em>. This function can be used for 
direct memory writing/reading or for hacking newLISP internals
(e.g., type bits in newLISP cells, or building functions with binary 
executable code on the fly).</p>

<p>Note that this function should only be used when familiar with newLISP internals.
<tt>cpymem</tt> can crash the system or make it unstable if used incorrectly.</p>

<!-- example -->

<pre>
(set 's "0123456789")

(cpymem "xxx" (+ (address s) 5) 3)

s  <span class='arw'>&rarr;</span> "01234xxx89")
</pre>


<p>The example copies a string directly into a string variable.</p>

<p>The following example creates a new function from scratch, 
runs a piece of binary code, and adds up two numbers. 
This assembly language snippet shows the x86 (Intel CPU) code 
to add up two numbers and return the result:</p>


<pre>
 55       push ebp
 8B EC    mov  ebp, esp
 8B 45 08 mov  eax, [ebp+08]
 03 45 0C add  eax, [ebp+0c]
 5D       pop  ebp
 C3       ret

 ; for Win32/stdcall change last line
 C2 08 00 ret 
</pre>


<p>The binary representation is attached to a new function created 
in newLISP:</p>


<pre>
; set up 32-bit version of machine code
; on Windows use 32-bit version of newLISP
(set 'foo-code (append
     (pack "bbbbbbbbbb" 0x55 0x8B 0xEC 0x8B 0x45 0x08 0x03 0x45 0x0C 0x5D)
     (if (= ostype "Windows") (pack "bbb" 0xC2 0x08 0x00) (pack "b" 0xC3))))

; put a function cell template into foo, protect symbol from deletion
(constant 'foo print)

; put the correct type, either 'stdcall' or 'cdecl'
(cpymem (pack "ld" (if (= ostype "Windows") 8456 4360)) (first (dump foo)) 4)

; put the address of foo-code into the new function cell
(cpymem (pack "ld" (address foo-code)) (+ (first (dump foo)) 12) 4)

; take the name address from the foo symbol, copy into function cell
(set 'sym-name (first (unpack "lu" (+ (address 'foo) 8))))
(cpymem (pack "ld" sym-name) (+ (first (dump foo)) 8) 4)

; test the new function
(println "3 * 4 -> " (foo 3 4))
</pre>

<p>The last example will not work on all hardware platforms and OSs.</p>

<p>Use the <a href="#dump">dump</a> function to retrieve binary addresses 
and the contents from newLISP cells.</p>


<br/><br/>

<a name="crc32"></a>
<h2><span class="function">crc32</span></h2>
<h4>syntax: (crc32 <em>str-data</em>)</h4>

<p>
	Calculates a running 32-bit CRC (Circular Redundancy Check) sum 
	from the buffer in <em>str-data</em>, 
	starting with a CRC of <tt>0xffffffff</tt> for the first byte. 
	<tt>crc32</tt> uses an algorithm published 
	by <a href="http://www.w3.org">www.w3.org</a>.
</p>

<!-- example -->

<pre>
(crc32 "abcdefghijklmnopqrstuvwxyz")  <span class='arw'>&rarr;</span> 1277644989
</pre>


<p>
	<tt>crc32</tt> is often used to verify data integrity 
	in unsafe data transmissions.
</p>

<br/><br/>

<a name="crit-chi2"></a>
<h2><span class="function">crit-chi2</span></h2>

<h4>syntax: (crit-chi2 <em>num-probability</em> <em>int-df</em>)</h4>

<p>Calculates the critical minimum <em>Chi&sup2;&nbsp;</em> for a given confidence probability 
<em>num-probability</em> under the null hypothesis and the degrees of freedom in
<em>int-df&nbsp;</em> for testing the significance of a statistical null hypothesis.</p>

<p>Note that versions prior to 10.2.0 took <em>(1.0 - p)</em> for the probability
instead of <em>p</em>.</p>

<!-- example -->

<pre>
(crit-chi2 0.01 4)  <span class='arw'>&rarr;</span> 13.27670443
</pre>


<p>
	See also the inverse function <a href="#prob-chi2">prob-chi2</a>.
</p> 

<br/><br/>

<a name="crit-f"></a>
<h2><span class="function">crit-f</span></h2>

<h4>syntax: (crit-f <em>num-probability</em> <em>int-df1</em> <em>int-df2</em>)</h4>

<p>Calculates the critical minimum <em>F&nbsp;</em> for a given confidence probability
<em>num-probability</em> under the null hypothesis and the degrees of freedom 
given in <em>int-df1</em> and <em>int-df2</em> for testing the significance of a 
statistical null hypothesis using the <em>F-test</em>.</p>

<!-- example -->

<pre>
(crit-f 0.05 10 12)  <span class='arw'>&rarr;</span> 2.753386727
</pre>

<p>
See also the inverse function <a href="#prob-f">prob-f</a>.
</p> 

<br/><br/>

<a name="crit-t"></a>
<h2><span class="function">crit-t</span></h2>

<h4>syntax: (crit-t <em>num-probability</em> <em>int-df</em>)</h4>

<p>Calculates the critical minimum <em>Student's t</em> for a given confidence probability 
<em>num-probability</em> under the null hypothesis and the degrees of freedom in
<em>int-df&nbsp;</em> for testing the significance of a statistical null hypothesis.
</p>

<!-- example -->

<pre>
(crit-t 0.05 14)  <span class='arw'>&rarr;</span> 1.761310142
</pre>


<p>
See also the inverse function <a href="#prob-t">prob-t</a>.
</p> 

<br/><br/>


<a name="crit-z"></a>
<h2><span class="function">crit-z</span></h2>
<h4>syntax: (crit-z <em>num-probability</em>)</h4>

<p>Calculates the critical normal distributed Z value 
of a given cumulated probability <em>num-probability</em> 
for testing of statistical significance and confidence intervals.</p>

<!-- example -->

<pre>
(crit-z 0.999)  <span class='arw'>&rarr;</span> 3.090232372
</pre>


<p>
	See also the inverse function <a href="#prob-z">prob-z</a>.
</p> 

<br/><br/>

<a name="current-line"></a>
<h2><span class="function">current-line</span></h2>
<h4>syntax: (current-line)</h4>

<p>
	Retrieves the contents of the last 
	<a href="#read-line">read-line</a> operation. 
	<tt>current-line</tt>'s contents are also implicitly used 
	when <a href="#write-line">write-line</a> 
	is called without a string parameter.
</p>

<p>
	The following source shows the typical code pattern 
	for creating a Unix command-line filter:
</p>

<!-- example -->

<pre>
#!/usr/local/bin/newlisp
 
(set 'inFile (open (main-args 2) "read"))
(while (read-line inFile) 
  (if (starts-with (current-line) ";;")
    (write-line)))
(exit)
</pre>


<p>
	The program is invoked:
</p>


<pre>
./filter myfile.lsp
</pre>


<p>
	This displays all comment lines starting with <tt>;;</tt> 
	from a file given as a command-line argument 
	when invoking the script <tt>filter</tt>.
</p>

<br/><br/>

<a name="curry"></a>

<h2><span class="function">curry</span></h2>
<h4>syntax: (curry <em>func</em> <em>exp</em>)</h4>

<p>Transforms <em>func</em> from a function <em>f(x, y)</em> that takes 
two arguments into a function <em>fx(y)</em> that takes a single argument. 
<tt>curry</tt> works like a macro in that it does not evaluate its arguments. 
Instead, they are evaluated during the application of <em>func</em>.</p>


<!-- example -->

<pre>
(set 'f (curry + 10))  <span class='arw'>&rarr;</span> (lambda ($x) (+ 10 $x))

(f 7)  <span class='arw'>&rarr;</span> 17

(filter (curry match '(a *)) '((a 10) (b 5) (a 3) (c 8) (a 9)))
<span class='arw'>&rarr;</span>  ((a 10) (a 3) (a 9))

(clean (curry match '(a *)) '((a 10) (b 5) (a 3) (c 8) (a 9)))
<span class='arw'>&rarr;</span>  ((b 5) (c 8))

(map (curry list 'x) (sequence 1 5))
<span class='arw'>&rarr;</span>  ((x 1) (x 2) (x 3) (x 4) (x 5))
</pre>


<p><tt>curry</tt> can be used on all functions taking two arguments.</p>

<br/><br/>

<a name="date"></a>

<h2><span class="function">date</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (date)<br/>
syntax: (date <em>int-secs</em> [<em>int-offset</em>])<br/>
syntax: (date <em>int-secs</em> <em>int-offset</em> <em>str-format</em>)</h4>

<p>
The first syntax returns the local time zone's 
current date and time as a string representation.
If <em>int-secs</em> is out of range, <tt>nil</tt> is returned.    
</p>

<p>
In the second syntax, <tt>date</tt> translates the number of seconds 
in <em>int-secs</em> into its date/time string representation 
for the local time zone. 
The number in <em>int-secs</em> is usually retrieved from the system 
using <a href="#date-value">date-value</a>. 
Optionally, a time-zone offset (in minutes) can be specified 
in <em>int-offset</em>, which is added 
or subtracted before conversion of <em>int-sec</em> to a string.
If <em>int-secs</em> is out of range or an invalid <em>str-format</em>
is specified, an empty string <tt>""</tt> is returned.
</p>


<!-- example -->

<pre>
(date)                   <span class='arw'>&rarr;</span> "Fri Oct 29 09:56:58 2004"

(date (date-value))      <span class='arw'>&rarr;</span> "Sat May 20 11:37:15 2006" 
(date (date-value) 300)  <span class='arw'>&rarr;</span> "Sat May 20 16:37:19 2006"  ; 5 hours offset
(date 0)                 <span class='arw'>&rarr;</span> "Wed Dec 31 16:00:00 1969"
(date 0 (now 0 -2))      <span class='arw'>&rarr;</span> "Thu Jan  1 00:00:00 1970"  ; Unix epoch
</pre>

<p>The way the date and time are presented in a string 
depends on the underlying operating system.</p>

<p>The second example would show 1-1-1970 0:0 when in the Greenwich time zone, 
but it displays a time lag of 8 hours when in Pacific Standard Time (PST).
<tt>date</tt> assumes the <em>int-secs</em> given are in Coordinated Universal 
Time (UTC; formerly Greenwich Mean Time (GMT)) and converts it according to the 
local time-zone.</p>

<p>The third syntax makes the date string fully customizable by using a format 
specified in <em>str-format</em>.  This allows the day and month names to be 
translated into results appropriate for the current locale:</p>

<!-- example -->

<pre>
(set-locale "german") <span class='arw'>&rarr;</span> "de_DE"      

; on Linux - no leading 0 on day with %-d
(date (date-value) 0 "%A %-d. %B %Y")  <span class='arw'>&rarr;</span> "Montag  7. M&auml;rz 2005" 

(set-locale "C")  ; default POSIX

(date (date-value) 0 "%A %B %d %Y")    <span class='arw'>&rarr;</span> "Monday March 07 2005"

; suppressing leading 0 on MS Windows using #
(date (date-value) 0 "%a %#d %b %Y")   <span class='arw'>&rarr;</span> "Mon 7 Mar 2005" 

(set-locale "german")

(date (date-value) 0 "%x") <span class='arw'>&rarr;</span> "07.03.2005"   ; day month year

(set-locale "C")

(date (date-value) 0 "%x") <span class='arw'>&rarr;</span> "03/07/05"     ; month day year
</pre>


<p>The following table summarizes all format specifiers available 
on both MS Windows and Linux/Unix platforms.  More format options are 
available on Linux/Unix. For details, consult the manual page for 
the C function <tt>strftime()</tt> of the individual platform's C library.
</p>

<table width="98%"  summary="date formatting">
<tr align="left"><th>format</th><th>description</th></tr>
<tr><td>%a</td><td>abbreviated weekday name according to the current locale</td></tr>

<tr><td>%A</td><td>full weekday name according to the current locale</td></tr>
<tr><td>%b</td><td>abbreviated month name according to the current locale</td></tr>
<tr><td>%B</td><td>full month name according to the current locale</td></tr>
<tr><td>%c</td><td>preferred date and time representation for the current locale</td></tr>
<tr><td>%d</td><td>day of the month as a decimal number (range 01&ndash;31)</td></tr>
<tr><td>%H</td><td>hour as a decimal number using a 24-hour clock (range 00&ndash;23)</td></tr>

<tr><td>%I</td><td>hour as a decimal number using a 12-hour clock (range 01&ndash;12)</td></tr>
<tr><td>%j</td><td>day of the year as a decimal number (range 001&ndash;366)</td></tr>
<tr><td>%m</td><td>month as a decimal number (range 01&ndash;12)</td></tr>
<tr><td>%M</td><td>minute as a decimal number</td></tr>
<tr><td>%p</td><td>either 'am' or 'pm' according to the given time value or
the corresponding strings for the current locale</td></tr>
<tr><td>%S</td><td>second as a decimal number 0&ndash;61 (60 and 61 to account
for occasional leap seconds)</td></tr>

<tr><td>%U</td><td>week number of the current year as a decimal number,
starting with the first Sunday as the first day of the first week</td></tr>
<tr><td>%w</td><td>day of the week as a decimal, Sunday being 0</td></tr>
<tr><td>%W</td><td>week number of the current year as a decimal number,
starting with the first Monday as the first day of the first week</td></tr>
<tr><td>%x</td><td>preferred date representation for the current locale
without the time</td></tr>
<tr><td>%X</td><td>preferred time representation for the current locale
without the date</td></tr>
<tr><td>%y</td><td>year as a decimal number without a century (range 00&ndash;99)</td></tr>

<tr><td>%Y</td><td>year as a decimal number including the century</td></tr>
<tr><td>%z</td><td>time zone or name or abbreviation (same as %Z on MS Windows,
different on Unix)</td></tr>
<tr><td>%Z</td><td>time zone or name or abbreviation (same as %z on MS Windows,
different on Unix)</td></tr>
<tr><td>%%</td><td>a literal '%' character</td></tr>
</table><br/>

<p>
Leading zeroes in the display of decimal day numbers can be suppressed 
using <tt>"%-d"</tt> on Linux and FreeBSD and using <tt>"%e"</tt> 
on OpenBSD, SunOS/Solaris and macOS.  On MS Windows use <tt>"%#d"</tt>.
</p>

<p>See also <a href="#date-value">date-value</a>, <a href="#date-list">date-list</a>,
<a href="#date-parse">date-parse</a>, 
<a href="#time-of-day">time-of-day</a>, 
<a href="#time">time</a>, and <a href="#now">now</a>.
</p>

<br/><br/>

<a name="date-list"></a>
<h2><span class="function">date-list</span></h2>
<h4>syntax: (date-list <em>int-seconds</em> [<em>int-index</em>])<br/>
syntax: (date-list)</h4>

<p>Returns a list of year, month, date, hours, minutes, seconds, day of year 
and day of week from a time value given in seconds after January 1st, 1970 00:00:00.
The date and time values aren given as UTC, which may differ from the local timezone.
</p>
<p>When no parameters are given <tt>date-list</tt> generates the list from the
number of seconds for the current time, return of <tt>(date-value)</tt>.</p>

<p>The week-day value ranges from 1 to 7 for Monday thru Sunday.</p>

<pre>
(date-list 1282479244)      <span class='arw'>&rarr;</span> (2010 8 22 12 14 4 234 1)
(date-list 1282479244 0)    <span class='arw'>&rarr;</span> 2010 ; year
(date-list 1282479244 -2)   <span class='arw'>&rarr;</span> 234  ; day of year

(date-value (date-list 1282479244)) <span class='arw'>&rarr;</span> 1282479244

(date-list 0)   <span class='arw'>&rarr;</span> (1970 1 1 0 0 0 1 4) ; Thursday 1st, Jan 1970
</pre>

<p>A second optional <em>int-index</em> parameter can be used to return
a specific member of the list.</p>

<p><tt>date-list</tt> is the inverse operation of <a href="#date-value">date-value</a>.</p>

<br/><br/>

<a name="date-parse"></a>
<h2><span class="function">date-parse</span></h2>
<h4>syntax: (date-parse <em>str-date</em> <em>str-format</em>)</h4>

<p>Parses a date from a text string in <em>str-date</em>
using a format as defined in <em>str-format</em>, which uses
the same formatting rules found in <a href="#date">date</a>.
The function <tt>date-parse</tt> returns the number of UTC seconds passed   
since January 1st, 1970 UTC starting with 0 and up to 2147472000 for a date 
of January 19th, 2038.</p> 

<p>This function is not available on MS Windows platforms. The function was
named <tt>parse-date</tt> in previous versions. The old form is deprecated.</p>

<!-- example -->

<pre>
(date-parse "2007.1.3" "%Y.%m.%d")    <span class='arw'>&rarr;</span> 1167782400
(date-parse "January 10, 07" "%B %d, %y")    <span class='arw'>&rarr;</span> 1168387200

; output of date-parse as input value to date-list produces the same date 

(date-list (date-parse "2010.10.18 7:00" "%Y.%m.%d %H:%M"))
<span class='arw'>&rarr;</span> (2010 10 18 7 0 0 290 1)
</pre>


<p>See the <a href="#date">date</a> function for all possible format descriptors.</p>

<br/><br/>
<a name="date-value"></a>
<h2><span class="function">date-value</span></h2>

<h4>syntax: (date-value <em>int-year</em> <em>int-month</em> <em>int-day</em> [<em>int-hour</em> <em>int-min</em> <em>int-sec</em>])<br/> 
syntax: (date-value <em>list-date-time</em>)<br/> 
syntax: (date-value)</h4>

<p>In the first syntax, <tt>date-value</tt> returns the time 
in seconds since 1970-1-1 00:00:00 for a given date and time.  
The parameters for the hour, minutes and seconds are optional. 
The time is assumed to be Coordinated Universal Time (UTC), 
not adjusted for the current time zone.</p>
<p>In the second syntax the same data can be given in a list.
As with the first syntax, numbers for the hour, minutes 
and seconds are optional.</p>

<p>In the third syntax, <tt>date-value</tt> returns the time value 
in seconds for the current time.</p>

<!-- example -->

<pre>
(date-value 2002 2 28)       <span class='arw'>&rarr;</span> 1014854400
(date-value '(2002 2 28))    <span class='arw'>&rarr;</span> 1014854400
(date-value 1970 1 1 0 0 0)  <span class='arw'>&rarr;</span> 0
                                 
(date (date-value (now)))    <span class='arw'>&rarr;</span> "Wed May 24 10:02:47 2006" 
(date (date-value))          <span class='arw'>&rarr;</span> "Wed May 24 10:02:47 2006"
(date)                       <span class='arw'>&rarr;</span> "Wed May 24 10:02:47 2006"
</pre>


<p>The function <a href="#date-list">date-list</a> can be used to transform 
a <tt>date-value</tt> back into a list:</p>


<pre>
(date-list 1014854400)  <span class='arw'>&rarr;</span> (2002 2 28 0 0 0)
(date-value (date-list 1014854400))  <span class='arw'>&rarr;</span> 1014854400
</pre>


<p>See also <a href="#date">date</a>, 
<a href="#date-list">date-list</a>, <a href="#date-parse">date-parse</a>, 
<a href="#time-of-day">time-of-day</a>, <a href="#time">time</a>, and 
<a href="#now">now</a>.</p>

<br/><br/>

<a name="debug"></a>
<h2><span class="function">debug</span></h2>
<h4>syntax: (debug <em>func</em>)</h4>

<p>
Calls <a href="#trace">trace</a> and begins evaluating the user-defined 
function in <em>func</em>. <tt>debug</tt> is a shortcut for executing 
<tt>(trace true)</tt>, then entering the function to be debugged.</p>

<!-- example -->

<pre>
;; instead of doing
(trace true)
(my-func a b c)
(trace nil)

;; use debug as a shortcut
(debug (my-func a b c))
</pre>

<p>When in <tt>debug</tt> or <a href="#trace">trace</a> mode, error messages
will be printed. The function causing the exception will return either
<tt>0</tt> or <tt>nil</tt> and processing will continue. This way, variables 
and the current state of the program can still be inspected while debugging.</p>

<p>See also the <a href="#trace">trace</a> function.</p>

<br/><br/>

<a name="dec"></a>
<h2><span class="function">dec</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (dec <em>place</em> [<em>num</em>])</h4>

<p>
The number in <em>place</em> is decremented by <tt>1.0</tt> or the optional number 
<em>num</em> and returned.  <tt>dec</tt> performs float arithmetic and converts
integer numbers passed into floating point type.</p>

<p><em>place</em> is either a symbol or a place in a list structure holding a
number, or a number returned by an expression.</p>

<!-- example -->

<pre>
(set x 10)    <span class='arw'>&rarr;</span> 10
(dec x)       <span class='arw'>&rarr;</span> 9
x             <span class='arw'>&rarr;</span> 9
(dec x 0.25)  <span class='arw'>&rarr;</span> 8.75
x             <span class='arw'>&rarr;</span> 8.75
</pre>


<p>If the symbol for <em>place</em> contains <tt>nil</tt>, it is treated
as if containing <tt>0.0</tt>:</p>


<pre>
z             <span class='arw'>&rarr;</span> nil
(dec z)       <span class='arw'>&rarr;</span> -1

(set z nil)
(dec z 0.01)  <span class='arw'>&rarr;</span> -0.01
</pre>


<p>Places in a list structure or a number returned by another expression
can be updated too:</p>


<pre>
(set 'l '(1 2 3 4))

(dec (l 3) 0.1) <span class='arw'>&rarr;</span> 3.9

(dec (first l)) <span class='arw'>&rarr;</span> 0

l <span class='arw'>&rarr;</span> (0 2 3 3.9)

(dec (+ 3 4)) <span class='arw'>&rarr;</span> 6
</pre>

<p>Use the <a href="#deci">--</a> function to decrement in integer mode.
Use the <a href="#inc">inc</a> function to increment numbers floating point mode.</p>

<br/><br/>

<a name="def-new"></a>
<h2><span class="function">def-new</span></h2>
<h4>syntax: (def-new <em>sym-source</em> [<em>sym-target</em>])</h4>

<p>
	This function works similarly to <a href="#new">new</a>, 
	but it only creates a copy of one symbol 
	and its contents from the symbol in <em>sym-source</em>. 
	When <em>sym-target</em> is not given, 
	a symbol with the same name is created 
	in the current context. 
	All symbols referenced inside <em>sym-source</em> 
	will be translated into symbol references into the current context,
    which must not be MAIN.</p>
<p>
If an argument is present in <em>sym-target</em>, the copy will be made
into a symbol and context as referenced by the symbol in <em>sym-target</em>. 
In addition to allowing renaming of the function while copying, this also 
enables the copy to be placed in a different context.  All symbol references 
in <em>sym-source</em> with the same context as <em>sym-source</em> will 
be translated into symbol references of the target context.
</p>

<p>
	<tt>def-new</tt> returns the symbol created:
</p>

<!-- example -->

<pre>
&gt; (set 'foo:var '(foo:x foo:y))
<b>(foo:x foo:y)</b>

&gt; (def-new 'foo:var 'ct:myvar)
<b>ct:myvar</b>

&gt; ct:myvar
<b>(ct:x ct:y)</b>

&gt; (context 'K)

K&gt; (def-new 'foo:var)
<b>var</b>

K&gt; var
<b>(x y)</b>
</pre>

<p>The following example shows how a statically scoped function can
be created by moving it its own namespace:</p>

<pre>
&gt; (set 'temp (lambda (x) (+ x x)))
<b>(lambda (x) (+ x x))</b>
&gt; (def-new 'temp 'double:double)
<b>double:double</b>
&gt; (double 10)
<b>20</b>
&gt; double:double
<b>(lambda (double:x) (+ double:x double:x))</b>
</pre>

<p>The following definition of <tt>def-static</tt> can be used to
create functions living in their own lexically protected name-space:</p>
<pre>
(define (def-static s body) 
      (def-new 'body (sym s s)))

(def-static 'acc (lambda (x)
          (inc sum x)))

&gt; (acc 1)
<b>1</b>
&gt; (acc 1)
<b>2</b>
&gt; (acc 8)
<b>10</b>
&gt;
</pre>

<p>The function <tt>def-new</tt> can also be used to configure contexts 
or context objects in a more granular fashion than is possible 
with <a href="#new">new</a>, which copies a whole context.</p>

<br/><br/>

<a name="default"></a>
<h2><span class="function">default</span></h2>
<h4>syntax: (default <em>context</em>)</h4>

<p>Return the contents of the default functor in <em>context</em>.</p>

<!-- example -->

<pre>
(define Foo:Foo 123)

(default Foo) <span class='arw'>&rarr;</span> 123

(setf (default Foo) 456)
(set 'ctx Foo)

(default ctx) <span class='arw'>&rarr;</span> 456
Foo:Foo       <span class='arw'>&rarr;</span> 456
</pre>


<p>In many situations newLISP defaults automatically to the default functor
when seeing a context name. In circumstances where this is not the case,
the <tt>default</tt> function can be used.</p>

<br/><br/>

<a name="define"></a>
<h2><span class="function">define</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (define (<em>sym-name</em> [<em>sym-param-1</em> ... ]) [<em>body-1</em> ... ])<br/>
syntax: (define (<em>sym-name</em> [(<em>sym-param-1</em> <em>exp-default</em>) ... ]) [<em>body-1</em> ... ])<br/>
syntax: (define <em>sym-name</em> <em>exp</em>)</h4>

<p>
	Defines the new function <em>sym-name</em>, 
	with optional parameters <em>sym-param-1</em>&mdash;. 
	<tt>define</tt> is equivalent to assigning 
	a lambda expression to <em>sym-name</em>. 
	When calling a defined function, 
	all arguments are evaluated and assigned 
	to the variables in <em>sym-param-1</em>&mdash;, 
	then the <em>body-1&mdash;	</em> expressions are evaluated. 
	When a function is defined, the lambda expression 
	bound to <em>sym-name</em> is returned.
</p>

<p>
	All parameters defined are optional. 
	When a user-defined function is called without arguments, 
	those parameters assume the value <tt>nil</tt>. 
<!-- 8.9.4 -->If those parameters have a default value 
	specified in <em>exp-default</em>,
	they assume that value.
</p>

<p>
	The return value of <tt>define</tt> 
	is the assigned <em>lambda</em> expression. 
	When calling a user-defined function, 
	the return value is the last expression evaluated 
	in the function body.
</p>

<!-- example -->

<pre>
(define (area x y) (* x y))  <span class='arw'>&rarr;</span> (lambda (x y) (* x y))
(area 2 3)                   <span class='arw'>&rarr;</span> 6
</pre>


<p>
	As an alternative, <tt>area</tt> could be defined 
	as a function without using <tt>define</tt>.
</p>


<pre>
(set 'area (lambda (x y) (* x y))
</pre>


<p>
	<em>lambda</em> or <em>fn</em> expressions may be used by themselves 
	as <em>anonymous</em> functions without being defined as a symbol:
</p>


<pre>
((lambda ( x y) (* x y)) 2 3)  <span class='arw'>&rarr;</span> 6
((fn ( x y) (* x y)) 2 3)      <span class='arw'>&rarr;</span> 6
</pre>


<p>
	<tt>fn</tt> is just a shorter form of writing <tt>lambda</tt>.
</p>
	
<!-- 8.9.4 -->
<p>
	Parameters can have default values specified:
</p>


<pre>
(define (foo (a 1) (b 2))
  (list a b))
    
(foo)      <span class='arw'>&rarr;</span> (1 2)
(foo 3)    <span class='arw'>&rarr;</span> (3 2)
(foo 3 4)  <span class='arw'>&rarr;</span> (3 4)
</pre>


<p>
	Expressions in <em>exp-default</em> 
	are evaluated in the function's
	current environment.
</p>


<pre>
(define (foo (a 10) (b (div a 2))) 
  (list a b))

(foo)      <span class='arw'>&rarr;</span> (10 5)
(foo 30)   <span class='arw'>&rarr;</span> (30 15)
(foo 3 4)  <span class='arw'>&rarr;</span> (3 4)
</pre>


<p>
	The second version of <tt>define</tt> 
	works like the <a href="#set">set</a> function.
</p>

<!-- example -->

<pre>
(define x 123)  <span class='arw'>&rarr;</span>   123
;; is equivalent to
(set 'x 123)    <span class='arw'>&rarr;</span>   123

(define area (lambda ( x y) (* x y)))
;; is equivalent to
(set 'area (lambda ( x y) (* x y)))
;; is equivalent to
(define (area x y) (* x y))
</pre>


<p>
	Trying to redefine a protected symbol will cause an error message.
</p>

<br/><br/>

<a name="define-macro"></a>
<h2><span class="function">define-macro</span></h2>
<h4>syntax: (define-macro (<em>sym-name</em> [<em>sym-param-1</em> ... ]) <em>body</em>)<br/>
syntax: (define-macro (<em>sym-name</em> [(<em>sym-param-1</em> <em>exp-default</em>) ... ]) <em>body</em>)</h4>

<p>Functions defined using <tt>define-macro</tt> are called <i>fexpr</i>
in other LISPs as they don't do variable expansion. In newLISP they are still
called macros, because they are written with the same purpose of creating
special syntax forms with non-standard evaluation patterns of arguments. 
Functions created using <tt>define-macro</tt> can be combined with template 
expansion using <a href="#expand">expand</a> or <a href="#letex">letex</a>.</p>

<p>Since v.10.5.8, newLISP also has expansion macros using <a href="#macro">macro</a>.</p>

<p>Defines the new fexpr <em>sym-name</em>, with optional arguments <em>sym-param-1</em>. 
<tt>define-macro</tt> is equivalent to assigning a lambda-macro expression to a symbol. 
When a <tt>define-macro</tt> function is called, unevaluated arguments are assigned to 
the variables in <em>sym-param-1 ...</em>. Then the <em>body</em> expressions are evaluated.  
When evaluating the <tt>define-macro</tt> function, the lambda-macro expression is returned.
</p>

<!-- example -->

<pre>
(define-macro (my-setq p1 p2) (set p1 (eval p2))) 
<span class='arw'>&rarr;</span> (lambda-macro (p1 p2) (set p1 (eval p2)))

(my-setq x 123)  <span class='arw'>&rarr;</span> 123
x                <span class='arw'>&rarr;</span> 123
</pre>


<p>New functions can be created to behave like built-in functions
that delay the evaluation of certain arguments.  Because fexprs can 
access the arguments inside a parameter list, they can be used to 
create flow-control functions like those already built-in to newLISP.</p>

<p>All parameters defined are optional.  When a macro is called without 
arguments, those parameters assume the value <tt>nil</tt>. 
If those parameters have a default value specified in <em>exp-default</em>,
they assume that default value.</p>
	

<pre>
(define-macro (foo (a 1) (b 2))
  (list a b))
    
(foo)      <span class='arw'>&rarr;</span> (1 2)
(foo 3)    <span class='arw'>&rarr;</span> (3 2)
(foo 3 4)  <span class='arw'>&rarr;</span> (3 4)
</pre>


<p>Expressions in <em>exp-default</em> are evaluated in the function's 
current environment.</p>


<pre>
(define-macro (foo (a 10) (b (div a 2))) 
  (list a b))

(foo)      <span class='arw'>&rarr;</span> (10 5)
(foo 30)   <span class='arw'>&rarr;</span> (30 15)
(foo 3 4)  <span class='arw'>&rarr;</span> (3 4)
</pre>


<p>Note that in <em>fexprs</em>, the danger exists of passing a parameter 
with the same variable name as used in the <tt>define-macro</tt> definition. 
In this case, the <em>fexpr's</em> internal variable would end up 
receiving <tt>nil</tt> instead of the intended value:</p>


<pre>
;; not a good definition!

(define-macro (my-setq x y) (set x (eval y)))  

;; symbol name clash for x

(my-setq x 123)  <span class='arw'>&rarr;</span> 123
x                <span class='arw'>&rarr;</span> nil
</pre>


<p>There are several methods that can be used 
to avoid this problem, known as <em>variable capture</em>,
by writing <em>hygienic</em> <tt>define-macro</tt>s:</p>

<ul>
<li>
Put the definition into its own lexically closed namespace context. 
If the function has the same name as the context, it can be 
called by using the context name alone.  A function with this 
characteristic is called a <a href="#default_function">
<em>default function</em></a>. This is the preferred method in 
newLISP to write <tt>define-macro</tt>s.
</li>
<br/>
<li>Use <a href="#args">args</a> to access arguments passed by the 
function.</li>
</ul>

<!-- example -->

<pre>
;; a define-macro as a lexically isolated function
;; avoiding variable capture in passed parameters

(context 'my-setq)

(define-macro (my-setq:my-setq x y) (set x (eval y)))  

(context MAIN)

(my-setq x 123)  <span class='arw'>&rarr;</span> 123  ; no symbol clash
x                <span class='arw'>&rarr;</span> 123
</pre>


<p>
	The definition in the example is lexically isolated, 
	and no variable capture can occur. 
	Instead of the function being called using <tt>(my-setq:my-setq &hellip;)</tt>, 
	it can be called with just <tt>(my-setq &hellip;)</tt> 
	because it is a <a href="#default_function"><em>default function</em></a>.
</p>

<p>
	The second possibility is to refer to passed parameters 
	using <a href="#args">args</a>:
</p>

<!-- example -->

<pre>
;; avoid variable capture in macros using the args function

(define-macro (my-setq) (set (args 0) (eval (args 1))))
</pre>

<p>See also the <a href="#macro">macro</a> expansion function not
susceptible to variable capture.</p>

<br/><br/>

<a name="delete"></a>
<h2><span class="function">delete</span>&nbsp;
<a href="#shared-lib"><font size="+2">&#x26A0;</font></a></h2>
<h4>syntax: (delete <em>symbol</em> [<em>bool</em>])<br/>

syntax: (delete <em>sym-context</em> [<em>bool</em>])</h4>

<p>In the first syntax deletes a symbol <em>symbol</em> and references to 
the symbol in other expressions will be changed to <tt>nil</tt>.</p>

<p>In the second syntax all symbols of the namespace referred to by 
<em>sym-context</em> will be deleted and references to them in other
espressions will be changed to <tt>nil</tt>. The context symbol 
<em>sym-context</em> will be changed to a normal symbol 
containing <tt>nil</tt>.</p>

<p>When the expression in <em>bool</em> evaluates 
to <tt>true</tt>, symbols are only deleted when they are not referenced.
</p>

<p>
When the expression in <em>bool</em> evaluates 
to <tt>nil</tt>, symbols will be deleted without any reference checking.
Note that this mode should only be used, if no references to the symbol
exist outside it's namespace. If external references exist, this mode
can lead to system crashes, as the external reference is not set to
<tt>nil</tt> when using this mode. This mode can be used to delete
namespace hashes and to delete namespaces in object systems, where variables are
strictly treated as private.</p>

<p>
Protected symbols of built-in functions and special symbols 
like <tt>nil</tt> and <tt>true</tt> cannot be deleted.
</p>

<p>
<tt>delete</tt> returns <tt>true</tt> if the symbol was deleted 
successfully or <tt>nil</tt> if the symbol was not deleted.
</p>

<p>When deleting a context symbol, the first <tt>delete</tt> removes the context 
namespace contents and demotes the context symbol to a normal mono-variable symbol. 
A second <tt>delete</tt> will remove the symbol from the symbol table.</p>

<!-- example -->

<pre>
(set 'lst '(a b aVar c d))

(delete 'aVar)  ; aVar deleted, references marked nil

lst  <span class='arw'>&rarr;</span> (a b nil c d)

(set 'lst '(a b aVar c d))

(delete 'aVar true)  
<span class='arw'>&rarr;</span> nil ; protect aVar if referenced

lst  <span class='arw'>&rarr;</span> (a b aVar c d)

;; delete all symbols in a context
(set 'foo:x 123)
(set 'foo:y "hello")

(delete 'foo)  <span class='arw'>&rarr;</span> foo:x, foo:y deleted
</pre>


<p>In the last example only the symbols inside context <tt>foo</tt>
will be deleted but not the context symbol <tt>foo</tt> itself. It
will be converted to a normal unprotected symbol and contain <tt>nil</tt>.
</p>

<p>
Note that deleting a symbol that is part of an expression 
which is currently executing can crash the system 
or have other unforeseen effects.
</p>

<br/><br/>

<a name="delete-file"></a>
<h2><span class="function">delete-file</span></h2>
<h4>syntax: (delete-file <em>str-file-name</em>)</h4>

<p>Deletes a file given in <em>str-file-name</em>. 
Returns <tt>true</tt> if the file was deleted 
successfully.</p>

<p>On failure the function returns <tt>nil</tt>. For error information, 
use <a href="#sys-error">sys-error</a> when used on files. When used
on URLs <a href="#net-error">net-error</a> gives more error
information.</p>

<p>The file name can be given as a URL.</p>

<!-- example -->

<pre>
(delete-file "junk")

(delete-file "http://asite.com/example.html")

(delete-file "file://aFile.txt")
</pre>

<p>
The first example deletes the file <tt>junk</tt> in the current directory.
The second example shows how to use a URL to specify the file. 
In this form, additional parameters can be given. 
See <a href="#delete-url">delete-url</a> for details.
</p>

<br/><br/>

<a name="delete-url"></a>
<h2><span class="function">delete-url</span></h2>
<h4>syntax: (delete-url <em>str-url</em>)</h4>

<p>This function deletes the file on a remote HTTP server specified in <em>str-url</em>.
The HTTP <tt>DELETE</tt> protocol must be enabled on the target web server, 
or an error message string may be returned. The target file must also have  
access permissions set accordingly. Additional parameters such as timeout and custom headers 
are available exactly as in the <a href="#get-url">get-url</a> function.</p>

<p>If <em>str-url</em> starts with <tt>file://</tt> a file on the local file system
is deleted.</p>

<p>This feature is also available when the <a href="#delete-file">delete-file</a>
function is used and a URL is specified for the filename.</p>

<!-- example -->

<pre>
(delete-url "http://www.aserver.com/somefile.txt")
(delete-url "http://site.org:8080/page.html" 5000)

; delete on the local file system
(delete-url "file:///home/joe/somefile.txt")
</pre>


<p>The second example configures a timeout option of five seconds. 
Other options such as special HTTP protocol headers 
can be specified, as well. 
See the <a href="#get-url">get-url</a> function for details.</p>

<p><tt>delete-url</tt> requests are also understood by newLISP server nodes, but will
not be served when the server is started in <tt>-http-safe</tt> mode.</p>

<br/><br/>

<a name="destroy"></a>
<h2><span class="function">destroy</span></h2>
<h4>syntax: (destroy <em>int-pid</em>)<br/>
syntax: (destroy <em>int-pid</em> <em>int-signal</em>)</h4>

<p>Destroys a process with process id in <em>int-pid</em> and returns <tt>true</tt>
on success or <tt>nil</tt> on failure. The process id is normally obtained from a 
previous call to <a href="#fork">fork</a> on macOS and other Unix or 
<a href="#process">process</a> on all platforms. On Unix, <tt>destroy</tt> works like 
the system utility <em>kill</em> using the SIGKILL signal.</p>

<p>CAUTION! If <em>int-pid</em> is <tt>0</tt> the signal is sent to all processes whose 
group ID is equal to the process group ID of the sender. If <em>int-pid</em> is <tt>-1</tt> 
all processes with the current user id will be killed, if newLISP is started with 
super user privileges, all processes except system processes are destroyed.
</p>

<p>When specifying <em>int-signal</em>, <tt>destroy</tt> works like a Unix <tt>kill</tt> 
command sending the specified Unix signal to the process in <em>int-pid</em>. 
This second syntax is not available on MS Windows.</p>

<!-- example -->

<pre>
(set 'pid (process "/usr/local/bin/bc" bcin bcout)) 
(destroy pid)

(set 'pid (fork (dotimes (i 1000) (println i) (sleep 10))))
(sleep 100) (destroy pid)
</pre>

<br/><br/>

<a name="det"></a>
<h2><span class="function">det</span></h2>
<h4>syntax: (det <em>matrix</em> [<em>float-pivot</em>])</h4>

<p>Returns the determinant of a square matrix.  A matrix can either 
be a nested list or an <a href="#array">array</a>.</p>

<p>Optionally <tt>0.0</tt> or a very small value can be specified
in <em>float-pivot</em>. This value substitutes pivot elements in
the LU-decomposition algorithm, which result in zero when
the algorithm deals with a singular matrix.</p>

<!-- example -->

<pre>
(set 'A '((-1 1 1) (1 4 -5) (1 -2 0)))
(det A)  <span class='arw'>&rarr;</span> -1

; treatment of singular matrices
(det '((2 -1) (4 -2)))        <span class='arw'>&rarr;</span> nil
(det '((2 -1) (4 -2)) 0)      <span class='arw'>&rarr;</span> -0
(det '((2 -1) (4 -2)) 1e-20)  <span class='arw'>&rarr;</span> -4e-20
</pre>

<p>If the matrix is singular and <em>float-pivot</em> is not
specified, <tt>nil</tt> is returned.</p>

<p>See also the other matrix operations 
<a href="#invert">invert</a>, <a href="#mat">mat</a>, 
<a href="#multiply">multiply</a> and <a href="#transpose">transpose</a>.
</p>

<br/>

<a name="device"></a>
<h2><span class="function">device</span></h2>
<h4>syntax: (device [<em>int-io-handle</em>])</h4>

<p><em>int-io-handle</em> is an I/O device number, which is set to 0 (zero)
for the default STD I/O pair of handles, 0 for <i>stdin</i>, 1
for <i>stdout</i> and 2 for <i>stderr</i>. <em>int-io-handle</em> may also 
be a file handle previously obtained using <a href="#open">open</a>. In this 
case both, input and output are channeled through this handle.
When no argument is supplied, the current I/O device number is returned. 
</p>

<p>The I/O channel specified by <tt>device</tt> is used internally 
by the functions <a href="#print">print</a>, <a href="#println">println</a>,
<a href="#write">write</a>, <a href="#write-line">write-line</a> and
<a href="#read-char">read-char</a>, <a href="#read-line">read-line</a>. 
When the current I/O device is 0 or 1, <a href="#print">print</a> 
sends output to the console window and <a href="#read-line">read-line</a> 
accepts input from the keyboard. If the current I/O device has been set 
by opening a file, then <a href="#print">print</a> and <a href="#read-line">read-line</a> 
work on that file.</p>

<p>Note, that on Unix like operating systems, stdin channel 0 can also be used
for output and stdout channel 1 can also be used for reading input. This is
not the case on Windows, where 0 is strictly for input and stdout 1 strictly
for output.</p>
 
<!-- example -->

<pre>
(device (open "myfile" "write"))  <span class='arw'>&rarr;</span> 5
(print "This goes in myfile")     <span class='arw'>&rarr;</span> "This goes in myfile"
(close (device))                  <span class='arw'>&rarr;</span> true
</pre>


<p>Note that using <a href="#close">close</a> on <tt>device</tt> 
automatically resets <tt>device</tt> to 0 (zero).</p>

<br/><br/>

<a name="difference"></a>
<h2><span class="function">difference</span></h2>
<h4>syntax: (difference <em>list-A</em> <em>list-B</em>)<br/>
syntax: (difference <em>list-A</em> <em>list-B</em> <em>bool</em>)</h4>

<p>
	In the first syntax, <tt>difference</tt> returns 
	the <em>set</em> difference between <em>list-A</em> and <em>list-B</em>. 
	The resulting list only has elements occurring in <em>list-A</em>, 
	but not in <em>list-B</em>. 
	All elements in the resulting list are unique, 
	but <em>list-A</em> and <em>list-B</em> need not be unique. 
	Elements in the lists can be any type of Lisp expression.
</p>

<!-- example -->

<pre>
(difference '(2 5 6 0 3 5 0 2) '(1 2 3 3 2 1))  <span class='arw'>&rarr;</span> (5 6 0)
</pre>


<p>
	In the second syntax, <tt>difference</tt> works in <em>list</em> mode. 
	<em>bool</em> specifies <tt>true</tt> 
	or an expression not evaluating to <tt>nil</tt>. 
	In the resulting list, all elements of <em>list-B</em> 
	are eliminated in <em>list-A</em>, 
	but duplicates of other elements in <em>list-A</em> are left.
</p>
<!-- example -->

<pre>
(difference '(2 5 6 0 3 5 0 2) '(1 2 3 3 2 1) true)  <span class='arw'>&rarr;</span> (5 6 0 5 0)
</pre>



<p>
	See also the set functions <a href="#intersect">intersect</a>,
	<a href="#unique">unique</a> and <a href="#union">union</a>.
</p>

<br/><br/>

<a name="directory"></a>
<h2><span class="function">directory</span></h2>
<h4>syntax: (directory [<em>str-path</em>])<br/>
syntax: (directory <em>str-path</em> <em>str-pattern</em> [<em>regex-option</em>])</h4>

<p>
	A list of directory entry names is returned 
	for the directory path given in <em>str-path</em>. 
	On failure, <tt>nil</tt> is returned. 
	When <em>str-path</em> is omitted, 
	the list of entries in the current directory is returned.
</p>

<!-- example -->

<pre>
(directory "/bin")

(directory "c:/")
</pre>


<p>
	The first example returns the directory of <tt>/bin</tt>, 
	the second line returns a list of directory entries 
	in the root directory of drive C:. 
	Note that on MS Windows systems, 
	a forward slash (<tt>/</tt>) can be included in path names. 
	When used, a backslash (<tt>\</tt>) must be 
	preceded by a second backslash.
</p>

<p>
	In the second syntax, <tt>directory</tt> can take 
	a regular expression pattern in <em>str-pattern</em>. 
	Only filenames matching the pattern will be returned 
	in the list of directory entries. 
	In <em>regex-option</em>, special regular expression options 
	can be specified; see <a href="#regex">regex</a> for details.
</p>

<!-- example -->

<pre>
(directory "." "\\.c")  <span class='arw'>&rarr;</span> ("foo.c" "bar.c")
;; or using braces as string pattern delimiters
(directory "." {\.c})  <span class='arw'>&rarr;</span> ("foo.c" "bar.c")

; show only hidden files (starting with dot)
(directory "." "^[.]")   <span class='arw'>&rarr;</span> ("." ".." ".profile" ".rnd" ".ssh")</pre>


<p>
	The regular expression forces <tt>directory</tt> 
	to return only file names containing the string <tt>".c"</tt>.
</p>

<p>
	Other functions that use regular expressions 
	are <a href="#find">find</a>, <!-- 8.9.4 --><a href="#find-all">find-all</a>,
	<a href="#parse">parse</a>, 
	<a href="#regex">regex</a>, <a href="#replace">replace</a>, 
	and <a href="#search">search</a>.
</p>

<br/><br/>

<a name="directoryp"></a>
<h2><span class="function">directory?</span></h2>
<h4>syntax: (directory? <em>str-path</em>)</h4>

<p>
	Checks if <em>str-path</em> is a directory. 
	Returns <tt>true</tt> or <tt>nil</tt> depending on the outcome.
</p>


<pre>
(directory? "/etc")             <span class='arw'>&rarr;</span> true
(directory? "/usr/local/bin/emacs/")  <span class='arw'>&rarr;</span> nil
</pre>

<br/><br/>

<a name="display-html"></a>
<h2><span class="function">display-html
<a href="#JS"><font size="-1">JS</font></a></span></h2>
<h4>syntax: (display-html <em>str-html</em>)<br/>
syntax: (display-html <em>str-html</em> <em>bool-flag</em>)</h4>

<p>Using the first syntax, the function replaces the current page in 
the browser with the HTML page found in <em>str-html</em>.</p> 

<p>If <em>bool-flag</em> evaluates to <tt>true</tt>, the page gets
opened in a new browser tab and the current page is not affected.</p>

<p>This function is only available on newLISP compiled to JavaScript.</p>

<pre>
(set 'page [text]
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Hello App&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt;Hello World&lt;/h2&gt;
&lt;/body&gt;
&lt;/html&gt;
[/text])

; open the page in a new browser tab
(display-html page true) <span class='arw'>&rarr;</span> "92"
</pre>

<p>The function returns the length of the HTML document displayed as a string.
</p>

<p>See also the function <a href="#eval-string-js">eval-string-js</a> for
evaluation of JavaScript in the current page.</p>

<br/><br/>

<a name="div"></a>
<h2><span class="function">div</span></h2>
<h4>syntax: (div <em>num-1</em> <em>num-2</em> [<em>num-3</em> ... ])<br/>
syntax: (div <em>num-1</em>)</h4>

<p>
	Successively divides <em>num-1</em>
	by the number in <em>num-2&mdash;</em>. 
	<tt>div</tt> can perform mixed-type arithmetic, 
	but it always returns floating point numbers. 
	Any floating point calculation 
	with <tt>NaN</tt> also returns <tt>NaN</tt>.
</p>
	

<!-- example -->

<pre>
(div 10 3)                 <span class='arw'>&rarr;</span> 3.333333333
(div 120 (sub 9.0 6) 100)  <span class='arw'>&rarr;</span> 0.4

(div 10)                   <span class='arw'>&rarr;</span> 0.1
</pre>


<p>
	When <em>num-1</em> is the only argument,
	<tt>div</tt> calculates the inverse of <em>num-1</em>.
</p>

<br/><br/>

<a name="do-until"></a>

<h2><span class="function">do-until</span></h2>
<h4>syntax: (do-until <em>exp-condition</em> [<em>body</em>])</h4>

<p>
	The expressions in <em>body</em> are evaluated 
	before <em>exp-condition</em> is evaluated. 
	If the evaluation of <em>exp-condition</em> is not <tt>nil</tt>, 
	then the <tt>do-until</tt> expression is finished; 
	otherwise, the expressions in <em>body</em> get evaluated again. 
	Note that <tt>do-until</tt> evaluates the conditional expression 
	<em>after</em> evaluating the body expressions, 
	whereas <a href="#until">until</a> checks the condition 
	<em>before</em> evaluating the body. 
	The return value of the <tt>do-until</tt> expression 
	is the last evaluation of the <em>body</em> expression.
	If <em>body</em> is empty, the last result of <em>exp-condition</em>
    is returned.
</p>

<p><tt>do-until</tt> also updates the system iterator symbol <tt>$idx</tt>.</p>

<!-- example -->

<pre>
(set 'x 1)
(do-until (&gt; x 0) (inc x))
x  <span class='arw'>&rarr;</span> 2

(set 'x 1)
(until (&gt; x 0) (inc x))
x  <span class='arw'>&rarr;</span> 1
</pre>


<p>
	While <tt>do-until</tt> goes through the loop at least once, 
	<a href="#until">until</a> never enters the loop.
</p>

<p>
	See also the functions <a href="#while">while</a> 
	and <a href="#do-while">do-while</a>.
</p>

<br/><br/>

<a name="do-while"></a>
<h2><span class="function">do-while</span></h2>
<h4>syntax: (do-while <em>exp-condition body</em>)</h4>

<p>
	The expressions in <em>body</em> are evaluated 
	before <em>exp-condition</em> is evaluated. 
	If the evaluation of <em>exp-condition</em> is <tt>nil</tt>, 
	then the <tt>do-while</tt> expression is finished; 
	otherwise the expressions in <em>body</em> get evaluated again. 
	Note that <tt>do-while</tt> evaluates the conditional expression 
	<em>after</em> evaluating the body expressions, 
	whereas <a href="#while">while</a> checks the condition 
	<em>before</em> evaluating the body. 
	The return value of the <tt>do-while</tt> expression 
	is the last evaluation of the <em>body</em> expression.
</p>

<p><tt>do-while</tt> also updates the system iterator symbol <tt>$idx</tt>.</p>

<!-- example -->

<pre>
(set 'x 10)
(do-while (&lt; x 10) (inc x))
x  <span class='arw'>&rarr;</span> 11

(set 'x 10)
(while (&lt; x 10) (inc x)) 
x  <span class='arw'>&rarr;</span> 10
</pre>


<p>
	While <tt>do-while</tt> goes through the loop at least once, 
	<a href="#while">while</a> never enters the loop.
</p>

<p>
	See also the functions <a href="#until">until</a> 
	and <a  href="#do-until">do-until</a>.
</p>

<br/><br/>

<a name="doargs"></a>
<h2><span class="function">doargs</span></h2>
<h4>syntax: (doargs (<em>sym</em> [<em>exp-break</em>])<em> body</em>)</h4>

<p>Iterates through all members of the argument list 
inside a user-defined function or macro. This function or macro can be defined using <a href="#define">define</a>, 
<a href="#define-macro">define-macro</a>, <a href="#lambda">lambda</a>, or
<a href="#lambda-macro">lambda-macro</a>.
The variable in <em>sym</em> is set sequentially to all members in the argument list 
until the list is exhausted or an optional break expression 
(defined in <em>exp-break</em>) evaluates to <tt>true</tt> or a logical true value. 
The <tt>doargs</tt> expression always returns the result of the last evaluation.</p>

<p><tt>doargs</tt> also updates the system iterator symbol <tt>$idx</tt>.</p>

<!-- example -->

<pre>
(define (foo)
    (doargs (i) (println i)))

<b>&gt;</b> (foo 1 2 3 4)
<b>1
2
3
4</b>
</pre>


<p>The optional break expression causes <tt>doargs</tt> 
to interrupt processing of the arguments:</p>


<pre>
(define-macro (foo)
    (doargs (i (= i 'x)) 
        (println i)))

<b>&gt;</b> (foo a b x c d e)
<b>a
b
true</b>
</pre>


<p>Use the <a href="#args">args</a> function to access the entire argument list at once.</p>

<br/><br/>

<a name="dolist"></a>
<h2><span class="function">dolist</span></h2>
<h4>syntax: (dolist (<em>sym</em>  <em>list</em>|<em>array</em> [<em>exp-break</em>])<em> body</em>)</h4>

<p>
	The expressions in <em>body</em> are evaluated 
	for each element in <em>list</em> or <em>array</em>. 
	The variable in <em>sym</em> is set to each of the elements 
	before evaluation of the body expressions. 
	The variable used as loop index is local 
	and behaves according to the rules of dynamic scoping.
</p>

<p>
	Optionally, a condition for early loop exit 
	may be defined in <em>exp-break</em>. 
	If the break expression evaluates to any non-<tt>nil</tt> value, 
	the <tt>dolist</tt> loop returns with the value of <em>exp-break</em>. 
	The break condition is tested before evaluating <em>body.</em></p>

<!-- example -->

<pre>
(set 'x 123)
(dolist (x '(a b c d e f g))  ; prints: abcdefg
    (print x))  <span class='arw'>&rarr;</span> g          ; return value

(dolist (x '(a b c d e f g) (= x 'e))  ; prints: abcd
    (print x))

;; x is local in dolist
;; x has still its old value outside the loop

x  <span class='arw'>&rarr;</span> 123  ; x has still its old value
</pre>


<p>
	This example prints <tt>abcdefg</tt> in the console window. 
	After the execution of <tt>dolist</tt>,
	the value for <tt>x</tt> remains unchanged 
	because the <tt>x</tt> in <tt>dolist</tt> has local scope. 
	The return value of <tt>dolist</tt> is the result 
	of the last evaluated expression.
</p>

<p>
	The internal system variable <tt>$idx</tt> 
	keeps track of the current offset 
	into the list passed to <tt>dolist</tt>,
	and it can be accessed during its execution:
</p>


<pre>
(dolist (x '(a b d e f g))
  (println $idx ":" x))  <span class='arw'>&rarr;</span> g

<b>0:a
1:b
2:d
3:e
4:f
5:g</b>
</pre>


<p>
	The console output is shown in boldface. 
	<tt>$idx</tt> is protected and cannot be changed by the user.
</p>

<br/><br/>

<a name="dostring"></a>
<h2><span class="function">dostring</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (dostring (<em>sym</em>  <em>string</em> [<em>exp-break</em>]) <em>body</em>)</h4>

<p>
	The expressions in <em>body</em> are evaluated 
	for each character in <em>string</em>. 
	The variable in <em>sym</em> is set to each ASCII or UTF-8 integer value of the characters 
	before evaluation of the body expressions. 
	The variable used as loop index is local 
	and behaves according to the rules of dynamic scoping.
</p>

<p>
	Optionally, a condition for early loop exit 
	may be defined in <em>exp-break</em>. 
	If the break expression evaluates to any non-<tt>nil</tt> value, 
	the <tt>dolist</tt> loop returns with the value of <em>exp-break</em>. 
	The break condition is tested before evaluating <em>body.</em>
</p>

<!-- example -->

<pre>
; ASCII example
(set 'str "abcdefg")
(dostring (c str) (println c " - " (char c)))

<b>97 - a
98 - b
99 - c
100 - d
101 - e
102 - f
103 - g</b>

; UTF8 example
(set 'utf8str "我能吞下玻璃而不伤身体。")
(dostring (c utf8str) (println c " - " (char c)))

<b>25105 - 我
33021 - 能
21534 - 吞
 ...
20307 - 体
12290 - 。 </b>
</pre>


<p>
	This example prints the value of each character
  in the console window. In UTF-8 enabled versions of newLISP,
  individual characters may be longer than one byte and the
  number in the loop variable may exceed 255.
	The return value of <tt>dostring</tt> is the result 
	of the last evaluated expression.
</p>

<p>
	The internal system variable <tt>$idx</tt> 
	keeps track of the current offset 
	into the string passed to <tt>dostring</tt>,
	and it can be accessed during its execution.
</p>

<br/><br/>

<a name="dotimes"></a>
<h2><span class="function">dotimes</span></h2>
<h4>syntax: (dotimes (<em>sym-var</em> <em>int-count</em> [<em>exp-break</em>]) <em>body</em>)</h4>

<p>
	The expressions in <em>body</em> are evaluated <em>int</em> times. 
	The variable in <em>sym</em> is set from 0 (zero) to (<em>int</em> - 1) 
	each time before evaluating the body expression(s). 
	The variable used as the loop index is local to the <tt>dotimes</tt> 
	expression and behaves according the rules of dynamic scoping. 
	The loop index is of integer type. 
	<tt>dotimes</tt> returns the result of 
	the last expression evaluated in <em>body</em>.
	After evaluation of the <tt>dotimes</tt>
    statement <em>sym</em> assumes its previous
    value.
</p>

<p>
	Optionally, a condition for early loop exit 
	may be defined in <em>exp-break</em>. 
	If the break expression evaluates to any non-<tt>nil</tt> value, 
	the <tt>dotimes</tt> loop returns with the value of <em>exp-break</em>. 
	The break condition is tested before evaluating <em>body</em>.
</p>

<!-- example -->

<pre>
(dotimes (x 10)
  (print x))  <span class='arw'>&rarr;</span> 9  ; return value
</pre>


<p>
	This prints <tt>0123456789</tt> to the console window.
</p>

<br/><br/>

<a name="dotree"></a>
<h2><span class="function">dotree</span></h2>
<h4>syntax: (dotree (<em>sym</em> <em>sym-context</em> [<em>bool</em>]) <em>body</em>)</h4>

<p>The expressions in <em>body</em> are evaluated for all symbols in <em>sym-context</em>. 
The symbols are accessed in a sorted order.  Before each evaluation of the body expression(s),
the variable in <em>sym</em> is set to the next symbol from <em>sym-context</em>. 
The variable used as the loop index is local to the <tt>dotree</tt> expression 
and behaves according the rules of dynamic scoping.</p>

<p>When the optional <em>bool</em> expression evaluates to not <tt>nil</tt>, only symbols 
starting with an underscore character <tt>_</tt> are accessed. Symbol names starting with 
an <tt>_</tt> underscore are used for <a href="#hash">hash keys</a> and symbols created by 
<a href="#bayes-train">bayes-train</a>.</p>

<p><tt>dotree</tt> also updates the system iterator symbol <tt>$idx</tt>.</p>

<!-- example -->

<pre>
;; faster and less memory overhead
(dotree (s SomeCTX) (print s " "))

;; slower and higher memory usage
(dolist (s (symbols SomeCTX)) (print s " "))
</pre>


<p>
This example prints the names of all symbols inside SomeCTX to the console window.
</p>

<br/><br/>

<a name="dump"></a>
<h2><span class="function">dump</span></h2>
<h4>syntax: (dump [<em>exp</em>])</h4>

<p>
	Shows the binary contents of a newLISP cell. 
	Without an argument, this function outputs 
	a listing of all Lisp cells to the console. 
	When <em>exp</em> is given, 
	it is evaluated and the contents 
	of a Lisp cell are returned in a list.
</p>

<!-- example -->

<pre>
(dump 'a)   <span class='arw'>&rarr;</span> (9586996 5 9578692 9578692 9759280)

(dump 999)  <span class='arw'>&rarr;</span> (9586996 130 9578692 9578692 999)
</pre>


<p>
	The list contains the following memory addresses and information:
</p>

<table summary="dump data types">
<tr align="left"><th>offset</th><th>description</th></tr>
<tr><td>0</td><td>memory address of the newLISP cell</td></tr>
<tr><td>1</td><td>cell-&gt;type: major/minor type, see newlisp.h for details</td></tr>
<tr><td>2</td><td>cell-&gt;next: linked list ptr</td></tr>
<tr><td>3</td><td>cell-&gt;aux:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;string length+1 or <br/>
&nbsp;&nbsp;&nbsp;&nbsp;low (little endian) or high (big endian) word of 64-bit integer or<br/>
&nbsp;&nbsp;&nbsp;&nbsp;low word of IEEE 754 double float</td></tr>
<tr><td>4</td><td>cell-&gt;contents:<br/>
&nbsp;&nbsp;&nbsp;&nbsp;string/symbol address or<br/> 
&nbsp;&nbsp;&nbsp;&nbsp;high (little endian) or low (big endian) word of 64-bit integer or<br/>
&nbsp;&nbsp;&nbsp;&nbsp;high word of IEEE 754 double float</td></tr>
</table><br/>

<p>
	This function is valuable for changing type bits in cells 
	or hacking other parts of newLISP internals. 
	See the function <a href="#cpymem">cpymem</a> 
	for a comprehensive example.
</p>

<br/><br/>

<a name="dup"></a>
<h2><span class="function">dup</span></h2>

<h4>syntax: (dup <em>exp</em> <em>int-n</em> [<em>bool</em>])<br/>
syntax: (dup <em>exp</em>)</h4>

<p>
	If the expression in <em>exp</em> evaluates to a string, 
	it will be replicated <em>int-n</em> times within a string and returned. 
	When specifying an expression evaluating 
	to anything other than <tt>nil</tt> in <em>bool</em>, 
	the string will not be concatenated 
	but replicated in a list like any other data type.
</p>

<p>
	If <em>exp</em> contains any data type other than string, 
	the returned list will contain <em>int-n</em> evaluations of <em>exp</em>.
</p>

<p>Without the repetition parameter, <tt>dup</tt> assumes 2.</p>

<!-- example -->

<pre>
(dup "A" 6)       <span class='arw'>&rarr;</span> "AAAAAA"
(dup "A" 6 true)  <span class='arw'>&rarr;</span> ("A" "A" "A" "A" "A" "A")
(dup "A" 0)       <span class='arw'>&rarr;</span> ""
(dup "AB" 5)      <span class='arw'>&rarr;</span> "ABABABABAB"
(dup 9 7)         <span class='arw'>&rarr;</span> (9 9 9 9 9 9 9)
(dup 9 0)         <span class='arw'>&rarr;</span> ()
(dup 'x 8)        <span class='arw'>&rarr;</span> (x x x x x x x x)
(dup '(1 2) 3)    <span class='arw'>&rarr;</span> ((1 2) (1 2) (1 2))
(dup "\000" 4)    <span class='arw'>&rarr;</span> "\000\000\000\000"

(dup "*")         <span class='arw'>&rarr;</span> "**"
</pre>


<p>
	The last example shows handling of binary information, 
	creating a string filled with four binary zeroes.
</p>

<p>
	See also the functions <a href="#sequence">sequence</a> 
	and <a href="#series">series</a>.
</p>

<br/><br/>

<a name="emptyp"></a>
<h2><span class="function">empty?</span></h2>
<h4>syntax: (empty? <em>exp</em>)<br/>
syntax: (empty? <em>str</em>)</h4>


<p>
	<em>exp</em> is tested for an empty list 
	(or <em>str</em> for an empty string). 
	Depending on whether the argument contains elements, 
	<tt>true</tt> or <tt>nil</tt> is returned.
</p>

<!-- example -->

<pre>
(set 'var '())
(empty? var)         <span class='arw'>&rarr;</span> true
(empty? '(1 2 3 4))  <span class='arw'>&rarr;</span> nil
(empty? "hello")     <span class='arw'>&rarr;</span> nil
(empty? "")          <span class='arw'>&rarr;</span> true
</pre>


<p>
	The first example checks a list, 
	while the second two examples check a string.
</p>

<br/><br/>

<a name="encrypt"></a>
<h2><span class="function">encrypt</span></h2>
<h4>syntax: (encrypt <em>str-source</em> <em>str-pad</em>)</h4>

<p>
Performs a <a href="http://en.wikipedia.org/wiki/One-time_pad">one-time pad</a> (OTP)
encryption of <em>str-source</em> using the encryption pad in <em>str-pad</em>. 
The longer <em>str-pad</em> is and the more random the bytes are, 
the safer the encryption.  If the pad is as long as the source text, 
is fully random, and is used only once, then one-time&ndash;pad encryption 
is virtually impossible to break, since the encryption seems to contain only 
random data.  To retrieve the original, the same function and pad 
are applied again to the encrypted text:
</p>

<!-- example -->

<pre>
(set 'secret 
  (encrypt "A secret message" "my secret key")) 
<span class='arw'>&rarr;</span> ",YS\022\006\017\023\017TM\014\022\n\012\030E"

(encrypt secret "my secret key")  <span class='arw'>&rarr;</span> "A secret message"
</pre>


<p>
	The second example encrypts a whole file:
</p>



<pre>
(write-file "myfile.enc" 
  (encrypt (read-file "myfile") "29kH67*"))
</pre>


<br/><br/>

<a name="ends-with"></a>
<h2><span class="function">ends-with</span></h2>
<h4>syntax: (ends-with <em>str-data</em> <em>str-key</em> [<em>num-option</em>])<br/>
syntax: (ends-with <em>list</em> <em>exp</em>)</h4>

<p>In the first syntax, <tt>ends-with</tt> tests the string in <em>str-data</em> to see if it
ends with the string specified in <em>str-key</em>.  It returns <tt>true</tt> or <tt>nil</tt> 
depending on the outcome. </p>
	
<p>If a regular expression <em>option</em> number is 
specified, <em>str-key</em> contains a regular expression pattern. See
<a href="#regex">regex</a> for valid numbers for <em>option</em>.
</p>

<!-- example -->

<pre>
(ends-with "newLISP" "LISP")         <span class='arw'>&rarr;</span> true
(ends-with "newLISP" "lisp")         <span class='arw'>&rarr;</span> nil
;; use regular expressions
(ends-with "newLISP" "lisp|york" 1)  <span class='arw'>&rarr;</span> true
</pre>


<p>
	In the second syntax, 
	<tt>ends-with</tt> checks if a list 
	ends with the list element in <em>exp</em>. 
	<tt>true</tt> or <tt>nil</tt> is returned depending on outcome.
</p>

<!-- example -->

<pre>
(ends-with '(1 2 3 4 5) 5)             <span class='arw'>&rarr;</span> true
(ends-with '(a b c d e) 'b)            <span class='arw'>&rarr;</span> nil
(ends-with '(a b c (+ 3 4)) '(+ 3 4))  <span class='arw'>&rarr;</span> true
</pre>


<p>
	The last example shows that <em>exp</em> could be a list by itself.
</p>

<p>
	See also the <a href="#starts-with">starts-with</a> function.
</p>

<br/><br/>

<a name="env"></a>

<h2><span class="function">env</span></h2>
<h4>syntax: (env)<br/>
syntax: (env <em>var-str</em>)<br/>
syntax: (env <em>var-str</em> <em>value-str</em>)</h4>

<p>In the first syntax (without arguments), the operating system's environment is 
retrieved as an association list in which each entry is a key-value pair of
environment variable and value.</p>

<!-- example -->

<pre>
(env)  
<span class='arw'>&rarr;</span> (("PATH" "/bin:/usr/bin:/sbin") ("TERM" "xterm-color") ... ))
</pre>


<p>In the second syntax, the name of an environment variable 
is given in <em>var-str</em>. <tt>env</tt> returns the value 
of the variable or <tt>nil</tt> if the variable does not exist 
in the environment.</p>

<!-- example -->

<pre>
(env "PATH")  <span class='arw'>&rarr;</span> "/bin:/usr/bin:/usr/local/bin"
</pre>


<p>The third syntax (variable name in <em>var-str</em> 
and value pair in <em>value-str</em>) sets or creates 
an environment variable. If <em>value-str</em> is the 
empty string <tt>""</tt>, then the variable is completely
removed from the environment except when running on Solaris,
where the variable stays with an empty string.</p>

<!-- example -->

<pre>
(env "NEWLISPBIN" "/usr/local/bin/")  <span class='arw'>&rarr;</span> true
(env "NEWLISPBIN")              <span class='arw'>&rarr;</span> "/usr/bin/"
(env "NEWLISPBIN" "")           <span class='arw'>&rarr;</span> true
(env "NEWLISPBIN")              <span class='arw'>&rarr;</span> nil
</pre>

<br/><br/>

<a name="erf"></a>
<h2><span class="function">erf</span></h2>
<h4>syntax: (erf <em>num</em>)</h4>

<p>
	<tt>erf</tt> calculates the error function 
	of a number in <em>num</em>. 
	The error function is defined as:
</p>

<p><b><em>erf (x) = 2/sqrt(pi) * integral from 0 to x of exp(-t^2) dt</em></b></p>

<!-- example -->

<pre>
(map erf (sequence 0.0 6.0 0.5))
<span class='arw'>&rarr;</span> 
(0 0.5204998778 0.8427007929 0.9661051465 0.995322265 0.999593048 
 0.9999779095 0.9999992569 0.9999999846 0.9999999998 1 1 1) 
</pre>

<br/><br/>

<a name="error-event"></a>
<h2><span class="function">error-event</span></h2>
<h4>syntax: (error-event <em>sym-event-handler | func-event-handler</em>)<br/>
syntax: (error-event nil)</h4>

<p><em>sym-event-handler</em> contains a user-defined function for handling errors. 
Whenever an error occurs, the system performs a <a href="#reset">reset</a> 
and executes the user-defined error handler.  The error handler can use the 
built-in function <a href="#last-error">last-error</a> to retrieve the number 
and text of the error. The event handler is specified as either a quoted
symbol or a lambda function.</p>

<p>To cancel <tt>error-event</tt>, use the second syntax.</p>

<!-- example -->

<pre>
(define (my-handler)    
  (print "error # " (first (last-error)) " has occurred\n") )

(error-event 'my-handler)  <span class='arw'>&rarr;</span> my-handler

;; specify a function directly

(error-event my-handler)  <span class='arw'>&rarr;</span> $error-event

(error-event 
  (fn () (print "error # " (first (last-error)) " has occurred\n")))

(error-event exit)  <span class='arw'>&rarr;</span> $error-event
</pre>


<p>For a different way of handling errors, see the <a href="#catch">catch</a> function. 
Use <a href="#throw-error">throw-error</a> to throw user-defined errors.</p>

<br/><br/>

<a name="eval"></a>
<h2><span class="function">eval</span></h2>
<h4>syntax: (eval <em>exp</em>)</h4>

<p><em>eval</em> evaluates the result of evaluating <em>exp</em> in the current 
variable environment.</p>

<!-- example -->

<pre>
(set 'expr '(+ 3 4))  <span class='arw'>&rarr;</span> (+ 3 4)
(eval expr)           <span class='arw'>&rarr;</span> 7
(eval (list + 3 4))   <span class='arw'>&rarr;</span> 7
(eval ''x)            <span class='arw'>&rarr;</span> x
(set 'y 123)          
(set 'x 'y)           
x            <span class='arw'>&rarr;</span> y
(eval x)     <span class='arw'>&rarr;</span> 123
</pre>


<p>As usual, evaluation of variables happens in the current variable environment:</p>


<pre>
; eval in global (top level) environment
(set 'x 3 'y 4)
(eval '(+ x y))          <span class='arw'>&rarr;</span> 7

; eval in local environment
(let ( (x 33) (y 44) ) 
    (eval '(+ x y)))     <span class='arw'>&rarr;</span> 77

; old environment after leaving local let environment
(eval '(+ x y))          <span class='arw'>&rarr;</span> 7
</pre>


<p>
	newLISP passes all arguments by value. 
	Using a quoted symbol, 
	expressions can be passed 
	by reference through the symbol. 
	<tt>eval</tt> can be used 
	to access the original contents of the symbol:
</p>


<pre>
(define (change-list aList) (push 999 (eval aList)))

(set 'data '(1 2 3 4 5))

(change-list 'data)  <span class='arw'>&rarr;</span> (999 1 2 3 4 5)
</pre>


<p>
In the example, the parameter <tt>'data </tt> is quoted, 
so <tt>push</tt> can work on the original list.
</p>

<p>
There is a safer method to pass arguments by reference in newLISP 
by enclosing the data inside context objects. 
See the chapter <a href="#pass_big">Passing data by reference</a>.
Passing references into user defined
function using namespace ids avoids <em>variable capture</em> of
the passed symbol, in case the symbol passed is the same used as a
parameter in the function.</p>

<br/><br/>

<a name="eval-string"></a>
<h2><span class="function">eval-string</span></h2>
<h4>syntax: (eval-string <em>str-source</em> [<em>sym-context</em> [<em>exp-error</em> [<em>int-offset</em>]]])</h4>

<p>
The string in <em>str-source</em> is compiled into newLISP's internal format
and then evaluated. The evaluation result is returned. If the string contains 
more than one expression, the result of the last evaluation is returned.
</p>

<p>An optional second argument can be used to specify the context to which 
the string should be parsed and translated.</p>

<p>If an error occurs while parsing and evaluating <em>str-source</em> then
<em>exp-error</em> will be evaluated and the result returned.</p>

<p><em>int-offset</em> specifies an optional offset into <em>str-source</em>, 
where to start evaluation.</p>

<!-- example -->

<pre>
(eval-string "(+ 3 4)")  <span class='arw'>&rarr;</span> 7
(set 'X 123)             <span class='arw'>&rarr;</span> 123
(eval-string "X")        <span class='arw'>&rarr;</span> 123

(define (repl) ; read print eval loop
  (while true
    (println "=&gt; " (eval-string (read-line) MAIN (last-error)))
  )
)

(set 'a 10)
(set 'b 20)
(set 'foo:a 11)
(set 'foo:b 22)

(eval-string "(+ a b)")       <span class='arw'>&rarr;</span> 30
(eval-string "(+ a b)" 'foo)  <span class='arw'>&rarr;</span> 33
</pre>


<p>The second example shows a simple newLISP interpreter eval loop.</p> 

<p>The last example shows how to specify a target context for translation. The symbols 
<tt>a</tt> and <tt>b</tt> now refer to symbols and their values in context <tt>foo</tt> instead of 
<tt>MAIN</tt>.</p>
	
<p>See also the function <a href="#read-expr">read-expr</a> which translates a string
without evaluating it.</p>

<br/><br/>

<a name="eval-string-js"></a>
<h2><span class="function">eval-string-js
<a href="#JS"><font size="-1">JS</font></a></span></h2>
<h4>syntax: (eval-string-js <em>str-JavaScript-source</em>)</h4>
<p>The function takes a program source in <em>str-JavaScript-source</em>
and returns the result in a string.</p>

<p>This function is only available on newLISP compiled to JavaScript.</p>

<pre>
(eval-string-js "window.prompt('Enter some text:', '')")

; for single and double quotes inside a string passed to a
; JavaScropt function, single and double quotes must be
; preceded by a backslash \ and the whole string passed
; to eval-string-js limited by [text], [/text] tags.

(eval-string-js [text]alert('A double quote: \" and a single quote: \' ')[/text])

(eval-string-js "6 * 7")
</pre>

<p>The first expression will pop up a dialog box to enter text. The function
will return the text string entered. The second expression will return the
string <tt>42</tt>.</p>

<p>See also the function <a href="#display-html">display-html</a> for displaying
an HTML page in the browser.</p>

<br/><br/>

<a name="evenp"></a>
<h2><span class="function">even?</span>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (even? <em>int-number</em>)</h4>

<p>Checks if an integer number is <em>even divisible</em> by <tt>2</tt>, without remainder. 
When a floating point number is passed for <em>int-number</em>, it will be converted to an
integer by cutting off its fractional part.</p>

<pre>
(even? 123)  <span class='arw'>&rarr;</span> nil
(even? 8)    <span class='arw'>&rarr;</span> true
(even? 8.7)  <span class='arw'>&rarr;</span> true
</pre>

<p>Use <a href="#oddp">odd?</a> to check if an integer is not divisible by <tt>2</tt>.</p>

<br/><br/>

<a name="exec"></a>
<h2><span class="function">exec</span></h2>
<h4>syntax: (exec <em>str-process</em>)<br/>
syntax: (exec <em>str-process</em> [<em>str-stdin</em>])</h4>

<p>In the first form, <tt>exec</tt> launches a process described in <em>str-process</em>
and returns all standard output as a list of strings 
(one for each line in standard out (STDOUT)).  <tt>exec</tt> returns <tt>nil</tt>
if the process could not be launched. If the process could be launched but 
only returns and error and no valid output, the empty list will be returned.
</p> 


<!-- example -->

<pre>
(exec "ls *.c")  <span class='arw'>&rarr;</span> ("newlisp.c" "nl-math.c" "nl-string.c")
</pre>


<p>
	The example starts a process and performs the shell command <tt>ls</tt>,
	capturing the output in an array of strings.
</p>

<p>
	In the second form, 
	<tt>exec</tt> creates a process pipe, 
	starts the process in <em>str-process</em>, 
	and receives from <em>str-stdin</em> 
	standard input for this process.
	The return value is <tt>true</tt> 
	if the process was successfully launched; 
	otherwise it is <tt>nil</tt>.
</p>

<!-- example -->

<pre>
(exec "cgiProc" query)
</pre>


<p>
	In this example, 
	cgiProc could be a cgi processor (e.g., Perl or newLISP) 
	that receives and processes standard input supplied by a string 
	contained in the variable query.
</p>

<br/><br/>

<a name="exists"></a>
<h2><span class="function">exists</span></h2>
<h4>syntax: (exists <em>func-condition</em> <em>list</em>)</h4>

<p>Successively applies <em>func-condition</em> 
to the elements of <em>list</em> 
and returns the first element 
that meets the condition in <em>func-condition</em>. 
If no element meets the condition, 
<tt>nil</tt> is returned.</p>

<!-- example -->

<pre>
(exists string? '(2 3 4 6 "hello" 7))       <span class='arw'>&rarr;</span> "hello"

(exists string? '(3 4 2 -7 3 0))            <span class='arw'>&rarr;</span> nil

(exists zero? '(3 4 2 -7 3 0))              <span class='arw'>&rarr;</span> 0 ; check for 0 or 0.0

(exists &lt; '(3 4 2 -7 3 0))                  <span class='arw'>&rarr;</span> -7 ; check for negative

(exists (fn (x) (&gt; x 3)) '(3 4 2 -7 3 0))   <span class='arw'>&rarr;</span> 4

(exists (fn (x) (= x 10)) '(3 4 2 -7 3 0))  <span class='arw'>&rarr;</span> nil 
</pre>


<p>If <em>func-condition</em> is <tt>nil?</tt>, the result <tt>nil</tt> is ambiguous.
In this case <a href="#index">index</a> or <a href="#find">find</a> are the better 
method when looking for <tt>nil</tt>.</p>

<p>Use the <a href="#for-all">for-all</a> function 
to check if a condition is met for all elements in a list.</p>

<br/><br/>

<a name="exit"></a>
<h2><span class="function">exit</span></h2>
<h4>syntax: (exit [<em>int</em>])</h4>

<p>
	Exits newLISP. 
	An optional exit code, <em>int</em>, may be supplied. 
	This code can be tested by the host operating system. 
	When newLISP is run in <a href="#daemon">daemon server mode</a> 
 	using <tt>-d</tt> as a command-line option, 
	only the network connection is closed, 
	while newLISP stays resident, 
	listening for a new connection.
</p>

<!-- example -->

<pre>
(exit 5)
</pre>

<br/><br/>

<a name="exp"></a>
<h2><span class="function">exp</span></h2>
<h4>syntax: (exp <em>num</em>)</h4>

<p>The expression in <em>num</em> is evaluated, and the exponential function 
is calculated based on the result.  <tt>exp</tt> is the inverse function of 
<a href="#log">log</a>.
</p>

<!-- example -->

<pre>
(exp 1)        <span class='arw'>&rarr;</span> 2.718281828
(exp (log 1))  <span class='arw'>&rarr;</span> 1
</pre>

<br/><br/>

<a name="expand"></a>
<h2><span class="function">expand</span></h2>

<h4>syntax: (expand <em>exp</em> <em>sym-1</em> [<em>sym-2</em> ... ])<br/>
syntax: (expand <em>exp</em> <em>list-assoc</em> [<em>bool</em>])<br/>
syntax: (expand <em>exp</em>)</h4>

<p>In the first syntax, one symbol in <em>sym</em> 
(or more in <em>sym-2</em> through <em>sym-n</em>) 
is looked up in a simple or nested expression <em>exp</em>. 
They are then expanded to the current binding of the symbol 
and the expanded expression is returned. The original list remains unchanged.
</p>

<!-- example -->

<pre>
(set 'x 2 'a '(d e))
(set 'foo 'a)
(expand foo 'a)               <span class='arw'>&rarr;</span> (d e)
(expand '(a x b) 'x)           <span class='arw'>&rarr;</span> (a 2 b)
(expand '(a x (b c x)) 'x)     <span class='arw'>&rarr;</span> (a 2 (b c 2))
(expand '(a x (b c x)) 'x 'a)  <span class='arw'>&rarr;</span> ((d e) 2 (b c 2))
</pre>


<p><tt>expand</tt> is useful when composing lambda expressions 
and doing variable expansion as in rewrite macros.</p>


<pre>
(define (raise-to power)
  (expand (fn (base) (pow base power)) 'power))

(define square (raise-to 2))
(define cube (raise-to 3))

(square 5)  <span class='arw'>&rarr;</span> 25
(cube 5)    <span class='arw'>&rarr;</span> 125
</pre>


<p>
	If more than one symbol is present, 
	<tt>expand</tt> will work in an incremental fashion:
</p>


<pre>
(set 'a '(b c))
(set 'b 1)

(expand '(a b c) 'a 'b)  <span class='arw'>&rarr;</span> ((1 c) 1 c) 
</pre>


<p>
	Like the <a href="#apply">apply</a> function, 
	<tt>expand</tt> <em>reduces</em> its argument list.
</p>

<h4>syntax: (expand <em>list</em> <em>list-assoc</em> [<em>bool</em>])</h4>

<p>The second syntax of <tt>expand</tt> allows expansion bindings to be specified 
on the fly, without performing a <a href="#set">set</a> on the participating variables:
</p>

<p>If the <em>bool</em> evaluates to <tt>true</tt>, the value parts in the
association list are evaluated.</p>

<!-- example -->

<pre>
(expand '(a b c) '((a 1) (b 2)))                <span class='arw'>&rarr;</span> (1 2 c)
(expand '(a b c) '((a 1) (b 2) (c (x y z))))    <span class='arw'>&rarr;</span> (1 2 (x y z))
(expand '(a b) '((a (+ 1 2)) (b (+ 3 4))))      <span class='arw'>&rarr;</span> ((+ 1 2) (+ 3 4))
(expand '(a b) '((a (+ 1 2)) (b (+ 3 4))) true) <span class='arw'>&rarr;</span> (3 7)
</pre>


<p>
	Note that the contents of the variables 
	in the association list will not change. 
	This is different from the <a href="#letex">letex</a> function, 
	where variables are set by evaluating 
	and assigning their association parts.
</p>

<p>
	This form of <tt>expand</tt> is frequently used 
	in logic programming, 
	together with the <a href="#unify">unify</a> function.
</p>

<h4>syntax: (expand <em>list</em>)</h4>

<p>
	A third syntax is used to expand only the contents 
	of variables starting with an uppercase character. 
	This PROLOG mode may also be used 
	in the context of logic programming. 
	As in the first syntax of <tt>expand</tt>, 
	symbols must be preset. 
	Only uppercase variables and those bound 
	to anything other than <tt>nil</tt> 
	will be expanded:
</p>


<!-- example -->

<pre>
(set 'A 1 'Bvar 2 'C nil 'd 5 'e 6)
(expand '(A (Bvar) C d e f))  <span class='arw'>&rarr;</span> (1 (2) C d e f)
</pre>


<p>
	Only the symbols <tt>A</tt> and <tt>Bvar</tt> are expanded 
	because they have capitalized names 
	and non-<tt>nil</tt> contents.
</p>

<p>
	The <em>currying</em> function in the example 
	demonstrating the first syntax of <tt>expand</tt> 
	can now be written even more simply 
	using an uppercase variable:
</p>


<pre>
(define (raise-to Power) 
  (expand (fn (base) (pow base Power))))

&gt; (define cube (raise-to 3))
<b>(lambda (base) (pow base 3))</b>

&gt; (cube 4)
<b>64</b>

&gt; _
</pre>


<p>
	See the <a href="#letex">letex</a> function, 
	which also provides an expansion mechanism, 
	and the function <a href="#unify">unify</a>, 
	which is frequently used together with <tt>expand</tt>.
</p>

<br/><br/>

<a name="explode"></a>
<h2><span class="function">explode</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (explode <em>str</em> [<em>int-chunk</em> [<em>bool</em>]])<br/>
syntax: (explode <em>list</em> [<em>int-chunk</em> [<em>bool</em>]])</h4>

<p>
In the first syntax, 
<tt>explode</tt> transforms the string (<em>str</em>)
into a list of single-character strings. 
Optionally, a chunk size can be specified in <em>int-chunk</em> 
to break the string into multi-character chunks. 
When specifying a value for <em>bool</em> other than <tt>nil</tt>,
the last chunk will be omitted 
if it does not have the full length specified 
in <em>int-chunk</em>.
</p>

<!-- example -->

<pre>
(explode "newLISP")  <span class='arw'>&rarr;</span> ("n" "e" "w" "L" "I" "S" "P")

(join (explode "keep it together"))  <span class='arw'>&rarr;</span> "keep it together"

(explode "newLISP" 2)    <span class='arw'>&rarr;</span> ("ne" "wL" "IS" "P")

(explode "newLISP" 3)    <span class='arw'>&rarr;</span> ("new" "LIS" "P")

; omit last chunk if too short
(explode "newLISP" 3 true)    <span class='arw'>&rarr;</span> ("new" "LIS")
</pre>


<p>
Only on non UTF8&ndash; enabled versions, <tt>explode</tt> also works on binary content:
</p>


<pre>
(explode "\000\001\002\003") 
<span class='arw'>&rarr;</span> ("\000" "\001" "\002" "\003")
</pre>

<p>
When called in UTF-8&ndash;enabled versions of newLISP, 
<tt>explode</tt> will work on character boundaries rather than byte boundaries. 
In UTF-8&ndash;encoded strings, characters may contain more than one byte.
Processing will stop when a zero byte character is found.
</p>

<p>To explode binary contents on UTF-8&ndash;enabled versions of newLISP
use <a href="#unpack">unpack</a> as shown in the following example:</p>

<pre>
(set 'str "\001\002\003\004") <span class='arw'>&rarr;</span> "\001\002\003\004"

(unpack (dup "c" (length str)) str) <span class='arw'>&rarr;</span> (1 2 3 4)
(unpack (dup "s" (length str)) str) <span class='arw'>&rarr;</span> ("\001" "\002" "\003" "\004")
</pre>

<p>
In the second syntax, 
<tt>explode</tt> explodes a list (<em>list</em>)
into sublists of chunk size <em>int-chunk</em>, 
which is 1 (one) by default.
</p>

<p>
The following shows an example of the last chunk being omitted 
when the value for <em>bool</em> is other than <tt>nil</tt>,
and the chunk does not have the full length specified 
in <em>int-chunk</em>.
</p>

<!-- example -->

<pre>
(explode '(a b c d e f g h))    <span class='arw'>&rarr;</span> ((a) (b) (c) (d) (e) (f) (g) (h))
(explode '(a b c d e f g) 2)  <span class='arw'>&rarr;</span> ((a b) (c d) (e f) (g))

; omit last chunk if too short
(explode '(a b c d e f g) 2 true)  <span class='arw'>&rarr;</span> ((a b) (c d) (e f))

(transpose (explode '(a b c d e f g h) 2)) 
<span class='arw'>&rarr;</span> ((a c e g) (b d f h))
</pre>


<p>
The <a href="#join">join</a> and <a href="#append">append</a> functions
are inverse operations of <tt>explode</tt>.
</p>

<br/><br/>

<a name="extend"></a>
<h2><span class="function">extend</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (extend <em>list-1</em> [<em>list-2</em> ... ])<br/>
syntax: (extend <em>string-1</em> [<em>string-2</em> ... ])</h4>

<p>The list in <em>list-1</em> is extended by appending <em>list-2</em>. More
than one list may be appended.</p>

<p>The string in <em>string-1</em> is extended by appending <em>string-2</em>. More
than one string may be appended. The string can contain binary <tt>0</tt> (zero) 
characters.</p>

<p>The first parameter can be an un-initialized variable.</p>

<p>The extended list or string is returned.</p>

<!-- example -->

<pre>
; extending lists

(extend lst '(a b) '(c d)) <span class='arw'>&rarr;</span> (a b c d)
(extend lst '(e f g)) <span class='arw'>&rarr;</span> (a b c d e f)
lst <span class='arw'>&rarr;</span> (a b c d e f g)

; extending strings

(extend str "ab" "cd") <span class='arw'>&rarr;</span> "abcd"
(extend str "efg") <span class='arw'>&rarr;</span> "abcdefg"
str <span class='arw'>&rarr;</span> "abcdefg"

; extending in place

(set 'L '(a b "CD" (e f)))
(extend (L 2) "E")
L <span class='arw'>&rarr;</span> (a b "CDE" (e f))

(extend (L 3) '(g))
L <span class='arw'>&rarr;</span> (a b "CDE" (e f g))
</pre>

<p>For a non-destructive list or string extension see <a href="#append">append</a>.</p>

<br/><br/>

<a name="factor"></a>
<h2><span class="function">factor</span></h2>
<h4>syntax: (factor <em>int</em>)</h4>

<p>Factors the number in <em>int</em> into its prime components. 
When floating point numbers  are passed, they are truncated to 
their integer part first.</p>

<!-- example -->

<pre>
(factor 123456789123456789)  <span class='arw'>&rarr;</span> (3 3 7 11 13 19 3607 3803 52579)

;; check correctness of factoring
(= (apply * (factor 123456789123456789)) 123456789123456789)
<span class='arw'>&rarr;</span> true

;; factor the biggest integer
(factor 9223372036854775807)  <span class='arw'>&rarr;</span> (7 7 73 127 337 92737 649657)

;; primes.lsp - return all primes in a list, up to n 

(define (primes n , p)
  (dotimes (e n) 
    (if (= (length (factor e)) 1) 
      (push e p -1))) p)
           
(primes 20)  <span class='arw'>&rarr;</span> (2 3 5 7 11 13 17 19)         
</pre>


<p>
	<tt>factor</tt> returns <tt>nil</tt> 
	for numbers smaller than <tt>2</tt>.
	For numbers larger than 9,223,372,036,854,775,807
	(the largest 64-bit integer)
	converted from floating point numbers, 
	the largest integer is factored.
</p>

<br/><br/>

<a name="fft"></a>
<h2><span class="function">fft</span></h2>
<h4>syntax: (fft <em>list-num</em>)</h4>

<p>
	Calculates the discrete Fourier transform 
	on the list of complex numbers in <em>list-num</em> 
	using the FFT method (Fast Fourier Transform). 
	Each complex number is specified by its real part 
	followed by its imaginary part. 
	If only real numbers are used, 
	the imaginary part is set to <tt>0.0</tt> (zero). 
	When the number of elements in <em>list-num</em> 
	is not a power of 2, 
	<tt>fft</tt> increases the number of elements 
	by padding the list with zeroes. 
	When the imaginary part of a complex number is <tt>0</tt>, 
	simple numbers can be used instead.
</p>

<!-- example -->

<pre>
(ifft (fft '((1 0) (2 0) (3 0) (4 0)))) 
<span class='arw'>&rarr;</span> ((1 0) (2 0) (3 0) (4 0))

;; when imaginary part is 0, plain numbers work too
;; plain numbers and complex numbers can be intermixed

(fft '(1 2 3 4))      <span class='arw'>&rarr;</span> ((10 0) (-2 -2) (-2 0) (-2 2))
(fft '(1 2 (3 0) 4))  <span class='arw'>&rarr;</span> ((10 0) (-2 -2) (-2 0) (-2 2))
</pre>


<p>
	The inverse operation of <tt>fft</tt> 
	is the <a href="#ifft">ifft</a> function.
</p>

<br/><br/>

<a name="file-info"></a>
<h2><span class="function">file-info</span></h2>
<h4>syntax: (file-info <em>str-name</em> [<em>int-index</em> [<em>bool-flag</em>]])</h4>

<p>Returns a list of information about the file or directory in <em>str_name</em>. 
The optional index specifies the list member to return.  When no <em>bool-flag</em>
is specified or when <em>bool-flag</em> evaluates to <tt>nil</tt> information about
the link is returned if the file is a link to an original file. If <em>bool-flag</em>
evaluates to anything else than <tt>nil</tt>, information about the original file
referenced by the link is returned.</p>

<table   summary="file attributes">
<tr align="left"><th>offset</th><th>contents</th></tr>
<tr><td>0</td><td>size</td></tr>
<tr><td>1</td><td>mode (differs with <tt>true</tt> flag)</td></tr>
<tr><td>2</td><td>device mode</td></tr>
<tr><td>3</td><td>user ID</td></tr>

<tr><td>4</td><td>group ID</td></tr>
<tr><td>5</td><td>access time</td></tr>
<tr><td>6</td><td>modification time</td></tr>
<tr><td>7</td><td>status change time</td></tr>
</table><br/>

<p>Depending on <em>bool-flag</em> set, the function reports on either
the link (no flag or <tt>nil</tt> flag) or on the original linked file 
(<tt>true</tt> flag).</p>

<!-- example -->

<pre>
(file-info ".bashrc")   
<span class='arw'>&rarr;</span> (124 33188 0 500 0 920951022 920951022 920953074)

(file-info ".bashrc" 0)  <span class='arw'>&rarr;</span> 124

(date (file-info "/etc" -1))  <span class='arw'>&rarr;</span> "Mon Mar 8 18:23:17 2005"
</pre>


<p> In the second example, the last status change date 
for the directory <em>/etc</em> is retrieved.</p>

<p><tt>file-info</tt> gives file statistics (size) for a linked file,
not the link, except for the <em>mode</em> field.</p>

<br/><br/>

<a name="filep"></a>
<h2><span class="function">file?</span></h2>

<h4>syntax: (file? <em>str-path-name</em> [<em>bool</em>])</h4>

<p>Checks for the existence of a file in <em>str-name</em>. Returns <tt>true</tt> 
if the file exists; otherwise, it returns <tt>nil</tt>.  This function will also return 
<tt>true</tt> for directories. If the optional <em>bool</em> value is <tt>true</tt>,
the file must not be a directory and <em>str-path-name</em> is returned or <tt>nil</tt>
if the file is a directory. The existence of a file does not imply anything about its 
read or write permissions for the current user.</p>

<!-- example -->

<pre>
(if (file? "afile") (set 'fileNo (open "afile" "read")))

(file? "/usr/local/bin/newlisp" true) <span class='arw'>&rarr;</span> "/usr/local/bin/newlisp"
(file? "/usr/bin/foo" true)     <span class='arw'>&rarr;</span> nil
</pre>

<br/><br/>

<a name="filter"></a>
<h2><span class="function">filter</span></h2>
<h4>syntax: (filter <em>exp-predicate</em> <em>exp-list</em>)</h4>

<p>
	The predicate <em>exp-predicate</em> is applied 
	to each element of the list <em>exp-list</em>. 
	A list is returned containing the elements 
	for which <em>exp-predicate</em> is true. 
	<tt>filter</tt> works like <a href="#clean">clean</a>, 
	but with a negated predicate.
</p>

<!-- example -->

<pre>
(filter symbol? '(1 2 d 4 f g 5 h))  <span class='arw'>&rarr;</span> (d f g h)

(define (big? x) (&gt; x 5))  <span class='arw'>&rarr;</span> (lambda (x) (&gt; x 5))

(filter big? '(1 10 3 6 4 5 11))  <span class='arw'>&rarr;</span> (10 6 11)

; filter with comparison functor
(set 'L '((a 10 2 7) (b 5) (a 8 3) (c 8) (a 9)))

(filter (curry match '(a *)) L)   <span class='arw'>&rarr;</span> ((a 10 2 7) (a 8 3) (a 9))

(filter (curry match '(? ?)) L)   <span class='arw'>&rarr;</span> ((b 5) (c 8) (a 9))

(filter (curry match '(* 8 *)) L) <span class='arw'>&rarr;</span> ((a 8 3) (c 8))
</pre>


<p>
The predicate may be a built-in predicate, a user-defined function, 
or a lambda expression.
</p>

<p>
For filtering a list of elements with the elements from another list, 
use the <a href="#difference"> difference</a> function or 
<a href="#intersect">intersect</a> (with the <em>list</em> option).
</p>

<p>
See also the related function <a href="#index">index</a>, which returns the 
indices of the filtered elements and <a href="#clean">clean</a>, 
which returns all elements of a list for which a predicate is false.
</p>

<br/><br/>

<a name="find"></a>
<h2><span class="function">find</span></h2>
<h4>syntax: (find <em>exp-key</em> <em>list</em> [<em>func-compare</em> | <em>regex-option</em>])<br/>
syntax: (find <em>str-key</em> <em>str-data</em> [<em>regex-option</em> [<em>int-offset</em>]])</h4>


<h3>Find an expression in a list</h3>

<p>If the second argument evaluates to a <em>list</em>, then <tt>find</tt> returns 
the index position (offset) of the element derived from evaluating <em>exp-key</em>.</p>

<p>
Optionally, an operator or user-defined function can be specified in <em>func-compare</em>. 
If the <em>exp-key</em> is a string, a regular expression option
can be specified with the <em>regex-option</em> parameter.</p>

<p>When using regular expressions or comparison functors the system
variable <tt>$0</tt> is set to the last element found.</p>

<!-- example -->

<pre>
; find an expression in a list
(find '(1 2) '((1 4) 5 6 (1 2) (8 9)))  <span class='arw'>&rarr;</span> 3

(find "world" '("hello" "world"))       <span class='arw'>&rarr;</span> 1
(find "hi" '("hello" "world"))          <span class='arw'>&rarr;</span> nil

(find "newlisp" '("Perl" "Python" "newLISP") 1)  <span class='arw'>&rarr;</span> 2
; same with string option
(find "newlisp" '("Perl" "Python" "newLISP") "i")  <span class='arw'>&rarr;</span> 2

; use the comparison functor
(find 3 '(8 4 3  7 2 6) &gt;)  <span class='arw'>&rarr;</span> 4
$0 <span class='arw'>&rarr;</span> 2

(find "newlisp" '("Perl" "Python" "newLISP") 
                 (fn (x y) (regex x y 1))) <span class='arw'>&rarr;</span> 2
$0 <span class='arw'>&rarr;</span> "newLISP"

(find 5 '((l 3) (k 5) (a 10) (z 22)) 
         (fn (x y) (= x (last y))))  <span class='arw'>&rarr;</span> 1
$0 <span class='arw'>&rarr;</span> (k 5)

(find '(a ?) '((l 3) (k 5) (a 10) (z 22)) match)  <span class='arw'>&rarr;</span> 2
$0 <span class='arw'>&rarr;</span> (a 10)

(find '(X X) '((a b) (c d) (e e) (f g)) unify)  <span class='arw'>&rarr;</span> 2
$0 <span class='arw'>&rarr;</span> (e e)

; define the comparison functor first for better readability
(define (has-it-as-last x y) (= x (last y)))

(find 22 '((l 3) (k 5) (a 10) (z 22)) has-it-as-last)  <span class='arw'>&rarr;</span> 3
$0 <span class='arw'>&rarr;</span> (z 22)
</pre>


<p>
Using <a href="#match">match</a> and <a href="#unify">unify</a>, 
list searches can be formulated which are as powerful 
as regular expression searches are for strings.
</p>

<h3>Find a string in a string</h3>

<p>If the second argument, <em>str-data</em>, 
evaluates to a string, then the offset position 
of the string <em>str-key</em> (found in the first argument, 
<em>str-data</em>) is returned.  In this case, <tt>find</tt> 
also works on binary <em>str-data</em>. The offset position
returned is always based on counting single byte characters
even when running the UTF-8 enabled version of newLISP.</p>

<p>The presence of a third parameter specifies a search 
using the regular expression pattern specified in <em>str-pattern</em>, 
as well as an option number specified in <em>regex-option</em>
(i.e., 1 (one) for case-insensitive search or <tt>0</tt> (zero) 
for no special options). If <em>regex-option</em> is specified
an optional <em>int-offset</em> argument can be specified too
to start the search not at the beginning but at the offset given.
In any case the position returned by <tt>find</tt> is calculated
relative to the beginning of the string.</p>

<p>To specify <em>int-offset</em> in a simple string search without regular
expressions, specify <tt>nil</tt> for <em>regex-option</em>.</p>

<p>In newLISP, regular expressions are standard 
Perl Compatible Regular Expression (PCRE) searches. 
Found expressions or subexpressions are returned 
in the system variables <tt>$0</tt>, <tt>$1</tt>, <tt>$2</tt>, etc., 
which can be used like any other symbol. 
As an alternative, 
the contents of these variables 
can also be accessed 
by using <tt>($ 0)</tt>, <tt>($ 1)</tt>, <tt>($ 2)</tt>, etc. 
This method allows indexed access 
(i.e., <tt>($ i)</tt>, where <tt>i</tt> is an integer).
</p> 
	
<p>See <a href="#regex">regex</a> for the meaning of the  
option numbers and more information on regular expression searching.
</p> 
	
<!-- example -->

<pre>
; simple string search
(find "world" "Hello world")  <span class='arw'>&rarr;</span> 6
(find "WORLD" "Hello woRLd")  <span class='arw'>&rarr;</span> nil

; case-insensitive regex

(find "WorlD" "Hello woRLd" 1)  <span class='arw'>&rarr;</span> 6   
; or
(find "WorlD" "Hello woRLd" "i")  <span class='arw'>&rarr;</span> 6   
                                
(find "hi" "hello world")       <span class='arw'>&rarr;</span> nil
(find "Hello" "Hello world")    <span class='arw'>&rarr;</span> 0

; regex with default options

(find "cat|dog" "I have a cat" 0)  <span class='arw'>&rarr;</span> 9 
$0                                 <span class='arw'>&rarr;</span> "cat"
(find "cat|dog" "my dog" 0)        <span class='arw'>&rarr;</span> 3
$0                                 <span class='arw'>&rarr;</span> "dog"
(find "cat|dog" "MY DOG" 1)        <span class='arw'>&rarr;</span> 3
$0                                 <span class='arw'>&rarr;</span> "DOG"

; use an optional offset
(find "cat|dog" "I have a cat and a dog" 0)    <span class='arw'>&rarr;</span> 9
(find "cat|dog" "I have a cat and a dog" 0 12) <span class='arw'>&rarr;</span> 19

;; find with subexpressions in regular expression
;; and access with system variables

(set 'str  "http://nuevatec.com:80")

(find "http://(.*):(.*)" str 0)  <span class='arw'>&rarr;</span> 0
                                 
$0  <span class='arw'>&rarr;</span> "http://nuevatec.com:80"
$1  <span class='arw'>&rarr;</span> "nuevatec.com"
$2  <span class='arw'>&rarr;</span> "80"

;; system variables as an indexed expression (since 8.0.5)
($ 0)  <span class='arw'>&rarr;</span> "http://nuevatec.com:80"
($ 1)  <span class='arw'>&rarr;</span> "nuevatec.com"
($ 2)  <span class='arw'>&rarr;</span> "80"
</pre>


<p>
	For other functions using regular expressions, 
	see <a href="#directory">directory</a>, 
    <a href="#find-all">find-all</a>,
	<a href="#parse">parse</a>, 
	<a href="#regex">regex</a>, 
	<a href="#replace">replace</a>, 
	and <a href="#search">search</a>.
</p>

<p>
	To find expressions in nested 
	or multidimensional lists, 
	use the <a href="#ref">ref</a> and <a href="#ref-all">ref-all</a> functions.
</p>

<br/><br/>

<a name="find-all"></a>
<h2><span class="function">find-all</span></h2>
<h4>syntax: (find-all <em>str-regex-pattern</em> <em>str-text</em> [<em>exp</em> [<em>regex-option</em>]])<br/>
syntax: (find-all <em>list-match-pattern</em> <em>list</em> [<em>exp</em>])<br/>
syntax: (find-all <em>exp-key</em> <em>list</em> [<em>exp</em> [<em>func-compare</em>]])</h4>

<p>
In the first syntax, <tt>find-all</tt> finds all occurrences of <em>str-regex-pattern</em> 
in the text <em>str-text</em>, returning a list containing all matching strings. 
The empty list <tt>()</tt> is returned if no matches are found. In the first syntax
string searches are always done using regular expression patterns, even if no
<em>regex-option</em> is specified. The system variable <tt>$count</tt> is updated
with the number of matches found.</p>

<p>
Optionally, an expression can be specified to process the found string or regular subexpressions 
before placing them into the returned list. An additional option, <em>regex-option</em>, 
specifies special regular expression options 
(see <a href="#regex">regex</a> for further details).
</p>


<!-- example -->

<pre>
(find-all {\d+} "lkjhkljh34ghfdhgfd678gfdhfgd9")
<span class='arw'>&rarr;</span> ("34" "678" "9")

$count <span class='arw'>&rarr;</span> 3

(find-all {(new)(lisp)} "newLISPisNEWLISP" (append $2 $1) 1)
<span class='arw'>&rarr;</span> ("LISPnew" "LISPNEW")

(unique (sort 
    (find-all {[a-zA-Z]+} 
        (replace "&lt;[^&gt;]+&gt;" (get-url "http://newlisp.org") "" 0) )
))
<span class='arw'>&rarr;</span> ("A" "ACC" "AI" "API" "About" "All" "Amazing" "Apps"
...
"where" "whole" "width" "wiki" "will" "with" "work" "written")

; use $count in evaluated expr
(find-all "a" "ababab" (string $count $it)) <span class='arw'>&rarr;</span> ("1a" "2a" "3a")
</pre>


<p>
The first example discovers all numbers in a text. 
The second example shows how an optional expression in <em>exp</em> 
can work on subexpressions found by the regular expression pattern 
in <em>str-pattern</em>. The last example retrieves a web page, 
cleans out all HTML tags, and then collects all words 
into a unique and sorted list.
</p>

<p>
Note that <tt>find-all</tt> with strings always performs a regular expression search, 
even if the option in <em>regex-option</em> is omitted.
</p>

<p>In the second syntax, <tt>find-all</tt> searches for all list 
<a href="#match">match</a> patterns <em>list-match-pattern</em> in 
<em>list</em>. As in <tt>find-all</tt> for strings, an expression can 
be specified in <em>exp</em> to process further the matched sublist found in 
<em>list</em>. The system variable <tt>$count</tt> is updated with the number
 of matches found.</p>

<!-- example -->

<pre>
(find-all '(? 2) '((a 1) (b 2) (a 2) (c 4))) <span class='arw'>&rarr;</span> ((b 2) (a 2))

(find-all '(? 2) '((a 1) (b 2) (a 2) (c 4)) (first $it)) <span class='arw'>&rarr;</span> (b a)

$count <span class='arw'>&rarr;</span> 2
</pre>


<p><tt>find-all</tt> for list matches always uses <a href="#match">match</a> to compare when
searching for sublists and always needs a list for the pattern expression.</p>

<p>In the third syntax, <tt>find-all</tt> can specify a built-in or user-defined
function used for comparing list elements with the key expression in <em>exp-key</em>:</p>


<!-- example -->

<pre>
(find-all 5 '(2 7 4 5 9 2 4 9 7 4 8) $it &lt;) <span class='arw'>&rarr;</span> (7 9 9 7 8)

; process the found element available in $it

(find-all 5 '(2 7 4 5 9 2 4 9 7 4 8) (* 3 $it) &lt;) <span class='arw'>&rarr;</span> (21 27 27 21 24)
; same as
(find-all 5 '(2 7 4 5 9 2 4 9 7 4 8) (* 3 $it) (fn (x y) (&lt; x y))) <span class='arw'>&rarr;</span> (21 27 27 21 24)


(find-all 5 '(2 7 4 5 9 2 4 9 7 4 8) ("abcdefghijk" $it) &lt;) <span class='arw'>&rarr;</span> ("h" "j" "j" "h" "i")

$count <span class='arw'>&rarr;</span> 5

; use $count
(find-all 'a '(a b a b a b) (list $count $it)) <span class='arw'>&rarr;</span> ((1 a) (2 a) (3 a))
</pre>


<p>Any type of expression can be searched for or can be contained in the list. <tt>find-all</tt>
in this syntax works similar to <a href="#filter">filter</a> but with the added benefit of
being able to define a processing expression for the found element.</p>

<p>If no <em>func-compare</em> is defined and <em>exp-key</em> is a list, then
<a href="#match">match</a> will be used for comparison, as in the second syntax.</p>

<br/><br/>

<a name="first"></a>
<h2><span class="function">first</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (first <em>list</em>)<br/>
syntax: (first <em>array</em>)<br/>
syntax: (first <em>str</em>)</h4>

<p>
Returns the first element of a list or the first character of a string.
The operand is not changed.  This function is equivalent to <em>car</em>
or <em>head</em> in other Lisp dialects.</p>

<!-- example -->

<pre>
(first '(1 2 3 4 5))       <span class='arw'>&rarr;</span> 1
(first '((a b) c d))       <span class='arw'>&rarr;</span> (a b)
(set 'aList '(a b c d e))  <span class='arw'>&rarr;</span> (a b c d e)
(first aList)              <span class='arw'>&rarr;</span> a
aList                      <span class='arw'>&rarr;</span> (a b c d e)
(set 'A (array 3 2 (sequence 1 6)))
<span class='arw'>&rarr;</span>  ((1 2) (3 4) (5 6))
(first A)                  <span class='arw'>&rarr;</span> (1 2)

(first '())                <span class='arw'>&rarr;</span> <span class='err'>ERR: list is empty</span>
</pre>


<p>In the third syntax, the first character is returned 
from the string in <em>str</em> as a string.</p>

<!-- example -->

<pre>
(first "newLISP")         <span class='arw'>&rarr;</span> "n"
(first (rest "newLISP"))  <span class='arw'>&rarr;</span> "e"
</pre>


<p>
	Note that <a href="#first">first</a> works on character boundaries 
	rather than byte boundaries 
	when the UTF-8&ndash;enabled version of newLISP is used.
	See also the functions <a href="#last">last</a> 
	and <a href="#rest">rest</a>.
</p>

<br/><br/>

<a name="flat"></a>
<h2><span class="function">flat</span></h2>
<h4>syntax: (flat <em>list</em> [<em>int-level</em>])</h4>

<p>Returns a flattened list from a list:</p>

<!-- example -->

<pre>
(set 'lst '(a (b (c d))))
(flat lst)  <span class='arw'>&rarr;</span> (a b c d)

; extract a list of index vectors of all elements

(map (fn (x) (ref x lst)) (flat lst)) 
<span class='arw'>&rarr;</span> ((0) (1 0) (1 1 0) (1 1 1))
</pre>

<p>The optional <em>int-level</em> parameter can be used to limit
the recursion level when flattening the list:</p>

<!-- example -->

<pre>
(flat '(a b (c d (e f)) (g h (i j))) )   <span class='arw'>&rarr;</span> (a b c d e f g h i j)

(flat '(a b (c d (e f)) (g h (i j))) 1)  <span class='arw'>&rarr;</span> (a b c d (e f) g h (i j))

(flat '(a b (c d (e f)) (g h (i j))) 2)  <span class='arw'>&rarr;</span> (a b c d e f g h i j)
</pre>

<p>If <em>int-level</em> is <tt>0</tt>, no flattening will occur.</p>

<p><tt>flat</tt> can be used to iterate through nested lists.</p>


<br/><br/>

<a name="float"></a>
<h2><span class="function">float</span></h2>
<h4>syntax: (float <em>exp</em> [<em>exp-default</em>])</h4>

<p>
	If the expression in <em>exp</em> 
	evaluates to a number or a string, 
	the argument is converted to a float 
	and returned. 
	If <em>exp</em> cannot be converted to a float 
	then <tt>nil</tt> or, if specified, 
	the evaluation of <em>exp-default</em> 
	will be returned.
	This function is mostly used to convert strings 
	from user input or when reading and parsing text. 
	The string must start with a digit 
	or the <tt>+</tt> (plus sign), <tt>-</tt> (minus sign), 
	or <tt>.</tt> (period). 
	If <em>exp</em> is invalid,
	<tt>float</tt> returns <tt>nil</tt> 
	as a default value.
</p>

<p>
	Floats with exponents larger than 1e308 
	or smaller than -1e308 
	are converted to +INF or -INF, respectively. 
	The display of +INF and -INF 
	differs on different platforms and compilers.
</p>

<!-- example -->

<pre>
(float "1.23")       <span class='arw'>&rarr;</span> 1.23
(float " 1.23")      <span class='arw'>&rarr;</span> 1.23
(float ".5")         <span class='arw'>&rarr;</span> 0.50
(float "-1.23")      <span class='arw'>&rarr;</span> -1.23
(float "-.5")        <span class='arw'>&rarr;</span> nil
(float "#1.23")      <span class='arw'>&rarr;</span> nil
(float "#1.23" 0.0)  <span class='arw'>&rarr;</span> 0

(float? 123)          <span class='arw'>&rarr;</span> nil
(float? (float 123))  <span class='arw'>&rarr;</span> true

(float '(a b c))    <span class='arw'>&rarr;</span> nil
(float '(a b c) 0)  <span class='arw'>&rarr;</span> 0
(float nil 0)       <span class='arw'>&rarr;</span> 0

(float "abc" "not a number")  <span class='arw'>&rarr;</span> "not a number"
(float "1e500")               <span class='arw'>&rarr;</span> inf
(float "-1e500")              <span class='arw'>&rarr;</span> -inf

(print "Enter a float num:")
(set 'f-num (float (read-line)))
</pre>


<p>
	Use the <a href="#int">int</a> function 
	to parse integer numbers.
</p>

<br/><br/>

<a name="floatp"></a>
<h2><span class="function">float?</span></h2>
<h4>syntax: (float? <em>exp</em>)</h4>

<p>
	<tt>true</tt> is returned only 
	if <em>exp</em> evaluates to a floating point number;
	otherwise, <tt>nil</tt> is returned.
</p>

<!-- example -->

<pre>
(set 'num 1.23)
(float? num)  <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="floor"></a>
<h2><span class="function">floor</span></h2>
<h4>syntax: (floor <em>number</em>)</h4>

<p>
	Returns the next lowest integer below <em>number</em> 
	as a floating point.
</p>

<!-- example -->

<pre>
(floor -1.5)  <span class='arw'>&rarr;</span> -2
(floor 3.4)   <span class='arw'>&rarr;</span> 3
</pre>


<p>
	See also the <a href="#ceil">ceil</a> function.
</p>

<br/><br/>

<a name="flt"></a>
<h2><span class="function">flt</span></h2>
<h4>syntax: (flt <em>number</em>)</h4>

<p>
	Converts <em>number</em> to a 32-bit float 
	represented by an integer. 
	This function is used when passing 32-bit floats 
	to library routines.
	newLISP floating point numbers 
	are 64-bit and are passed as 64-bit floats 
	when calling imported C library routines.
</p>


<!-- example -->

<pre>
(flt 1.23)  <span class='arw'>&rarr;</span> 1067282596

;; pass 32-bit float to C-function: foo(float value) 
(import "mylib.so" "foo")
(foo (flt 1.23))

(get-int (pack "f" 1.23))  <span class='arw'>&rarr;</span> 1067282596

(unpack "f" (pack "ld" (flt 1.2345)))  <span class='arw'>&rarr;</span> (1.234500051)
</pre>


<p>
	The last two statements illustrate 
	the inner workings of <tt>flt</tt>.
</p>

<p>
	Use the <a href="#import">import</a> function 
	to import libraries.
</p>

<br/><br/>

<a name="fn"></a>
<h2><span class="function">fn</span></h2> 
<h4>syntax: (fn (<em>list-parameters</em>) <em>exp-body</em>)</h4>

<p>
<tt>fn</tt> or <tt>lambda</tt> are used to define anonymous functions, 
which are frequently used in <a href="#map">map</a>, <a href="#sort">sort</a>, 
and all other expressions where functions can be used as arguments.
The <tt>fn</tt> or <tt>lambda</tt> word does not exist on its own as a symbol,
but indicates a special list type: the <em>lambda list</em>. Together with <tt>fn-macro</tt>
and <tt>lambda-macro</tt> these terms are recognized during source parsing. They indicate a 
specialized type of list which can be used and applied like a function or operator. 
</p>

<p>
Using an anonymous function eliminates the need to define a new function with 
<a href="#define">define</a>. Instead, a function is defined on the fly:
</p>

<!-- example -->

<pre>
(map (fn (x) (+ x x)) '(1 2 3 4 5)) <span class='arw'>&rarr;</span> (2 4 6 8 10)

(sort '(".." "..." "." ".....") (fn (x y) (&gt; (length x) (length y))))
<span class='arw'>&rarr;</span> ("....." "..." ".." ".")
</pre>


<p>
The example defines the function <em>fn(x)</em>, which takes an integer 
(<em>x</em>) and doubles it.  The function is <em>mapped</em> onto a list of 
arguments using <a href="#map">map</a>. The second example shows strings being 
sorted by length.
</p>

<p>
The <a href="#lambda">lambda</a> function (the longer, traditional form of writing)
can be used in place of <tt>fn</tt>.
</p>

<br/><br/>

<a name="for"></a>
<h2><span class="function">for</span></h2>
<h4>syntax: (for (<em>sym</em> <em>num-from</em> <em>num-to</em> [<em>num-step</em> [<em>exp-break</em>]]) <em>body</em>)</h4>

<p>
	Repeatedly evaluates the expressions in <em>body</em> 
	for a range of values specified
	in <em>num-from</em> and <em>num-to</em>, inclusive.  
	A step size may be specified with <em>num-step</em>.  
	If no step size is specified, <tt>1</tt> is assumed.
</p>

<p>
	Optionally, a condition for early loop exit
	may be defined in <em>exp-break</em>. 
	If the break expression evaluates 
	to any non-<tt>nil</tt> value, 
	the <tt>for</tt> loop returns with 
	the value of <em>exp-break</em>. 
	The break condition is tested 
	before evaluating <em>body</em>. If a
	break condition is defined, <em>num-step</em>
	must be defined, too.
</p>

<p>
	The symbol <em>sym</em> 
	is local in dynamic scope 
	to the <tt>for</tt> expression.
	It takes on each value successively 
	in the specified range as an integer value 
	if no step size is specified, or
	as a floating point value when a step size is
	present. After evaluation of the <tt>for</tt>
    statement <em>sym</em> assumes its previous
    value.
</p>

<!-- example -->

<pre>
&gt; (for (x 1 10 2) (println x))
<b>1
3
5
7
9</b>

&gt; (for (x 8 6 0.5) (println x))
<b>8
7.5
7
6.5
6</b>

&gt; (for (x 1 100 2 (&gt; (* x x) 30)) (println x))
<b>1
3
5
true</b>
&gt; _
</pre>


<p>
	The second example uses 
	a range of numbers 
	from highest to lowest. 
	Note that the step size 
	is always a positive number. 
	In the third example, 
	a break condition is tested.
</p>

<p>
	Use the <a href="#sequence">sequence</a> function
	to make a sequence of numbers.
</p>

<br/><br/>

<a name="for-all"></a>
<h2><span class="function">for-all</span></h2>

<h4>syntax: (for-all <em>func-condition</em> <em>list</em>)</h4>

<p>Applies the function in <em>func-condition</em> 
to all elements in <em>list</em>.
If all elements meet the condition in <em>func-condition</em>,
the result is <tt>true</tt>;
otherwise, <tt>nil</tt> is returned.</p>

<!-- example -->

<pre>
(for-all number? '(2 3 4 6 7))                 <span class='arw'>&rarr;</span> true

(for-all number? '(2 3 4 6 "hello" 7))         <span class='arw'>&rarr;</span> nil

(for-all (fn (x) (= x 10)) '(10 10 10 10 10))  <span class='arw'>&rarr;</span> true
</pre>


<p>Use the <a href="#exists">exists</a> function
to check if at least one element in a list 
meets a condition.</p>

<br/><br/>

<a name="fork"></a>
<h2><span class="function">fork</span></h2>

<h4>syntax: (fork <em>exp</em>)</h4>

<p>
The expression in <em>exp</em> is launched as a newLISP child process-thread 
of the platforms OS. The new process inherits the entire address space, 
but runs independently so symbol or variable contents changed in the child process 
will not affect the parent process or vice versa. The child process ends 
when the evaluation of <em>exp</em> finishes.
</p>

<p>
On success, <tt>fork</tt> returns with the child process ID; on failure, 
<tt>nil</tt> is returned. See also the <a href="#wait-pid">wait-pid</a> function, 
which waits for a child process to finish.
</p>

<p>
This function is only available on Linux/Unix versions of newLISP 
and is based on the <tt>fork()</tt> implementation of the underlying OS. </p>

<p>A much simpler automated method to launch processes and collect
results is available with <a href="#spawn">spawn</a> and the <a href="#cilk">Cilk API</a>.</p>

<!-- example -->

<pre>
&gt; (set 'x 0)
<b>0</b>
&gt; (fork (while (&lt; x 20) (println (inc x)) (sleep 1000)))
<b>176</b>

&gt; <b>1
2
3
4
5
6</b>
</pre>


<p>
The example illustrates how the child process-thread inherits the symbol space 
and how it is independent of the parent process. The <tt>fork</tt> statement 
returns immediately with the process ID <tt>176</tt>. The child process increments 
the variable <tt>x</tt> by one each second and prints it to standard out (boldface). 
In the parent process, commands can still be entered. Type <tt>x</tt> to see that 
the symbol <tt>x</tt> still has the value <tt>0</tt> (zero) in the parent process. 
Although statements entered will mix with the display of the child process output, 
they will be correctly input to the parent process.</p>

<p>
The second example illustrates how <a href="#pipe">pipe</a> can be used 
to communicate between processes.</p>

<!-- example -->

<pre>
#!/usr/local/bin/newlisp

(define (count-down-proc x channel)
  (while (!= x 0)
      (write-line channel (string x))
      (dec x)))

(define (observer-proc channel)
  (do-until (= i "1")
    (println "process " (setq i (read-line channel)))))

(map set '(in out) (pipe))
(set 'observer (fork (observer-proc in)))
(set 'counter (fork (count-down-proc 5 out)))

; avoid zombies
(wait-pid observer)
(wait-pid counter)

(exit)
</pre>


<p>The following output is generated by observer-proc</p>


<pre>
<b>process 5
process 4
process 3
process 2
process 1</b>
</pre>


<p>
The <tt>count-down-proc</tt> writes numbers to the communication pipe, 
where they are picked up by the <tt>observer-process</tt> and displayed.
</p>

<p>A forked process can either exit by itself or it can be destroyed using
the <a href="#destroy">destroy</a> function.</p>


<pre>
(define (fork-destroy-demo)
    (set 'pid (fork (dotimes (i 1000) (println i) (sleep 10))))
    (sleep 50)
    (destroy pid) 
)

&gt; (fork-destroy-demo)
<b>0
1
2
3
4
true</b>
&gt; 
</pre>


<p>The process started by <tt>fork-destroy-demo</tt> will not finish but is
destroyed 50 milli-seconds after start by a call to <a href="#destroy">destroy</a>.
</p> 

<p>
Use the <a href="#semaphore">semaphore</a> function for synchronizing processes 
and <a href="#share">share</a> for sharing memory between processes.
</p>

<p>See <a href="#spawn">spawn</a> for a much simpler and automated way to
synchronize processes and collect results.</p>

<br/><br/>

<a name="format"></a>
<h2><span class="function">format</span></h2>
<h4>syntax: (format <em>str-format exp-data-1</em> [<em>exp-data-2</em> ... ])<br/>
syntax: (format <em>str-format</em> <em>list-data</em>)</h4>

<p>Constructs a formatted string from <em>exp-data-1</em> 
using the format specified in the evaluation of <em>str-format</em>. 
The format specified is identical to the format used for the <tt>printf()</tt> 
function in the ANSI C language.  Two or more <em>exp-data</em> arguments 
can be specified for more than one format specifier in <em>str-format</em>.</p>

<p>In an alternative syntax, the data to be formatted 
can be passed inside a list in <em>list-data</em>.</p>

<p><tt>format</tt> checks for a valid format string, 
matching data type, and the correct number of arguments. 
Wrong formats or data types result in error messages. 
<a href="#int">int</a>, <a href="#float">float</a>, 
or <a href="#string">string</a> can be used 
to ensure correct data types and to avoid error messages.</p>

<p>The format string has the following general format:</p>


<b>"%w.pf"</b>

<p>The <tt>%</tt> (percent sign) starts a format specification. 
To display a <tt>%</tt> inside a format string, double it: <tt>%%</tt></p>

<p>On Linux the percent sign can be followed by a single quote <tt>%'</tt>
to insert thousand's separators in number formats.</p>


<p>The <tt>w</tt> represents the width field. Data is right-aligned, except when 
preceded by a minus sign, in which case it is left-aligned. If preceded by a 
<tt>+</tt> (plus sign), positive numbers are displayed with a <tt>+</tt>.  
When preceded by a <tt>0</tt> (zero), the unused space is filled with leading 
zeroes. The width field is optional and serves all data types.</p>

<p>The <tt>p</tt> represents the precision number of decimals (floating point only) 
or strings and is separated from the width field by a period. Precision is 
optional. When using the precision field on strings, the number of characters 
displayed is limited to the number in <tt>p</tt>.</p>

<p>The <tt>f</tt> represents a type flag and is essential; 
it cannot be omitted.</p>

<p>Below are the types in <tt>f</tt>:</p>

<table summary="format characters">
<tr align="left"><th>format</th><th>description</th></tr>
<tr><td>s</td><td>text string</td></tr>
<tr><td>c</td><td>character (value 1 - 255)</td></tr>
<tr><td>d</td><td>decimal (32-bit)</td></tr>
<tr><td>u</td><td>unsigned decimal (32-bit)</td></tr>
<tr><td>x</td><td>hexadecimal lowercase</td></tr>
<tr><td>X</td><td>hexadecimal uppercase</td></tr>
<tr><td>o</td><td>octal (32-bits) (not supported on all of newLISP flavors)</td></tr>
<tr><td>f</td><td>floating point</td></tr>
<tr><td>e</td><td>scientific floating point</td></tr>
<tr><td>E</td><td>scientific floating point</td></tr>
<tr><td>g</td><td>general floating point</td></tr>
</table><br/>

<p>Formatting 64-bit numbers using the 32-bit format specifiers from above table 
will truncate and format the lower 32 bits of the number on 64-bit systerms and overflow to
<tt>0xFFFFFFFF</tt> on 32-bit systems.</p>

<p>For 32-bit and 64-bit numbers use the following format 
strings. 64-bit numbers will be truncated to 32-bit on
32-bit platforms:</p>

<table summary="format characters">
<tr align="left"><th>format</th><th>description</th></tr>
<tr><td>ld</td><td>decimal (32/64-bit)</td></tr>
<tr><td>lu</td><td>unsigned decimal (32/64-bit)</td></tr>
<tr><td>lx</td><td>hexadecimal (32/64-bit)</td></tr>
<tr><td>lX</td><td>hexadecimal uppercase (32/64-bit)</td></tr>
</table><br/>

<p>For 64-bit numbers use the following format strings on Unix-like 
operating systems and on MS Windows (not supported on TRU64):</p>

<table summary="format characters">
<tr align="left"><th>format</th><th>description</th></tr>
<tr><td>lld</td><td>decimal (64-bit)</td></tr>
<tr><td>llu</td><td>unsigned decimal (64-bit)</td></tr>
<tr><td>llx</td><td>hexadecimal (64-bit)</td></tr>
<tr><td>llX</td><td>hexadecimal uppercase(64-bit)</td></tr>
</table><br/>

<p>On Windows platforms only the following characters apply 
for 64 bit numbers:</p>

<table summary="format characters">
<tr align="left"><th>format</th><th>description</th></tr>
<tr><td>I64d</td><td>decimal (64-bit)</td></tr>
<tr><td>I64u</td><td>unsigned decimal (64-bit)</td></tr>
<tr><td>I64x</td><td>hexadecimal (64-bit)</td></tr>
<tr><td>I64X</td><td>hexadecimal uppercase(64-bit)</td></tr>
</table><br/>

<p>
	Other text may occur between, 
	before, or after the format specs.
</p>
	
<p>Note that on Tru64 Unix the format character <tt>i</tt> can be used instead
of <tt>d</tt>.
</p>

<br/><br/>

<!-- example -->

<pre>
(format "&gt;&gt;&gt;%6.2f&lt;&lt;&lt;" 1.2345)     <span class='arw'>&rarr;</span> "&gt;&gt;&gt;  1.23&lt;&lt;&lt;"
(format "&gt;&gt;&gt;%-6.2f&lt;&lt;&lt;" 1.2345)    <span class='arw'>&rarr;</span> "&gt;&gt;&gt;1.23  &lt;&lt;&lt;"
(format "&gt;&gt;&gt;%+6.2f&lt;&lt;&lt;" 1.2345)    <span class='arw'>&rarr;</span> "&gt;&gt;&gt; +1.23&lt;&lt;&lt;"
(format "&gt;&gt;&gt;%+6.2f&lt;&lt;&lt;" -1.2345)   <span class='arw'>&rarr;</span> "&gt;&gt;&gt; -1.23&lt;&lt;&lt;"
(format "&gt;&gt;&gt;%-+6.2f&lt;&lt;&lt;" -1.2345)  <span class='arw'>&rarr;</span> "&gt;&gt;&gt;-1.23 &lt;&lt;&lt;"

(format "%e" 123456789)        <span class='arw'>&rarr;</span> "1.234568e+08"
(format "%12.10E" 123456789)   <span class='arw'>&rarr;</span> "1.2345678900E+08"

(format "%10g" 1.23)   <span class='arw'>&rarr;</span> "      1.23"
(format "%10g" 1.234)  <span class='arw'>&rarr;</span> "     1.234"

(format "Result = %05d" 2)  <span class='arw'>&rarr;</span> "Result = 00002"

(format "%14.2f" 12345678.12)   <span class='arw'>&rarr;</span> "   12345678.12"
; on UNIX glibc compatible platforms only (Linux, MAC OS X 10.9) on some locales
(format "%'14.2f" 12345678.12) <span class='arw'>&rarr;</span> " 12,345,678.12"

(format "%8d" 12345)   <span class='arw'>&rarr;</span> "   12345"
; on UNIX glibc compatible platforms only (Linux, MAC OS X 10.9) on some locales
(format "%'8d" 12345)  <span class='arw'>&rarr;</span> "  12,345"

(format "%-15s" "hello")        <span class='arw'>&rarr;</span> "hello          "
(format "%15s %d" "hello" 123)  <span class='arw'>&rarr;</span> "          hello 123"
(format "%5.2s" "hello")        <span class='arw'>&rarr;</span> "   he"
(format "%-5.2s" "hello")       <span class='arw'>&rarr;</span> "he   "

(format "%o" 80)    <span class='arw'>&rarr;</span> "120"
                                
(format "%x %X" -1 -1)  <span class='arw'>&rarr;</span> "ffffffff FFFFFFFF"

; 64 bit numbers on Windows
(format "%I64X" 123456789012345678)  <span class='arw'>&rarr;</span> "1B69B4BA630F34E"

; 64 bit numbers on Unix (except TRU64)
(format "%llX" 123456789012345678)   <span class='arw'>&rarr;</span> "1B69B4BA630F34E"
                                
(format "%c" 65)  <span class='arw'>&rarr;</span> "A"
</pre>


<p>
	The data to be formatted 
	can be passed inside a list:
</p>


<pre>
(set 'L '("hello" 123))
(format "%15s %d" L)  <span class='arw'>&rarr;</span> "          hello 123"
</pre>


<p>
	If the format string requires it, 
	newLISP's <tt>format</tt> will 
	automatically convert integers 
	into floating points 
	or floating points into integers:
</p>


<pre>
(format "%f" 123)      <span class='arw'>&rarr;</span> 123.000000
                       
(format "%d" 123.456)  <span class='arw'>&rarr;</span> 123
</pre>

<br/><br/>

<a name="fv"></a>
<h2><span class="function">fv</span></h2>
<h4>syntax: (fv <em>num-rate</em> <em>num-nper</em> <em>num-pmt</em> <em>num-pv</em> [<em>int-type</em>])</h4>

<p>Calculates the future value of a loan with constant payment <em>num-pmt</em> 
and constant interest rate <em>num-rate</em> after <em>num-nper</em> period of 
time and a beginning principal value of <em>num-pv</em>.  If payment is at the 
end of the period, <em>int-type</em> is <tt>0</tt> (zero) or <em>int-type</em> is
omitted; for payment at the beginning of each period, <em>int-type</em> is 1. 
</p>

<!-- example -->

<pre>
(fv (div 0.07 12) 240 775.30 -100000)  <span class='arw'>&rarr;</span> -0.5544645052
</pre>


<p>
The example illustrates how a loan of $100,000 is paid down to a residual 
of $0.55 after 240 monthly payments at a yearly interest rate of 7 percent.
</p>

<p>
	See also the functions <a href="#irr">irr</a>, 
	<a href="#nper">nper</a>, <a href="#npv">npv</a>, 
	<a href="#pmt">pmt</a>, and <a href="#pv">pv</a>.
</p>

<br/><br/>

<a name="gammai"></a>
<h2><span class="function">gammai</span></h2>
<h4>syntax: (gammai <em>num-a</em> <em>num-b</em>)</h4>

<p>
	Calculates the incomplete Gamma function 
	of values <em>a</em> and <em>b</em> in <em>num-a</em> and <em>num-b</em>,
	respectively.
</p>

<!-- example -->

<pre>
(gammai 4 5)  <span class='arw'>&rarr;</span> 0.7349740847
</pre>


<p>
	The incomplete Gamma function is used to derive 
	the probability of Chi&sup2; to exceed a 
	given value for a degree of freedom, df, as follows:
</p>

<BLOCKQUOTE>
<em><b>Q(Chi&sup2;|df) = Q(df/2, Chi&sup2;/2) = gammai(df/2, Chi&sup2;/2)</b></em>
</BLOCKQUOTE>

<p>
	See also the <a href="#prob-chi2">prob-chi2</a> function.
</p>

<br/><br/>

<a name="gammaln"></a>
<h2><span class="function">gammaln</span></h2>
<h4>syntax: (gammaln <em>num-x</em>)</h4>

<p>
	Calculates the log Gamma function of the value <em>x</em> in <em>num-x</em>.
</p>

<!-- example -->

<pre>
(exp (gammaln 6))  <span class='arw'>&rarr;</span> 120
</pre>


<p>
	The example uses the equality of <em>n! = gamma(n + 1)</em> 
	to calculate the factorial value of 5.
</p>

<p>
	The log Gamma function is also related to the Beta function, 
	which can be derived from it:
</p>

<BLOCKQUOTE>
<em><b>Beta(z,w) = Exp(Gammaln(z) + Gammaln(w) - Gammaln(z+w))</b></em>

</BLOCKQUOTE>

<br/><br/>

<a name="gcd"></a>
<h2><span class="function">gcd</span>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (gcd <em>int-1</em> [<em>int-2</em> ... ])</h4>

<p>
Calculates the greatest common divisor 
of a group of integers.
The greatest common divisor of two integers 
that are not both zero 
is the largest integer that divides both numbers.
<tt>gcd</tt> will calculate the greatest common divisor 
for the first two integers in <em>int-i</em> 
and then further reduce the argument list 
by calculating the greatest common divisor of the result 
and the next argument in the parameter list.
</p>

<!-- example -->

<pre>
(gcd 0)        <span class='arw'>&rarr;</span> 0
(gcd 0 0)      <span class='arw'>&rarr;</span> 0
(gcd 10)       <span class='arw'>&rarr;</span> 10
(gcd 12 36)    <span class='arw'>&rarr;</span> 12
(gcd 15 36 6)  <span class='arw'>&rarr;</span> 3 
</pre>


<p>
See 
<a href="http://en.wikipedia.org/wiki/Greatest_common_divisor">Wikipedia</a>
for details and theory about gcd numbers in mathematics.
</p>

<br/><br/>

<a name="get-char"></a>
<h2><span class="function">get-char</span>&nbsp;
<a href="#shared-lib"><font size="+2">&#x26A0;</font></a></h2>
<h4>syntax: (get-char <em>int-address</em>)</h4>

<p>
Gets an 8-bit character from an address 
specified in <em>int-address</em>. 
This function is useful when using 
imported shared library functions 
with <a href="#import">import</a>.
</p>

<!-- example -->

<pre>
char * foo(void)
{
char * result;
result = "ABCDEFG";
return(result);
}
</pre>


<p>
Consider the above C function 
from a shared library, which returns a 
character pointer (address to a string).
</p>


<pre>
(import "mylib.so" "foo")
(print (get-char (foo) ))       <span class='arw'>&rarr;</span>  65 ; ASCII "A"
(print (get-char (+ (foo) 1)))  <span class='arw'>&rarr;</span>  66 ; ASCII "B"
</pre>



<p>
Note that it is unsafe to use the <tt>get-char</tt> function 
with an incorrect address in <em>int-address</em>. Doing so 
could result in the system crashing or becoming unstable.
</p>

<p>
See also the <a href="#address">address</a>, 
<a href="#get-int">get-int</a>, 
<a href="#get-long">get-long</a>, 
<a href="#get-float">get-float</a>, 
<a href="#get-string">get-string</a>, 
<a href="#pack">pack</a>, and <a href="#unpack">unpack</a> functions.
</p>

<br/><br/>

<a name="get-float"></a>
<h2><span class="function">get-float</span>&nbsp;
<a href="#shared-lib"><font size="+2">&#x26A0;</font></a></h2>
<h4>syntax: (get-float <em>int-address</em>)</h4>

<p>
Gets a 64-bit double float from an address 
specified in <em>int-address</em>.
This function is helpful when using 
imported shared library functions (with <tt>import</tt>)
that return an address pointer to a double float 
or a pointer to a structure containing double floats.
</p>

<!-- example -->

<pre>
double float * foo(void)
{
double float * result;
&hellip;
*result = 123.456;
return(result);
}
</pre>


<p>
The previous C function is compiled 
into a shared library.
</p>


<pre>
(import "mylib.so" "foo")
(get-float (foo))  <span class='arw'>&rarr;</span> 123.456
</pre>


<p>
<tt>foo</tt> is imported and returns a pointer 
to a double float when called.
Note that <tt>get-float</tt> is unsafe when used 
with an incorrect address in <em>int-address</em> 
and may result in the system crashing or becoming unstable.
</p>

<p>
See also the <a href="#address">address</a>, 
<a href="#get-int">get-int</a>, 
<a href="#get-long">get-long</a>,
<a href="#get-char">get-char</a>, 
<a href="#get-string">get-string</a>,
<a href="#pack">pack</a>, 
and <a href="#unpack">unpack</a> functions.
</p>

<br/><br/>

<a name="get-int"></a>
<h2><span class="function">get-int</span>&nbsp;
<a href="#shared-lib"><font size="+2">&#x26A0;</font></a></h2>
<h4>syntax: (get-int <em>int-address</em>)</h4>

<p>
Gets a 32-bit integer from 
the address specified in <em>int-address</em>.
This function is handy when using 
imported shared library functions with <tt>import</tt>,
a function returning an address pointer 
to an integer, or a pointer to a structure containing integers.
</p>

<!-- example -->

<pre>
int * foo(void)
{
int * result;
&hellip;
*result = 123;
return(result);
}

int foo-b(void)
{
int result;
&hellip;
result = 456;
return(result);
}
</pre>


<p>
Consider the C function <tt>foo</tt> (from a shared library), 
which returns an integer pointer (address of an integer).
</p>


<pre>
(import "mylib.so" "foo")
(get-int (foo))  <span class='arw'>&rarr;</span> 123
(foo-b)          <span class='arw'>&rarr;</span> 456
</pre>


<p>
Note that using <tt>get-int</tt> with an incorrect address 
in <em>int-address</em> is unsafe and could result 
in the system crashing or becoming unstable.
</p>

<p>
See also the <a href="#address">address</a>, 
<a href="#get-char">get-char</a>, 
<a href="#get-float">get-float</a>, 
<a href="#get-long">get-long</a>,
<a href="#get-string">get-string</a>,
<a href="#pack">pack</a>, 
and <a href="#unpack">unpack</a> functions.
</p>

<br/><br/>

<a name="get-long"></a>
<h2><span class="function">get-long</span>&nbsp;
<a href="#shared-lib"><font size="+2">&#x26A0;</font></a></h2>
<h4>syntax: (get-long <em>int-address</em>)</h4>

<p>
Gets a 64-bit integer from 
the address specified in <em>int-address</em>.
This function is handy when using <tt>import</tt>
to import shared library functions, 
a function returning an address pointer to a long integer, 
or a pointer to a structure containing long integers.
</p>

<!-- example -->

<pre>
long long int * foo(void)
{
int * result;
&hellip;
*result = 123;
return(result);
}

long long int foo-b(void)
{
int result;
&hellip;
result = 456;
return(result);
}
</pre>


<p>
Consider the C function <tt>foo</tt> (from a shared library), 
which returns an integer pointer (address of an integer).
</p>


<pre>
(import "mylib.so" "foo")
(get-int (foo))  <span class='arw'>&rarr;</span> 123
(foo-b)          <span class='arw'>&rarr;</span> 456
</pre>


<p>
Note that using <tt>get-long</tt> with an incorrect address 
in <em>int-address</em> is unsafe and could result 
in the system crashing or becoming unstable.
</p>

<p>
See also the <a href="#address">address</a>, 
<a href="#get-char">get-char</a>, 
<a href="#get-float">get-float</a>, 
<a href="#get-int">get-int</a>,
<a href="#get-string">get-string</a>,
<a href="#pack">pack</a>, 
and <a href="#unpack">unpack</a> functions.
</p>

<br/><br/>

<a name="get-string"></a>
<h2><span class="function">get-string</span>&nbsp;
<a href="#shared-lib"><font size="+2">&#x26A0;</font></a></h2>
<h4>syntax: (get-string <em>int-address</em> [<em>int-bytes</em> [<em>str-limit</em>])</h4>

<p>Copies a character string from the address specified in <em>int-address</em>.
This function is helpful when using imported shared library functions with 
<a href="#import">import</a> and a C-function returns the address to a memory buffer.</p>

<!-- example -->

<pre>
char * foo(void)
{
char * result;
result = "ABCDEFG";
return(result);
}
</pre>


<p>
Consider the above C function from a shared library, 
which returns a character pointer (address to a string).
</p>


<pre>
(import "mylib.so" "foo")
(print (get-string (foo)))  <span class='arw'>&rarr;</span> "ABCDEFG"
</pre>


<p>
When a string is passed as an argument, 
<tt>get-string</tt> will take its address as the argument. 
Without the optional <em>int-bytes</em> argument <tt>get-string</tt> breaks off 
at the first first <tt>\000</tt> (null character) it encounters. This works for
retrieving ASCII strings from raw memory addresses:</p>

<!-- example -->

<pre>
(set 'buff "ABC\000\000\000DEF")  <span class='arw'>&rarr;</span> "ABC\000\000\000DEF"

(length buff)  <span class='arw'>&rarr;</span> 9

(get-string buff)  <span class='arw'>&rarr;</span> "ABC"

(length (get-string buff))  <span class='arw'>&rarr;</span> 3

; get a string from offset into a buffer
(get-string (+ (address buff) 6)) <span class='arw'>&rarr;</span> "DEF"
</pre>

<p>When specifyung the number of bytes in the optional <em>int-bytes</em>
parameter, reading does not stpop at the first zero byte found, but
copies exactly <em>int-bytes</em> number of bytes from the address or string
buffer:</p>

<pre>
(set 'buff "ABC\000\000\000DEF")  <span class='arw'>&rarr;</span> "ABC\000\000\000DEF"

; without specifying the number of bytes
; buff is equivalent to (address buff)
(get-string buff)  <span class='arw'>&rarr;</span> "ABC"

; specifying the number of bytes to get
(get-string buff 9) <span class='arw'>&rarr;</span> "ABC\000\000\000DEF"
</pre>

<p>The addtional <em>str-limit</em> parameter can be used to limit reading
the buffer at a certain string. If <em>int-bytes</em> are read before
<em>str-limit</em> is found, only <em>int-bytes</em> are read:</p>

<pre>
(set 'buff "ABC\000\000EFG\000DQW") <span class='arw'>&rarr;</span> "ABC\000\000EFG\000DQW"

; buff is eqivalent to (address buff)
(get-string buff 4 "FG") <span class='arw'>&rarr;</span> "ABC\000"

(get-string buff 10) <span class='arw'>&rarr;</span> "ABC\000\000EFG\000D"

(get-string buff 10 "FG") <span class='arw'>&rarr;</span> "ABC\000\000E"
</pre>
 
<p>Although UTF-16 and UTF-32 encoding does not specify string termination characters,
the sequences "\000\000" and "\000\000\000\000" are used often to terminate UTF-16
and UTF-32 encodings. The additional optional  <em>str-limit</em> can be used to limit
the string when reading from the buffer address:</p>
<pre>
(set 'utf32 (unicode "我能吞下玻璃而不伤身体。"))

(set 'addr (address utf32)) <span class='arw'>&rarr;</span> 140592856255712

; get-string automatically takes the address when a buffer is passed
; utf32 is equivalent to (address utf32) for get-string

(get-string utf32 80 "\000\000\000\000")
<span class='arw'>&rarr;</span> "\017b\000\000??\000\000\030T\000\000\011N\ 000\000?s\
000\000?t\000\000\f?\000\000\rN\000\000$O\000\000??\000\000SO\000\000\0020\000\000"
</pre>

<p>When using  "\000\000" or "\000\000\000\000" as limit strings, the search for these
limits is aligned to a 2-byte or 4-byte border.</p>

<p>
See also the <a href="#get-char">get-char</a>, 
<a href="#get-int">get-int</a>,
<a href="#get-float">get-float</a>,
<a href="#pack">pack</a>,
and <a href="#unpack">unpack</a> functions.
</p>

<p>
Note that <tt>get-string</tt> can crash the system 
or make it unstable if the wrong address is specified.
</p>

<br/><br/>

<a name="get-url"></a>
<h2><span class="function">get-url</span></h2>
<h4>syntax: (get-url <em>str-url</em> [<em>str-option</em>] [<em>int-timeout</em> [<em>str-header</em>]])</h4>

<p>Reads a web page or file specified by the URL in <em>str-url</em> using 
the HTTP GET protocol.  Both <tt>http://</tt> and <tt>file://</tt>
URLs are handled.  <tt>"header"</tt> can be specified in the optional argument 
<em>str-option</em> to retrieve only the header.  The  option <tt>"list"</tt> 
causes header and page information to be returned as separate strings in a list
and also includes the server status code as the third list member (since 10.6.4).
The <tt>"raw"</tt> option (since 10.6.4), which can be used alone or combined
with other options, suppresses header location redirection.</p>

<p>A <tt>"debug"</tt> option can be specified either alone or after the
<tt>"header"</tt> or <tt>"list"</tt> option separated by one character, 
i.e. <tt>"header debug"</tt> or <tt>"list debug"</tt>. Including "debug" 
outputs all outgoing information to the console window.</p>

<p>The optional argument <em>int-timeout</em> can specify a value in milliseconds.
If no data is available from the host after the specified timeout, <tt>get-url</tt> 
returns the string <tt>ERR: timeout</tt>. When other error conditions occur, 
<tt>get-url</tt> returns a string starting with <tt>ERR:</tt> and the description 
of the error.</p>

<p><tt>get-url</tt> handles redirection if it detects a <tt>Location:</tt> spec 
in the received header and automatically does a second request.
<tt>get-url</tt> also understands the <tt>Transfer-Encoding: chunked</tt> 
format and will unpack data into an unchunked format.</p>

<p><tt>get-url</tt> requests are also understood by newLISP server nodes.
</p>

<!-- example -->

<pre>
(get-url "http://www.nuevatec.com")
(get-url "http://www.nuevatec.com" 3000)
(get-url "http://www.nuevatec.com" "header")
(get-url "http://www.nuevatec.com" "header" 5000)
(get-url "http://www.nuevatec.com" "list")

(get-url "file:///home/db/data.txt") ; access local file system

(env "HTTP_PROXY" "http://ourproxy:8080")
(get-url "http://www.nuevatec.com/newlisp/")
</pre>


<p>
The index page from the site specified 
in <em>str-url</em> is returned as a string.
In the third line, 
only the HTTP header 
is returned in a string. 
Lines 2 and 4 show a 
timeout value being used.
</p>

<p> The second example shows usage of a <tt>file://</tt> URL
to access <tt>/home/db/data.txt</tt> on the local file system.</p>

<p>
The third example illustrates 
the use of a proxy server. 
The proxy server's URL must be 
in the operating system's environment. 
As shown in the example, 
this can be added using 
the <a href="#env">env</a> 
function.
</p>

<p>
The <em>int-timeout</em> can be followed 
by an optional custom header in <em>str-header</em>:
</p>

<h3>Custom header</h3>

<p>The custom header may contain options 
for browser cookies or other directives to the server. 
When no <em>str-header</em> is specified, 
newLISP sends certain header information by default. 
After the following request:
</p>


<pre>
(get-url "http://somehost.com" 5000)
</pre>


<p>
newLISP will configure and send 
the request and header below:
</p>


<pre>
GET / HTTP/1.1        
Host: somehost.com
User-Agent: newLISP v10603
Connection: close
</pre>


<p>
As an alternative, the <em>str-header</em> 
option could be used:
</p>


<pre>
(get-url "http://somehost.com" 5000 
"User-Agent: Mozilla/4.0\r\nCookie: name=fred\r\n")
</pre>


<p>
newLISP will now send the 
following request and header:
</p>


<pre>
GET / HTTP/1.1        
Host: somehost.com
User-Agent: Mozilla/4.o
Cookie: name=fred
Connection: close
</pre>


<p>
Note that when using a custom header, 
newLISP will only supply the <tt>GET</tt> request line, 
as well as the <tt>Host:</tt> and <tt>Connection:</tt> header entries. 
newLISP inserts all other entries supplied in the custom header 
between the <tt>Host:</tt> and <tt>Connection:</tt> entries. 
Each entry must end with a carriage return 
line-feed pair: <tt>\r\n</tt>.

</p>

<p>
See an HTTP transactions reference 
for valid header entries.
</p>

<p>
Custom headers can also be used 
in the <a href="#put-url">put-url</a> 
and <a href="#post-url">post-url</a> functions.
</p>

<br/><br/>

<a name="global"></a>
<h2><span class="function">global</span></h2>

<h4>syntax: (global <em>sym-1</em> [<em>sym-2</em> ... ])</h4>

<p>
One or more symbols in <em>sym-1</em> [<em>sym-2</em> ... ] 
can be made globally accessible from contexts other than MAIN. 
The statement has to be executed in the MAIN context, 
and only symbols belonging to MAIN can be made global. 
<tt>global</tt> returns the last symbol made global.
</p>

<!-- example -->

<pre>
(global 'aVar 'x 'y 'z)  <span class='arw'>&rarr;</span> z

(define (foo x) 
(&hellip;))

(constant (global 'foo))
</pre>


<p>
The second example shows how <a href="#constant">constant</a> 
and <tt>global</tt> can be combined into one statement, 
protecting and making a previous function definition global.
</p>

<br/><br/>

<a name="globalp"></a>
<h2><span class="function">global?</span></h2>

<h4>syntax: (global? <em>sym</em>)</h4>

<p>Checks if symbol in <em>sym</em> is global. Built-in functions, context
symbols, and all symbols made global using the function <a href="#global">global</a>
are global:</p>

<!-- example -->

<pre>
global? 'print)   <span class='arw'>&rarr;</span> true
(global 'var)     <span class='arw'>&rarr;</span> var
(global? 'var)    <span class='arw'>&rarr;</span> true

(constant (global 'foo))

(global? 'foo)    <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="history"></a>
<h2><span class="function">history</span></h2>

<h4>syntax: (history [<em>bool-params</em>])</h4>

<p><em>history</em> returns a list of the call history of the enclosing function.
Without the optional <em>bool-params</em>, a list of function symbols is returned.
The first symbol is the name of the enclosing function. When the optional 
<em>bool-params</em> evaluates to <em>true</em>, the call arguments are included 
with the symbol.</p>
<br/><br/>

<pre>
(define (foo x y) 
    (bar (+ x 1) (* y 2)))

(define (bar a b) 
    (history))

; history returns names of calling functions
(foo 1 2) <span class='arw'>&rarr;</span> (bar foo)

; the addtional 'true' forces inclusion of callpatterns
(define (bar a b) 
    (history true))

(foo 1 2) <span class='arw'>&rarr;</span> ((bar (+ x 1) (* y 2)) (foo 1 2))
</pre>

<br/><br/>

<a name="if"></a>
<h2><span class="function">if</span></h2>

<h4>syntax: (if <em>exp-condition</em> <em>exp-1</em> [<em>exp-2</em>])<br/>

syntax: (if <em>exp-cond-1</em> <em>exp-1</em>  <em>exp-cond-2</em> <em>exp-2</em> [ ... ])</h4>

<p>If the value of <em>exp-condition</em> is neither <tt>nil</tt> nor an empty list, 
the result of evaluating <em>exp-1</em> is returned; otherwise, the value of 
<em>exp-2</em> is returned.  If <em>exp-2</em> is absent, the value of 
<em>exp-condition</em> is returned.</p>

<p><tt>if</tt> also sets the anaphoric system variable <tt>$it</tt> to the value
of the conditional expression in <tt>if</tt>.</p>

<!-- example -->

<pre>
(set 'x 50)                   <span class='arw'>&rarr;</span> 50
(if (&lt; x 100) "small" "big")  <span class='arw'>&rarr;</span> "small"
(set 'x 1000)                 <span class='arw'>&rarr;</span> 1000
(if (&lt; x 100) "small" "big")  <span class='arw'>&rarr;</span> "big"
(if (&gt; x 2000) "big")         <span class='arw'>&rarr;</span> nil

; more than one statement in the true or false
; part must be blocked with (begin ...)
(if (= x y)
  (begin
    (some-func x)
    (some-func y))
  (begin
    (do-this x y)
    (do-that x y))
)

; if also sets the anaphoric system variable $it
(set 'lst '(A B C))
(if lst (println (last $it)))  <span class='arw'>&rarr;</span> C
</pre>


<p>
The second form of <tt>if</tt> works similarly 
to <a href="#cond">cond</a>, except it does not take 
parentheses around the condition-body pair of expressions. 
In this form, <tt>if</tt> can have 
an unlimited number of arguments.
</p>

<!-- example -->

<pre>
(define (classify x)
(if
(&lt; x 0) "negative"
(&lt; x 10) "small"
(&lt; x 20) "medium"
(&gt;= x 30) "big"
"n/a"))

(classify 15)   <span class='arw'>&rarr;</span> "medium"
(classify 100)  <span class='arw'>&rarr;</span> "big"
(classify 22)   <span class='arw'>&rarr;</span> "n/a"
(classify -10)  <span class='arw'>&rarr;</span> "negative"
</pre>


<p>The last expression, <tt>"n/a"</tt>, is optional. When this option 
is omitted, the evaluation of <tt>(&gt;= x 30)</tt> is returned, behaving 
exactly like a traditional <a href="#cond">cond</a> but without requiring 
parentheses around the condition-expression pairs.</p>

<p> In any case, the whole <tt>if</tt> expression 
always returns the last expression or condition evaluated.</p>

<p>
See also the <a href="#when">when</a> and <a href="#unless">unless</a> functions.
</p>

<br/><br/>

<!--
<a name="if-not"></a>
<h2><span class="function">if-not</span></h2>
<h4>syntax: (if-not <em>exp-condition</em> <em>exp-1</em> [<em>exp-2</em>])</h4>

<p>
<tt>if-not</tt> is equivalent to (<a href="#if">if</a> (<a href="#not">not</a> 
<em>exp-condition</em> <em>exp-1</em> [<em>exp-2</em>])).
If the value of <em>exp-condition</em> is <tt>nil</tt> 
or the empty list <tt>()</tt>, <em>exp-1</em> is evaluated;
otherwise, the optional <em>exp-2</em> is evaluated.
</p>	

<p>Contrary to the <a href="#if">if</a> function, <tt>if-not</tt> does
not permit multiple consequent and alternative clauses.</p>

<pre>
(set 'x 50)                       <span class='arw'>&rarr;</span> 50 
(if-not (&lt; x 100) "big" "small")  <span class='arw'>&rarr;</span> "small"
(set 'x 1000)                     <span class='arw'>&rarr;</span> 1000 
(if-not (&lt; x 100) "big" "small")  <span class='arw'>&rarr;</span> "big" 
</pre>

<br/><br/>
-->

<a name="ifft"></a>
<h2><span class="function">ifft</span></h2>
<h4>syntax: (ifft <em>list-num</em>)</h4>

<p>
Calculates the inverse discrete Fourier transform 
on a list of complex numbers in <em>list-num</em> 
using the FFT method (Fast Fourier Transform). 
Each complex number is specified by its real part, 
followed by its imaginary part. 
In case only real numbers are used, 
the imaginary part is set to <tt>0.0</tt> (zero). 
When the number of elements in <em>list-num</em> 
is not an integer power of 2, 
<tt>ifft</tt> increases the number of elements 
by padding the list with zeroes. 
When complex numbers are <tt>0</tt> in the imaginary part, 
simple numbers can be used.
</p>

<!-- example -->

<pre>
(ifft (fft '((1 0) (2 0) (3 0) (4 0)))) 
<span class='arw'>&rarr;</span> ((1 0) (2 0) (3 0) (4 0))

;; when imaginary part is 0, plain numbers work too

(ifft (fft '(1 2 3 4))) 
<span class='arw'>&rarr;</span> ((1 0) (2 0) (3 0) (4 0))
</pre>


<p>
The inverse operation of <tt>ifft</tt> 
is the <a href="#fft">fft</a> function.
</p>

<br/><br/>

<a name="import"></a>
<h2><span class="function">import</span>&nbsp;
<a href="#shared-lib"><font size="+2">&#x26A0;</font></a></h2>
<h4>syntax: (import <em>str-lib-name</em> <em>str-function-name</em> ["cdecl"])<br/>
syntax: (import <em>str-lib-name</em> <em>str-function-name</em> <em>str-return-type</em> [<em>str-param-type</em> . . .])<br/>
syntax: (import <em>str-lib-name</em>)</h4>

<p>Imports the function specified in <em>str-function-name</em> 
from a shared library named in <em>str-lib-name</em>. Depending on the syntax used, string 
labels for return and parameter types can be specified</p>

<p>If the library in <em>str-lib-name</em> is not in the system's library path, the
full path name should be specified.</p>

<p>A function can be imported only once. A repeated import of the same function
will simply return the same - already allocated - function address.</p>

<p>Note, that the first simple syntax is available on <u>all</u> versions of newLISP, even those compiled without <em>libffi</em> support. On <em>libffi</em> enabled versions - capable of the second extended syntax -
imported symbols are protected against change and can only be modified using
<a href="#constant">constant</a>.</p>

<p>The third syntax - on OSX, Linux and other Unix only -  allows pre-loading libraries 
without importing functions. This is necessary when other library imports need access 
internally to other functions from pre-loaded libraries.</p>

<p>Incorrectly using <tt>import</tt> can cause a system bus error or a segfault can occur 
and crash newLISP or leave it in an unstable state.</p>

<h3>The simple <tt>import</tt> syntax</h3>
<p>Most library functions can be imported using the simpler first syntax. 
This form is present on <u>all</u> compile flavors of newLISP. The API expects
all function arguments to be passed on the stack in either <em>cdecl</em> or <em>stdcall</em> 
conventions. On 32-bit platforms, integers, pointers to strings and buffers sometimes floating 
point values can be passed as parameters. On 64-bit platforms only 
integers  can be passed but no floating point values. 
As return values only 32-bit or 64-bit values and pointers are allowed. 
No floating point numbers can be returned. Strings must be retrieved with the
<a href="#get-string">get-string</a> helper function. Regardless of these 
limitations, most  modules included in the distribution use 
this simple import API.</p>

<p>If pointers are returned to strings or structures the following helper functions
can be used extract data:
<a href="#get-char">get-char</a>, 
<a href="#get-int">get-int</a>, 
<a href="#get-float">get-float</a>, 
<a href="#get-string">get-string</a>, 
<a href="#unpack">unpack</a> </p>

<p>To pass pointers for data structures the following functions help to pack data
and calculate addresses:
<a href="#address">address</a>, 
<a href="#pack">pack</a>.</p>

<p>To transform newLISP data types into the data types needed by the 
imported function, use the functions 
<a href="#float">float</a> for 64-bit double floats, 
<a href="#flt">flt</a> for 32-bit floats, 
and <a href="#int">int</a> for 32-bit integers. 
By default, newLISP passes floating point numbers as 64-bit double floats, 
integers as 32-bit integers, and strings as 32-bit integers for string 
addresses (pointers in C). Floats can only be used with 32-bit versions
of newLISP and libraries. To use floating point numbers in a 64-bit
environment use the <a href="#extended_import">extended <tt>import</tt> syntax</a>. 
</p>

<!-- example -->

<pre>
;; define LIBC platform independent

(define LIBC (lookup ostype '(
("Windows" "msvcrt.dll")
("OSX" "libc.dylib")

(import LIBC "printf")
(printf "%g %s %d %c\n" 1.23 "hello" 999 65)
<b>1.23 hello 999 A</b>
<span class='arw'>&rarr;</span> 17 ; return value

;; import MS Windows DLLs in 32-bit versions 

(import "kernel32.dll" "GetTickCount")  <span class='arw'>&rarr;</span> GetTickCount
(import "user32.dll" "MessageBoxA")     <span class='arw'>&rarr;</span> MessageBoxA
(GetTickCount)                          <span class='arw'>&rarr;</span> 3328896
</pre>

<p>In the first example, the string "1.23 hello 999 A" 
is printed as a side effect, and the value 17 (number of 
characters printed) is returned. Any C function can be imported 
from any shared library in this way.
</p>

<p>The message box example pops up a Windows dialog box, which may be hidden 
behind the console window.  The console prompt does not return until the 
'OK' button is pressed in the message box.</p>

<pre>
;;this pops up a message box

(MessageBoxA 0 "This is the body" "Caption" 1) 
</pre>

<p>The other examples show several imports of MS Windows DLL functions and 
the details of passing values <em>by value</em> or <em>by reference</em>. 
Whenever strings or numbers are passed by reference, space must be 
reserved beforehand.</p>


<pre>
(import "kernel32.dll" "GetWindowsDirectoryA")

;; allocating space for a string return value
(set 'str (dup "\000" 64))  ; reserve space and initialize

(GetWindowsDirectoryA str (length str))

str  <span class='arw'>&rarr;</span> "C:\\WINDOWS\000\000\000 ... "

;; use trim or get-string to cut of trailing binary zeros
(get-string str)  <span class='arw'>&rarr;</span> "C:\\WINDOWS"
(trim str)        <span class='arw'>&rarr;</span> "C:\\WINDOWS"

(import "kernel32.dll" "GetComputerNameA")

;; allocate memory and initialize to zeros
(set 'str (dup "\000" 64))
(set 'len (length str)

;; call the function
;; the length of the string is passed as address reference 
;; string str is automatically past by address (C pointer)
(GetComputerNameA str (address len)) 

str  <span class='arw'>&rarr;</span> "LUTZ-PC\000\000 ... "

(trim str)  <span class='arw'>&rarr;</span> "LUTZ-PC"
</pre>

<p><tt>import</tt> returns the address of the function, which can be 
used to assign a different name to the imported function.</p>

<pre>
(set 'imprime (import "libc.so.6" "printf")) 
<span class='arw'>&rarr;</span> printf@400862A0

(imprime "%s %d" "hola" 123)                 
<span class='arw'>&rarr;</span> "hola 123"
</pre>

<p>The MS Windows and Cygwin versions of newLISP uses standard call <em>stdcall</em> conventions 
to call DLL library routines by default.  This is necessary for calling DLLs that belong 
to the MS Windows operating system.  Most third-party DLLs are compiled for 
C declaration <em>cdecl</em> calling conventions and may need to specify the string 
<tt>"cdecl"</tt> as an additional last argument when importing functions. 
newLISP compiled for macOS, Linux and other Unix systems uses the 
<em>cdecl</em> calling conventions by default and ignores any additional string.</p>

<pre>
;; force cdecl calling conventions on MS Windows
(import "sqlite.dll" "sqlite_open" "cdecl")  <span class='arw'>&rarr;</span> sqlite_open &lt;673D4888&gt;
</pre>

<p>Imported functions may take up to fourteen arguments. Note that 
floating point arguments take up two spaces each
(e.g., passing five floats takes up ten of the fourteen parameters).
</p>

<a name="extended_syntax"></a>
<a name="extended_import"></a>
<h3>The extended <tt>import</tt> syntax</h3>

<p>The extended import API works with the second syntax. It is based on the popular 
<tt>libffi</tt> library which is pre-installed on most OS platforms. The startup banner
of newLISP should show the word <tt>libffi</tt> indicating the running version
of newLISP is compiled to use the extended <tt>import</tt> API. The function
<a href="#sys-info">sys-info</a> can also be used to check for <tt>libffi</tt>-support.</p>

<p>The API works with all atomic C data types for passed parameters and return values. 
The extended API requires that parameter types are specified in the <tt>import</tt> 
statement as string type labels. Programs written with extended import API will run 
without change on 32-bit and 64-bit newLISP and libraries. Integers, floating point 
values and strings can be returned without using helper functions.</p>

<p>The following types can be specified for the return value in <em>str-return-type</em>
and for function parameters in <em>str-param-type</em>:</p>

<table summary="data types">
<tr align="left"><th>label</th><th>C type for return value and arguments</th><th>newLISP return and argument type</th></tr>
<tr><td>"void"</td><td>void</td><td><tt>nil</tt> is returned for return type</td></tr>
<tr><td>"byte"</td><td>byte unsigned 8 bit</td><td>integer</td></tr>
<tr><td>"char"</td><td>char signed 8 bit</td><td>integer</td></tr>
<tr><td>"unsigned short int"</td><td>unsigned short int 16 bit</td><td>integer</td></tr>
<tr><td>"short int"</td><td>short int signed 16 bit</td><td>integer</td></tr>
<tr><td>"unsigned int"</td><td>unsigned int 32 bit</td><td>integer</td></tr>
<tr><td>"int"</td><td>int signed 32 bit</td><td>integer</td></tr>
<tr><td>"long"</td><td>long signed 32 or 64 bit depending on platform</td><td>integer</td></tr>
<tr><td>"long long"</td><td>long long signed 64 bit</td><td>integer</td></tr>
<tr><td>"float"</td><td>float 32 bit</td><td>IEEE-754 64 bit float cut to 32-bit precision</td></tr>
<tr><td>"double"</td><td>double 64 bit</td><td>IEEE-754 64 bit float</td></tr>
<tr><td>"char*"</td><td>char* 32 or 64 bit ptr depending on platform</td><td>displayable string return (zero terminated)<br/>string buffer arg (no addr. since 10.4.2)</td></tr>
<tr><td>"void*"</td><td>void* 32 or 64 bit ptr depending on platform</td><td>integer address return<br/>either string buffer or integer address arg</td></tr>
</table>

<p>The types <tt>"char*"</tt> and <tt>"void*</tt> can be interchanged and are treated 
identical inside <tt>libffi</tt>. Depending on the type of arguments passed and the type
of return values, one or the other is used.</p>

<p>Aggregate types can be composed using the <a href="#struct">struct</a> function and 
can be used for arguments and return values.</p>

<p>The following examples show how the extended <tt>import</tt> syntax can
handle return values of floating point values and strings:</p>

<pre>
;; return a float value, LIBC was defined earlier
;             name   return   arg
(import LIBC "atof" "double" "char*")
(atof "3.141") <span class='arw'>&rarr;</span> 3.141

;; return a copied string
;             name     return  arg-1   arg-2
(import LIBC "strcpy" "char*" "char*" "char*")
(set 'from "Hello World")

(set 'to (dup "\000" (length from))) ; reserve memory
(strcpy to from) <span class='arw'>&rarr;</span> "Hello World"
</pre>

<p>The <tt>char*</tt> type takes a string buffer only. The <tt>"void*</tt> type can take either
a string buffer or a memory address number as input. When using <tt>"void*"</tt>
as a return type the address number of the result buffer will be returned. This is
useful when returning pointers to data structures. These pointers can then
be used with <a href="#unpack">unpack</a> and <a href="#struct">struct</a> for destructuring. 
In the following example the return type is changed to <tt>void*</tt>:</p>

<pre>
(import LIBC "strcpy" "void*" "char*" "char*")
(set 'from "Hello World")
(set 'to (dup "\000" (length from)))

(strcpy to from)       <span class='arw'>&rarr;</span> 2449424
(address to)           <span class='arw'>&rarr;</span> 2449424
(unpack "s11" 2449424) <span class='arw'>&rarr;</span> "Hello World"
(get-string 2449424)   <span class='arw'>&rarr;</span> "Hello World"
to                     <span class='arw'>&rarr;</span> "Hello World"
</pre>

<p>A newLISP string is always passed by it's address reference.</p>


<p>For a more complex example see this
<a href="http://www.newlisp.org/syntax.cgi?code/opengl-demo-ffi-lsp.txt">OpenGL demo</a>.</p>

<h3>Memory management</h3>

<p>Any allocation performed by imported foreign functions has to be 
de-allocated manually if there's no call in the imported API to do so.
See the <a href="http://www.newlisp.org/CodePatterns.html">Code Patterns in newLISP</a>
document for an example.</p>

<p>In case of calling foreign functions with passing by reference, 
memory for variables needs to be allocated beforehand by newLISP
&mdash; see import of <tt>GetWindowsDirectoryA</tt> above &mdash; 
and hence, memory needs not be deallocated manually, because it is
managed automatically by newLISP.</p>

<br/><br/>

<a name="inc"></a>
<h2><span class="function">inc</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (inc <em>place</em> [<em>num</em>])</h4>

<p>
Increments the number in <em>place</em> by <tt>1.0</tt> or by the optional 
number <em>num</em> and returns the result. <tt>inc</tt> performs float 
arithmetic and converts integer numbers passed into floating point type.</p>

<p><em>place</em> is either a symbol or a place in a list structure holding a
number, or a number returned by an expression.</p>

<!-- example -->

<pre>
(set 'x 0)    <span class='arw'>&rarr;</span> 0
(inc x)       <span class='arw'>&rarr;</span> 1
x             <span class='arw'>&rarr;</span> 1
(inc x 0.25)  <span class='arw'>&rarr;</span> 1.25
x             <span class='arw'>&rarr;</span> 1.25
(inc x)       <span class='arw'>&rarr;</span> 2.25
</pre>


<p>If a symbol  for <em>place</em> contains <tt>nil</tt>, it is treated
as if containing  <tt>0.0</tt>:</p>


<pre>
z             <span class='arw'>&rarr;</span> nil
(inc z)       <span class='arw'>&rarr;</span> 1

(set 'z nil)
(inc z 0.01)  <span class='arw'>&rarr;</span> 0.01
</pre>


<p>Places in a list structure or a number returned by another expression
can be updated too:</p>


<pre>
(set 'l '(1 2 3 4))

(inc (l 3) 0.1) <span class='arw'>&rarr;</span> 4.1

(inc (first l)) <span class='arw'>&rarr;</span> 2

l <span class='arw'>&rarr;</span> (2 2 3 4.1)

(inc (+ 3 4)) <span class='arw'>&rarr;</span> 8
</pre>


<p>Use the <a href="#inci">++</a> function for incrementing numbers in
integer mode. Use <a href="#dec">dec</a> to decrement numbers in floating point mode.</p>


<br/><br/>

<a name="index"></a>
<h2><span class="function">index</span></h2>
<h4>syntax: (index <em>exp-predicate</em> <em>exp-list</em>)</h4>

<p>Applies the predicate <em>exp-predicate</em> to each element of the 
list <em>exp-list</em> and returns a list containing the indices of the elements 
for which <em>exp-predicate</em> is true.</p>

<!-- example -->

<pre>
(index symbol? '(1 2 d 4 f g 5 h))  <span class='arw'>&rarr;</span> (2 4 5 7)

(define (big? x) (&gt; x 5))  <span class='arw'>&rarr;</span> (lambda (x) (&gt; x 5))

(index big? '(1 10 3 6 4 5 11))  <span class='arw'>&rarr;</span> (1 3 6)

(select '(1 10 3 6 4 5 11) '(1 3 6)) <span class='arw'>&rarr;</span> (10 6 11) 
</pre>


<p>
The predicate may be a built-in predicate, 
a user-defined function, or a lambda expression.
</p>

<p>
Use the <a href="#filter">filter</a> function 
to return the elements themselves.
</p>

<br/><br/>

<a name="infp"></a>
<h2><span class="function">inf?</span></h2>
<h4>syntax: (inf? <em>float</em>)</h4>

<p>If the value in <em>float</em> is infinite the function returns
<tt>true</tt> else <tt>nil</tt>.</p>

<!-- example -->

<pre>
(inf? (div 1 0)) <span class='arw'>&rarr;</span> true

(div 0 0) <span class='arw'>&rarr;</span> NaN
</pre>


<p>Note that an integer division by zero e.g. <tt>(/ 1 0)</tt> will
throw an "division by zero" error and not yield infinity. See also
<a href="#NaNp">NaN?</a> to check if a floating point number is valid.</p>

<br/><br/>

<a name="int"></a>
<h2><span class="function">int</span></h2>

<h4>syntax: (int <em>exp</em> [<em>exp-default</em> [<em>int-base</em>]])</h4>

<p>
If the expression in <em>exp</em> evaluates to a number or a string, the result 
is converted to an integer and returned.  If <em>exp</em> cannot be converted 
to an integer, then <tt>nil</tt> or the evaluation of <em>exp-default</em> will 
be returned.  This function is mostly used when translating strings from user 
input or from parsing text. If <em>exp</em> evaluates to a string, the string 
must start with a digit; one or more spaces; or the <tt>+</tt> or <tt>-</tt> sign.
The string must begin with '<tt>0x</tt>' for hexadecimal strings or '<tt>0</tt>' 
(zero) for octal strings.  If <em>exp</em> is invalid, <tt>int</tt> returns 
<tt>nil</tt> as a default value if not otherwise specified.
</p>

<p>A second optional parameter can be used to force the number base 
of conversion to a specific value.</p>

<p>Integers larger than 9,223,372,036,854,775,807 are truncated to 
9,223,372,036,854,775,807.  Integers smaller than -9,223,372,036,854,775,808 
are truncated to -9,223,372,036,854,775,808.</p>

<p>When converting from a float (as in the second form of <tt>int</tt>), 
floating point values larger or smaller than the integer maximum or minimum 
are also truncated. A floating point expression evaluating to <tt>NaN</tt> 
is converted to <tt>0</tt> (zero).</p>


<!-- example -->

<pre>
(int "123")          <span class='arw'>&rarr;</span> 123
(int " 123")         <span class='arw'>&rarr;</span> 123
(int "a123" 0)       <span class='arw'>&rarr;</span> 0
(int (trim " 123"))  <span class='arw'>&rarr;</span> 123
(int "0xFF")         <span class='arw'>&rarr;</span> 255
(int "0b11111")      <span class='arw'>&rarr;</span> 31
(int "055")          <span class='arw'>&rarr;</span> 45
(int "1.567")        <span class='arw'>&rarr;</span> 1
(int 1.567)          <span class='arw'>&rarr;</span> 1

(integer? 1.00)        <span class='arw'>&rarr;</span> nil
(integer? (int 1.00))  <span class='arw'>&rarr;</span> true

(int "1111" 0 2)  <span class='arw'>&rarr;</span> 15   ; base 2 conversion
(int "0FF" 0 16)  <span class='arw'>&rarr;</span> 255  ; base 16 conversion

(int 'xyz)     <span class='arw'>&rarr;</span> nil
(int 'xyz 0)   <span class='arw'>&rarr;</span> 0
(int nil 123)  <span class='arw'>&rarr;</span> 123

(int "abc" (throw-error "not a number"))  
<span class='arw'>&rarr;</span> <span class='err'>ERR: user error : not a number</span>

(print "Enter a num:")
(set 'num (int (read-line)))

(int (bits 12345) 0 2) <span class='arw'>&rarr;</span> 12345
</pre>


<p>The inverse function to <tt>int</tt> with base <tt>2</tt> is
<a href="#bits">bits</a>.</p>

<p>Use the <a href="#float">float</a> function 
to convert arguments to floating point numbers.</p>

<br/><br/>

<a name="integerp"></a>
<h2><span class="function">integer?</span></h2>
<h4>syntax: (integer? <em>exp</em>)</h4>

<p>
Returns <tt>true</tt> only if the value 
of <em>exp</em> is an integer; 
otherwise, it returns <tt>nil</tt>.
</p>

<!-- example -->

<pre>
(set 'num 123)  <span class='arw'>&rarr;</span> 123
(integer? num)  <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="intersect"></a>
<h2><span class="function">intersect</span></h2>
<h4>syntax: (intersect <em>list-A</em> <em>list-B</em>)<br/>

syntax: (intersect <em>list-A</em> <em>list-B</em> <em>bool</em>)</h4>

<p>
In the first syntax, 
<tt>intersect</tt> returns a list 
containing one copy of each element 
found both in <em>list-A</em> and <em>list-B</em>.
</p>

<!-- example -->

<pre>
(intersect '(3 0 1 3 2 3 4 2 1) '(1 4 2 5))  
<span class='arw'>&rarr;</span> (2 4 1)
</pre>


<p>
In the second syntax, 
<tt>intersect</tt> returns a list of all elements 
in <em>list-A</em> that are also in <em>list-B</em>, 
without eliminating duplicates in <em>list-A</em>. 
<em>bool</em> is an expression evaluating to <tt>true</tt> 
or any other value not <tt>nil</tt>.
</p>

<!-- example -->

<pre>
(intersect '(3 0 1 3 2 3 4 2 1) '(1 4 2 5) true)
<span class='arw'>&rarr;</span> (1 2 4 2 1)
</pre>


<p>
See also the set functions 
<a href="#difference">difference</a>, <a href="#unique">unique</a>
and <a href="#union">union</a>.
</p>

<br/><br/>

<a name="invert"></a>
<h2><span class="function">invert</span></h2>
<h4>syntax: (invert <em>matrix</em> [<em>float-pivot</em>])</h4>

<p>Returns the inversion of a two-dimensional matrix in <em>matrix</em>. 
The matrix must be square, with the same number 
of rows and columns, and <em>non-singular</em> (invertible). 
Matrix inversion can be used to solve systems of linear equations 
(e.g., multiple regression in statistics).  newLISP uses LU-decomposition of 
the matrix to find the inverse.</p>

<p>Optionally <tt>0.0</tt> or a very small value can be specified
in <em>float-pivot</em>. This value substitutes pivot elements in
the LU-decomposition algorithm, which result in zero when
the algorithm deals with a singular matrix.</p>

<p>The dimensions of a matrix are defined by the number of rows 
times the number of elements in the first row.  For missing elements 
in non-rectangular matrices, <tt>0.0</tt> (zero) is assumed.  
A matrix can either be a nested list or an <a href="#array">array</a>.
</p> 

<!-- example -->

<pre>
(set 'A '((-1 1 1) (1 4 -5) (1 -2 0)))
(invert A)  <span class='arw'>&rarr;</span> ((10 2 9) (5 1 4) (6 1 5))
(invert (invert A)) <span class='arw'>&rarr;</span> ((-1 1 1) (1 4 -5) (1 -2 0))

; solve Ax = b for x
(multiply (invert A) '((1) (2) (3))) <span class='arw'>&rarr;</span> ((41) (19) (23))

; treatment of singular matrices
(invert '((2 -1) (4 -2)))        <span class='arw'>&rarr;</span> nil
(invert '((2 -1) (4 -2)) 0.0)    <span class='arw'>&rarr;</span> ((inf -inf) (inf -inf))
(invert '((2 -1) (4 -2)) 1e-20)  <span class='arw'>&rarr;</span> ((5e+19 -2.5e+19) (1e+20 -5e+19)) 
</pre>

<p><tt>invert</tt> will return <tt>nil</tt> if the matrix is <i>singular</i> 
and cannot be inverted, and <em>float-pivot</em> is not specified. </p>


<p>
All operations shown here on lists 
can be performed on arrays, as well.
</p>

<p>
See also the matrix functions <a href="#det">det</a>,
<a href="#mat">mat</a>, <a href="#multiply">multiply</a>
and <a href="#transpose">transpose</a>.
</p>

<br/><br/>

<a name="irr"></a>
<h2><span class="function">irr</span></h2>
<h4>syntax: (irr <em>list-amounts</em> [<em>list-times</em> [<em>num-guess</em>]])</h4>

<p>
Calculates the internal rate of return 
of a cash flow per time period. 
The internal rate of return is the interest rate 
that makes the present value of a cash flow equal to <tt>0.0</tt> (zero). 
In-flowing (negative values) and out-flowing (positive values) 
amounts are specified in  <em>list-amounts</em>. 
If no time periods are specified in <em>list-times</em>, 
amounts in <em>list-amounts</em> correspond to 
consecutive time periods increasing by 1 (1, 2, 3&mdash;). 
The algorithm used is iterative, 
with an initial guess of 0.5 (50 percent). 
Optionally, a different 
initial guess can be specified. 
The algorithm returns when a precision 
of 0.000001 (0.0001 percent) is reached. 
<tt>nil</tt> is returned if the algorithm 
cannot converge after 50 iterations.
</p>

<p>
<em>irr</em> is often used to decide 
between different types of investments.
</p>

<!-- example -->

<pre>
(irr '(-1000 500 400 300 200 100))  
<span class='arw'>&rarr;</span> 0.2027

(npv 0.2027 '(500 400 300 200 100)) 
<span class='arw'>&rarr;</span> 1000.033848 ; ~ 1000

(irr '(-1000 500 400 300 200 100) '(0 3 4 5 6 7)) 
<span class='arw'>&rarr;</span> 0.0998

(irr '(-5000 -2000 5000 6000) '(0 3 12 18)) 
<span class='arw'>&rarr;</span> 0.0321
</pre>


<p>
If an initial investment of 1,000 
yields 500 after the first year, 
400 after two years, and so on, 
finally reaching <tt>0.0</tt> (zero) after five years, 
then that corresponds to a yearly return 
of about 20.2 percent. 
The next line demonstrates the relation 
between <tt>irr</tt> and <a href="#npv">npv</a>. 
Only 9.9 percent returns are necessary when making 
the first withdrawal after three years.
</p>

<p>
In the last example, securities 
were initially purchased for 5,000, 
then for another 2,000 three months later. 
After a year, securities for 5,000 are sold. 
Selling the remaining securities 
after 18 months renders 6,000. 
The internal rate of return is 3.2 percent per month, 
or about 57 percent in 18 months.
</p>

<p>
See also the <a href="#fv">fv</a>, 
<a href="#nper">nper</a>, 
<a href="#npv">npv</a>, 
<a href="#pmt">pmt</a>, 
and <a href="#pv">pv</a> functions.
</p> 

<br/><br/>

<a name="json-error"></a>
<h2><span class="function">json-error</span></h2>
<h4>syntax: (json-error)</h4>

<p>When <a href="#json-parse">json-parse</a> returns <tt>nil</tt> due
to a failed JSON data translation, this function retrieves an error
description and the last scan position of the parser.</p>

<pre>
; failed parse returns nil
(json-parse [text]{"address" "http://example.com"}[/text]) <span class='arw'>&rarr;</span> nil

; inspect the error information
(json-error) <span class='arw'>&rarr;</span> ("missing : colon" 11)
</pre>

<br/><br/>

<a name="json-parse"></a>
<h2><span class="function">json-parse</span></h2>
<h4>syntax: (json-parse <em>str-json-data</em>)</h4>

<p>This function parses JSON formatted text and translates it to newLISP S-expressions.
All data types conforming to the ECMA-262 standard are translated. The JSON values 
<tt>false</tt> and <tt>null</tt> will be represented by the symbols <tt>false</tt> 
and <tt>null</tt> in the symbolic newLISP expressions. Arrays in JSON will be represented 
by lists in newLISP. The resulting lists from JSON object data can be processed using 
<a href="#assoc">assoc</a>, <a href="#lookup">lookup</a> and <a href="#ref">ref</a>.</p>

<p>For JSON attribute values not recognized or wrong JSON syntax, <tt>json-parse</tt> 
returns <tt>nil</tt> and <a href="#json-error">json-error</a> can be used to retrieve
the error text.</p>

<p>The following example shows a nested JSON object from a file <tt>person.json</tt>:</p>

<!-- example -->

<pre>
{
"name": "John Smith",
"age": 32,
"employed": true,
"address": {
"street": "701 First Ave.",
"city": "Sunnyvale, CA 95125",
"country": "United States"
},
"children": [
{
    "name": "Richard",
    "age": 7
},
{
    "name": "Susan",
    "age": 4
},
{
    "name": "James",
    "age": 3
}
]
}
</pre>
<p>The file is read, parsed and the resulting S-expression stored in <tt>jsp</tt>:</p>

<pre>
(set 'jsp (json-parse (read-file "person.json")))
<span class='arw'>&rarr;</span>
( ("name" "John Smith") 
("age" 32) 
("employed" true) 
("address" ( ("street" "701 First Ave.") 
       ("city" "Sunnyvale, CA 95125") 
       ("country" "United States")) ) 
("children" (
(("name" "Richard") ("age" 7)) 
(("name" "Susan") ("age" 4)) 
(("name" "James") ("age" 3))) )
)
</pre>

<p>Data can be extracted using <a href="#assoc">assoc</a>, <a href="#lookup">lookup</a> 
or <a href="#ref">ref</a>:</p>

<pre>
; the address
(lookup "address" jsp)
<span class='arw'>&rarr;</span> (("street" "701 First Ave.") ("city" "Sunnyvale, CA 95125") ("country" "United States"))

; the city of the address
(lookup "city" (lookup "address" jsp)) 
<span class='arw'>&rarr;</span> "Sunnyvale, CA 95125"

; a child named Susan
(ref '(( * "Susan") *) jsp match true) 
<span class='arw'>&rarr;</span> (("name" "Susan") ("age" 4))

; all names
(map last (ref-all '("name" *) jsp match true)) 
<span class='arw'>&rarr;</span> ("John Smith" "Richard" "Susan" "James")

; only names of children
(map last (ref-all '("name" *) (lookup "children" jsp) match true))
<span class='arw'>&rarr;</span>
("Richard" "Susan" "James")

; names of children other method
(map last (map first (lookup "children" jsp)))
<span class='arw'>&rarr;</span>
("Richard" "Susan" "James")
</pre>

<p>Although most of the time JSON object types are parsed, all JSON data
types can be parsed directly, without occurring as part of a JSON object.
The following examples show parsing of a JSON array:</p>

<pre>
; parse a JSON array data type

(json-parse "[1, 2, 3, 4, 5]") <span class='arw'>&rarr;</span> (1 2 3 4 5)
</pre>

<p>When the UTF-8 capable version of newLISP is used, JSON formatted Unicode
gets translated into UTF-8:</p>

<pre>
; parse a JSON object data type ands Unicode
; the outer {,} are newLISP string delimiters [text],[/text] tags could also be used
; the inner {,} are JSON object delimiters

(json-parse { {"greek letters" : "\u03b1\u03b2\u03b3\u03b4"} }) <span class='arw'>&rarr;</span> (("greek letters" "αβγδ"))

; strings longer than 2047 bytes should be delimted with [text], [/text] tags

(json-parse [text]{"greek letters" : "\u03b1\u03b2\u03b3\u03b4"}[/text]) <span class='arw'>&rarr;</span> (("greek letters" "αβγδ"))
</pre>

<p>The hex-code representation of Unicoder characters in JSON is the same as can be used in
UTF-8 enabled newLISP.</p>

<p>Because JSON objects contain <tt>{,},"</tt> characters, quotes should not be used
to limit JSON data, or all quotes inside the JSON data would need a preceding backslash <tt>\</tt>.
<tt>{,}</tt> braces can be used as long as braces inside the JSON data are balanced.
The safest delimiter are <tt>[text], [/text]</tt> tags &mdash; they suppress all special processing
of the string when read by newLISP and are suitable to delimit large data sizes greater
2047 bytes.</p>

<br/><br/>

<a name="join"></a>
<h2><span class="function">join</span></h2>
<h4>syntax: (join <em>list-of-strings</em> [<em>str-joint</em> [<em>bool-trail-joint</em>]])</h4>

<p>
Concatenates the given   
list of strings 
in <em>list-of-strings</em>. 
If <em>str-joint</em> is present, 
it is inserted between each string in the join.
If <em>bool-trail-joint</em> is <tt>true</tt>
then a joint string is also appended to the last string.
</p>

<!-- example -->

<pre>
(set 'lst '("this" "is" "a" "sentence"))

(join lst " ")  <span class='arw'>&rarr;</span> "this is a sentence"

(join (map string (slice (now) 0 3)) "-")  <span class='arw'>&rarr;</span> "2003-11-26"

(join (explode "keep it together"))  <span class='arw'>&rarr;</span> "keep it together"

(join '("A" "B" "C") "-")         <span class='arw'>&rarr;</span> "A-B-C"
(join '("A" "B" "C") "-" true)    <span class='arw'>&rarr;</span> "A-B-C-"
</pre>


<p>
See also the <a href="#append">append</a>, 
<a href="#string">string</a>, 
and <a href="#explode">explode</a> functions, 
which are the inverse of the <tt>join</tt> operation.
</p>

<br/><br/>

<a name="kmeans-query"></a>
<h2><span class="function">kmeans-query</span></h2>
<h4>syntax: (kmeans-query <em>list-data</em> <em>matrix-centroids</em>)<br/>
syntax: (kmeans-query <em>list-data</em> <em>matrix-data)</em></h4>

<p>In the first usage, <tt>kmeans-query</tt> calculates the Euclidian distances
from the data vector given in <em>list-data</em> to the centroids given in
<em>matrix-centroids</em>. The data vector in <em>list-data</em> has <i>m</i> 
elements. The 2-dimensional list in <em>matrix-centroids</em>, result from a previous
<a href="#kmeans-train">kmeans-train</a> clustering, has <i>k</i> rows and <i>m</i> 
columns for <i>k</i> centroids measuring <i>m</i> features.</p>

<pre>
; centroids from previous kmeans-train
K:centroids <span class='arw'>&rarr;</span>
( (6.39 7.188333333 5.935) 
(7.925714286 3.845714286 9.198571429) 
(2.207142857 2.881428571 0.8885714286) )

(kmeans-query '(1 2 3) K:centroids) <span class='arw'>&rarr;</span>
(8.036487279 9.475994267 2.58693657) ; distances to cluster 1, 2 and 3
</pre>

<p>The data record <tt>(1 2 3)</tt> shows the smallest distance to the 3rd
cluster centroid and would be classified as belonging to that cluster.</p>

<p>In the second application <tt>kmeans-query</tt> calculates Euclidian distances to a list
of other data points which are not centroids. The following example
calculates distances of the <tt>(1 2 3)</tt> data vector to all original points
from the original <a href="#kmeans-train">kmeans-train</a> data analysis.</p>

<p>The data in <em>matrix-data</em> can be either a nested list or a 2-dimensional
array.</p>

<p>This vector could be sorted for a subsequent kNN (k Nearest Neighbor)
analysis:</p>

<pre>
(kmeans-query '(1 2 3) data) <span class='arw'>&rarr;</span>
(10.91671196 3.190626898 9.19723328 3.014415366 9.079763213 
6.83130295 8.533111976 9.624816881 6.444261013 2.013107051 
3.186549858 9.475199206 9.32936761 2.874786949 7.084638311 
10.96221237 10.50080473 3.162419959 2.423674896 9.526436899)

; show distances to members in each cluster

; for cluster labeled 1
(select (kmeans-query '(1 2 3) data) (K:clusters 0)) <span class='arw'>&rarr;</span> 
(9.079763213 6.83130295 9.624816881 6.444261013 7.084638311 10.50080473)

; for cluster labeled 2
(select (kmeans-query '(1 2 3) data) (K:clusters 1)) <span class='arw'>&rarr;</span>
(10.91671196 9.19723328 8.533111976 9.475199206 9.32936761 10.96221237 9.526436899)

; for cluster labeled 3
(select (kmeans-query '(1 2 3) data) (K:clusters 2)) <span class='arw'>&rarr;</span>
(3.190626898 3.014415366 2.013107051 3.186549858 2.874786949 3.162419959 2.423674896)
</pre>

<p>We see that the smallest distances are shown for the data points in
the 3rd cluster at offset 2.</p>

<p>If the numbers of elements - features - in records of <em>list-data</em> 
is different from the number of columns in the data or centroid matrix, 
then the smaller is taken for calculating the Euclidian distances. This 
is useful when the last column of the data matrix does not contain feature 
data, but labels identifying the cluster membership of a data point.</p>

<br/><br/>

<a name="kmeans-train"></a>
<h2><span class="function">kmeans-train</span></h2>
<h4>syntax: (kmeans-train <em>matrix-data</em> <em>int-k</em> <em>context</em> [<em>matrix-centroids</em>])</h4>

<p>The function performs Kmeans cluster analysis on <em>matrix-data</em>. 
All <i>n</i> data records in <em>matrix-data</em> are partitioned into a number 
of <em>int-k</em> different groups. </p>

<p>Both, the <i>n * m</i> <em>matrix-data</em> and the optional <i>k * m</i>
<em>matrix-centroids</em> can be either nested lists or 2-dimensional arrays.</p>

<p>The Kmeans algorithm tries to minimize the sum of squared inner cluster 
distances (SSQ) from the cluster centroid. With each iteration the centroids get 
moved closer to their final position. On some data sets, the end result can depend 
on the starting centroid points.  The right choice of initial centroids can speed 
up the process and avoid not wanted local minima.</p>

<p>When no optional <em>matrix-centroids</em> are given, <tt>kmeans-train</tt> will 
assign an initial random cluster membership to each data row and calculate starting 
centroids. </p>

<p><tt>kmeans-train</tt> returns a vector of total SSQs, the sum of squared inner distances
from the centroid inside the cluster for all clusters. The Iterating algorithm stops when the
change of SSQ from one to the next iteration is less than 1e-10.</p>

<p>Other results of the analysis are stored as lists in variables of <em>context</em>.</p>

<p>The following example analyses 20 data records measuring <i>m = 3</i> features 
and tries to partition data into <i>k = 3</i> clusters. Other numbers than <i>k = 3</i> 
could be tried. The target is a result with few clusters of high density measured by the
average inner cluster distances.</p>

<pre>
(set 'data '(
(6.57 4.96 11.91) 
(2.29 4.18 1.06) 
(8.63 2.51 8.11) 
(1.85 1.89 0.11) 
(7.56 7.93 5.06) 
(3.61 7.95 5.11) 
(7.18 3.46 8.7) 
(8.17 6.59 7.49) 
(5.44 5.9 5.57) 
(2.43 2.14 1.59) 
(2.48 2.26 0.19) 
(8.16 3.83 8.93) 
(8.49 5.31 7.47) 
(3.12 3.1 1.4) 
(6.77 6.04 3.76) 
(7.01 4.2 11.9) 
(6.79 8.72 8.62) 
(1.17 4.46 1.02) 
(2.11 2.14 0.85) 
(9.44 2.65 7.37)))

(kmeans-train data 3 'MAIN:K) <span class='arw'>&rarr;</span> 
(439.7949357 90.7474276 85.06633163 82.74597619)

; cluster membership
K:labels <span class='arw'>&rarr;</span> (2 3 2 3 1 1 2 1 1 3 3 2 2 3 1 2 1 3 3 2)

; the centroid for each cluster
K:centroids <span class='arw'>&rarr;</span>
( (6.39 7.188333333 5.935) 
(7.925714286 3.845714286 9.198571429) 
(2.207142857 2.881428571 0.8885714286) )
</pre>

<p>The returned list of SSQs shows how in each iteration the sum of inner squared
distances decreases. The list in <tt>K:labels</tt> shows the membership fo each
data point in the same order as in the data.</p>

<p>The centroids in <tt>K:centroids</tt> can be used for later classification
of new data records using <a href="#kmeans-query">kmeans-query</a>. When the
number of clusters specified in <em>int-k</em> is too big, <tt>kmeans-train</tt>
will produce unused centroids with <tt>nan</tt> or <tt>NaN</tt> data. When
unused cluster centroids are present, the number in <em>int-k</em> should be
reduced. </p>

<p>The average inner <tt>K:deviations</tt> from cluster members to their centroid
show how dense a cluster is packed. Formally, deviations are calculated similarly
to Euclidian distances and to standard deviations in conventional statistics. 
Squaring the deviations and multiplying each with their cluster size 
(number of members in the cluster) shows the inner SSQ of each cluster:</p>

<pre>
; average inner deviations of cluster members to the centroid
; deviation = sqrt(ssq-of-cluster / n-of-cluster)
K:deviations  <span class='arw'>&rarr;</span> (2.457052209 2.260089397 1.240236975)

; calculating inner SSQs from cluster deviations
(map mul '(6 7 7) (map mul K:deviations K:deviations)) <span class='arw'>&rarr;</span>
(36.22263333 35.75602857 10.76731429) ; inner SSQs

; SSQ from last iteration as sum of inner SSQs
(apply add '(36.22263333 35.75602857 10.76731429)) <span class='arw'>&rarr;</span> 82.74597619
</pre>

<p><tt>K:clusters</tt> gives indices of data records into the original data
for each cluster. With these, individual clusters can be extracted from the
data for further analysis:</p>

<pre>
; ceach of the result clusters with indices into the data set
K:clusters <span class='arw'>&rarr;</span> 
( (4 5 7 8 14 16) 
(0 2 6 11 12 15 19) 
(1 3 9 10 13 17 18) )

; cluster of data records labeled 1 at offset 0
(select data (K:clusters 0)) <span class='arw'>&rarr;</span>
( (7.56 7.93 5.06) 
(3.61 7.95 5.11) 
(8.17 6.59 7.49) 
(5.44 5.9 5.57) 
(6.77 6.04 3.76) 
(6.79 8.72 8.62) )

; cluster of data records labeled 2 at offset 1
(select data (K:clusters 1)) <span class='arw'>&rarr;</span>
( (6.57 4.96 11.91) 
(8.63 2.51 8.11) 
(7.18 3.46 8.7) 
(8.16 3.83 8.93) 
(8.49 5.31 7.47) 
(7.01 4.2 11.9) 
(9.44 2.65 7.37) )

; cluster of data records labeled 3 at offset 2
(select data (K:clusters 2)) <span class='arw'>&rarr;</span>
( (2.29 4.18 1.06) 
(1.85 1.89 0.11) 
(2.43 2.14 1.59) 
(2.48 2.26 0.19) 
(3.12 3.1 1.4) 
(1.17 4.46 1.02) 
(2.11 2.14 0.85) )
</pre>


<p>In the last example the cluster labels (from 1 to 3) are added
to the data:</p>

<pre>
; append a cluster label to each data record
(set 'labeled-data (transpose (push K:labels (transpose data) -1)))

labeled-data: <span class='arw'>&rarr;</span>
( (6.57 4.96 11.91 2) 
(2.29 4.18 1.06 3) 
(8.63 2.51 8.11 2) 
(1.85 1.89 0.11 3) 
(7.56 7.93 5.06 1) 
(3.61 7.95 5.11 1) 
... ...
(2.11 2.14 0.85 3) 
(9.44 2.65 7.37 2) )
</pre>

<p>The result context should be prefixed with <tt>MAIN</tt> when code is
written in a namespace context. If the context does not exists already, it
will be created.</p>

<p>Results in <tt>K:labels</tt>, <tt>K:clusters</tt>, <tt>K:centroids</tt>
and <tt>K:deviations</tt> will be overwritten, if already present from previous 
runs of <tt>kmeans-train</tt>.</p>

<br/><br/>


<a name="lambda"></a>

<h2><span class="function">lambda</span></h2>

<p>See the description of <a href="#fn">fn</a>, which is a shorter form of writing <tt>lambda</tt>.</p>

<br/><br/>

<a name="lambda-macro"></a>

<h2><span class="function">lambda-macro</span></h2>

<p>See the description of <a href="#define-macro">define-macro</a>.</p>

<br/><br/>

<a name="lambdap"></a>
<h2><span class="function">lambda?</span></h2>
<h4>syntax: (lambda? <em>exp</em>)</h4>

<p>Returns <tt>true</tt> only if the value of <em>exp</em> is a lambda expression;
otherwise, returns <tt>nil</tt>.
</p>

<!-- example -->

<pre>
(define (square x) (* x x)) <span class='arw'>&rarr;</span> (lambda (x) (* x x))

square <span class='arw'>&rarr;</span> (lambda (x) (* x x))

(lambda? square)  <span class='arw'>&rarr;</span> true
</pre>


<p>See <a href="#define">define</a> and <a href="#define-macro">define-macro</a> for
more information about <em>lambda</em> expressions.
</p>
<br/><br/>

<a name="last"></a>

<h2><span class="function">last</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (last <em>list</em>)<br/>
syntax: (last <em>array</em>)<br/>
syntax: (last <em>str</em>)</h4>

<p>Returns the last element of a list or a string.
</p>

<!-- example -->

<pre>
(last '(1 2 3 4 5))  <span class='arw'>&rarr;</span> 5
(last '(a b (c d)))  <span class='arw'>&rarr;</span> (c d)

(set 'A (array 3 2 (sequence 1 6)))
<span class='arw'>&rarr;</span> ((1 2) (3 4) (5 6))
(last A)             <span class='arw'>&rarr;</span> (5 6)

(last '())           <span class='arw'>&rarr;</span> <span class='err'>ERR: list is empty</span>
</pre>


<p>In the second version the last character in the string <em>str</em> is returned as a
string.
</p>

<!-- example -->

<pre>
(last "newLISP")  <span class='arw'>&rarr;</span> "P"
</pre>


<p>
Note that <a href="#last">last</a> works on character boundaries 
rather than byte boundaries 
when the UTF-8&ndash;enabled version of newLISP is used.
See also <a href="#first">first</a>, <a href="#rest">rest</a> and <a href="#nth">nth</a>.
</p>

<br/><br/>

<a name="last-error"></a>
<h2><span class="function">last-error</span></h2>
<h4>syntax: (last-error)<br/>
syntax: (last-error <em>int-error</em>)</h4>

<p>Reports the last error generated by newLISP due to syntax errors or exhaustion of 
some resource. For a summary of all possible errors see the chapter 
<a href="#error_codes">Error codes</a> in the appendix.</p>

<p>If no error has occurred since the newLISP session was started,
<tt>nil</tt> is returned.</p>

<p>When <em>int-error</em> is specified, a list of the number and the error 
text is returned.</p>

<!-- example -->

<pre>
(last-error)  <span class='arw'>&rarr;</span> nil

(abc)

<span class='err'>ERR: invalid function : (abc)</span>

(last-error) <span class='arw'>&rarr;</span> (24 "ERR: invalid function : (abc)")

(last-error 24) <span class='arw'>&rarr;</span> (24 "invalid function")
(last-error 1) <span class='arw'>&rarr;</span> (1 "not enough memory")
(last-error 12345) <span class='arw'>&rarr;</span> (12345 "Unknown error")
</pre>


<p>For error numbers out of range the string <tt>"Unknown error"</tt> is
given for the error text.</p>

<p>Errors can be trapped by <a href="#error-event">error-event</a> 
and user defined error handlers.</p>

<p>See also <a href="#net-error">net-error</a> for errors generated by
networking conditions and <a href="#sys-error">sys-error</a> for errors
generated by the operating system.</p>

<br/><br/>

<a name="legalp"></a>
<h2><span class="function">legal?</span></h2>
<h4>syntax: (legal? <em>str</em>)</h4>

<p>
The token in <em>str</em> is verified as a legal newLISP symbol. 
Non-legal symbols can be created using the <a href="#sym">sym</a> function
(e.g. symbols containing spaces, quotes, or other characters not normally allowed). 
Non-legal symbols are created frequently 
when using them for associative data access:
</p>

<!-- example -->

<pre>
(symbol? (sym "one two"))  <span class='arw'>&rarr;</span> true

(legal? "one two")         <span class='arw'>&rarr;</span> nil  ; contains a space

(set (sym "one two") 123)  <span class='arw'>&rarr;</span> 123

(eval (sym "one two"))     <span class='arw'>&rarr;</span> 123
</pre>


<p>The example shows that the string <tt>"one two"</tt> does not contain a legal
symbol although a symbol can be created from this string and treated like a
variable.
</p>

<br/><br/>

<a name="length"></a>
<h2><span class="function">length</span>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (length <em>exp</em>)</h4>

<p>Returns the number of elements in a list, the number of rows in
an array and the number of bytes in a string or in a symbol name.</p>

<p>Applied to a number, <tt>length</tt> returns the number of digits for 
normal and big integers and the number of digits before the decimal 
separator for floats.</p>

<p><tt>length</tt> returns <tt>0</tt> on all other types.</p>

<p>Before version 10.5.6 <tt>length</tt> returned the storage size in bytes
for integers (4 or 8) and floats (8).</p>

<!-- example -->

<pre>
; number of top level elements in a list
(length '(a b (c d) e))         <span class='arw'>&rarr;</span> 4
(length '())                    <span class='arw'>&rarr;</span> 0
(set 'someList '(q w e r t y))  <span class='arw'>&rarr;</span> (q w e r t y)
(length someList)               <span class='arw'>&rarr;</span> 6

; number of top level elements in an array
(set 'ary (array 2 4 '(0)))  <span class='arw'>&rarr;</span> ((1 2 3 4) (5 6 7 8))
(length ary)                 <span class='arw'>&rarr;</span> 2

; number of bytes in a string or byte buffer
(length "Hello World")  <span class='arw'>&rarr;</span> 11
(length "")             <span class='arw'>&rarr;</span> 0
(length "\000\001\003") <span class='arw'>&rarr;</span> 3

; number of bytes in a symbol name string
(length 'someVar)  <span class='arw'>&rarr;</span> 7

; number of int digits in a number
(length 0)         <span class='arw'>&rarr;</span> 0
(length 123)       <span class='arw'>&rarr;</span> 3
(length 1.23)      <span class='arw'>&rarr;</span> 1
(length 1234567890123456789012345L) <span class='arw'>&rarr;</span> 25 
</pre>

<p>Use <a href="#utf8len">utf8len</a> to calculate the number of UTF-8 characters 
in a string.</p>

<br/><br/>

<a name="let"></a>
<h2><span class="function">let</span></h2>
<h4>syntax: (let ((<em>sym1</em> [<em>exp-init1</em>]) [(<em>sym2</em> [<em>exp-init2</em>]) ... ]) <em>body</em>)<br/>
syntax: (let (<em>sym1</em> <em>exp-init1</em> [<em>sym2</em> <em>exp-init2</em> ... ]) <em>body</em>)</h4>

<p>One or more variables <em>sym1</em>, <em>sym2</em>, ... are declared locally and
initialized with expressions in <em>exp-init1</em>, <em>exp-init2</em>, etc.
In the fully parenthesized first syntax, initializers are optional and assumed 
<tt>nil</tt> if missing.</p>

<p>When the local variables are initialized, the initializer expressions evaluate
 using symbol bindings as before the <tt>let</tt> statement. To incrementally use
symbol bindings as evaluated during the initialization of locals in <tt>let</tt>,
use <a href="#letn">letn</a>.</p>

<p>One or more expressions in <em>exp-body</em> are evaluated using the local
definitions of <em>sym1</em>, <em>sym2</em> etc. <tt>let</tt> is useful for
breaking up complex expressions by defining local variables close to the
place where they are used. The second form omits the parentheses around the
variable expression pairs but functions identically.</p>

<!-- example -->

<pre>
(define (sum-sq a b)
    (let ((x (* a a)) (y (* b b)))
        (+ x y)))

(sum-sq 3 4) <span class='arw'>&rarr;</span> 25

(define (sum-sq a b)           ; alternative syntax
    (let (x (* a a) y (* b b))
        (+ x y)))
</pre>


<p>The variables <tt>x</tt> and <tt>y</tt> are initialized, then the expression

<tt>(+ x y)</tt> is evaluated. The let form is just an optimized version and syntactic
convenience for writing:
</p>


<pre>
((lambda (<em>sym1</em> [<em>sym2</em> ... ]) <em>exp-body</em> ) <em>exp-init1</em> [ <em>exp-init2</em> ])
</pre>


<p>See also <a href="#letn">letn</a> for an incremental or nested form of
<tt>let</tt> and local for initializing to <tt>nil</tt>. See <a href="#local">local</a>
for automatic initialization of variables to <tt>nil</tt>.</p>

<br/><br/>

<a name="letex"></a>
<h2><span class="function">letex</span></h2>
<h4>syntax: (letex ((<em>sym1</em> [<em>exp-init1</em>]) [(<em>sym2</em> [<em>exp-init2</em>]) ... ]) <em>body</em>)<br/>

syntax: (letex (<em>sym1</em> <em>exp-init1</em> [<em>sym2</em> <em>exp-init2</em> ... ]) <em>body</em>)</h4>

<p>This function combines <a href="#let">let</a> and <a href="#expand">expand</a> to 
expand local variables into an expression before evaluating it. In the fully parenthesized 
first syntax initializers are optional and assumed <tt>nil</tt> if missing.</p>

<p>Both forms provide the same functionality, but in the second form the parentheses 
around the initializers can be omitted:
</p>

<!-- example -->

<pre>
(letex (x 1 y 2 z 3) '(x y z))    <span class='arw'>&rarr;</span> (1 2 3)

(letex ( (x 1) (y '(a b c)) (z "hello") ) '(x y z)) 

<span class='arw'>&rarr;</span> (1 (a b c) "hello")
</pre>

<p>Before the expression <tt>'(x y z)</tt> gets evaluated, <tt>x, y</tt> and <tt>z</tt>
are literally replaced with the initializers from the <tt>letex</tt> initializer list.
The final expression which gets evaluated is <tt>'(1 2 3)</tt>.
</p>

<p>In the second example a function <tt>make-adder</tt> is defined
for making adder functions:</p>

<pre>
(define (make-adder n)
    (letex (c n) (lambda (x) (+ x c))))

(define add3 (make-adder 3)) <span class='arw'>&rarr;</span> (lambda (x) (+ x 3))

(add3 10) <span class='arw'>&rarr;</span> 13

; letex can expand symbols into themselves
; the following form also works

(define (make-adder n)
     (letex (n n) (lambda (x) (+ x n))))

</pre>

<p><tt>letex</tt> evaluates <tt>n</tt> to the constant <tt>3</tt> and replaces <tt>c</tt>
with it in the lambda expression. The second examples shows, how a <tt>letex</tt>
variable can be expanded into itself.</p>
<br/><br/>

<a name="letn"></a>
<h2><span class="function">letn</span></h2>
<h4>syntax: (letn ((<em>sym1</em> [<em>exp-init1</em>]) [(<em>sym2</em> [<em>exp-init2</em>]) ... ]) <em>body</em>)<br/>
syntax: (letn (<em>sym1</em> <em>exp-init1</em> [<em>sym2</em> <em>exp-init2</em> ... ]) <em>body</em>)</h4>

<p><tt>letn</tt> is like a <em>nested let</em> and works similarly to <a href="#let">let</a>, 
but will incrementally use the new symbol bindings when evaluating the initializer expressions 
as if several <a href="#let">let</a> were nested. In the fully parenthesized first syntax, 
initializers are optional and assumed <tt>nil</tt> if missing.</p>

<p>The following comparison
of <a href="#let">let</a> and <tt>letn</tt> show the difference:
</p>

<!-- example -->

<pre>
(set 'x 10)
(let ((x 1) (y (+ x 1))) 
(list x y))           <span class='arw'>&rarr;</span> (1 11)

(letn ((x 1) (y (+ x 1))) 
(list x y))          <span class='arw'>&rarr;</span> (1 2)
</pre>


<p>While in the first example using <a href="#let">let</a> the variable <tt>y</tt> is
calculated using the binding of <tt>x</tt> before the <a href="#let">let</a> expression,
in the second example using <tt>letn</tt> the variable <tt>y</tt> is calculated using 
the new local binding of <tt>x</tt>.
</p>


<pre>
(letn  (x 1 y x) 
    (+ x y))     <span class='arw'>&rarr;</span>  2

;; same as nested let's

(let (x 1)
    (let (y x)
      (+ x y)))  <span class='arw'>&rarr;</span>  2
</pre>


<p><tt>letn</tt> works like several <em>nested</em> <a href="#let">let</a>. The parentheses
around the initializer expressions can be omitted.
</p>

<br/><br/>

<a name="list"></a>
<h2><span class="function">list</span></h2>
<h4>syntax: (list <em>exp-1</em> [<em>exp-2</em> ... ])</h4>

<p>The <em>exp</em> are evaluated and the values used to construct a new list.
Note that arguments of array type are converted to lists. See the chapter 
<a href="#arrays">Arrays</a> for dealing with multidimensional lists.
</p>

<!-- example -->

<pre>
(list 1 2 3 4 5)                <span class='arw'>&rarr;</span> (1 2 3 4 5)
(list 'a '(b c) (+ 3 4) '() '*) <span class='arw'>&rarr;</span> (a (b c) 7 () *)
</pre>


<p>See also <a href="#cons">cons</a> and <a href="#push">push</a> for other
forms of building lists.
</p>

<br/><br/>

<a name="listp"></a>
<h2><span class="function">list?</span></h2>
<h4>syntax: (list? <em>exp</em>)</h4>

<p>Returns <tt>true</tt> only if the value of <em>exp</em> is a list; otherwise
returns <tt>nil</tt>.  Note that lambda and lambda-macro
expressions are also recognized as special instances of a list expression.
</p>

<!-- example -->

<pre>
(set 'var '(1 2 3 4))    <span class='arw'>&rarr;</span> (1 2 3 4)
(list? var)              <span class='arw'>&rarr;</span> true

(define (double x) (+ x x))

(list? double)           <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="load"></a>

<h2><span class="function">load</span></h2>
<h4>syntax: (load <em>str-file-name-1</em> [<em>str-file-name-2</em> ... ] [<em>sym-context</em>])</h4>

<p>Loads and translates newLISP from a source file specified in one or more <em>str-file-name</em> 
and evaluates the expressions contained in the file(s). When loading is successful,
<tt>load</tt> returns the result of the last expression in the last file evaluated. If a file 
cannot be loaded, <tt>load</tt> throws an error.
</p>

<p>
An optional <em>sym-context</em> can be specified, 
which becomes the context of evaluation, 
unless such a context switch is already present 
in the file being loaded. 
By default, 
files which do not contain <a href="#context">context</a> switches 
will be loaded into the <tt>MAIN</tt> context.
</p>

<p>The <em>str-file-name</em> specs can contain URLs. Both <tt>http://</tt> and <tt> file://</tt>
URLs are supported.
</p>

<!-- example -->

<pre>
(load "myfile.lsp")    

(load "a-file.lsp" "b-file.lsp") 

(load "file.lsp" "http://mysite.org/mypro")

(load "http://192.168.0.21:6000//home/test/program.lsp")

(load "a-file.lsp" "b-file.lsp" 'MyCTX)

(load "file:///usr/local/share/newlisp/mysql.lsp")
</pre>


<p>In case expressions evaluated during the <tt>load</tt> are changing the 
<a href="#context">context</a>, this will not influence the programming
module doing the <tt>load</tt>. </p>

<p>The current context after the <tt>load</tt> statement will always be 
the same as before the <tt>load</tt>.</p>

<p>Normal file specs and URLs can be mixed in the same load command.</p>

<p><tt>load</tt> with <tt>HTTP</tt> URLs can also be used to load code
remotely from newLISP server nodes running on a Unix-like operating system. 
In this mode, <tt>load</tt> will issue
an HTTP GET request to the target URL. Note that a double backslash is required 
when path names are specified relative to the root directory. <tt>load</tt>
in <tt>HTTP</tt> mode will observe a 60-second timeout.</p>

<p>The second to last line causes the files to be loaded into the context <tt>MyCTX</tt>.
The quote forces the context to be created if it did not exist.
</p>

<p>The <tt>file://</tt> URL is followed by a third <tt>/</tt> for the directory spec.
</p>

<br/><br/>

<a name="local"></a>
<h2><span class="function">local</span></h2>
<h4>syntax: (local (<em>sym-1</em> [<em>sym-2</em> ... ]) <em>body</em>)</h4>

<p>
Initializes one or more symbols 
in <em>sym-1&mdash;</em> to <tt>nil</tt>, 
evaluates the expressions in <em>body</em>, 
and returns the result of the last evaluation.
</p>

<p>
<tt>local</tt> works similarly to <a href="#let">let</a>,
but local variables are all initialized to <tt>nil</tt>.
</p>

<p>
<tt>local</tt> provides a simple way 
to localize variables 
without explicit initialization.
</p>

<br/><br/>

<a name="log"></a>
<h2><span class="function">log</span></h2>
<h4>syntax: (log <em>num</em>)<br/>

syntax: (log <em>num</em> <em>num-base</em>)</h4>

<p>In the first syntax, the expression in <em>num</em> is evaluated and the natural
logarithmic function is calculated from the result.
</p>

<!-- example -->

<pre>
(log 1)         <span class='arw'>&rarr;</span> 0
(log (exp 1))   <span class='arw'>&rarr;</span> 1
</pre>


<p>In the second syntax, an arbitrary base can be specified in <em>num-base</em>.
</p>

<!-- example -->

<pre>
(log 1024 2)             <span class='arw'>&rarr;</span> 10
(log (exp 1) (exp 1))    <span class='arw'>&rarr;</span>  1
</pre>


<p>See also <a href="#exp">exp</a>, which is the inverse function to <tt>log</tt> with
base <b><em>e</em></b> (2.718281828).</p>

<br/><br/>

<a name="lookup"></a>
<h2><span class="function">lookup</span></h2>
<h4>syntax: (lookup <em>exp-key</em> <em>list-assoc</em> [<em>int-index</em> [<em>exp-default</em>]])</h4>

<p>Finds in <em>list-assoc</em> an association, the key element of which
has the same value as <em>exp-key</em>, and returns the <em>int-index</em> element of
association (or the last element if <em>int-index</em> is absent).</p>

<p>Optionally, <em>exp-default</em> can be specified, which is returned if an association matching
<em>exp-key</em> cannot be found. If the <em>exp-default</em> is absent and no association
has been found, <tt>nil</tt> is returned.</p>

<p>See also <a href="#indexing">Indexing elements of strings and lists</a>.
</p>

<p><tt>lookup</tt> is similar to <a href="#assoc">assoc</a> but goes one step further 
by extracting a specific element found in the list.
</p>

<!-- example -->

<pre>
(set 'params '(
    (name "John Doe") 
    (age 35) 
    (gender "M") 
    (balance 12.34)
))

(lookup 'age params)             <span class='arw'>&rarr;</span> 35

; use together with setf to modify and association list
(setf (lookup 'age params) 42)   <span class='arw'>&rarr;</span> 42
(lookup 'age params)             <span class='arw'>&rarr;</span> 42

(set 'persons '(
    ("John Doe" 35 "M" 12.34) 
    ("Mickey Mouse" 65 "N" 12345678)
))

(lookup "Mickey Mouse" persons 2)    <span class='arw'>&rarr;</span> "N"
(lookup "Mickey Mouse" persons -3)   <span class='arw'>&rarr;</span> 65
(lookup "John Doe" persons 1)        <span class='arw'>&rarr;</span> 35 
(lookup "John Doe" persons -2)       <span class='arw'>&rarr;</span> "M"

(lookup "Jane Doe" persons 1 "N/A")  <span class='arw'>&rarr;</span> "N/A"
</pre>

<p>See also <a href="#assoc">assoc</a></p>

<br/><br/>

<a name="lower-case"></a>
<h2><span class="function">lower-case</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (lower-case <em>str</em>)</h4>

<p>
Converts the characters of the string 
in <em>str</em> to lowercase. 
A new string is created, 
and the original is left unaltered.
</p> 


<!-- example -->

<pre>
(lower-case "HELLO WORLD")  <span class='arw'>&rarr;</span> "hello world"
(set 'Str "ABC")
(lower-case Str)  <span class='arw'>&rarr;</span> "abc"
Str               <span class='arw'>&rarr;</span> "ABC"
</pre>


<p>
See also the <a href="#upper-case">upper-case</a> and 
<a href="#title-case">title-case</a> functions.
</p>

<br/><br/>

<a name="macro"></a>
<h2><span class="function">macro</span></h2>
<h4>syntax: (macro (<em>sym-name</em> [<em>sym-param-1</em> ... ]) [<em>body-1</em> ... ])</h4>

<p>The <tt>macro</tt> function is used to define expansion macros. The syntax 
of <tt>macro</tt> is identical to the syntax of <a href="#define-macro">define-macro</a>.
But while <tt>define-macro</tt> defines are <em>fexprs</em> functions to be evaluated 
at run-time, <tt>macro</tt> defines a function to be used during the source loading 
and reading process to transform certain expression call patterns into different call 
patterns.</p>

<p>Symbols defined with <tt>macro</tt> are protected from re-definition.</p>

<pre>
(macro (double X) (+ X X)) <span class='arw'>&rarr;</span> (lambda-macro (X) (expand '(+ X X)))

(double 123) <span class='arw'>&rarr;</span> 246

(protected? 'double) <span class='arw'>&rarr;</span> true
</pre>

<p>Internally all <tt>macro</tt> defined symbol call patterns are translated using
the <a href="#expand">expand</a> expression during source reading. This can be shown using
the <a href="#read-expr">read-expr</a> function:</p>

<pre>
(read-expr "(double 123)") <span class='arw'>&rarr;</span> (+ 123 123)
</pre>

<p>All variable names to be expanded must start in upper-case. Macros can be nested containing 
other macros defined earlier. But <tt>macro</tt> definitions cannot be repeated for the same 
symbol during the same newLISP session. To redefine a macro, e.g. for reading source with a 
different definition of an exisiting <tt>macro</tt> definition, use the 
<a href="#constant">constant</a> function in the following way:</p>

<pre>
; change existing macro 'double' to allow floating point parameters
; use upper-case for variables for expansion

(constant 'double (lambda-macro (X) (expand '(add X X))))
<span class='arw'>&rarr;</span> (lambda-macro (X) (expand '(add X X)))

(double 1.23) <span class='arw'>&rarr;</span> 2.46
</pre>

<p>Note, that <a href="#constant">constant</a> can be used only to re-define macros, not
to create new macros. Internally newLISP knows that <tt>macro</tt> defined symbols
are executed during source reading, not evaluation.</p>

<p>The redefinition will only affect future read code, it will not affect
code already load and translated by the reader routines.</p>

<h3>Using <tt>map</tt> and <tt>apply</tt> with <tt>macro</tt></h3>

<p>When mapping macros using <a href="#map">map</a> or <a href="#apply">apply</a>
the expansion function is mapped:</p>

<pre>
<b>&gt;</b> (macro (double X) (+ X X))
<b>(lambda-macro (X) (expand '(+ X X)))</b>

<b>&gt;</b> (map double '(1 2 3 4 5))
<b>((+ 1 1) (+ 2 2) (+ 3 3) (+ 4 4) (+ 5 5))

&gt;</b> (map eval (map double '(1 2 3 4 5)))
<b>(2 4 6 8 10)</b>

<b>&gt;</b> (apply double '(10))
<b>(+ 10 10)
&gt;</b>
</pre>
<p>This is useful to find out how the expansion mechanism of our <tt>macro</tt>
definition works during source load time.</p>

<h3>Differences between <tt>macro</tt> and <tt>define-macro</tt> and potential problems.</h3>

<p><tt>macro</tt> definitions are not susceptible to <em>variable capture</em> as 
are fexprs made with <a href="define-macro">define-macro</a>:</p>

<pre>
(define-macro (fexpr-add A B) 
    (+ (eval A) (eval B)))

(macro (mac-add A B) 
    (+ A B))

(set 'A 11 'B 22)

; variable capture when using the same symbols 
; used as locals in define-macro for callling

(fexpr-add A B) <span class='arw'>&rarr;</span>
; or
(fexpr-add B A) <span class='arw'>&rarr;</span>
<span class="err">ERR: value expected : A
called from user defined function fexpr-add</span>

; no variable capture when doing the same with 
; expansion macros

(mac-add A B) <span class='arw'>&rarr;</span> 33

(mac-add B A) <span class='arw'>&rarr;</span> 33
</pre>

<p>But expansion macros using <tt>macro</tt> are susceptible to unwanted double
evaluation, just like <tt>define-macro is</tt>:</p>

<pre>
(define-macro (fexpr-double X) 
    (+ (eval X) (eval X)))

(macro (mac-double X) 
    (+ X X))

(set 'a 10)
(fexpr-double (inc a)) <span class='arw'>&rarr;</span> 23 ; not 22 as expected

(set 'a 10)
(mac-double (inc a)) <span class='arw'>&rarr;</span> 23 ; not 22 as expected
</pre>

<p>In both cases the incoming expression <tt>(inc a)</tt> gets evaulated twice.
This must be considered when writing both, <tt>macro</tt> or <tt>define-macro</tt>
expressions and symbols occur more than once in the body of the definition.</p> 

<p>See also <a href="#reader-event">reader-event</a> for general preprocessing
of expressions during reading of source code.</p>

<br/><br/>

<a name="macrop"></a>
<h2><span class="function">macro?</span></h2>
<h4>syntax: (macro? <em>exp</em>)</h4>

<p>Returns <tt>true</tt> if <em>exp</em> evaluates to a lambda-macro expression.
If <em>exp</em> evaluates to a symbol and the symbol contains
a macro-expansion expression made with the <a href="#macro">macro</a> function,
<tt>true</tt> is also returned. In all other cases <tt>nil</tt> is returned.</p>

<!-- example -->

<pre>
(define-macro (mysetq lv rv) (set lv (eval rv)))

(macro? mysetq)  <span class='arw'>&rarr;</span> true

(macro (my-setq Lv Rv) (set 'Lv Rv)) 
<span class='arw'>&rarr;</span> (lambda-macro (Lv Rv) (expand '(set 'Lv Rv)))

; my-setq contains a lambda-macro expression
(macro? my-setq)   <span class='arw'>&rarr;</span> true

; my-setq symbol was created with macro function
(macro? 'my-setq)   <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="main-args"></a>
<h2><span class="function">main-args</span></h2>
<h4>syntax: (main-args)<br/>

syntax: (main-args <em>int-index</em>)</h4>

<p>
<tt>main-args</tt> returns a list 
with several string members, 
one for program invocation 
and one for each of 
the command-line arguments.
</p>

<!-- example -->

<pre>
newlisp 1 2 3

&gt; (main-args)
<b>("/usr/local/bin/newlisp" "1" "2" "3")</b>
</pre>


<p>
After <tt>newlisp 1 2 3</tt> is executed at the command prompt, 
<tt>main-args</tt> returns a list containing the name of 
the invoking program and three command-line arguments.
</p>

<p>
Optionally, <tt>main-args</tt> can take 
an <em>int-index</em> for indexing into the list.
Note that an index out of range will cause <tt>nil</tt>
to be returned, not the last element of the list like
in list-indexing.
</p>


<pre>
newlisp a b c

&gt; (main-args 0)   
<b>"/usr/local/bin/newlisp"</b>
&gt; (main-args -1)  
<b>"c"</b>
&gt; (main-args 2)   
<b>"b"</b>
&gt; (main-args 10)
<b>nil</b>
</pre>


<p>
Note that when newLISP is executed from a script, 
<tt>main-args</tt> also returns the <em>name</em> 
of the script as the second argument:
</p>


<pre>
#!/usr/local/bin/newlisp
# 
# script to show the effect of 'main-args' in script file

(print (main-args) "\n")
(exit)

# end of script file

;; execute script in the OS shell:

script 1 2 3

<b>("/usr/local/bin/newlisp" "./script" "1" "2" "3")</b>
</pre>


<p>
Try executing this script with different 
command-line parameters.
</p>

<br/><br/>

<a name="make-dir"></a>
<h2><span class="function">make-dir</span></h2>
<h4>syntax: (make-dir <em>str-dir-name</em> [<em>int-mode</em>])</h4>

<p>
Creates a directory as specified in <em>str-dir-name</em>, 
with the optional access mode <em>int-mode</em>. 
Returns <tt>true</tt> or <tt>nil</tt> 
depending on the outcome. 
If no access mode is specified, 
most Unix systems default to <tt>drwxr-xr-x</tt>.
</p>

<p>
On Unix systems, the access mode specified 
will also be masked by the OS's <em>user-mask</em> 
set by the system administrator. 
The <em>user-mask</em> can be retrieved 
on Unix systems using the command <tt>umask</tt> 
and is usually <tt>0022</tt> (octal),
which masks write (and creation) permission 
for non-owners of the file.
</p>

<!-- example -->

<pre>
;; 0 (zero) in front of 750 makes it an octal number

(make-dir "adir" 0750)  
</pre>


<p>
This example creates a directory named <tt>adir</tt> 
in the current directory with an access mode of 
<tt>0750</tt> (octal 750 = <tt>drwxr-x---</tt>).
</p>
<br/><br/>

<a name="map"></a>

<h2><span class="function">map</span></h2>
<h4>syntax: (map <em>exp-functor</em> <em>list-args-1</em> [<em>list-args-2</em> ... ])</h4>

<p>Successively applies the primitive function, defined function, or lambda expression 
<em>exp-functor</em> to the arguments specified in <em>list-args-1 list-args-2&mdash;</em>, 
returning all results in a list. Since version 10.5.5 <em>list-args</em>
can also be array vectors, but the returned result will always be a list.</p>

<!-- example -->

<pre>
(map + '(1 2 3) '(50 60 70))  <span class='arw'>&rarr;</span> (51 62 73)

(map if '(true nil true nil true) '(1 2 3 4 5) '(6 7 8 9 10))
<span class='arw'>&rarr;</span> '(1 7 3 9 5)

(map (fn (x y) (* x y)) '(3 4) '(20 10))
<span class='arw'>&rarr;</span> (60 40)
</pre>

<p>
The second example shows how to dynamically 
create a function for <tt>map</tt>:
</p>

<pre>
(define (foo op p) 
    (append (lambda (x)) (list (list op p 'x))))
</pre>

<p>We can also use the shorter <tt>fn</tt>:</p>

<pre>
(define (foo op p) 
    (append (fn (x)) (list (list op p 'x))))
</pre>

<p><tt>foo</tt> now works like a function-maker:</p>

<pre>
(foo 'add 2)  <span class='arw'>&rarr;</span> (lambda (x) (add 2 x))

(map (foo add 2) '(1 2 3 4 5))  <span class='arw'>&rarr;</span> (3 4 5 6 7)

(map (foo mul 3) '(1 2 3 4 5))  <span class='arw'>&rarr;</span> (3 6 9 12 15)
</pre>

<p>
Note that the quote before the operand can be omitted 
because primitives evaluate to themselves in newLISP.
</p>

<p>By incorporating <tt>map</tt> into the function definition, 
we can do the following:</p>

<pre>
(define (list-map op p lst) 
    (map (lambda (x) (op p x)) lst))

(list-map + 2 '(1 2 3 4))  <span class='arw'>&rarr;</span> (3 4 5 6)

(list-map mul 1.5 '(1 2 3 4))  <span class='arw'>&rarr;</span> (1.5 3 4.5 6)
</pre>

<p><tt>map</tt> also sets the internal list index <tt>$idx</tt>.</p>

<pre>
(map (fn (x) (list $idx x)) '(a b c)) <span class='arw'>&rarr;</span> ((0 a) (1 b) (2 c))
</pre>

<p>
The number of arguments used is determined by the length of the first argument list. 
Arguments missing in other argument lists cause map to stop collecting parameters
for that level of arguments. This ensures that the nth parameter list gets converted
to the nth column during the transposition occurring. If an argument list contains too 
many elements, the extra ones will be ignored.
</p>

<p>
Special forms which use parentheses as syntax cannot be mapped 
(i.e. <a href="#case">case</a>).</p>

<br/><br/>

<a name="mat"></a>
<h2><span class="function">mat</span></h2>
<h4>syntax: (mat <em>+</em> | <em>-</em> | <em>*</em> | <em>/</em> <em>matrix-A matrix-B</em>)<br/>
syntax: (mat <em>+</em> | <em>-</em> | <em>*</em> | <em>/</em> <em>matrix-A number</em>)</h4>

<p>Using the first syntax, this function performs fast floating point 
scalar operations on two-dimensional matrices in <em>matrix-A</em> or <em>matrix-B</em>. 
The type of operation is specified by one of the four arithmetic operators 
<tt>+</tt>, <tt>-</tt>, <tt>*</tt>, or <tt>/</tt>. 
This type of arithmetic operator is typically used for integer 
operations in newLISP. In the case of <tt>mat</tt>, however,
all operations will be performed as floating point operations
(<tt>add</tt>, <tt>sub</tt>, <tt>mul</tt>, <tt>div</tt>).</p>

<p>Matrices in newLISP are two-dimensional lists or arrays. 
Internally, newLISP translates lists and arrays into fast, accessible 
C-language data objects. 
This makes matrix operations in newLISP 
as fast as those coded directly in C. 
The same is true for the matrix operations 
<a href="#multiply">multiply</a> and <a href="#invert">invert</a>.</p>

<!-- example -->

<pre>
(set 'A '((1 2 3) (4 5 6)))
(set 'B A)

(mat + A B)    <span class='arw'>&rarr;</span> ((2 4 6) (8 10 12))
(mat - A B)    <span class='arw'>&rarr;</span> ((0 0 0) (0 0 0))
(mat * A B)    <span class='arw'>&rarr;</span> ((1 4 9) (16 25 36))
(mat / A B)    <span class='arw'>&rarr;</span> ((1 1 1) (1 1 1))

; specify the operator in a variable

(set 'op +)
(mat op A B)    <span class='arw'>&rarr;</span> ((2 4 6) (8 10 12)) 
</pre>


<p>Using the second syntax, all cells in <em>matrix-A</em> 
are operated on with a scalar in <em>number</em>:</p>


<pre>
(mat + A 5)    <span class='arw'>&rarr;</span> ((6 7 8) (9 10 11))
(mat - A 2)    <span class='arw'>&rarr;</span> ((-1 0 1) (2 3 4))
(mat * A 3)    <span class='arw'>&rarr;</span> ((3 6 9) (12 15 18))
(mat / A 10)   <span class='arw'>&rarr;</span> ((.1 .2 .3) (.4 .5 .6))
</pre>


<p>See also the other matrix operations <a href="#det">det</a>, 
<a href="#invert">invert</a>, <a href="#multiply">multiply</a>,
and <a href="#transpose">transpose</a>.</p>

<br/><br/>

<a name="match"></a>
<h2><span class="function">match</span></h2>
<h4>syntax: (match <em>list-pattern</em> <em>list-match</em> [<em>bool</em>])</h4>

<p>
The pattern in <em>list-pattern</em> is matched 
against the list in <em>list-match</em>, 
and the matching expressions are returned in a list. 
The three wildcard characters <tt>?</tt>, <tt>+</tt>, 
and <tt>*</tt> can be used in <em>list-pattern</em>.
</p>

<p>
Wildcard characters may be nested. 
<tt>match</tt> returns a 
list of matched expressions. 
For each <tt>?</tt> (question mark), 
a matching expression element is returned. 
For each <tt>+</tt> (plus sign) or 
<tt>*</tt> (asterisk), a list containing 
the matched elements is returned. 
If the pattern cannot be matched 
against the list in <em>list-match</em>, 
<tt>match</tt> returns <tt>nil</tt>.
If no wildcard characters are present
in the pattern an empty list is returned.
</p>

<p>Optionally, the Boolean value <tt>true</tt> (or any other expression not 
evaluating to <tt>nil</tt>) can be supplied as a third argument.  This 
causes <tt>match</tt> to show all elements in the returned result.</p>

<p>
<tt>match</tt> is frequently employed as a functor parameter
in <a href="#find">find</a>, <a href="#ref">ref</a>,
<a href="#ref-all">ref-all</a> and <a href="#replace">replace</a> and
is internally used by <a href="#find-all">find-all</a> for lists.</p>

<!-- example -->

<pre>
(match '(a ? c) '(a b c))  <span class='arw'>&rarr;</span> (b)

(match '(a ? ?) '(a b c))  <span class='arw'>&rarr;</span> (b c)

(match '(a ? c) '(a (x y z) c))  <span class='arw'>&rarr;</span> ((x y z))

(match '(a ? c) '(a (x y z) c) true)  <span class='arw'>&rarr;</span> (a (x y z) c)

(match '(a ? c) '(a x y z c))  <span class='arw'>&rarr;</span> nil


(match '(a * c) '(a x y z c))  <span class='arw'>&rarr;</span> ((x y z))

(match '(a (b c ?) x y z) '(a (b c d) x y z))  <span class='arw'>&rarr;</span> (d)

(match '(a (*) x ? z) '(a (b c d) x y z))  <span class='arw'>&rarr;</span> ((b c d) y)


(match '(+) '())  <span class='arw'>&rarr;</span> nil

(match '(+) '(a))  <span class='arw'>&rarr;</span> ((a))

(match '(+) '(a b))  <span class='arw'>&rarr;</span> ((a b))

(match '(a (*) x ? z) '(a () x y z))  <span class='arw'>&rarr;</span> (() y)

(match '(a (+) x ? z) '(a () x y z))  <span class='arw'>&rarr;</span> nil 
</pre>


<p>Note that the <tt>*</tt> operator tries to grab the fewest number of 
elements possible, but <tt>match</tt> backtracks and grabs more elements 
if a match cannot be found.</p>

<p>The <tt>+</tt> operator works similarly to the <tt>*</tt> operator, 
but it requires at least one list element.</p>

<p>The following example shows how the matched expressions can be bound 
to variables.</p>


<pre>
(map set '(x y) (match '(a (? c) d *) '(a (b c) d e f)))

x  <span class='arw'>&rarr;</span> b
y  <span class='arw'>&rarr;</span> (e f)
</pre>


<p>
Note that <tt>match</tt> for strings has been eliminated. 
For more powerful string matching, use <a href="#regex">regex</a>, 
<a href="#find">find</a>, <a href="#find-all">find-all</a> 
or <a href="#parse">parse</a>.
</p>

<p> <a href="#unify">unify</a> is another function for matching 
expressions in a PROLOG like manner.</p>

<br/><br/>

<a name="max"></a>
<h2><span class="function">max</span></h2>
<h4>syntax: (max <em>num-1</em> [<em>num-2</em> ... ])</h4>

<p>Evaluates the expressions <em>num-1</em>&mdash; and returns 
the largest number.</p>

<!-- example -->

<pre>
(max 4 6 2 3.54 7.1)  <span class='arw'>&rarr;</span> 7.1
</pre>


<p>
See also the <a href="#min">min</a> function.
</p>

<br/><br/>

<a name="member"></a>
<h2><span class="function">member</span></h2>
<h4>syntax: (member <em>exp</em> <em>list</em>)<br/>
syntax: (member <em>str-key</em> <em>str</em> [<em>num-option</em>])</h4>
<p>
In the first syntax, 
<tt>member</tt> searches 
for the element <em>exp</em> 
in the list <em>list</em>. 
If the element is a member of the list, 
a new list starting with the element found 
and the rest of the original list 
is constructed and returned. 
If nothing is found, 
<tt>nil</tt> is returned. 
When specifying <em>num-option</em>,
<tt>member</tt> performs a regular expression search.
</p>

<!-- example -->

<pre>
(set 'aList '(a b c d e f g h))  <span class='arw'>&rarr;</span> (a b c d e f g h)
(member 'd aList)                <span class='arw'>&rarr;</span> (d e f g h)
(member 55 aList)                <span class='arw'>&rarr;</span> nil
</pre>


<p>
In the second syntax, 
<tt>member</tt> searches 
for <em>str-key</em> in <em>str</em>. 
If <em>str-key</em> is found, all of <em>str</em> 
(starting with <em>str-key</em>) is returned. 
<tt>nil</tt> is returned if nothing is found.
</p>

<!-- example -->

<pre>
(member "LISP" "newLISP")  <span class='arw'>&rarr;</span> "LISP"
(member "LI" "newLISP")    <span class='arw'>&rarr;</span> "LISP"
(member "" "newLISP")      <span class='arw'>&rarr;</span> "newLISP"
(member "xyz" "newLISP")   <span class='arw'>&rarr;</span> nil
(member "li" "newLISP" 1)  <span class='arw'>&rarr;</span> "LISP"
</pre>


<p>
See also the related functions 
<a href="#slice">slice</a> and 
<a href="#find">find</a>.
</p>

<br/><br/>

<a name="min"></a>
<h2><span class="function">min</span></h2>
<h4>syntax: (min <em>num-1</em> [<em>num-2</em> ... ])</h4>

<p>
Evaluates the expressions <em>num-1</em>&mdash; 
and returns the smallest number.
</p>

<!-- example -->

<pre>
(min 4 6 2 3.54 7.1)  <span class='arw'>&rarr;</span> 2
</pre>

<p>
See also the <a href="#max">max</a> function.
</p>

<br/><br/>

<a name="mod"></a>
<h2><span class="function">mod</span></h2>
<h4>syntax: (mod <em>num-1</em> <em>num-2</em> [<em>num-3</em> ... ])<br/>
syntax: (mod <em>num-1</em>)</h4>

<p>
Calculates the modular value of the 
numbers in <em>num-1</em> and <em>num-2</em>. 
<tt>mod</tt> computes the remainder 
from the division of the numerator <em>num-i</em> 
by the denominator <em>num-i + 1</em>. 
Specifically, the return value is 
<em>numerator - n * denominator</em>, 
where <tt>n</tt> is the quotient 
of the numerator divided by the denominator, 
rounded towards zero to an integer. 
The result has the same sign as 
the numerator and its magnitude 
is less than the magnitude 
of the denominator.</p>

<p>In the second syntax <tt>1</tt> is assumed for <em>num-2</em> and the
result is the fractional part of <em>num-1</em>.</p>

<!-- example -->

<pre>
(mod 10.5 3.3)   <span class='arw'>&rarr;</span>  0.6
(mod -10.5 3.3)  <span class='arw'>&rarr;</span> -0.6
(mod -10.5)      <span class='arw'>&rarr;</span> -0.5
</pre>


<p>
Use the <a href="#arithmetic">%</a> (percent sign) 
function when working with integers only.
</p>

<br/><br/>

<a name="mul"></a>
<h2><span class="function">mul</span></h2>
<h4>syntax: (mul <em>num-1</em> <em>num-2</em> [<em>num-3</em> ... ])</h4>

<p>
Evaluates all expressions <em>num-1</em>&mdash;, 
calculating and returning the product. 
<tt>mul</tt> can perform mixed-type arithmetic, 
but it always returns floating point numbers. 
Any floating point calculation with 
<tt>NaN</tt> also returns <tt>NaN</tt>.
</p>

<!-- example -->


<pre>
(mul 1 2 3 4 5 1.1)  <span class='arw'>&rarr;</span> 132
(mul 0.5 0.5)        <span class='arw'>&rarr;</span> 0.25
</pre>

<br/><br/>

<a name="multiply"></a>
<h2><span class="function">multiply</span></h2>
<h4>syntax: (multiply <em>matrix-A</em> <em>matrix-B</em>)</h4>

<p>
Returns the matrix multiplication of matrices 
in <em>matrix-A</em> and <em>matrix-B</em>. 
If <em>matrix-A</em> has the dimensions <em>n</em> by <em>m</em> 
and <em>matrix-B</em> the dimensions <em>k</em> by <em>l</em> 
(<em>m</em> and <em>k</em> must be equal), 
the result is an <em>n</em> by <em>l</em> matrix. 
<tt> multiply</tt> can perform mixed-type arithmetic, 
but the results are always double precision floating points, 
even if all input values are integers.
</p>

<p>
The dimensions of a matrix are determined 
by the number of rows and the number 
of elements in the first row. 
For missing elements 
in non-rectangular matrices, 
<tt>0.0</tt> is assumed. 
A matrix can either be a nested list 
or <a href="#array">array</a>.
</p>

<!-- example -->

<pre>
(set 'A '((1 2 3) (4 5 6)))
(set 'B '((1 2) (1 2) (1 2)))
(multiply A B)  <span class='arw'>&rarr;</span> ((6 12) (15 30))

(set 'v '(10 20 30))
(multiply A (transpose (list v))) <span class='arw'>&rarr;</span> ((140) (320))
</pre>

<p>When multiplying a matrix with a vector of <tt>n</tt> elements, the vector
must be transformed into <tt>n</tt> rows by <tt>1</tt> column matrix using
 <a href="#transpose">transpose</a>.</p>

<p>
All operations shown here on lists 
can be performed on arrays, as well.
</p>

<p>
See also the matrix operations <a href="#det">det</a>,
<a href="#invert">invert</a>, <a href="#mat">mat</a> 
and <a href="#transpose">transpose</a>.
</p>

<br/><br/>

<a name="name"></a>
<h2><span class="function">name</span></h2>
<p>This function is deprecated, use <a href="#term">term</a> instead.</p>

<br/><br/>

<a name="NaNp"></a>
<h2><span class="function">NaN?</span></h2>
<h4>syntax: (NaN? <em>float</em>)</h4>

<p>
Tests if the result of a floating point math operation is a <tt>NaN</tt>.  
Certain floating point operations return a special IEEE 754 number format 
called a <tt>NaN</tt> for 'Not a Number'.</p>

<!-- example -->

<pre>
; floating point operation on NaN yield NaN
(set 'x (sqrt -1))  <span class='arw'>&rarr;</span> NaN
(NaN? x)            <span class='arw'>&rarr;</span> true
(add x 123)         <span class='arw'>&rarr;</span> NaN
(mul x 123)         <span class='arw'>&rarr;</span> NaN

; integer operations treat NaN as zero
(+ x 123)  <span class='arw'>&rarr;</span> 123
(* x 123)  <span class='arw'>&rarr;</span> 0

; comparisons with NaN values yield nil
(&gt; x 0)   <span class='arw'>&rarr;</span> nil
(&lt;= x 0)  <span class='arw'>&rarr;</span> nil
(= x x)   <span class='arw'>&rarr;</span> nil

(set 'infinity (mul 1.0e200 1.0e200)) <span class='arw'>&rarr;</span> inf
(NaN? (sub infinity infinity)) <span class='arw'>&rarr;</span> true
</pre>


<p>
Note that all floating point arithmetic operations 
with a <tt>NaN</tt> yield a <tt>NaN</tt>. 
All comparisons with <tt>NaN</tt> return <tt>nil</tt>, 
but <tt>true</tt> when comparing to itself. 
Comparison with itself, however, 
would result in <em>not</em> <tt>true</tt> when using ANSI C.  Integer operations 
treat <tt>NaN</tt> as <tt>0</tt> (zero) values.</p>

<p>See also <a href="#infp">inf?</a> for testing a floating point value for infinity.</p>

<br/><br/>

<a name="net-accept"></a>
<h2><span class="function">net-accept</span></h2>
<h4>syntax: (net-accept <em>int-socket</em>)</h4>

<p>
Accepts a connection on a socket 
previously put into listening mode. 
Returns a newly created socket handle 
for receiving and sending data 
on this connection.
</p>

<!-- example -->

<pre>
(set 'socket (net-listen 1234))
(net-accept socket)
</pre>


<p>
Note that for ports less than 1024, 
newLISP must be started in superuser mode
on Unix-like operating systems.
</p>

<p> See also the files <tt>server</tt> and <tt>client</tt> examples
in the <tt>examples/</tt> directory of the source distribution.</p>

<br/><br/>

<a name="net-close"></a>
<h2><span class="function">net-close</span></h2>
<h4>syntax: (net-close <em>int-socket</em> [<em>true</em>])</h4>

<p>
Closes a network socket in <em>int-socket</em> that 
was previously created by a 
<a href="#net-connect">net-connect</a> 
or <a href="#net-accept">net-accept</a> function. 
Returns <tt>true</tt> on success and 
<tt>nil</tt> on failure.
</p>

<!-- example -->

<pre>
(net-close aSock)
</pre>


<p>The optional <em>true</em> flag suppresses immediate shutdown
of sockets by waiting for pending data transmissions to finish.</p>

<br/><br/>

<a name="net-connect"></a>
<h2><span class="function">net-connect</span></h2>
<h4>syntax: (net-connect <em>str-remote-host</em> <em>int-port</em> [<em>int-timeout-ms</em>])<br/>
syntax: (net-connect <em>str-remote-host</em> <em>int-port</em> [<em>str-mode</em> [<em>int-ttl</em>]])<br/>
syntax: (net-connect <em>str-file-path</em>)</h4>

<p>In the first syntax, connects to a remote host computer specified in 
<em>str-remote-host</em> and a port specified in <em>int-port</em>. 
Returns a socket handle after having connected successfully; 
otherwise, returns <tt>nil</tt>.</p>

<!-- example -->

<pre>
(set 'socket (net-connect "example.com" 80))
(net-send socket "GET /\r\n\r\n")
(net-receive socket buffer 10000)
(println buffer)
(exit)
</pre>

<p>If successful, the <tt>net-connect</tt> function returns a socket
number which can be used to send and receive information from the host.
In the example a HTTP GET request is sent and subsequently a web page
received. Note that newLISP has already a built-in function 
<a href="#get-url">get-url</a> offering the same functionality.</p>

<p>Optionally a timeout value <em>int-timeout</em> in milliseconds
can be specified. Without a timeout value the function will wait up
to 10 seconds for an open port. With a timeout value the function can
be made to return on an unavailable port much earlier or later. The
following example shows a port scanner looking for open ports:</p>

<!-- example -->
<pre>
(set 'host (main-args 2))
(println "Scanning: " host)
(for (port 1 1024)
    (if (set 'socket (net-connect host port 500))
        (println "open port: " port " " (or (net-service port "tcp") ""))
        (print port "\r"))
)
</pre>

<p>The programs takes the host string from the shell command line as
either a domain name or an IP number in dot notation then tries to 
open each port from 1 to 1024. For each open port the port number and 
the service description string is printed. If no description is available,
an empty string "" is output. For closed ports the function outputs 
numbers in the shell window staying on the same line.</p>

<p>On Unix <tt>net-connect</tt> may return with <tt>nil</tt> before
the timeout expires, when the port is not available. On MS Windows 
<tt>net-connect</tt> will always wait for the timeout to expire before
failing with <tt>nil</tt>.</p>

<h3>UDP communications</h3>

<p>In the second syntax, a third parameter, the string <tt>"udp"</tt>
or <tt>"u"</tt> can be specified in the optional <em>str-mode</em> 
to create a socket suited for UDP (User Datagram Protocol) communications.
In UDP mode, <tt>net-connect</tt> does <em>not</em> try to connect 
to the remote host, but creates the socket and binds it to the
remote address, if an address is specified. 
A subsequent <a href="#net-send">net-send</a> will send a UDP packet 
containing that target address. 
When using <a href="#net-send-to">net-send-to</a>, only one of the
two functions <tt>net-connect</tt> or <tt>net-send-to</tt> should 
provide a target address. The other function should specify and empty
string <tt>""</tt> as the target address.</p>

<pre>
;; example server
(net-listen 4096 "226.0.0.1" "udp")  <span class='arw'>&rarr;</span> 5
(net-receive-from 5 20)

;; example client I
(net-connect "226.0.0.1" 4096 "udp") <span class='arw'>&rarr;</span> 3
(net-send 3 "hello")

;; example client II
(net-connect "" 4096 "udp") &rarr; 3
(net-send-to "226.0.0.1" 4096 "hello" 3)
</pre>

<p>The functions <a href="#net-receive">net-receive</a> and 
<a href="#net-receive-from">net-receive-from</a>
can both be used and will perform UDP communications when the <tt>"udp"</tt>
option as been used in <tt>net-listen</tt> or <tt>net-connect</tt>. 
<a href="#net-select">net-select</a> and <a href="#net-peek">net-peek</a> 
can be used to check for received data in a non-blocking fashion.</p>

<p><a href="#net-listen">net-listen</a> binds a specific 
local address and port to the socket. When <tt>net-connect</tt> is used,
the local address and port will be picked by the socket-stack 
functions of the host OS.</p>

<h3>UDP multicast communications</h3>

<p>When specifying <tt>"multi"</tt> 
or <tt>"m"</tt> as a third parameter for <em>str-mode</em>,
a socket for UDP multicast communications 
will be created. 
Optionally, the fourth parameter
<tt>int-ttl</tt> can be specified 
as a TTL (time to live) value. 
If no <em>int-ttl</em> value is specified, 
a value of 3 is assumed.
</p>

<p>
Note that specifying UDP multicast mode 
in <tt>net-connect</tt> does not actually establish 
a connection to the target multicast address 
but only puts the socket into UDP multicasting mode. 
On the receiving side, 
use <a href="#net-listen">net-listen</a> 
together with the UDP multicast option.
</p>

<!-- example -->

<pre>
;; example client I
(net-connect "" 4096 "multi")  <span class='arw'>&rarr;</span> 3
(net-send-to "226.0.0.1" 4096 "hello" 3)

;; example client II
(net-connect "226.0.0.1" 4096 "multi")  <span class='arw'>&rarr;</span> 3
(net-send 3 "hello")

;; example server
(net-listen 4096 "226.0.0.1" "multi")  <span class='arw'>&rarr;</span> 5
(net-receive-from 5 20)               
<span class='arw'>&rarr;</span> ("hello" "192.168.1.94" 32769)
</pre>


<p>
On the server side, <a href="#net-peek">net-peek</a> or <a href="#net-select">net-select</a>
can be used for non-blocking communications.  In the above example, the server would block
until a datagram is received.</p>

<p>The address <tt>226.0.0.1</tt> is just one multicast address 
in the Class D range of multicast addresses from <tt>224.0.0.0</tt> 
to <tt>239.255.255.255</tt>.</p>

<p>
The <a href="#net-send">net-send</a> and 
<a href="#net-receive">net-receive</a> functions
can also be used instead of <a href="#net-send-to">net-send-to</a> 
and <a href="#net-receive-from">net-receive-from</a>.
</p>


<h3>UDP broadcast communications</h3>

<p>
Specifying the string <tt>"broadcast"</tt> or <tt>"b"</tt> 
in the third parameter, <em>str-mode</em>, causes
UDP broadcast communications to be set up. 
In this case, the broadcast address 
ending in 255 is used.
</p>

<!-- example -->

<pre>
;; example client
(net-connect "192.168.2.255" 3000 "broadcast")  <span class='arw'>&rarr;</span> 3
(net-send 3 "hello")

;; example server
(net-listen 3000 "" "udp")  <span class='arw'>&rarr;</span> 5

(net-receive 5 buff 10)
buff  <span class='arw'>&rarr;</span> "hello"
;; or
(net-receive-from 5 10)
<span class='arw'>&rarr;</span> ("hello" "192.168.2.1" 46620)
</pre>


<p>
Note that on the receiving side, 
<a href="#net-listen">net-listen</a> should be used 
with the default address 
specified with an <tt>""</tt> (empty string). 
Broadcasts will not be received 
when specifying an address. 
As with all UDP communications, 
<a href="#net-listen">net-listen</a> does not actually put 
the receiving side in listen mode, 
but rather sets up the sockets 
for the specific UDP mode.
</p>

<p>
The <a href="#net-select">net-select</a> 
or <a href="#net-peek">net-peek</a> functions 
can be used to check for 
incoming communications 
in a non-blocking fashion.
</p>

<h3>Local domain Unix sockets</h3>

<p>In the third syntax, <tt>net-connect</tt> connects to a server on the 
local file system via a <em>local domain Unix socket</em> named using 
<em>str-file-path</em>.	Returns a socket handle after having connected 
successfully; otherwise, returns <tt>nil</tt>.
</p>

<!-- example -->

<pre>
(net-connect "/tmp/mysocket")  <span class='arw'>&rarr;</span> 3

; on OS/2 use "\\socket\\" prefix

(net-connect "\\socket\\mysocket")
</pre>


<p>A <em>local domain</em> file system socket is created and returned.
On the server side, <em>local domain</em> sockets have been created
using <a href="#net-listen">net-listen</a> and <a href="#net-accept">net-accept</a>.
After the connection has been established the functions <a href="#net-select">net-select</a>,
<a href="#net-send">net-send</a> and <a href="#net-receive">net-receive</a> can be used
as usual for TCP/IP stream communications. This type of connection can be used as a fast
bi-directional communications channel between processes on the same file system.
This type of connection is not available on MS Windows platforms.</p>

<br/><br/>

<a name="net-error"></a>
<h2><span class="function">net-error</span></h2>
<h4>syntax: (net-error)<br/>
syntax: (net-error <em>int-error</em>)</h4>

<p>Retrieves the last error that occurred when calling a any of the
following functions: <a href="#net-accept">net-accept</a>,
<a href="#net-connect">net-connect</a>,
<a href="#net-eval">net-eval</a>,
<a href="#net-listen">net-listen</a>,
<a href="#net-lookup">net-lookup</a>,
<a href="#net-receive">net-receive</a>,
<a href="#net-receive-udp">net-receive-udp</a>,
<a href="#net-select">net-select</a>,
<a href="#net-send">net-send</a>,
<a href="#net-send-udp">net-send-udp</a>,
and <a href="#net-service">net-service</a>. 
Whenever one of these functions fails, it returns <tt>nil</tt> and <tt>net-error</tt>
can be used to retrieve more information.</p>

<p>Functions that communicate using sockets close the socket automatically and 
remove it from the <a href="#net-sessions">net-sessions</a> list.</p>

<p>Each successful termination of a <a href="#socket_tcpip">net-*</a> 
function clears the error number.</p>

<p>The following messages are returned:</p>

<table summary="net-error">
<tr align="left"><th>no</th><th>description</th></tr>
<tr><td>1</td><td>Cannot open socket</td></tr>
<tr><td>2</td><td>DNS resolution failed</td></tr>
<tr><td>3</td><td>Not a valid service</td></tr>
<tr><td>4</td><td>Connection failed</td></tr>
<tr><td>5</td><td>Accept failed</td></tr>
<tr><td>6</td><td>Connection closed</td></tr>
<tr><td>7</td><td>Connection broken</td></tr>
<tr><td>8</td><td>Socket send() failed</td></tr>
<tr><td>9</td><td>Socket recv() failed</td></tr>
<tr><td>10</td><td>Cannot bind socket</td></tr>
<tr><td>11</td><td>Too many sockets in net-select</td></tr>
<tr><td>12</td><td>Listen failed</td></tr>
<tr><td>13</td><td>Badly formed IP</td></tr>
<tr><td>14</td><td>Select failed</td></tr>
<tr><td>15</td><td>Peek failed</td></tr>
<tr><td>16</td><td>Not a valid socket</td></tr>
<tr><td>17</td><td>Cannot unblock socket</td></tr>
<tr><td>18</td><td>Operation timed out</td></tr>
<tr><td>19</td><td>HTTP bad formed URL</td></tr>
<tr><td>20</td><td>HTTP file operation failed</td></tr>
<tr><td>21</td><td>HTTP transfer failed</td></tr>
<tr><td>22</td><td>HTTP invalid response from server</td></tr>
<tr><td>23</td><td>HTTP no response from server</td></tr>
<tr><td>24</td><td>HTTP no content</td></tr>
<tr><td>25</td><td>HTTP error in header</td></tr>
<tr><td>26</td><td>HTTP error in chunked format</td></tr>
</table><br/>

<br/><br/>
<!-- example -->

<pre>
(net-error) <span class='arw'>&rarr;</span> nil

(net-connect "jhghjgkjhg" 80)  <span class='arw'>&rarr;</span>  nil

(net-error)  <span class='arw'>&rarr;</span>  (2 "ERR: "DNS resolution failed") 
</pre>

<p>When <em>int-error</em> is specified the number and error text for
that error number is returned.</p>


<pre>
(net-error 10) <span class='arw'>&rarr;</span> (10 "Cannot bind socket")
</pre>


<p>See also <a href="#last-error">last-error</a> and <a href="#sys-error">sys-error</a>.</p>

<br/><br/>

<a name="net-eval"></a>
<h2><span class="function">net-eval</span></h2> 
<h4>syntax: (net-eval <em>str-host</em> <em>int-port</em> <em>exp</em> [<em>int-timeout</em> [<em>func-handler</em>]])<br/>
syntax: (net-eval '((<em>str-host</em> <em>int-port</em> <em>exp</em>) ... )  [<em>int-timeout</em> [<em>func-handler</em>]])</h4>

<p>Can be used to evaluate source remotely on one or more newLISP servers. 
This function handles all communications necessary to connect to the remote servers, 
send source for evaluation, and wait and collect responses.</p>

<p>The expression in <em>exp</em> will be evaluated remotely in the environment 
of the target node. The <em>exp</em> is either a quoted expression, or it is 
enclosed in string delimiters. For bigger expressions <tt>[text] ... [/text]</tt> 
delimiters can be used instead of double quotes <tt>" ... "</tt>.  Only one 
expression should be enclosed in the string. When more than one are specified,
all will get evaluated in the target node, but only the result of the first 
will be returned.</p>

<p>The remote TCP/IP servers are started in the following way:</p>


<pre>
newlisp -c -d 4711 &amp;

; preloading function definitions

newlisp preload.lsp -c -d 12345 &amp;

; logging connections

newlisp -l -c -d 4711 &amp;

; communicating via Unix local domain sockets

newlisp -c /tmp/mysocket
</pre>

<p>The <tt>-c</tt> option is necessary to suppress newLISP emitting
prompts.</p>

<p>The <tt>-d</tt> daemon mode allows newLISP to maintain state between 
connections.  When keeping state between connections is not desired, 
the <a href="#inetd_daemon">inetd daemon mode</a> offers more advantages. 
The Internet <tt>inetd</tt> or <tt>xinetd</tt> services daemon 
will start a new newLISP process for each client connection. 
This makes for much faster servicing of multiple connections. 
In <tt>-d</tt> daemon mode, each new client request 
would have to wait for the previous request to be finished. 
See the chapter <a href="#inetd_daemon">inetd daemon mode</a> 
on how to configure this mode correctly.</p>

<p>Instead of <tt>4711</tt>, any other port number can be used. 
Multiple nodes can be started on different hosts and with the same 
or different port numbers.  The <tt>-l</tt> or <tt>-L</tt> logging options
can be specified to log connections and remote commands.</p>


<p>In the first syntax, <tt>net-eval</tt> talks to only one 
remote newLISP server node, sending the host in <em>str-host</em>
on port <em>int-port</em> a request to evaluate the expression 
<em>exp</em>.  If <em>int-timeout</em> is not given, 
<tt>net-eval</tt> will wait up to 60 seconds for a response
after a connection is made.
Otherwise, if the timeout in milliseconds has expired, 
<tt>nil</tt> is returned; else, the evaluation result of <em>exp</em> 
is returned.</p>

<!-- example -->

<pre>
; the code to be evaluated is given in a quoted expression
(net-eval "192.168.1.94" 4711 '(+ 3 4))       <span class='arw'>&rarr;</span> 7

; expression as a string (only one expression should be in the string)
(net-eval "192.168.1.94" 4711 "(+ 3 4)")      <span class='arw'>&rarr;</span> 7

; with timeout
(net-eval "192.168.1.94" 4711 '(+ 3 4) 1)     <span class='arw'>&rarr;</span> nil  ; 1ms timeout too short
(net-error)                                   <span class='arw'>&rarr;</span> (17 "ERR: Operation timed out")

(net-eval "192.168.1.94" 4711 '(+ 3 4) 1000)  <span class='arw'>&rarr;</span> 7

; program contained in a variable
(set 'prog '(+ 3 4))
(net-eval "192.168.1.94" 4711 prog)           <span class='arw'>&rarr;</span> 7

; specify a local-domain Unix socket (not available on MS Windows)
(net-eval "/tmp/mysocket" 0 '(+ 3 4))         <span class='arw'>&rarr;</span> 7
</pre>


<p>The second syntax of <tt>net-eval</tt> returns a list of the results
after all of the responses are collected or timeout occurs. Responses that 
time out return <tt>nil</tt>.  The last example line shows how to specify
a local-domain Unix socket specifying the socket path and a port number of 
<tt>0</tt>.  Connection errors or errors that occur when sending information 
to nodes are returned as a list of error numbers and descriptive error 
strings.  See the function <a href="#net-error">net-error</a> for a list of 
potential error messages.</p>

<!-- example -->

<pre>
; two different remote nodes different IPs
(net-eval '(
    ("192.168.1.94" 4711 '(+ 3 4)) 
    ("192.168.1.95" 4711 '(+ 5 6))
    ) 5000)
<span class='arw'>&rarr;</span> (7 11)

; two persistent nodes on the same CPU different ports
(net-eval '(
    ("localhost" 8081 '(foo "abc")) 
    ("localhost" 8082 '(myfunc 123)') 
    ) 3000)

; inetd or xinetd nodes on the same server and port
; nodes are loaded on demand
(net-eval '(
    ("localhost" 2000 '(foo "abc")) 
    ("localhost" 2000 '(myfunc 123))
    ) 3000)
</pre>


<p>The first example shows two expressions evaluated on two different remote 
nodes. In the second example, both nodes run on the local computer. This may 
be useful when debugging or taking advantage of multiple CPUs on the same 
computer.  When specifying <tt>0</tt> for the port number , <tt>net-eval</tt> 
takes the host name as the file path to the local-domain Unix socket.</p>

<p>Note that definitions of <tt>foo</tt> and <tt>myfunc</tt> must both
exist in the target environment. This can be done using a <tt>net-eval</tt>
sending <tt>define</tt> statements before. It also can be done by
preloading code when starting remote nodes.</p>

<p>When nodes are inetd or xinetd-controlled, several nodes may have the 
same IP address and port number. In this case, the Unix 
daemon inetd or xinetd will start multiple newLISP servers on demand. 
This is useful when testing distributed programs on just one machine. 
The last example illustrates this case. It is also useful on multi core
CPUs, where the platform OS can distribute different processes on to different
CPU cores.</p>

<p>The source sent for evaluation can consist of entire multiline programs. 
This way, remote nodes can be loaded with programs first, then specific 
functions can be called.  For large program files, the functions 
<a href="#put-url">put-url</a> or <a href="#save">save</a> (with a URL 
file name) can be used to transfer programs. The a <tt>net-eval</tt>
statement could load these programs.</p>

<p>Optionally, a handler function can be specified. This function will be 
repeatedly called while waiting and once for every remote evaluation completion.</p>

<!-- example -->

<pre>
(define (myhandler param)
    (if param
        (println param))
)

(set 'Nodes '(
    ("192.168.1.94" 4711)
    ("192.168.1.95" 4711)
))

(set 'Progs '(
    (+ 3 4)
    (+ 5 6)
))

(net-eval (map (fn (n p) (list (n 0) (n 1) p)) Nodes Progs) 5000 myhandler)
<span class='arw'>&rarr;</span>
("192.168.1.94" 4711 7)
("192.168.1.95" 4711 11)
</pre>

<p>The example shows how the list of node specs can be assembled from a list 
of nodes and sources to evaluate. This may be useful when connecting to a 
larger number of remote nodes.</p>

<pre>
(net-eval (list 
  (list (Nodes 0 0) (Nodes 0 1) (Progs 0)) 
  (list (Nodes 1 0) (Nodes 1 1) (Progs 1)) 
 ) 3000 myhandler)
</pre>

<p>While waiting for input from remote hosts, <tt>myhandler</tt> will be called 
with <tt>nil</tt> as the argument to <tt>param</tt>.  When a remote node result 
is completely received, <tt>myhandler</tt> will be called with <tt>param</tt> 
set to a list containing the remote host name or IP number, the port, and the 
resulting expression. <tt>net-eval</tt> will return <tt>true</tt> before a 
timeout or <tt>nil</tt> if the timeout was reached or exceeded.  All remote hosts 
that exceeded the timeout limit will contain a <tt>nil</tt> in their results list.
</p>

<p>For a longer example see this program:
<a href="http://www.newlisp.org/syntax.cgi?code/mapreduce.txt">mapreduce</a>.
The example shows how a word counting task gets distributed to three remote
nodes. The three nodes count words in different texts and the master node
receives and consolidates the results.</p>

<br/><br/>

<a name="net-interface"></a>
<h2><span class="function">net-interface</span></h2>
<h4>syntax: (net-interface <em>str-ip-addr</em>)<br/>
syntax: (net-interface)</h4>

<p>Sets the default local interface address to be used for network connections.
If not set then network functions will default to an internal default address,
except when overwritten by an optional interface address given in 
<a href="#net-listen">net-listen</a>.</p>

<p>When no <em>str-ip-addr</em> is specified, the current default is returned.
If the <tt>net-interface</tt> has not been used yet to specify an IP address,
the address <tt>0.0.0.0</tt> is returned. This means that all network routines
will use the default address preconfigured by the underlying operating system.</p>

<p>This function has only usage on multihomed servers with either multiple network 
interface hardware or otherwise supplied multiple IP numbers. On all other machines 
network functions will automatically select the single network interface installed.</p>

<p>On error the function returns <tt>nil</tt> and <a href="#net-error">net-error</a>
can be used to report the error.</p>

<!-- example -->

<pre>
(net-interface "192.168.1.95")  <span class='arw'>&rarr;</span> "192.168.1.95"
(net-interface "localhost")     <span class='arw'>&rarr;</span> "127.0.0.1"
</pre>


<p>An interface address can be defined as either an IP address or a name. The
return value is the address given in <em>str-ip-addr</em></p>

<br/><br/>

<a name="net-ipv"></a>
<h2><span class="function">net-ipv</span></h2>
<h4>syntax: (net-ipv <em>int-version</em>)<br/>
syntax: (net-ipv)</h4>

<p>Switches between IPv4 and IPv6 internet protocol versions. 
<em>int-version</em> contains either a 4 for IPv4 or a 6 for IPv6. When no 
parameter is given, <tt>net-ipv</tt> returns the current setting.</p>

<!-- example -->

<pre>
(net-ipv)      <span class='arw'>&rarr;</span> 4
(net-ipv 6)    <span class='arw'>&rarr;</span> 6
</pre>

<p>By default newLISP starts up in IPv4 mode. The IPv6 protocol mode can also 
be specified from the commandline when starting newlisp:</p>

<pre>
newlisp -6
</pre>

<p>Once a socket is connected with either 
<a href="#net-connect">net-connect</a> 
or listened on with 
<a href="#net-listen">net-listen</a>, the 
<a href="#net-accept">net-accept</a>, <a href="#net-select">net-select</a>, 
<a href="#net-send">net-send</a>, <a href="#net-receive">net-receive</a> and 
<a href="#net-receive-from">net-receive-from</a> 
functions automatically adjust to the address protocol used when creating the sockets.

Different connections with different IPv4/6 settings can be open at the same time.</p>

<p>Note, that currently <a href="#net-packet">net-packet</a> does not support IPv6 and will 
work in IPv4 mode regardless of settings.</p>

<br/><br/>

<a name="net-listen"></a>
<h2><span class="function">net-listen</span></h2>
<h4>syntax: (net-listen <em>int-port</em> [<em>str-ip-addr</em> [<em>str-mode</em>]])<br/>
syntax: (net-listen <em>str-file-path</em>)</h4>

<p>Listens on a port specified in <em>int-port</em>.  A call to <tt>net-listen</tt>
returns immediately with a socket number, which is then used by 
the blocking <a href="#net-accept">net-accept</a> function 
to wait for a connection.  As soon as a connection is accepted, 
<a href="#net-accept">net-accept</a> returns a socket number 
that can be used to communicate with the connecting client.</p>


<!-- example -->

<pre>
(set 'port 1234)
(set 'listen (net-listen port))
(unless listen (begin
    (print "listening failed\n")
    (exit)))

(print "Waiting for connection on: " port "\n")

(set 'connection (net-accept listen))
(if connection
    (while (net-receive connection buff 1024 "\n")
        (print buff)
        (if (= buff "\r\n") (exit)))
    (print "Could not connect\n"))
</pre>


<p>The example waits for a connection on port 1234, then reads incoming lines 
until an empty line is received.  Note that listening on ports lower than 1024
may require superuser access on Unix systems.</p>

<p>On computers with more than one interface card, specifying an optional 
interface IP address or name in <em>str-ip-addr</em> directs <tt>net-listen</tt> 
to listen on the specified address.</p>


<pre>
;; listen on a specific address
(net-listen port "192.168.1.54") 
</pre>


<h3>Local domain Unix sockets</h3>

<p>In the second syntax, <tt>net-listen</tt> listens for a client on the 
local file system via a <em>local domain Unix socket</em> named using 
<em>str-file-path</em>.	If successful, returns a socket handle that can be 
used with <a href="#net-accept">net-accept</a> to accept a client connection; 
otherwise, returns <tt>nil</tt>.</p>

<!-- example -->

<pre>
(net-listen "/tmp/mysocket")  <span class='arw'>&rarr;</span> 5

; on OS/2 use "\\socket\\" prefix

(net-listen "\\socket\\mysocket")

(net-accept 5)
</pre>


<p>A <em>local domain</em> file system socket is created and listened on.
A client will try to connect using the same <em>str-file-path</em>.
After a connection has been accepted the functions <a href="#net-select">net-select</a>,
<a href="#net-send">net-send</a> and <a href="#net-receive">net-receive</a> can be used 
as usual for TCP/IP stream communications. This type of connection can be used as a fast
 bi-directional communications channel between processes on the same file system.
This type of connection is not available on MS Windows platforms.</p>


<h3>UDP communications</h3>

<p>
As a third parameter, 
the optional string <tt>"udp"</tt> or <tt>"u"</tt> 
can be specified in <em>str-mode</em> 
to create a socket suited for UDP 
(User Datagram Protocol) communications.
A socket created in this way 
can be used directly with 
<a href="#net-receive-from">net-receive-from</a>
to await incoming UDP data 
<em>without</em> using <tt>net-accept</tt>, 
which is only used in TCP communications. 
The <a href="#net-receive-from">net-receive-from</a> call 
will block until a UDP data packet is received. 
Alternatively, <a href="#net-select">net-select</a> 
or <a href="#net-peek">net-peek</a> can be used 
to check for ready data in a non-blocking fashion. 
To send data back to the address and port received 
with <a href="#net-receive-from">net-receive-from</a>, 
use <a href="#net-send-to">net-send-to</a>.
</p>

<p>
Note that <a href="#net-peer">net-peer</a> will not work, 
as UDP communications do not maintain 
a connected socket with address information.
</p>


<pre>
(net-listen 10002 "192.168.1.120" "udp") 

(net-listen 10002 "" "udp") 
</pre>


<p>
The first example listens on a specific network adapter, 
while the second example listens on the default adapter. 
Both calls return a socket number 
that can be used in subsequent <a href="#net-receive">net-receive</a>,
<a href="#net-receive-from">net-receive-from</a>, 
<a href="#net-send-to">net-send-to</a>,
<a href="#net-select">net-select</a>, 
or <a href="#net-peek">net-peek</a> function calls.
</p>

<p>
Both a UDP server <em>and</em> UDP client 
can be set up using <tt>net-listen</tt> 
with the <tt>"udp"</tt> option. 
In this mode, <tt>net-listen</tt> 
does not really <em>listen</em>
as in TCP/IP communications; 
it just binds the socket 
to the local interface address and port.
</p>

<p>
For a working example, see the files 
<tt>examples/client</tt> and <tt>examples/server</tt>
in the newLISP source distribution.
</p>

<p>
Instead of <tt>net-listen</tt> 
and the <tt>"udp"</tt> option,
the functions <a href="#net-receive-udp">net-receive-udp</a>  
and <a href="#net-send-udp">net-send-udp</a>
can be used for short transactions 
consisting only of one data packet.
</p>

<p>
<tt>net-listen</tt>, <a href="#net-select">net-select</a>, 
and <a href="#net-peek">net-peek</a> can be used 
to facilitate non-blocking reading. 
The listening/reading socket is not closed 
but is used again for subsequent reads. 
In contrast, when the 
<a href="#net-receive-udp">net-receive-udp</a> 
and <a href="#net-send-udp">net-send-udp</a> pair is used, 
both sides close the sockets after sending and receiving.
</p>


<h3>UDP multicast communications</h3>

<p>
If the optional string <em>str-mode</em> is specified as 
<tt>"multi"</tt> or <tt>"m"</tt>, 
<tt>net-listen</tt> returns a socket suitable for multicasting. 
In this case, <em>str-ip-addr</em> contains one 
of the multicast addresses in the range <tt>224.0.0.0</tt> 
to <tt>239.255.255.255</tt>.
<tt>net-listen</tt> will register <em>str-ip-addr</em> 
as an address on which to receive multicast transmissions. 
This address should not be confused with the IP address 
of the server host.
</p>


<!-- example -->

<pre>
;; example client

(net-connect "226.0.0.1" 4096 "multi")  <span class='arw'>&rarr;</span> 3

(net-send-to "226.0.0.1" 4096 "hello" 3)


;; example server

(net-listen 4096 "226.0.0.1" "multi")  <span class='arw'>&rarr;</span> 5

(net-receive-from 5 20)               
<span class='arw'>&rarr;</span> ("hello" "192.168.1.94" 32769)
</pre>


<p>On the server side, 
<a href="#net-peek">net-peek</a> or <a href="#net-select">net-select</a>
can be used for non-blocking communications.  In the example above, 
the server would block until a datagram is received.</p>

<p>
The <a href="#net-send">net-send</a> 
and <a href="#net-receive">net-receive</a> functions 
can be used instead of <a href="#net-send-to">net-send-to</a> 
and <a href="#net-receive-from">net-receive-from</a>.
</p>


<h3>Packet divert sockets and ports</h3>

<p>If <em>str-mode</em> is specified as <tt>"divert"</tt> or <tt>"d"</tt>, 
a divert socket can be created for a divert port in <em>int-port</em> on 
BSD like platforms.  The content of IP address in <em>str-ip-addr</em> is 
ignored and can be specified as an empty string. Only the <em>int-port</em> 
is relevant and will be bound to the raw socket returned.</p>

<p>To use the divert option in <tt>net-listen</tt>, newLISP must run in
super-user mode. This option is only available on Unix like platforms.</p>

<p>The divert socket will receive all raw packets diverted 
to the divert port. Packets may also be written back to a divert socket, 
in which case they re-enter OS kernel IP packet processing.</p>

<p>Rules for packet diversion to the divert port must be defined using 
either the <em>ipfw</em> BSD or <em>ipchains</em> Linux configuration
utilities.</p>

<p>The <a href="#net-receive-from">net-receive-from</a> and
<a href="#net-send-to">net-send-to</a> functions are used to read
and write raw packets on the divert socket created and returned by the
<tt>net-listen</tt> statement. The same address received by
<a href="#net-receive-from">net-receive-from</a> is used in the 
<a href="#net-send-to">net-send-to</a> call when re-injecting the 
packet:</p>

<pre>
; rules have been previously configured for a divert port
(set 'divertSocket (net-listen divertPort "" "divert"))

(until (net-error)
    (set 'rlist (net-receive-from divertSocket maxBytes))
    (set 'buffer (rlist 1))
    ; buffer can be processed here before reinjecting
    (net-send-to (rlist 0) divertPort buffer divertSocket)
)
</pre>

<p>For more information see the Unix man pages for <em>divert</em>
and the <em>ipfw</em> (BSDs) or <em>ipchains</em> (Linux) configuration 
utilities.</p>

<br/><br/>

<a name="net-local"></a>
<h2><span class="function">net-local</span></h2>
<h4>syntax: (net-local <em>int-socket</em>)</h4>

<p>Returns the IP number and port of the local computer 
for a connection on a specific <em>int-socket</em>.</p>

<!-- example -->

<pre>
(net-local 16)  <span class='arw'>&rarr;</span> ("204.179.131.73" 1689)
</pre>

<p>
Use the <a href="#net-peer">net-peer</a> 
function to access the remote computer's 
IP number and port.
</p>

<br/><br/>

<a name="net-lookup"></a>
<h2><span class="function">net-lookup</span></h2>

<h4>syntax: (net-lookup <em>str-ip-number</em>)<br/>
syntax: (net-lookup <em>str-hostname</em> [<em>bool</em>])</h4>


<p>
Returns either a hostname string 
from <em>str-ip-number</em> 
in IP dot format or the IP number 
in dot format from <em>str-hostname</em>:
</p>

<!-- example -->

<pre>
(net-lookup "209.24.120.224")    <span class='arw'>&rarr;</span> "www.nuevatec.com"
(net-lookup "www.nuevatec.com")  <span class='arw'>&rarr;</span> "209.24.120.224"

(net-lookup "216.16.84.66.sbl-xbl.spamhaus.org" true)
<span class='arw'>&rarr;</span> "216.16.84.66"
</pre>


<p>
Optionally,	a <em>bool</em> flag 
can be specified in the second syntax. 
If the expression in <em>bool</em> 
evaluates to anything other than <tt>nil</tt>,
host-by-name lookup will be forced, 
even if the name string starts 
with an IP number.
</p>

<br/><br/>

<a name="net-packet"></a>
<h2><span class="function">net-packet</span></h2>
<h4>syntax: (net-packet <em>str-packet</em>)</h4>

<p>The function allows custom configured network packets to be sent via
a <em>raw sockets</em> interface. The packet in <em>str-packet</em> must
start with an IP (Internet Protocol) header followed by either
a TCP, UDP or ICMP header and optional data. newLISP must be run with
super user privileges, and this function is only available on macOS,
Linux and other Unix operating systems and only for IPv4.
Currently <tt>net-packet</tt> is IPv4 only and has been tested on
macOS, Linux and OpenBSD.</p> 

<p>On success the function returns the number of bytes sent. On failure
the function returns <tt>nil</tt> and both, <a href="#net-error">net-error</a>
and <a href="#sys-error">sys-error</a>, should be inspected.</p>

<p>When custom configured packets contain zeros in the checksum fields,
<tt>net-packet</tt> will calculate and insert the correct checksums. 
Already existing checksums stay untouched.</p>

<p>The following example injects a UDP packet for IP number <tt>192.168.1.92</tt>.
The IP header consists of 20 bytes ending with the target IP number. The following
UDP header has a length of 8 bytes and is followed by the data string
<tt>Hello World</tt>. The checksum bytes in both headers are left as 
<tt>0x00 0x00</tt> and will be recalculated internally.</p>

<!-- example -->

<pre>
; packet as generated by: (net-send-udp "192.168.1.92" 12345 "Hello World")

(set 'udp-packet (pack (dup "b" 39) '(
    0x45 0x00 0x00 0x27 0x4b 0x8f 0x00 0x00 0x40 0x11 0x00 0x00 192  168  1    95
    192  168  1    92   0xf2 0xc8 0x30 0x39 0x00 0x13 0x00 0x00 0x48 0x65 0x6c 0x6c
    0x6f 0x20 0x57 0x6f 0x72 0x6c 0x64)))

(unless (net-packet udp-packet)
    (println "net-error: " (net-error))
    (println "sys-error: " (sys-error)))
</pre>

<p>The <tt>net-packet</tt> function is used when testing net security. 
Its wrong application can upset the correct functioning of network routers and 
other devices connected to a network. For this reason the function should only 
be used on well isolated, private intra-nets and only by network professionals.</p>

<p>For other examples of packet configuration, see the file 
<tt>qa-specific-tests/qa-packet</tt> in the newLISP source distribution.</p>

<br/><br/>

<a name="net-peek"></a>
<h2><span class="function">net-peek</span></h2>
<h4>syntax: (net-peek <em>int-socket</em>)</h4>

<p>
Returns the number of bytes 
ready for reading 
on the network socket <em>int-socket</em>.
If an error occurs 
or the connection is closed,
<tt>nil</tt> is returned.
</p>

<!-- example -->

<pre>
(set 'aSock (net-connect "aserver.com" 123))

(while ( = (net-peek aSock) 0) 
    (do-something-else))

(net-receive aSock buff 1024)
</pre>


<p>
After connecting, the program 
waits in a while loop 
until <tt>aSock</tt> can be read.
</p>

<p>
Use the <a href="#peek">peek</a> function
to check file descriptors and <tt>stdin</tt>.
</p>

<br/><br/>

<a name="net-peer"></a>
<h2><span class="function">net-peer</span></h2>
<h4>syntax: (net-peer <em>int-socket</em>)</h4>

<p>
Returns the IP number and port number 
of the remote computer 
for a connection on <em>int-socket</em>.
</p>

<!-- example -->

<pre>
(net-peer 16)  <span class='arw'>&rarr;</span> ("192.100.81.100" 13)
</pre>


<p>
Use the <a href="#net-local">net-local</a> function
to access the local computer's IP number and port number.
</p>

<br/><br/>

<a name="net-ping"></a>

<h2><span class="function">net-ping</span></h2>
<h4>syntax: (net-ping <em>str-address</em> [<em>int-timeout</em> [<em>int-count</em> <em>bool</em>]]])<br/>
syntax: (net-ping <em>list-addresses</em> [<em>int-timeout</em> [<em>int-count</em> <em>bool</em>]]])</h4>

<p> This function is only available on Unix-based systems 
and must be run in superuser mode, i.e. using: <tt>sudo newlisp</tt> to
start newLISP on macOS or other BSD's, or as the root user on Linux.
 Broadcast mode and specifying ranges with the <tt>-</tt> (hyphen) or
<em>*</em> (star) are not available on IPv6 address mode.</p>

<p>Superuser mode is not required on macOS.</p>

<p> In the first syntax, <tt>net-ping</tt> sends a ping 
ICMP 64-byte echo request to the address specified in <em>str-address</em>. 
If it is a broadcast address, the ICMP packet will be received 
by all addresses on the subnet.  Note that for security reasons, 
many computers do not answer ICMP broadcast ping (ICMP_ECHO) requests. 
An optional timeout parameter can be specified in <em>int-timeout</em>.
If no timeout is specified, a waiting time of 1000 milliseconds 
(one second) is assumed.</p> 

<p> <tt>net-ping</tt> returns either a list of lists of IP strings 
and round-trip time in microseconds for which a response was received 
or an empty list if no response was received.</p>

<p>
A return value of <tt>nil</tt> 
indicates a failure. 
Use the <a href="#net-error">net-error</a> function 
to retrieve the error message.  If the message reads <tt>Cannot open socket</tt>, 
it is probably because newLISP is running without root permissions. 
newLISP can be started using:
</p>

<pre>
sudo newlisp
</pre>

<p>
Alternatively, newLISP can be installed 
with the set-user-ID bit set to run 
in superuser mode.
</p>

<!-- example -->

<pre>
(net-ping "newlisp.org")     <span class='arw'>&rarr;</span> (("66.235.209.72" 634080))
(net-ping "127.0.0.1")       <span class='arw'>&rarr;</span> (("127.0.0.1" 115))
(net-ping "yahoo.com" 3000)  <span class='arw'>&rarr;</span> nil
</pre>


<p>
In the second syntax, <tt>net-ping</tt> is run in <em>batch mode</em>. 
Only one socket is opened in this mode, but multiple ICMP packets are sent out&mdash;one 
each to multiple addresses specified in a list or specified by range.
Packets are sent out as fast as possible.  In this case, multiple answers can be received.
If the same address is specified multiple times, the receiving IP address will be flooded
with ICMP packets.</p>

<p>
To limit the number of responses to be waited for in broadcast or batch mode,
an additional argument indicating the maximum number of responses to receive
can be specified in <em>int-count</em>.  Usage of this parameter can cause 
the function to return sooner than the specified timeout. 
When a given number of responses has been received, <tt>net-ping</tt> will return  
before the timeout has occurred. Not specifying <em>int-count</em> or specifying <tt>0</tt>
assumes an <em>int-count</em> equal to the number of packets sent out.</p>

<p>As third optional parameter, a <tt>true</tt> value can be specified. This setting will
return an error string instead of the response time, if the host does not answer.</p>

<!-- example -->

<pre>
(net-ping '("newlisp.org" "192.168.1.255") 2000 20)
<span class='arw'>&rarr;</span> (("66.235.209.72" 826420) ("192.168.1.1" 124) ("192.168.1.254" 210))

(net-ping "192.168.1.*" 500) ; from 1 to 254
<span class='arw'>&rarr;</span> (("192.168.1.1" 120) ("192.168.1.2" 245) ("192.168.2.3" 180) ("192.168.2.254" 234))

(net-ping "192.168.1.*" 500 2) ; returns after 2 responses
<span class='arw'>&rarr;</span> (("192.168.1.3" 115) ("192.168.1.1" 145))

(net-ping "192.168.1.1-10" 1000) ; returns after 1 second
<span class='arw'>&rarr;</span> (("192.168.1.3" 196) ("192.168.1.1" 205))

(net-ping '("192.168.1.100-120" "192.168.1.124-132") 2000) ; returns after 2 seconds
<span class='arw'>&rarr;</span> ()
</pre>



<p>
Broadcast or batch mode&mdash;as well as normal addresses 
and IP numbers or hostnames&mdash; can be mixed in one <tt>net-ping</tt> statement by 
putting all of the IP specs into a list.</p> 

<p>
The second and third lines show how the batch mode of <tt>net-ping</tt> 
can be initiated by specifying the <tt>*</tt> (asterisk) 
as a wildcard character for the last subnet octet
in the IP number. The fourth and fifth lines show how an IP
range can be specified for the last subnet octet in the IP number.
<tt>net-ping</tt> will iterate through all numbers 
from either 1 to 254 for the star <tt>*</tt> or the range specified, 
sending an ICMP packet to each address. 
Note that this is different from the <em>broadcast</em> mode 
specified with an IP octet of <tt>255</tt>. 
While in broadcast mode, <tt>net-ping</tt> sends out only one packet, 
which is received by multiple addresses.  Batch mode explicitly generates 
multiple packets, one for each target address. When specifying broadcast
mode, <em>int-count</em> should be specified, too.</p>

<p>
When sending larger lists of IPs in batch mode over one socket, 
a longer timeout may be necessary to allow enough time for all of the packets 
to be sent out over one socket.  If the timeout is too short, 
the function <tt>net-ping</tt> may return an incomplete list or the empty list <tt>()</tt>.
In this case, <a href="#net-error">net-error</a> will return a timeout error.
On error, <tt>nil</tt> is returned and  <a href="#net-error">net-error</a>
can be used to retrieve an error message.</p>

<p>
On some systems only lists up to a specific length can be handled 
regardless of the timeout specified. In this case, the range should
 be broken up into sub-ranges and used with multiple <tt>net-ping</tt>
invocations. In any case, <tt>net-ping</tt> will send out packages 
as quickly as possible.
</p>

<br/><br/>

<a name="net-receive"></a>
<h2><span class="function">net-receive</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (net-receive <em>int-socket</em> <em>sym-buffer</em> <em>int-max-bytes</em> [<em>wait-string</em>])</h4>

<p>
Receives data on the socket <em>int-socket</em> into a string contained in <em>sym-buffer</em>. 
<em>sym-buffer</em> can also be a default functor specified by a context symbol 
for reference passing in and out of user-defined functions.</p>

<p> A maximum of 
<em>int-max-bytes</em> is received. <tt>net-receive</tt> returns the number of 
bytes read.  If there is a break in the connection, <tt>nil</tt> is returned. 
The space reserved in <em>sym-buffer</em> is exactly the size of bytes read.</p>

<p>
Note that <tt>net-receive</tt> is a blocking call 
and does not return until the data arrives at <em>int-socket</em>. 
Use <a href="#net-peek">net-peek</a> 
or <a href="#net-select">net-select</a> to find out 
if a socket is ready for reading.
</p>

<p>
Optionally, a <em>wait-string</em> 
can be specified 
as a fourth parameter. 
<tt>net-receive</tt> then returns after 
a character or string of characters 
matching <em>wait-string</em>
is received. 
The <em>wait-string</em> will be part 
of the data contained in <em>sym-buffer</em>.
</p>

<!-- example -->

<pre>
(define (gettime)
    (set 'socket (net-connect "netcom.com" 13))
    (net-receive socket buf 256)
    (print buf "\n")
    (net-close socket))
</pre>


<p>
When calling <tt>gettime</tt>, 
the program connects to port 13 
of the server netcom.com. 
Port 13 is a date-time service 
on most server installations. 
Upon connection, the server sends 
a string containing the date and time of day.
</p>


<pre>
(define (net-receive-line socket sBuff)
    (net-receive socket sBuff 256 "\n"))

(set 'bytesReceived (net-receive-line socket 'sm))
</pre>


<p>
The second example defines a new function 
<tt>net-receive-line</tt>, 
which returns after receiving a newline character 
(a string containing one character in this example) 
or 256 characters. 
The "\n" string is part of the contents of sBuff.
</p>

<p>
Note that when the fourth parameter is specified, 
<tt>net-receive</tt> is slower than the normal version 
because information is read character-by-character. 
In most situations, the speed difference can be neglected.
</p>

<br/><br/>

<a name="net-receive-from"></a>
<h2><span class="function">net-receive-from</span></h2>
<h4>syntax: (net-receive-from <em>int-socket</em> <em>int-max-size</em>)</h4>

<p>
<tt>net-receive-from</tt> can be used to set up 
non-blocking UDP communications. 
The socket in <em>int-socket</em> 
must previously have been opened 
by either <a href="#net-listen">net-listen</a> 
or <a href="#net-connect">net-connect</a> 
(both using the <tt>"udp"</tt> option).
<em>int-max-size</em> specifies 
the maximum number of bytes that will be received. 
On Linux/BSD, if more bytes are received, 
those will be discarded; on MS Windows, <tt>net-receive-from</tt>
returns <tt>nil</tt> and closes the socket.
</p>

<p>On success <tt>net-receive</tt> returns a list of the data string, remote 
IP number and remote port used. On failure it returns <tt>nil</tt>.</p>

<!-- example -->

<pre>
;; bind port 1001 and the default address
(net-listen 1001 "" "udp")  <span class='arw'>&rarr;</span> 1980 

;; optionally poll for arriving data with 100ms timeout
(while (not (net-select 1980 "r" 100000)) (do-something ... ))

(net-receive-from 1980 20)  <span class='arw'>&rarr;</span> ("hello" "192.168.0.5" 3240)

;; send answer back to sender
(net-send-to "192.168.0.5" 3240 "hello to you" 1980)

(net-close 1980) ; close socket
</pre>


<p>The second line in this example is optional.  Without it, the 
<tt>net-receive-from</tt> call would block until data arrives. 
A UDP server could be set up by listening and polling several ports, 
serving them as they receive data.</p>

<p>Both, the sender and the receiver have to issue 
<a href="#net-listen">net-listen</a> commands for UDP mode. Not for listening
as in TCP/IP protocol communications, but to create the socket bound to
the port and address. For a complete example see the files
<tt>udp-server.lsp</tt> and <tt>udp-client.lsp</tt> in the 
<tt>newlisp-x.x.x/examples/</tt> directory of the source distribution.</p>

<p>
Note that <tt>net-receive</tt> 
could not be used in this case 
because it does not return 
the sender's address and port information, 
which are required to talk back.
In UDP communications, 
the data packet itself 
contains the address of the sender,
<em>not</em> the socket over which 
communication takes place.
<tt>net-receive</tt> can also be used for TCP/IP communications.
</p>

<p>
See also the <a href="#net-connect">net-connect</a> function 
with the <tt>"udp"</tt> option and the 
<a href="#net-send-to">net-send-to</a> function 
for sending UDP data packets over open connections.
</p>

<p>
For blocking short UDP transactions, 
see the <a href="#net-send-udp">net-send-udp</a> 
and <a href="#net-receive-udp">net-receive-udp</a> functions.
</p>

<br/><br/>

<a name="net-receive-udp"></a>
<h2><span class="function">net-receive-udp</span></h2>
<h4>syntax: (net-receive-udp <em>int-port</em> <em>int-maxsize</em> [<em>int-microsec</em> [<em>str-addr-if</em>]])</h4>

<p>
Receives a User Datagram Protocol (UDP) packet on port <em>int-port</em>,
reading <em>int-maxsize</em> bytes. 
If more than <em>int-maxsize</em> bytes are received,
bytes over <em>int-maxsize</em> are discarded on Linux/BSD; 
on MS Windows, <tt>net-receive-udp</tt> returns <tt>nil</tt>.
<tt>net-receive-udp</tt> blocks until a datagram arrives 
or the optional timeout value in <em>int-microsec</em> expires. 
When setting up communications between datagram sender and receiver, 
the <tt>net-receive-udp</tt> statement must be set up first.
</p>


<p>
No previous setup using <tt>net-listen</tt> 
or <tt>net-connect</tt> is necessary.
</p>

<p>
<tt>net-receive-udp</tt> returns a list 
containing a string of the UDP packet 
followed by a string containing 
the sender's IP number and the port used.
</p>

<!-- example -->

<pre>
;; wait for datagram with maximum 20 bytes 
(net-receive-udp 10001 20) 

;; or
(net-receive-udp 10001 20 5000000)  ; wait for max 5 seconds
		  
;; executed on remote computer
(net-send-udp "nuevatec.com" 1001 "Hello")  <span class='arw'>&rarr;</span> 4 

;; returned from the net-receive-udp statement
<span class='arw'>&rarr;</span> ("Hello" "128.121.96.1" 3312)  

;; sending binary information
(net-send-udp "ahost.com" 2222 (pack "c c c c" 0 1 2 3))  
<span class='arw'>&rarr;</span> 4 

;; extracting the received info
(set 'buff (first (net-receive-udp 2222 10)))   

(print (unpack "c c c c" buff))  <span class='arw'>&rarr;</span> (0 1 2 3)
</pre>


<p>
See also the <a href="#net-send-udp">net-send-udp</a> 
function for sending datagrams and 
the <a href="#pack">pack</a> and <a href="#unpack">unpack</a> 
functions for packing and unpacking binary information.
</p>

<p>
To listen on a specified address 
on computers with more than one interface card, 
an interface IP address or name can be 
optionally specified in <em>str-addr-if</em>. 
When specifying <em>str-addr-if</em>, 
a timeout must also be specified
in <em>int-wait</em>.
</p>

<p>
As an alternative, UDP communication 
can be set up using <a href="#net-listen">net-listen</a>, 
or <a href="#net-connect">net-connect</a> 
together with the <tt>"udp"</tt> option 
to make non-blocking data exchange possible 
with <a href="#net-receive-from">net-receive-from</a> 
and <a href="#net-send-to">net-send-to</a>.
</p>

<br/><br/>

<a name="net-select"></a>
<h2><span class="function">net-select</span></h2>
<h4>syntax: (net-select <em>int-socket</em> <em>str-mode</em> <em>int-micro-seconds</em>)<br/>
syntax: (net-select <em>list-sockets</em> <em>str-mode</em> <em>int-micro-seconds</em>)</h4>

<p>
In the first form, 
<tt>net-select</tt> finds out about the status 
of one socket specified in  <em>int-socket</em>. 
Depending on <em>str-mode</em>, 
the socket can be checked 
if it is ready for reading or writing,
or if the socket has an error condition. 
A timeout value is specified in <em>int-micro-seconds</em>.
</p>

<p>
In the second syntax, 
<tt>net-select</tt> can check for a list of sockets 
in <em>list-sockets</em>.
</p>

<p>
The following value can be given for <em>str-mode</em>:
</p>


<tt>"read"</tt> or <tt>"r"</tt> to check if ready for reading or accepting.<br/>

<tt>"write"</tt> or <tt>"w"</tt> to check if ready for writing.<br/>
<tt>"exception"</tt> or <tt>"e"</tt> to check for an error condition.<br/>


<p>
Read, send, or accept operations 
can be handled without blocking
by using the <tt>net-select</tt> function. 
<tt>net-select</tt> waits 
for a socket to be ready 
for the value given in <em>int-micro-seconds</em>, 
then returns <tt>true</tt> or <tt>nil</tt>
depending on the readiness of the socket.
During the select loop, 
other portions of the program can run. 
On error, 
<a href="#net-error">net-error</a> is set. 
When <tt>-1</tt> is specified for <em>int-micro-seconds</em>, 
<tt>net-select</tt> will never time out.
</p>

<!-- example -->

<pre>
(set 'listen-socket (net-listen 1001))

;; wait for connection
(while (not (net-select listen-socket "read" 1000))
    (if (net-error) (print (net-error))))

(set 'connection (net-accept listen-socket))
(net-send connection "hello")

;; wait for incoming message
(while (not (net-select connection "read" 1000))
    (do-something)) 

(net-receive connection buff 1024)
</pre>


<p>
When <tt>net-select</tt> is used, 
several listen and connection sockets can be watched, 
and multiple connections can be handled. 
When used with a list of sockets, 
<tt>net-select</tt> will return a list of ready sockets. 
The following example would listen on two sockets 
and continue accepting and servicing connections:
</p>

<!-- example -->

<pre>
(set 'listen-list '(1001 1002))

; accept-connection, read-connection and write-connection
; are user defined functions

(while (not (net-error))
    (dolist (conn (net-select listen-list "r" 1000))
    (accept-connection conn))  ; build an accept-list

    (dolist (conn (net-select accept-list "r" 1000))
    (read-connection conn))    ; read on connected sockets

    (dolist (conn (net-select accept-list "w" 1000))
    (write-connection conn)))  ; write on connected sockets
</pre>


<p>
In the second syntax, 
a list  is returned 
containing all the sockets 
that passed the test;
if timeout occurred, 
an empty list is returned. 
An error causes 
<a href="#net-error">net-error</a> to be set.
</p>

<p>
Note that supplying a nonexistent socket to <tt>net-select</tt> 
will cause an error to be set in <a href="#net-error">net-error</a>.
</p>

<br/><br/>

<a name="net-send"></a>
<h2><span class="function">net-send</span></h2>
<h4>syntax: (net-send <em>int-socket</em> <em>str-buffer</em> [<em>int-num-bytes</em>])</h4>

<p>
Sends the contents of <em>str-buffer</em> on the connection specified by <em>int-socket</em>. 
If <em>int-num-bytes</em> is specified, up to <em>int-num-bytes</em> are sent. 
If <em>int-num-bytes</em> is not specified, the entire contents will be sent.
<tt>net-send</tt> returns the number of bytes sent or <tt>nil</tt> on failure.
</p>

<p>On failure, use <a href="#net-error">net-error</a> to get more error information.</p>

<!-- example -->

<pre>
(set 'buf "hello there")

(net-send sock buf)       <span class='arw'>&rarr;</span> 11
(net-send sock buf 5)     <span class='arw'>&rarr;</span> 5

(net-send sock "bye bye") <span class='arw'>&rarr;</span> 7
</pre>


<p>
The first <tt>net-send</tt> sends the string <tt>"hello there"</tt>, while
the second <tt>net-send</tt> sends only the string <tt>"hello"</tt>.
</p>

<br/><br/>

<a name="net-send-to"></a>
<h2><span class="function">net-send-to</span></h2>
<h4>syntax: (net-send-to <em>str-remotehost</em> <em>int-remoteport</em> <em>str-buffer</em> <em>int-socket</em>)</h4>

<p>Can be used for either UDP or TCP/IP communications. The socket in <em>int-socket</em> 
must have previously been opened with a <a href="#net-connect">net-connect</a> 
or <a href="#net-listen">net-listen</a> function. If the opening functions was used 
with the <tt>"udp"</tt> option, <tt>net-listen</tt> or <tt>net-connect</tt>
are not used to listen or to connect but only to create the UDP socket.
The host in <em>str-remotehost</em> can be specified either as 
a hostname or as an IP-number string.</p>

<p>When using <tt>net-connect</tt> together with <tt>net-send-to</tt>, then 
only one of the functions should specify the remote host. The other should leave 
the address as an empty string.</p>

<!-- example -->

<pre>
;;;;;;;;;;;;;;;;;; UDP server
(set 'socket (net-listen 10001 "" "udp"))
(if socket (println "server listening on port " 10001)
       (println (net-error)))
(while (not (net-error))
   (set 'msg (net-receive-from socket 255))
   (println "-&gt; " msg)
   (net-send-to (nth 1 msg) (nth 2 msg) 
                (upper-case (first msg)) socket))

;;;;;;;;;;;;;;;;;; UDP client
(set 'socket (net-listen 10002 "" "udp"))
(if (not socket) (println (net-error)))
(while (not (net-error))
   (print "&gt; ")
   (net-send-to "127.0.0.1" 10001 (read-line) socket)
   (net-receive socket buff 255)
   (println "-&gt; " buff))
</pre>


<p>In the examples both, the client and the server use <tt>net-listen</tt> to
create the UDP socket for sending and receiving. The server extracts 
the client address and port from the message received and uses it in the 
<tt>net-send-to</tt> statement.</p>

<p>See also the <a href="#net-receive-from">net-receive-from</a> function
and the <a href="#net-listen">net-listen</a> function with the 
<tt>"udp"</tt> option.</p>

<p>For blocking short UDP transactions use <a href="#net-send-udp">net-send-udp</a> 
and <a href="#net-receive-udp">net-receive-udp</a>.</p>

<br/><br/>

<a name="net-send-udp"></a>
<h2><span class="function">net-send-udp</span></h2>
<h4>syntax: (net-send-udp <em>str-remotehost</em> <em>int-remoteport</em> <em>str-buffer</em> [<em>bool</em>])</h4>

<p>
Sends a User Datagram Protocol (UDP) 
to the host specified in <em>str-remotehost</em> 
and to the port in <em>int-remoteport</em>. 
The data sent is in <em>str-buffer</em>.
</p>

<p>The theoretical maximum data size of a UDP packet on an IPv4 system
is 64K minus IP layer overhead, but much smaller on most Unix flavors.
8k seems to be a safe size on macOS, BSDs and Linux.</p>

<p>
No previous setup using <tt>net-connect</tt> 
or <tt>net-listen</tt> is necessary. 
<tt>net-send-udp</tt> returns immediately 
with the number of bytes sent 
and closes the socket used. 
If no <tt>net-receive-udp</tt> statement 
is waiting at the receiving side, 
the datagram sent is lost. 
When using datagram communications over insecure connections, 
setting up a simple protocol between sender and receiver 
is recommended for ensuring delivery. 
UDP communication by itself 
does not guarantee reliable delivery 
as TCP/IP does.
</p>

<!-- example -->

<pre>
(net-send-udp "somehost.com" 3333 "Hello")  <span class='arw'>&rarr;</span> 5
</pre>


<p>
<tt>net-send-udp</tt> is also suitable 
for sending binary information 
(e.g., the zero character or other non-visible bytes). 
For a more comprehensive example, 
see <a href="#net-receive-udp">net-receive-udp</a>.
</p>

<p>
Optionally, the sending socket 
can be put in broadcast mode 
by specifying <tt>true</tt> 
or any expression 
not evaluating to <tt>nil</tt> 
in <em>bool</em>:
</p>


<pre>
(net-send-udp "192.168.1.255" 2000 "Hello" true)  <span class='arw'>&rarr;</span> 5
</pre>


<p>
The UDP will be sent to all nodes 
on the <tt>192.168.1</tt> network. 
Note that on some operating systems, 
sending the network mask <tt>255</tt> 
without the <em>bool</em> <tt>true</tt> option 
will enable broadcast mode.
</p>

<p>
As an alternative, 
the <a href="#net-connect">net-connect</a> function 
using the <tt>"udp"</tt> option&mdash;together with 
the <a href="#net-send-to">net-send-to</a> function&mdash;can 
be used to talk to a UDP listener 
in a non-blocking fashion.
</p>

<br/><br/>

<a name="net-service"></a>
<h2><span class="function">net-service</span></h2>
<h4>syntax: (net-service <em>str-service</em> <em>str-protocol</em>)<br/>
syntax: (net-service <em>int-port</em> <em>str-protocol</em>)</h4>

<p>In the first syntax <tt>net-service</tt> makes a lookup in the 
<em>services</em> database and returns the standard port number for 
this service.</p>

<p>In the second syntax a service port is supplied in <em>int-port</em>
to look up the service name.</p>

<p>Returns <tt>nil</tt> on failure.</p>

<!-- example -->

<pre>
; get the port number from the name
(net-service "ftp" "tcp")       <span class='arw'>&rarr;</span> 21
(net-service "http" "tcp")      <span class='arw'>&rarr;</span> 80
(net-service "net-eval" "tcp")  <span class='arw'>&rarr;</span> 4711  ; if configured

; get the service name from the port number
(net-service 22 "tcp")          <span class='arw'>&rarr;</span> "ssh"
</pre>

<br/><br/>

<a name="net-sessions"></a>
<h2><span class="function">net-sessions</span></h2>
<h4>syntax: (net-sessions)</h4>

<p>
Returns a list of active listening and connection sockets.
</p>

<br/><br/>

<a name="new"></a>
<h2><span class="function">new</span></h2>
<h4>syntax: (new <em>context-source</em> <em>sym-context-target</em> [<em>bool</em>])<br/>
syntax: (new <em>context-source</em>)</h4>

<p>The context <em>context-source</em> is copied to <em>sym-context-target</em>.
If the target context does not exist, a new context with the same variable names 
and user-defined functions as in <em>context-source</em> is created.
If the target context already exists, then new symbols and definitions are added.
Existing symbols are only overwritten when the expression in <em>bool</em>
evaluates to anything other than <tt>nil</tt>; otherwise, the content of existing symbols
will remain. This makes <em>mixins</em> of context objects possible. 
<tt>new</tt> returns the target context, which cannot be MAIN.</p>

<p>In the second syntax, the existing context in <em>context-source</em> gets 
copied into the current context as the target context.</p>

<p>All references to symbols in the originating context 
will be translated to references in the target context.
This way, all functions and data structures referring to symbols
in the original context will now refer to symbols in the target context.</p>

<!-- example -->

<pre>
(new CTX 'CTX-2)  <span class='arw'>&rarr;</span> CTX-2   

;; force overwrite of existing symbols
(new CTX MyCTX true)  <span class='arw'>&rarr;</span> MyCTX   
</pre>


<p>
The first line in the example creates a new context 
called <tt>CTX-2</tt> that has the exact same structure 
as the original one. 
Note that <tt>CTX</tt> is not quoted 
because contexts evaluate to themselves, 
but CTX-2 must be quoted because it does not exist yet.
</p>

<p>
The second line merges the context <tt>CTX</tt> into <tt>MyCTX</tt>. 
Any existing symbols of the same name in <tt>MyCTX</tt> 
will be overwritten. 
Because <tt>MyCTX</tt> already exists, 
the quote before the context symbol can be omitted.
</p>

<p>
Context symbols need not be mentioned explicitly, 
but they can be contained in variables:
</p>

<!-- example -->

<pre>
(set 'foo:x 123)
(set 'bar:y 999)

(set 'ctxa foo)
(set 'ctxb bar)

(new ctxa ctxb)  ; from foo to bar

bar:x  <span class='arw'>&rarr;</span> 123  ; x has been added to bar
bar:y  <span class='arw'>&rarr;</span> 999)
</pre>


<p>
The example refers to contexts in variables 
and merges context <tt>foo</tt> into <tt>bar</tt>.
</p>


<p>
See also the function <a href="#def-new">def-new</a> 
for moving and merging single functions 
instead of entire contexts. 
See the <a href="#context">context</a> function 
for a more comprehensive example of <tt>new</tt>.
</p>

<br/><br/>

<a name="nilp"></a>
<h2><span class="function">nil?</span></h2>
<h4>syntax: (nil? <em>exp</em>)</h4>

<p>
If the expression in <em>exp</em> evaluates to <tt>nil</tt>, 
then <tt>nil?</tt> returns <tt>true</tt>; 
otherwise, it returns <tt>nil</tt>.
</p>

<!-- example -->

<pre>
(map nil? '(x nil  1 nil "hi" ()))
<span class='arw'>&rarr;</span> (nil true nil true nil nil)

(nil? nil)  <span class='arw'>&rarr;</span> true
(nil? '())  <span class='arw'>&rarr;</span> nil

; nil? means strictly nil
(nil? (not '()))  <span class='arw'>&rarr;</span> nil
</pre>


<p>
The <tt>nil?</tt> predicate 
is useful for distinguishing between 
<tt>nil</tt> and the empty list <tt>()</tt>.
</p>

<p>Note that <tt>nil?</tt> means <em>strictly</em> <tt>nil</tt>
while <tt>true?</tt> means everything not <tt>nil</tt> or the
empty list <tt>()</tt>.</p>

<br/><br/> 


<a name="normal"></a>
<h2><span class="function">normal</span></h2>
<h4>syntax: (normal <em>float-mean</em> <em>float-stdev</em> <em>int-n</em>)<br/>
syntax: (normal <em>float-mean</em> <em>float-stdev</em>)</h4>

<p>
In the first form, <tt>normal</tt> returns a list of length <em>int-n</em> 
of random, continuously distributed floating point numbers 
with a mean of <em>float-mean</em> 
and a standard deviation of <em>float-stdev</em>. 
The random generator used internally 
can be seeded using the <a href="#seed">seed</a> function.
</p>

<!-- example -->

<pre>
(normal 10 3 10)
<span class='arw'>&rarr;</span> (7 6.563476562 11.93945312 6.153320312 9.98828125
7.984375 10.17871094 6.58984375 9.42578125 12.11230469)
</pre>


<p>
In the second form, 
<tt>normal</tt> returns a single 
normal distributed floating point number:
</p>

<pre>
(normal 1 0.2) <span class='arw'>&rarr;</span> 0.646875
</pre>

<p> When no parameters are given, <tt>normal</tt> assumes a mean of <tt>0.0</tt> 
and a standard deviation of <tt>1.0</tt>.</p>


<p>
See also the <a href="#random">random</a> 
and <a href="#rand">rand</a> functions 
for evenly distributed numbers, 
<a href="#amb">amb</a> for randomizing evaluation 
in a list of expressions, 
and <a href="#seed">seed</a> for setting a different start point 
for pseudo random number generation.
</p>

<br/><br/>

<a name="not"></a>
<h2><span class="function">not</span></h2>

<h4>syntax: (not <em>exp</em>)</h4>

<p>If <em>exp</em> evaluates to <tt>nil</tt> or the empty list <tt>()</tt>, 
then <tt>true</tt> is returned; otherwise, <tt>nil</tt> is returned.</p>

<!-- example -->

<pre>
(not true)            <span class='arw'>&rarr;</span> nil
(not nil)             <span class='arw'>&rarr;</span> true
(not '())             <span class='arw'>&rarr;</span> true
(not (&lt; 1 10))        <span class='arw'>&rarr;</span> nil
(not (not (&lt; 1 10)))  <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="now"></a>
<h2><span class="function">now</span></h2>
<h4>syntax: (now [<em>int-minutes-offset</em> [<em>int-index</em>]])</h4>

<p>Returns information about the current date and time 
as a list of integers.  An optional time-zone offset 
can be specified in minutes in <em>int-minutes-offset</em>. 
This causes the time to be shifted forward or backward in time,
before being split into separate date values.</p>

<p>An optional list-index in <em>int-index</em> makes <tt>now</tt>
return a specific member in the result list.</p>

<!-- example -->

<pre>
(now)       <span class='arw'>&rarr;</span> (2002 2 27 18 21 30 140000 57 3 -300 0)
(now 0 -2)  <span class='arw'>&rarr;</span> -300 ; minutes west of GMT


(date-value (now))  <span class='arw'>&rarr;</span> 1014834090
</pre>


<p>The numbers represent the following date-time fields:</p>

<table  width="98%" summary="date formatting">
<tr align="left"><th>format</th><th>description</th></tr>

<tr><td>year</td><td>Gregorian calendar</td></tr>
<tr><td>month</td><td>               (1&ndash;12)</td></tr>
<tr><td>day</td><td>                 (1&ndash;31)</td></tr>
<tr><td>hour</td><td>                (0&ndash;23) UTC</td></tr>
<tr><td>minute</td><td>              (0&ndash;59)</td></tr>
<tr><td>second</td><td>              (0&ndash;59)</td></tr>
<tr><td>microsecond</td><td>         (0&ndash;999999) 
		OS-specific, millisecond resolution</td></tr>
		
<tr><td>day of current year</td><td>Jan 1st is 1</td></tr>
<tr><td>day of current week</td><td> (1&ndash;7) starting Monday</td></tr>
<tr><td>time zone offset in minutes</td><td> west of GMT including daylight savings bias</td></tr>
<tr><td>daylight savings time type </td><td> (0&ndash;6) on Linux/Unix
		or (0&ndash;2) on MS Windows</td></tr>
</table><br/>

<p>The second example returns the Coordinated Universal Time (UTC) 
time value of seconds after January 1, 1970.</p>

<p>Ranging from 0 to 23, hours are given in UTC and are not adjusted for 
the local time zone.  The resolution of the <tt>microseconds</tt> field 
depends on the operating system and platform.  On some platforms, 
the last three digits of the <tt>microseconds</tt> field are always 
<tt>0</tt> (zero).</p>

<p>The "day of the week" field starts with 1 on Monday conforming to the 
ISO 8601 international standard for date and time representation.</p>

<p>On some platforms, the daylight savings time flag is not active and 
returns <tt>0</tt> (zero) even during daylight savings time (dst).</p>

<p>Depending on the geographical area, the daylight savings time type
(dst) has a different value from 1 to 6:</p>

<table  width="35%" summary="date formatting">
<tr align="left"><th>UNIX type</th><th>area</th></tr>
<tr><td>0</td><td>not on daylight savings</td></tr>
<tr><td>1</td><td>USA style dst</td></tr>
<tr><td>2</td><td>Australian style daylight savings</td></tr>
<tr><td>3</td><td>Western European daylight savings</td></tr>
<tr><td>4</td><td>Middle European daylight savings</td></tr>
<tr><td>5</td><td>Eastern European daylight savings</td></tr>
<tr><td>6</td><td>Canada dst</td></tr>
</table>
<br />
<table  width="35%" summary="date formatting">
<tr align="left"><th>Windows type</th><th></th></tr>
<tr><td>0</td><td>Daylight saving time is not used</td></tr>
<tr><td>1</td><td>Standard date time range is used</td></tr>
<tr><td>2</td><td>Daylight date time range is used (daylight savings active)</td></tr> 
</table>



<p>See also the <a href="#date">date</a>, 
<a href="#date-list">date-list</a>, 
<a href="#date-parse">date-parse</a>, 
<a href="#date-value">date-value</a>, 
<a href="#time">time</a>, and <a href="#time-of-day">time-of-day</a> functions.
</p>

<br/><br/>

<a name="nper"></a>
<h2><span class="function">nper</span></h2>
<h4>syntax: (nper <em>num-interest</em> <em>num-pmt</em> <em>num-pv</em> 
[<em>num-fv</em> [<em>int-type</em>]])</h4>

<p>Calculates the number of payments required to pay a loan of <em>num-pv</em> 
with a constant interest rate of <em>num-interest</em> and payment <em>num-pmt</em>.
If payment is at the end of the period, <em>int-type</em> is <tt>0</tt> (zero) 
or <em>int-type</em> is omitted; for payment at the beginning of each period, 
<em>int-type</em> is 1.</p>

<!-- example -->

<pre>
(nper (div 0.07 12) 775.30 -100000)  <span class='arw'>&rarr;</span> 239.9992828
</pre>


<p>
The example calculates the number of monthly payments required to pay a loan of 
$100,000 at a yearly interest rate of 7 percent with payments of $775.30.
</p>

<p>
See also the <a href="#fv">fv</a>,
<a href="#irr">irr</a>,
<a href="#npv">npv</a>,
<a href="#pmt">pmt</a>,
and <a href="#pv">pv</a> functions.
</p>

<br/><br/>

<a name="npv"></a>
<h2><span class="function">npv</span></h2>
<h4>syntax: (npv <em>num-interest</em> <em>list-values</em>)</h4>

<p>
Calculates the net present value of an investment with a fixed interest rate 
<em>num-interest</em> and a series of future payments and income in <em>list-values</em>.
Payments are represented by negative values in <em>list-values</em>,
while income is represented by positive values in <em>list-values</em>.
</p>

<!-- example -->

<pre>
(npv 0.1 '(1000 1000 1000)) 
<span class='arw'>&rarr;</span> 2486.851991

(npv 0.1 '(-2486.851991 1000 1000 1000)) 
<span class='arw'>&rarr;</span> -1.434386832e-08  ; ~ 0.0 (zero)
</pre>


<p>
In the example,
an initial investment of $2,481.85 would allow for an income of $1,000 after the end of the first,
second,
and third years.
</p>

<p>
See also the <a href="#fv">fv</a>,
<a href="#irr">irr</a>,
<a href="#nper">nper</a>,
<a href="#pmt">pmt</a>,
and <a href="#pv">pv</a> functions.
</p> 

<br/><br/>

<a name="nth"></a>
<h2><span class="function">nth</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (nth <em>int-index</em> <em>list</em>)<br/>
syntax: (nth <em>int-index</em> <em>array</em>)<br/>
syntax: (nth <em>int-index</em> <em>str</em>)<br/><br/>

syntax: (nth <em>list-indices</em> <em>list</em>)<br/>
syntax: (nth <em>list-indices</em> <em>array</em>)</h4>

<p>
In the first syntax group <tt>nth</tt> uses <em>int-index</em> an index into the 
<em>list</em>, <em>array</em> or <em>str</em> found and returning the element found 
at that index. See also <a href="#indexing">Indexing elements of strings and lists</a>.</p>

<p>Multiple indices may be specified to recursively access elements in nested lists
or arrays. If there are more indices than nesting levels, the extra indices are ignored. 
When multiple indices are used, they must be put in a list as shown in the second
syntax group.</p>

<!-- example -->

<pre>
(set 'L '(a b c))
(nth 0 L)    <span class='arw'>&rarr;</span> a
; or simply
(L 0) <span class='arw'>&rarr;</span> a

(set 'names '(john martha robert alex)) 
<span class='arw'>&rarr;</span> (john martha robert alex)

(nth 2 names)    <span class='arw'>&rarr;</span> robert
; or simply
(names 2)        <span class='arw'>&rarr;</span> robert

(names -1)       <span class='arw'>&rarr;</span> alex


; multiple indices
(set 'persons '((john 30) (martha 120) ((john doe) 17)))

(persons 1 1)           <span class='arw'>&rarr;</span> 120

(nth '(2 0 1) persons)  <span class='arw'>&rarr;</span> doe

; or simply
(persons 2 0 1)        <span class='arw'>&rarr;</span> doe

; multiple indices in a vector
(set 'v '(2 0 1))
(persons v)       <span class='arw'>&rarr;</span> doe
(nth v persons)   <span class='arw'>&rarr;</span> doe

; negative indices
(persons -2 0)    <span class='arw'>&rarr;</span> martha

; out-of-bounds indices cause error
(persons 10)  <span class='arw'>&rarr;</span> <span class='err'>ERR: list index out of bounds</span>
(person -5)   <span class='arw'>&rarr;</span> <span class='err'>ERR: list index out of bounds</span>
</pre>


<p>The list <tt>L</tt> can be the context of the default functor <tt>L:L</tt>. 
This allows lists passed by reference:</p>

<pre>
(set 'L:L '(a b c d e f g))

(define (second ctx)
	(nth 1 ctx))

(reverse L) <span class='arw'>&rarr;</span> (g f e d c b a)
L:L <span class='arw'>&rarr;</span> (g f e d c b a)

;; passing the list in L:L by reference
(second L)   <span class='arw'>&rarr;</span> b

;; passing the list in L:L by value
(second L:L) <span class='arw'>&rarr;</span> b
</pre>

<p>Reference passing is faster and uses less memory in big lists and should
be used on lists with more than a few hundred items.</p>

<p>Note that the <i>implicit indexing</i> version of <tt>nth</tt> is not breaking newLISP
syntax rules but should be understood as a logical expansion of newLISP syntax rules to
other data types than built-in functions or lambda expressions. A list in the functor
position of an s-expression assumes self-indexing functionality using the index
arguments following.</p>

<p>
The implicit indexed syntax forms are faster but the other form with an explicit
<tt>nth</tt> may be more readable in some situations.
</p>

<p><tt>nth</tt> works on <a href="#array">arrays</a> just like it does on lists:</p>

<!-- example -->

<pre>
(set 'aArray (array 2 3 '(a b c d e f))) 
<span class='arw'>&rarr;</span> ((a b c) (d e f))
(nth 1 aArray)      <span class='arw'>&rarr;</span>  (d e f)
(aArray 1)          <span class='arw'>&rarr;</span>  (d e f)

(nth '(1 0) aArray)    <span class='arw'>&rarr;</span> d
(aArray 1 0)           <span class='arw'>&rarr;</span> d
(aArray '(1 0))        <span class='arw'>&rarr;</span> d

(set 'vec '(1 0))
(aArray vec)           <span class='arw'>&rarr;</span> d
</pre>


<p>
In the String version, <tt>nth</tt> returns the character found at the position 
<em>int-index</em> in <em>str</em> and returns it as a string.</p>

<!-- example -->

<pre>
(nth  0 "newLISP")   <span class='arw'>&rarr;</span> "n"

("newLISP" 0)        <span class='arw'>&rarr;</span> "n"

("newLISP" -1)       <span class='arw'>&rarr;</span> "P"
</pre>


<p>Note that <a href="#nth">nth</a> works on character boundaries rather than byte 
boundaries when using the UTF-8&ndash;enabled version of newLISP. To access ASCII and
binary string buffers on single byte boundaries use <a href="#slice">slice</a>.</p>

<p>See also <a href="#setf">setf</a> for modifying multidimensional lists and arrays and
<a href="#push">push</a> and <a href="#pop">pop</a> for modifying lists.</p>

<br/><br/>

<a name="nullp"></a>
<h2><span class="function">null?</span></h2>
<h4>syntax: (null? <em>exp</em>)</h4>

<p>
Checks if an expression evaluates to <tt>nil</tt>,
the empty list <tt>()</tt>,
the empty string <tt>""</tt>,
<tt>NaN</tt> (not a number),
or <tt>0</tt> (zero),
in which case it returns <tt>true</tt>.
In all other cases,
<tt>null?</tt> returns <tt>nil</tt>.
The predicate <tt>null?</tt> is useful in conjunction with the functions 
<a href="#filter">filter</a> or <a href="#clean">clean</a> to check the outcome of other newLISP operations.
</p>

<!-- example -->

<pre>
(set 'x (sqrt -1)) <span class='arw'>&rarr;</span> NaN ; or nan on UNIX
(null? x) <span class='arw'>&rarr;</span> true

(map null? '(1 0 0.0 2 "hello" "" (a b c) () true))
<span class='arw'>&rarr;</span> (nil true true nil nil true nil true nil) 

(filter null? '(1 0 2 0.0 "hello" "" (a b c) () nil true)) 
<span class='arw'>&rarr;</span> (0 0 "" () nil)

(clean null? '(1 0 2 0.0 "hello" "" (a b c) () nil true))
<span class='arw'>&rarr;</span> (1 2 "hello" (a b c) true)
</pre>

<p>
See also the predicates <a href="#emptyp">empty?</a>,
<a href="#nilp">nil?</a>
and <a href="#zerop">zero?</a>.
</p>

<br/><br/>

<a name="numberp"></a>
<h2><span class="function">number?</span>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (number? <em>exp</em>)</h4>

<p>
<tt>true</tt> is returned only if <em>exp</em> evaluates to a floating point number or an integer;
otherwise,
<tt>nil</tt> is returned.
</p>

<!-- example -->

<pre>
(set 'x 1.23)
(set 'y 456)
(number? x)      <span class='arw'>&rarr;</span> true
(number? y)      <span class='arw'>&rarr;</span> true
(number? "678")  <span class='arw'>&rarr;</span> nil  
</pre>


<p>
See the functions <a href="#floatp">float?</a> and <a href="#integerp">integer?</a> to test for a specific number type.
</p>

<br/><br/>

<a name="oddp"></a>
<h2><span class="function">odd?</span>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (odd? <em>int-number</em>)</h4>

<p>Checks the parity of an integer number. If the number is not <em>even divisible</em> by <tt>2</tt>, 
it has <em>odd</em> parity.  When a floating point number is passed for <em>int-number</em>, 
it will be converted first to an integer by cutting off its fractional part.</p>

<pre>
(odd? 123)  <span class='arw'>&rarr;</span> true
(odd? 8)    <span class='arw'>&rarr;</span> nil
(odd? 8.7)  <span class='arw'>&rarr;</span> nil
</pre>

<p>Use <a href="#evenp">even?</a> to check if an integer is even, divisible by <tt>2</tt>.</p>

<br/><br/>

<a name="open"></a>
<h2><span class="function">open</span></h2>

<h4>syntax: (open <em>str-path-file</em> <em>str-access-mode</em> [<em>str-option</em>])</h4>

<p>
The <em>str-path-file</em> is a file name,
and <em>str-access-mode</em> is a string specifying the file access mode.
<tt>open</tt> returns an integer,
which is a file handle to be used on subsequent read or write operations on the file.
On failure,
<tt>open</tt> returns <tt>nil</tt>.
The access mode <tt>"write"</tt> creates the file if it doesn't exist,
or it truncates an existing file to <tt>0</tt> (zero) bytes in length.
</p>

<p>
The following strings are legal access modes:
</p>


<tt>"read"</tt> or <tt>"r"</tt> for read only access<br/>
<tt>"write"</tt> or <tt>"w"</tt> for write only access<br/>
<tt>"update"</tt> or <tt>"u"</tt> for read/write access<br/>
<tt>"append"</tt> or <tt>"a"</tt> for append read/write access<br/>

<br/>

<!-- example -->

<pre>
(device (open "newfile.data" "write"))  <span class='arw'>&rarr;</span> 5
(print "hello world\n")  <span class='arw'>&rarr;</span> "hello world"
(close (device))         <span class='arw'>&rarr;</span> 5

(set 'aFile (open "newfile.data" "read"))
(seek aFile 6)
(set 'inChar (read-char aFile))
(print inChar "\n")
(close aFile)
</pre>


<p>
The first example uses <tt>open</tt> to set the device for <a href="#print">print</a> 
and writes the word <tt>"hello world"</tt> into the file <tt>newfile.data</tt>.
The second example reads a byte value at offset 6 in the same file (the ASCII value 
of <tt>'w'</tt> is 119). Note that using <tt>close</tt> on <a href="#device">(device)</a> 
automatically resets <a href="#device">device</a> to <tt>0</tt> (zero).
</p>

<p>
As an additional <em>str-option</em>,
<tt>"non-block"</tt> or <tt>"n"</tt> can be specified after the <tt>"read"</tt> or <tt>"write"</tt> option.
Only available on Unix systems,
non-blocking mode can be useful when opening <em>named pipes</em> but is not required to perform I/O on named pipes.
</p>

<br/><br/>

<a name="or"></a>
<h2><span class="function">or</span></h2>

<h4>syntax: (or <em>exp-1</em> [<em>exp-2</em> ... ])</h4>

<p>
Evaluates expressions <em>exp-x</em> from left to right until finding a result 
that does not evaluate to <tt>nil</tt> or the empty list <tt>()</tt>.
The result is the return value of the <tt>or</tt> expression.
</p>

<!-- example -->

<pre>
(set 'x 10)
(or (&gt; x 100) (= x 10))          <span class='arw'>&rarr;</span> true
(or "hello" (&gt; x 100) (= x 10))  <span class='arw'>&rarr;</span> "hello"
(or '())                         <span class='arw'>&rarr;</span> ()
(or true)                        <span class='arw'>&rarr;</span> true
(or)                             <span class='arw'>&rarr;</span> nil
</pre>

<br/><br/>

<a name="ostype"></a>
<h2><span class="function">ostype</span></h2>
<h4>syntax: ostype</h4>

<p><tt>ostype</tt> is a built-in system constant 
containing the name of the operating system 
newLISP is running on.</p>

<!-- example -->

<pre>
ostype  <span class='arw'>&rarr;</span> "Windows"
</pre>


<p>One of the following strings is returned: 
<tt>"Linux", "BSD", "OSX", "Tru64Unix", "Solaris", "SunOS", "Windows", "Cygwin", or "OS/2"</tt>.
</p>


<p><tt>ostype</tt> can be used to write platform-independent code:</p>

<pre>
(if 
    (= ostype "Linux") (import "libz.so")
    (= ostype "BSD") (import "libz.so")
    (= ostype "OSX") (import "libz.dylib")
    ...
    (println "cannot import libz on this platform")
)
</pre>


<p>Use <a href="#sys-info">sys-info</a> to learn more
about the current flavor of newLISP running.</p>

<p>For a table of other built-in system variables and symbols see the
chapter <a href="#system_symbols">System Symbols and Constants</a> in the
appendix.</p>

<br/><br/>

<a name="pack"></a>
<h2><span class="function">pack</span></h2>
<h4>syntax: (pack <em>str-format</em> [<em>exp-1</em> [<em>exp-2</em> ... ]])<br/>
syntax: (pack <em>str-format</em> [<em>list</em>])<br/><br/>
syntax: (pack <em>struct</em> [<em>exp-1</em> [<em>exp-2</em> ... ]])<br/>
syntax: (pack <em>struct</em> [<em>list</em>])</h4>

<p>When the first parameter is a string, <tt>pack</tt> packs one or more expressions 
(<em>exp-1</em> to <em>exp-n</em>) into a binary format specified in the format 
string <em>str-format</em>, and returning the binary structure in a string buffer.
The symmetrical <a href="#unpack">unpack</a> function is used 
for unpacking. The expression arguments can also be given in a <em>list</em>.
<tt>pack</tt> and <tt>unpack</tt> are useful when reading and writing binary files 
(see <a href="#read">read</a> and <a href="#write">write</a>) 
or when unpacking binary structures from return values of imported C functions 
using <tt>import</tt>.</p>

<p>When the first parameter is the symbol of a <a href="#struct">struct</a>
definition, <tt>pack</tt> uses the format as specified in <em>struct</em>. 
While <tt>pack</tt> with <em>str-format</em> literally packs as specified,
<tt>pack</tt> with <em>struct</em> will insert structure aligning pad-bytes
depending on data type, order of elements and CPU architecture.
Refer to the description of the <a href="#struct">struct</a> function for more detail.</p>

<p>When no data expressions or lists are specified, formats or structures are filled
with <tt>0</tt>s (zeros).</p> 

<p>The following characters are used in <em>str-format</em>:</p>

<table  summary="format characters">
<tr align="left"><th>format</th><th>description</th></tr>

<tr>
<td WIDTH="20%"><tt>c </tt></td>

<td WIDTH="80%">a signed 8-bit number</td>
</tr>

<tr>
<td><tt>b </tt></td>
<td>an unsigned 8-bit number</td>
</tr>

<tr>
<td><tt>d </tt></td>
<td>a signed 16-bit short number</td>
</tr>

<tr>
<td><tt>u </tt></td>
<td>an unsigned 16-bit short number</td>
</tr>

<tr>
<td><tt>ld</tt></td>
<td>a signed 32-bit long number</td>
</tr>

<tr>
<td><tt>lu</tt></td>
<td>an unsigned 32-bit long number</td>
</tr>

<tr>
<td><tt>Ld</tt></td>
<td>a signed 64-bit long number</td>
</tr>

<tr>
<td><tt>Lu</tt></td>
<td>an unsigned 64-bit long number</td>
</tr>

<tr>
<td><tt>f </tt></td>
<td>a float in 32-bit representation</td>
</tr>

<tr>
<td><tt>lf</tt></td>
<td>a double float in 64-bit representation</td>
</tr>

<tr>
<td><tt>sn</tt></td>
<td>a string of <em>n</em> null padded ASCII characters</td>
</tr>

<tr>
<td><tt>nn</tt></td>
<td><em>n</em> null characters</td>
</tr>

<tr>
<td><tt>&gt;</tt></td>

<td>switch to big endian byte order</td>
</tr>

<tr>
<td><tt>&lt;</tt></td>
<td>switch to little endian byte order</td>
</tr>

</table><br/>

<p>
<tt>pack</tt> will convert all floats into integers 
when passed to <tt>b</tt>, <tt>c</tt>, <tt>d</tt>, <tt>ld</tt>,
or <tt>lu</tt> formats.
It will also convert integers into floats
when passing them to <tt>f</tt> and <tt>lf</tt> formats.
</p>

<!-- example -->

<pre>
(pack "c c c" 65 66 67)  <span class='arw'>&rarr;</span> "ABC"
(unpack "c c c" "ABC")   <span class='arw'>&rarr;</span> (65 66 67)

(pack "c c c" 0 1 2)             <span class='arw'>&rarr;</span> "\000\001\002"
(unpack "c c c" "\000\001\002")  <span class='arw'>&rarr;</span> (0 1 2)

(set 's (pack "c d u" 10 12345 56789))
(unpack "c d u" s)  <span class='arw'>&rarr;</span> (10 12345 56789)

(set 's (pack "s10 f" "result" 1.23))
(unpack "s10 f" s)
<span class='arw'>&rarr;</span> ("result\000\000\000\000" 1.230000019)

(pack "n10") <span class='arw'>&rarr;</span> "\000\000\000\000\000\000\000\000\000\000"

(set 's (pack "s3 lf" "result" 1.23))
(unpack "s3 f" s)  <span class='arw'>&rarr;</span> ("res" 1.23)

(set 's (pack "c n7 c" 11 22))
(unpack "c n7 c" s)  <span class='arw'>&rarr;</span> (11 22))

(unpack "b" (pack "b" -1.0))  <span class='arw'>&rarr;</span> (255)
(unpack "f" (pack "f" 123))   <span class='arw'>&rarr;</span> (123)
</pre>


<p>
The last two statements show 
how floating point numbers are converted 
into integers when required by the format specification.
</p>

<p>The expressions to pack can also be given in a list:</p>


<pre>
(set 'lst '("A" "B" "C"))
(set 'adr (pack "lululu" lst))
(map get-string (unpack "lululu" adr))    <span class='arw'>&rarr;</span> ("A" "B" "C")
</pre>

<p>Note that the list should be referenced directly in <tt>pack</tt>,
so the pointers passed by <tt>adr</tt> are valid. <tt>adr</tt> would be written 
as <tt>char * adr[]</tt> in the C-programming language and represents a 32-bit pointer to an
array of 32-bit string pointers or a 64-bit pointers on the 64-bit version of newLISP.
</p>

<p>
The <tt>&gt;</tt> and <tt>&lt;</tt> specifiers 
can be used to switch between <em>little endian</em> 
and <em>big endian</em> byte order 
when packing or unpacking:
</p>


<pre>
(pack "d" 1)   <span class='arw'>&rarr;</span> "\001\000"  ;; on little endian CPU
(pack "&gt;d" 1)  <span class='arw'>&rarr;</span> "\000\001"  ;; force big endian

(pack "ld" 1)   <span class='arw'>&rarr;</span> "\001\000\000\000" ;; on little endian CPU
(pack "&lt;ld" 1)  <span class='arw'>&rarr;</span> "\000\000\000\001" ;; force big endian

(pack "&gt;u &lt;u" 1 1) <span class='arw'>&rarr;</span> "\000\001\001\000" ;; switch twice
</pre>


<p>
Switching the byte order will affect all number formats with 16-,
32-, or 64-bit sizes.
</p>

<p>
The pack and unpack format need not be the same:
</p>

<pre>
(set 's (pack "s3" "ABC"))
(unpack "c c c" s)  <span class='arw'>&rarr;</span> (65 66 67)
</pre>

<p>
The examples show spaces between the format specifiers.
These are not required but can be used to improve readability.
</p> 


<p>Using <tt>pack</tt> and <tt>unpack</tt> on UTF-8 strings:</p>

<pre>
(set 'txt "我能吞下玻璃而不伤身体。") 
<span class='arw'>&rarr;</span> "我能吞下玻璃而不伤身体。"
(set 'lst (unpack (dup "b" (length txt)) txt)) 
<span class='arw'>&rarr;</span> (230 136 145 232 ... 147 227 128 130)
(pack (dup "b" (length lst)) lst) 
<span class='arw'>&rarr;</span> "我能吞下玻璃而不伤身体。"
</pre>

<p>
See also the <a href="#address">address</a>,
<a href="#get-int">get-int</a>,
<a href="#get-long">get-long</a>,
<a href="#get-char">get-char</a>,
<a href="#get-string">get-string</a>,
and <a href="#unpack">unpack</a> functions.
</p>

<br/><br/>

<a name="parse"></a>

<h2><span class="function">parse</span></h2>
<h4>syntax: (parse <em>str-data</em> [<em>str-break</em>  [<em>regex-option</em>]])</h4>

<p>
Breaks the string that results from evaluating <em>str-data</em> into string tokens,
which are then returned in a list.
When no <em>str-break</em> is given,
<tt>parse</tt> tokenizes according to newLISP's internal parsing rules.
A string may be specified in <em>str-break</em> for tokenizing only at the occurrence of a string.
If an <em>regex-option</em> number or string is specified,
a regular expression pattern may be used in <em>str-break</em>.
</p>

<p>
When <em>str-break</em> is not specified,
the maximum token size is 2048 for quoted strings and 256 for identifiers.
In this case,
newLISP uses the same faster tokenizer it uses for parsing newLISP source.
If <em>str-break</em> is specified,
there is no limitation on the length of tokens.
A different algorithm is used that splits the source string <em>str-data</em> at the string in <em>str-break</em>.
</p>

<!-- example -->

<pre>
; no break string specified
(parse "hello how are you")     <span class='arw'>&rarr;</span> ("hello" "how" "are" "you")

; strings break after spaces, parentheses, commas, colons and numbers. 
; Spaces and the colon are swollowed
(parse "weight is 10lbs")       <span class='arw'>&rarr;</span>
(parse "one:two:three" ":")     <span class='arw'>&rarr;</span> ("one" "two" "three")

;; specifying a break string
(parse "one--two--three" "--")  <span class='arw'>&rarr;</span> ("one" "two" "three")

; a regex option causes regex parsing
(parse "one-two--three---four" "-+" 0) 
<span class='arw'>&rarr;</span> ("one" "two" "three" "four")

(parse "hello regular   expression 1, 2, 3" {,\s*|\s+} 0)
<span class='arw'>&rarr;</span> ("hello" "regular" "expression" "1" "2" "3")
</pre>


<p>The last two examples show a regular expression as the break string 
with the default option <tt>0</tt> (zero).  Instead of 
<tt>{</tt> and <tt>}</tt> (left and right curly brackets), double 
quotes can be used to limit the pattern.  In this case, double 
backslashes must be used inside the pattern.  The last pattern could 
be used for parsing CSV (Comma Separated Values) files.  For the regular expression option 
numbers, see <a href="#regex">regex</a>.</p>

<p>
<tt>parse</tt> will return empty fields 
around separators 
as empty strings:
</p>


<pre>
; empty fields around separators returned as empty strings
(parse "1,2,3," ",") <span class='arw'>&rarr;</span> ("1" "2" "3" "")
(parse "1,,,4" ",")  <span class='arw'>&rarr;</span> ("1" "" "" "4")
(parse "," ",")      <span class='arw'>&rarr;</span> ("" "")

(parse "")      <span class='arw'>&rarr;</span> ()
(parse "" " ")  <span class='arw'>&rarr;</span> ()
</pre>


<p>
This behavior is needed 
when parsing records 
with empty fields.
</p>

<p>
Parsing an empty string 
will always result 
in an empty list.
</p>

<p>
Use the <a href="#regex">regex</a> function 
to break strings up 
and the <a href="#directory">directory</a>, 
<a href="#find">find</a>, 
<a href="#find-all">find-all</a>,
<a href="#regex">regex</a>, 
<a href="#replace">replace</a>, 
and <a href="#search">search</a> functions 
for using regular expressions.
</p>

<br/><br/>


<a name="peek"></a>
<h2><span class="function">peek</span></h2>
<h4>syntax: (peek <em>int-handle</em>)</h4>

<p>
Returns the number of bytes ready to be read on a file descriptor;
otherwise,
it returns <tt>nil</tt> if the file descriptor is invalid.
<tt>peek</tt> can also be used to check <tt>stdin</tt>.
This function is only available on Unix-like operating systems.
</p>

<!-- example -->

<pre>
(peek 0)  ; check # of bytes ready on stdin
</pre>


<p>
Use the <a href="#net-peek">net-peek</a> function 
to check for network sockets, 
or for the number of available bytes on them. 
On Unix systems, 
<a href="#net-peek">net-peek</a> can be used 
to check file descriptors. 
The difference is that 
<a href="#net-peek">net-peek</a> also sets 
<a href="#net-error">net-error</a>.
</p>

<br/><br/>

<a name="pipe"></a>
<h2><span class="function">pipe</span></h2>
<h4>syntax: (pipe)</h4>

<p>
Creates an inter-process communications pipe and returns the 
<tt>read</tt> and <tt>write</tt> handles to it within a list.
</p>

<!-- example -->

<pre>
(pipe)  <span class='arw'>&rarr;</span> (3 4)  ; 3 for read, 4 for writing
</pre>


<p>
The pipe handles can be passed to a child process launched via 
   <a href="#process"> process</a> or to <a href="#fork">fork</a> for inter-process communications.
</p>

<p>
Note that the pipe does not block when being written to,
but it does block reading until bytes are available.
A <a href="#read-line">read-line</a> blocks until a newline character is received.
A <a href="#read">read</a> blocks when fewer characters than 
specified are available from a pipe that has not had the writing end closed by all processes.
</p>

<p>
More than one pipe can be opened if required.
</p>

<p>
newLISP can also use <em>named pipes</em>.
See the <a href="#open">open</a> function for further information.
</p>

<br/><br/>

<a name="pmt"></a>
<h2><span class="function">pmt</span></h2>

<b>syntax: (pmt <em>num-interest</em> <em>num-periods</em> <em>num-principal</em> 
[<em>num-future-value</em> [<em>int-type</em>]])</b>

<p>
Calculates the payment for a loan based on a constant interest of <em>num-interest</em> 
and constant payments over <em>num-periods</em> of time.
<em>num-future-value</em> is the value of the loan at the end (typically <tt>0.0</tt>).
If payment is at the end of the period, <em>int-type</em> is <tt>0</tt> (zero) 
or <em>int-type</em> is omitted; for payment at the beginning of each period, 
<em>int-type</em> is 1.</p>


<!-- example -->

<pre>
(pmt (div 0.07 12) 240 100000)  <span class='arw'>&rarr;</span> -775.2989356
</pre>


<p>
The above example calculates a payment of $775.30 for a loan of $100,000 at a yearly interest rate of 7 percent.
It is calculated monthly and paid over 20 years (20 * 12 = 240 monthly periods).
This illustrates the typical way payment is calculated for mortgages.
</p>

<p>
See also the <a href="#fv">fv</a>,
<a href="#irr">irr</a>,
<a href="#nper">nper</a>,
<a href="#npv">npv</a>,
and <a href="#pv">pv</a> functions.
</p>

<br/><br/>

<a name="pop"></a>
<h2><span class="function">pop</span>&nbsp;<a href="#destructive">!</a>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (pop <em>list</em> [<em>int-index-1</em> [<em>int-index-2</em> ... ]])<br/>
syntax: (pop <em>list</em> [<em>list-indexes</em>])<br/><br/>

syntax: (pop <em>str</em> [<em>int-index</em> [<em>int-length</em>]])</h4>

<p>Using <tt>pop</tt>, elements can be removed from lists and characters from strings.</p>

<p>
In the first syntax, <tt>pop</tt> extracts an element from the list found 
by evaluating <em>list</em>.
If a second parameter is present,
the element at <em>int-index</em> is extracted and returned.
See also <a href="#indexing">Indexing elements of strings and lists</a>.
</p>

<p>
In the second version,
indices are specified in the list <em>list-indexes</em>.
This way,
<tt>pop</tt> works easily together with <a href="#ref">ref</a>
and <a href="#ref-all">ref-all</a>,
which return lists of indices.
</p> 

<p>
<tt>pop</tt> changes the contents of the target list.
The popped element is returned.
</p>

<!-- example -->

<pre>
(set 'pList '((f g) a b c "hello" d e 10))

(pop pList)  <span class='arw'>&rarr;</span> (f g)
(pop pList)  <span class='arw'>&rarr;</span> a
pList        <span class='arw'>&rarr;</span> (b c "hello" d e 10)

(pop pList 3)    <span class='arw'>&rarr;</span> d
(pop pList -1)   <span class='arw'>&rarr;</span> 10
pList            <span class='arw'>&rarr;</span> (b c "hello" e)

(pop pList -1)  <span class='arw'>&rarr;</span> e
pList           <span class='arw'>&rarr;</span> (b c "hello")

(pop pList -2)  <span class='arw'>&rarr;</span> c
pList           <span class='arw'>&rarr;</span> (b "hello")

(set 'pList '(a 2 (x y (p q) z)))

(pop pList -1 2 0)  <span class='arw'>&rarr;</span> p

;; use indices in a list
(set 'pList '(a b (c d () e)))

(push 'x pList '(2 2 0))  
<span class='arw'>&rarr;</span> (a b (c d (x) e))

pList
<span class='arw'>&rarr;</span> (a b (c d (x) e))

(ref 'x pList)  <span class='arw'>&rarr;</span> (2 2 0)

(pop pList '(2 2 0))  <span class='arw'>&rarr;</span> x
</pre>


<p><tt>pop</tt> can also be used on strings with one index:</p>

<!-- example -->

<pre>
;; use pop on strings

(set 'str "newLISP")

(pop str -4 4)  <span class='arw'>&rarr;</span> "LISP"

str  <span class='arw'>&rarr;</span> "new"

(pop str 1)  <span class='arw'>&rarr;</span> "e"

str  <span class='arw'>&rarr;</span> "nw"

(set 'str "x")

(pop str)  <span class='arw'>&rarr;</span> "x"
(pop str)  <span class='arw'>&rarr;</span> ""
</pre>


<p>Popping an empty string will return an empty string.</p>

<p>
See also the <a href="#push">push</a> function, the inverse operation to <tt>pop</tt>.
</p>

<br/><br/>

<a name="pop-assoc"></a>
<h2><span class="function">pop-assoc</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (pop-assoc <em>exp-key</em> <em>list-assoc</em>)<br/>
syntax: (pop-assoc <em>list-keys</em> <em>list-assoc</em>)</h4>

<p>Removes an association referred to by the key in <em>exp-key</em> from the association 
list in <em>list-assoc</em> and returns the popped expression.</p>

<!-- example -->

<pre>
;; simple associations

(set 'L '((a 1) (b 2) (c 3)))
(pop-assoc 'b L) <span class='arw'>&rarr;</span> (b 2)
L <span class='arw'>&rarr;</span> ((a 1) (c 3))

;; nested associations

(set 'L '((a (b 1) (c (d 2)))))
(pop-assoc 'a L) <span class='arw'>&rarr;</span> (a (b 1) (c (d 2)))
L <span class='arw'>&rarr;</span> ()

(set 'L '((a (b 1) (c (d 2)))))
(pop-assoc '(a b) L)  <span class='arw'>&rarr;</span> (b 1)
L <span class='arw'>&rarr;</span>  ((a (c (d 2))))

(set 'L '((a (b 1) (c (d 2)))))
(pop-assoc '(a c) L)  <span class='arw'>&rarr;</span> (c (d 2))
L <span class='arw'>&rarr;</span> ((a (b 1))))
</pre>


<p>See also <a href="#assoc">assoc</a> for retrieving associations and <a href="#setf">setf</a> 
for modifying association lists.</p>

<br/><br/>

<a name="post-url"></a>
<h2><span class="function">post-url</span></h2>
<h4>syntax: (post-url <em>str-url</em> <em>str-content</em> [<em>str-content-type</em> [<em>str-option</em>] [<em>int-timeout</em> [ <em>str-header</em>]]])</h4>

<p>
Sends an HTTP POST request to the URL in <em>str-url</em>.
POST requests are used to post information collected from web entry forms to a web site.
Most of the time,
the function <tt>post-url</tt> mimics what a web browser would do when sending information 
collected in an HTML form to a server,
but it can also be used to upload files (see an HTTP reference).
The function returns the page returned from the server in a string.
</p>

<p>
When <tt>post-url</tt> encounters an error,
it returns a string description of the error beginning with <tt>ERR:</tt>.
</p>

<p>
The last parameter,
<em>int-timeout</em>,
is for an optional timeout value,
which is specified in milliseconds.
When no response from the host is received before the timeout has expired,
the string <tt>ERR:
timeout</tt> is returned. 
</p>
<!-- example -->

<pre>
;; specify content type
(post-url "http://somesite.com/form.pl" 
          "name=johnDoe&amp;city=New%20York" 
          "application/x-www-form-urlencoded")

;; specify content type and timeout
(post-url "http://somesite.com/form.pl" 
          "name=johnDoe&amp;city=New%20York" 
          "application/x-www-form-urlencoded" 8000)

;; assumes default content type and no timeout
(post-url "http://somesite.com/form.pl"
          "name=johnDoe&amp;city=New%20York" 
</pre>


<p>
The above example uploads a user name and city using a special format called 
<tt>application/x-www-form-urlencoded</tt>.
<tt>post-url</tt> can be used to post other content types such as files or binary data.
See an HTTP reference for other content-type specifications and data encoding formats.
When the content-type parameter is omitted,
<tt>post-url</tt> assumes <tt>application/x-www-form-urlencoded</tt> as the default content type.
</p>

<h3>Additional parameters</h3>
<p>
When <em>str-content-type</em> is specified, the optional <em>str-option</em> 
can take the same options as <a href="#get-url">get-url</a> for the returned 
content.  If the <em>int-timeout</em> option is specified, the custom header 
option <em>str-header</em> can be specified, as well. See the function 
<a href="#get-url">get-url</a> for details on all options.
</p>

<p>
See also the <a href="#get-url">get-url</a> and <a href="#put-url">put-url</a> functions.
</p>

<br/><br/>

<a name="pow"></a>
<h2><span class="function">pow</span></h2>
<h4>syntax: (pow <em>num-1</em> <em>num-2 </em> [<em>num-3</em> ... ])<br/>
syntax: (pow <em>num-1</em>)</h4>

<p>
Calculates <em>num-1</em> to the power of <em>num-2</em> and so forth.
</p>

<!-- example -->

<pre>
(pow 100 2)      <span class='arw'>&rarr;</span> 10000
(pow 100 0.5)    <span class='arw'>&rarr;</span> 10
(pow 100 0.5 3)  <span class='arw'>&rarr;</span> 1000

(pow 3)  <span class='arw'>&rarr;</span> 9
</pre>


<p>
When <em>num-1</em> is the only argument,
<tt>pow</tt> assumes 2 for the exponent.
</p>

<br/><br/>

<a name="prefix"></a>
<h2><span class="function">prefix</span></h2>
<h4>syntax: (prefix <em>sym</em>)</h4>

<p>Returns the context of a symbol in <em>sym</em>:</p>


<!-- example -->

<pre>
(setf s 'Foo:bar)      <span class='arw'>&rarr;</span> Foo:bar
(prefix s)             <span class='arw'>&rarr;</span> Foo
(context? (prefix s))  <span class='arw'>&rarr;</span> true

(term s)                         <span class='arw'>&rarr;</span> "bar"
(= s (sym (term s) (prefix s)))  <span class='arw'>&rarr;</span> true

<b>&gt;</b>(context (prefix s))   ; switches to context Foo
<b>Foo</b>
<b>Foo&gt;</b>
</pre>

<p>See also <a href="#term">term</a> to extract the term part of
a symbol.</p>

<br/><br/>

<a name="pretty-print"></a>

<h2><span class="function">pretty-print</span></h2>
<h4>syntax: (pretty-print [<em>int-length</em> [<em>str-tab</em> [<em>str-fp-format</em>]])</h4>

<p>
Reformats expressions for <a href="#print">print</a>,
<a href="#save">save</a>,
or <a href="#source">source</a> and when printing in an interactive console.
The first parameter, <em>int-length</em>, specifies the maximum line length,
and <em>str-tab</em> specifies the string used to indent lines. The third
parameter <em>str-fp-format</em> describes the default format for printing
floating point numbers.  All parameters are optional.  <tt>pretty-print</tt> 
returns the current settings or the new settings when parameters are specified.
</p>

<!-- example -->

<pre>
(pretty-print)  <span class='arw'>&rarr;</span> (80 " " "&#037;1.15g")  ; default setting

(pretty-print 90 "\t")  <span class='arw'>&rarr;</span> (90 "\t" "&#037;1.15g")

(pretty-print 100)  <span class='arw'>&rarr;</span> (100 "\t" "&#037;1.15g") 

(sin 1)    <span class='arw'>&rarr;</span> 0.841470984807897 
(pretty-print 80 " " "&#37;1.3f")
(sin 1)    <span class='arw'>&rarr;</span> 0.841

(set 'x 0.0)
x   <span class='arw'>&rarr;</span> 0.000
</pre>


<p>
The first example reports the default settings of 80 for the maximum line length and a 
<tt>space</tt> character for indenting. The second example changes the line length to 
90 and the indent to a TAB character. The third example changes the line length only.
The last example changes the default format for floating point numbers. This is useful
when printing unformatted floating point numbers without fractional parts, and these 
numbers should still be recognizable as floating point numbers. Without the custom
format, <tt>x</tt> would be printed as <tt>0</tt> indistinguishable from floating
point number. All situations where unformatted floating point numbers are printed,
are affected.</p>

<p>
Note that <tt>pretty-print</tt> cannot be used to prevent line breaks from being printed.
To completely suppress pretty printing, use the function <a href="#string">string</a> 
to convert the expression to a raw unformatted string as follows:</p>

<!-- example -->

<pre>
;; print without formatting

(print (string my-expression))	
</pre>

<br/><br/>

<a name="primitivep"></a>
<h2><span class="function">primitive?</span></h2>
<h4>syntax: (primitive? <em>exp</em>)</h4>

<p>
Evaluates and tests if <em>exp</em> is a primitive symbol and returns
<tt>true</tt> or <tt>nil</tt> depending on the result. All built-in
functions and functions created using <a href="#import">import</a>
are primitives.
</p>

<!-- example -->

<pre>
(set 'var define)
(primitive? var)  <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="print"></a>
<h2><span class="function">print</span></h2>
<h4>syntax: (print <em>exp-1</em> [<em>exp-2</em> ... ])</h4>

<p>
Evaluates and prints <em>exp-1</em>&mdash;
to the current I/O device,
which defaults to the console window.
See the built-in function <a href="#device">device</a> for details on how to specify a different I/O device.
</p>

<p>
List expressions are indented by the nesting levels of their opening parentheses.
</p>

<p>
Several special characters may be included in strings encoded with the escape character <tt>\</tt>:
</p>
 
<table  summary="escape characters in print">
<tr align="left" valign="bottom"><th>character</th><th>description</th></tr>

<tr>
<td><tt>\n</tt></td>
<td>the line-feed character (ASCII 10)</td>

</tr>

<tr>
<td><tt>\r</tt></td>
<td>the carriage-return character (ASCII 13)</td>
</tr>

<tr>
<td><tt>\t</tt></td>
<td>the tab character (ASCII 9)</td>
</tr>

<tr>

<td><tt>\nnn</tt></td>
<td>where <tt>nnn</tt> is a decimal ASCII code between 000 and 255</td>
</tr>

<tr>
<td><tt>\xnn</tt></td>
<td>where <tt>nn</tt> is a hexadecimal ASCII code between 00 and FF</td>
</tr>

</table><br/>

<br/>
<!-- example -->

<pre>
(print (set 'res (+ 1 2 3))) 
(print "the result is" res "\n")

"\065\066\067"  <span class='arw'>&rarr;</span> "ABC"
</pre>


<p>
	To finish printing with a line-feed,
	use <a href="#println">println</a>.
</p>

<br/><br/>

<a name="println"></a>
<h2><span class="function">println</span></h2>
<h4>syntax: (println <em>exp-1</em> [<em>exp-2</em> ... ])</h4>

<p>
	Evaluates and prints <em>exp-1</em>&mdash;
	to the current I/O device,
	which defaults to the console window.
	A line-feed is printed at the end.
	See the built-in function <a href="#device">device</a> for details on how to specify a different I/O device.
	<tt>println</tt> works exactly like <a href="#print">print</a> but emits a line-feed character at the end.
</p>

<p>
	See also the <a href="#write-line">write-line</a> and <a href="#print">print</a> functions.
</p>

<br/><br/>

<a name="prob-chi2"></a>
<h2><span class="function">prob-chi2</span></h2>
<h4>syntax: (prob-chi2 <em>num-chi2</em> <em>int-df</em>)</h4>

<p>Returns the probability of an observed <em>Chi&sup2;</em> statistic in <em>num-chi2</em> 
with <em>num-df</em> degrees of freedom to be equal or greater under the null hypothesis.
<tt>prob-chi2</tt> is derived from the incomplete Gamma function <a HREF="#gammai">gammai</a>.
</p>

<!-- example -->

<pre>
(prob-chi2 10 6)  <span class='arw'>&rarr;</span> 0.1246520195
</pre>


<p>
	See also the inverse function <a href="#crit-chi2">crit-chi2</a>.
</p> 

<br/><br/>

<a name="prob-f"></a>
<h2><span class="function">prob-f</span></h2>
<h4>syntax: (prob-f <em>num-f</em> <em>int-df1</em> <em>int-df2</em>)</h4>

<p>Returns the probability of an observed <em>F</em> statistic in <em>num-f</em> 
with <em>int-df1</em> and <em>int-df2</em> degrees of freedom to be equal or greater 
under the null hypothesis.</p>

<!-- example -->

<pre>
(prob-f 2.75 10 12)  <span class='arw'>&rarr;</span> 0.0501990804
</pre>


<p>
See also the inverse function <a href="#crit-f">crit-f</a>.
</p> 

<br/><br/>

<a name="prob-t"></a>
<h2><span class="function">prob-t</span></h2>
<h4>syntax: (prob-t <em>num-t</em> <em>int-df1</em>)</h4>

<p>Returns the probability of an observed <em>Student's t</em> statistic in <em>num-t</em> 
with <em>int-df</em> degrees of freedom to be equal or greater 
under the null hypothesis.</p>

<!-- example -->

<pre>
(prob-t 1.76 14)  <span class='arw'>&rarr;</span> 0.05011454551
</pre>


<p>
See also the inverse function <a href="#crit-t">crit-t</a>.
</p> 

<br/><br/>

<a name="prob-z"></a>
<h2><span class="function">prob-z</span></h2>
<h4>syntax: (prob-z <em>num-z</em>)</h4>

<p>
	Returns the probability of <em>num-z</em>,
	not to exceed the observed value where <em>num-z</em> is a normal distributed 
	value with a mean of <tt>0.0</tt> and a standard deviation of <tt>1.0</tt>.
</p>

<!-- example -->

<pre>
(prob-z 0.0)  <span class='arw'>&rarr;</span> 0.5
</pre>



<p>
	See also the inverse function <a href="#crit-z">crit-z</a>.
</p> 

<br/><br/>

<a name="process"></a>
<h2><span class="function">process</span></h2>
<h4>syntax: (process <em>str-command</em>)<br/>
syntax: (process <em>str-command</em> <em>int-pipe-in</em> <em>int-pipe-out</em> [<em>int-win-option</em>])<br/>
syntax: (process <em>str-command</em> <em>int-pipe-in</em> <em>int-pipe-out</em> [<em>int-unix-pipe-error</em>])</h4>

<p>
In the first syntax,
<tt>process</tt> launches a  process specified in <em>str-command</em> and immediately 
returns with a process ID or <tt>nil</tt> if a process could not be created. This 
process will execute the program specified or immediately die if <em>str-command</em> could not be executed.
</p>

<p>On macOS and other Unixes, the application or script must be specified with its full path-name.
The new process inherits the OS environment from the parent process.</p>

<p>Command line arguments are parsed out at spaces. Arguments containing spaces must be delimited using
single quotes on macOS and other Unixes. On MS Windows, double quotes are used. The process id returned
can be used to destroy the running process using <a href="#destroy">destroy</a>, if the process does
not exit by itself.</p>

<!-- example -->

<pre>
(process "c:/WINDOWS/system32/notepad.exe")  <span class='arw'>&rarr;</span> 1894 ; Windows
; or when in executable path
(process "notepad.exe")                      <span class='arw'>&rarr;</span> 1894 ; Windows


; find out the path of the program to start using exec, 
; if the path is not known

(process (first (exec "which xclock")))  <span class='arw'>&rarr;</span> 22607 ; on Unix
</pre>


<p>If the path of the executable is unknown, <tt>exec</tt> together with the Unix <tt>which</tt>
command can be used to start a program. The pid returned can be used to <a href="#destroy">destroy</a>
the process.</p>

<p>In the second syntax,
standard input and output of the created process can be redirected to pipe handles.
When remapping standard I/O of the launched application to a pipe,
it is possible to communicate with the other application via <a href="#write-line">write-line</a> 
and <a href="#read-line">read-line</a>  or <a href="#write">write</a> and 
<a href="#read">read</a> statements:</p>

<!-- example -->

<pre>
;; Linux/Unix
;; create pipes
(map set '(myin bcout) (pipe))
(map set '(bcin myout) (pipe))   

;; launch Unix 'bc' calculator application
(process "/usr/bin/bc" bcin bcout) <span class='arw'>&rarr;</span> 7916

(write-line myout "3 + 4")  ; bc expects a line-feed

(read-line myin)  <span class='arw'>&rarr;</span> "7"


;; bc can use bignums with arbitrary precision

(write-line myout "123456789012345 * 123456789012345")

(read-line myin)  <span class='arw'>&rarr;</span> "15241578753238669120562399025"

;; destroy the process
(destroy 7916)

;; MS Windows
(map set '(myin cmdout) (pipe))
(map set '(cmdin myout) (pipe))

(process "c:/Program Files/newlisp/newlisp.exe -c" cmdin cmdout)
<span class='arw'>&rarr;</span> 1284

(write-line myout "(+ 3 4)")

(read-line myin) <span class='arw'>&rarr;</span> "7"

;; destroy the process
(destroy 1284)
</pre>


<p>On MS Windows versions of newLISP, a fourth optional parameter of <em>int-win-option</em> 
can be specified to control the display status of the application.
This option defaults to <tt>1</tt> for showing the application's window,
<tt>0</tt> for hiding it, and <tt>2</tt> for showing it minimized on the Windows 
launch bar.</p>

<p>On both MS Windows and Linux/Unix systems, standard error will be redirected to 
standard out by default. On Linux/Unix, an optional pipe handle for standard 
error output can be defined in <em>int-unix-pipe-error</em>.</p>

<p>The function <a href="#peek">peek</a> can be used to check for information 
on the pipe handles:</p>


<pre>
;; create pipes
(map set '(myin bcout) (pipe))
(map set '(bcin myout) (pipe))   
(map set '(errin errout) (pipe))   

;; launch Unix 'bc' calculator application
(process "bc" bcin bcout errout)

(write myout command)

;; wait for bc sending result or error info
(while (and (= (peek myin) 0)
            (= (peek errin) 0)) (sleep 10))

(if (&gt; (peek errin) 0)
	(println (read-line errin)))
	
(if (&gt; (peek myin) 0)
	(println (read-line myin)))
</pre>


<p>
	 Not all interactive console applications 
   can have their standard I/O channels remapped.
	Sometimes only one channel,
	<em>in</em> or <em>out</em>,
	can be remapped.
	In this case,
	specify <tt>0</tt> (zero) for the unused channel.
	The following statement uses only the launched application's output:
</p>


<pre>
(process "app" 0 appout)
</pre>


<p>
	Normally,
	two pipes are used:
	one for communications to the child process and the other one for communications from the child process.
</p>

<p>
See also the <a href="#pipe">pipe</a> and <a href="#share">share</a> functions for inter-process 
communications and the <a href="#semaphore">semaphore</a> function for synchronization of several processes.
See the <a href="#fork">fork</a> and <a href="#spawn">spawn</a> functions for other ways of starting 
newLISP processes. Both are only available on macOS, Linux and other Unix like operating systems.
</p>

<br/><br/>

<a name="prompt-event"></a>
<h2><span class="function">prompt-event</span></h2>
<h4>syntax: (prompt-event <em>sym-event-handler</em> | <em>func-event-handler</em>)<br/>
syntax: (prompt-event nil)</h4>

<p>Refines the prompt as shown in the interactive newLISP shell.
The <em>sym-event-handler</em> or <em>func-event-handler</em>
is either a symbol of a user-defined function or a lambda expression:</p>

<p>To reset <tt>prompt-event</tt> to the original state, use the second syntax.</p>

<pre>
<b>></b> (prompt-event (fn (ctx) (string ctx ":" (real-path) "$ ")))
<b>$prompt-event</b>
<b>MAIN:/Users/newlisp$</b> (+ 3 4)
<b>7</b>
<b>MAIN:/Users/newlisp$</b>
</pre>

<p>The current context before calling the <tt>prompt-event</tt> code is passed as a 
parameter to the function. Computer output is shown in bold.</p>

<p>The example redefines the <tt>&gt;</tt> prompt to be the current context followed
by a colon <tt>:</tt>, followed by the directory name, followed by the dollar symbol. 
Together with the <a href="#command-event">command-event</a> function this can be 
used to create fully customized shells or custom command interpreters.</p>

<p>The function in <tt>prompt-event</tt> must return a string of 63 characters maximum. 
Not returning a string will leave the prompt unchanged.</p>

<br/><br/>

<a name="protectedp"></a>
<h2><span class="function">protected?</span></h2>

<h4>syntax: (protected? <em>sym</em>)</h4>

<p>Checks if a symbol in <em>sym</em> is protected. Protected symbols are built-in
functions, context symbols, and all symbols made constant using the <a href="#constant">constant</a>
function:</p>


<pre>
(protected? 'println)    <span class='arw'>&rarr;</span> true
(constant 'aVar 123)
(protected? 'aVar)       <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="push"></a>
<h2><span class="function">push</span>&nbsp;<a href="#destructive">!</a>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (push <em>exp</em> <em>list</em> [<em>int-index-1</em> [<em>int-index-2</em> ... ]])<br/>
syntax: (push <em>exp</em> <em>list</em> [<em>list-indexes</em>])<br/><br/>

syntax: (push <em>str-1</em> <em>str-2</em> [<em>int-index</em>])</h4>

<p>
Inserts the value of <em>exp</em> into the list <em>list</em>.
If <em>int-index</em> is present, the element is inserted at that index.
If the index is absent, the element is inserted at index <tt>0</tt> (zero),
the first element. <tt>push</tt> is a destructive operation that changes the 
contents of the target list.</p>

<p>The list changed is returned as a reference on which other built-in
functions can work. See also <a href="#indexing">Indexing elements of 
strings and lists</a>.</p>

<p>
If more than one <em>int-index</em> is present, the indices are used to 
access a nested list structure. Improper indices (those not matching list 
elements) are discarded.</p>

<p>
The second version takes a list of <em>list-indexes</em> but is otherwise 
identical to the first. In this way, <tt>push</tt> works easily together 
with <a href="#ref">ref</a> and <a href="#ref-all">ref-all</a>,
which return lists of indices.
</p>

<p>
If <em>list</em> does not contain a list, <em>list</em> must contain a 
<tt>nil</tt> and will be initialized to the empty list.
</p>

<p>
Repeatedly using <tt>push</tt> to the end of a list using <tt>-1</tt> as 
the <em>int-index</em> is optimized and as fast as pushing 
to the front of a list with no index at all. This can be used to efficiently 
grow a list.
</p>

<!-- example -->

<pre>
; inserting in front
(set 'pList '(b c))  <span class='arw'>&rarr;</span> (b c)
(push 'a pList)      <span class='arw'>&rarr;</span> (a b c)
pList                <span class='arw'>&rarr;</span> (a b c)

; insert at index
(push "hello" pList 2)  <span class='arw'>&rarr;</span> (a b "hello" c)

; optimized appending at the end
(push 'z pList -1)  <span class='arw'>&rarr;</span> (a b "hello" c z)

; inserting lists in lists
(push '(f g) pList)  <span class='arw'>&rarr;</span> ((f g) a b "hello" c z)

; inserting at negative index
(push 'x pList -3)  <span class='arw'>&rarr;</span> ((f g) a b "hello" x c z)

; using multiple indices
(push 'h pList 0 -1)  <span class='arw'>&rarr;</span> ((f g h) a b "hello" x c z)

; use indices in a list
(set 'pList '(a b (c d () e)))

(push 'x pList '(2 2 0))  <span class='arw'>&rarr;</span> (a b (c d (x) e))

(ref 'x pList)   <span class='arw'>&rarr;</span> (2 2 0)

(pop pList '(2 2 0))  <span class='arw'>&rarr;</span> x

; the target list is a place reference
(set 'lst '((a 1) (b 2) (c 3) (d)))

(push 4 (assoc 'd lst) -1) <span class='arw'>&rarr;</span> (d 4)

lst <span class='arw'>&rarr;</span> ((a 1) (b 2) (c 3) (d 4))


; push on un-initialized symbol
aVar  <span class='arw'>&rarr;</span> nil 

(push 999 aVar)  <span class='arw'>&rarr;</span> (999)

aVar  <span class='arw'>&rarr;</span> (999)
</pre>


<p><tt>push</tt> and pop can be combined to model a queue:</p>

<pre>
; pop and push a as a queue
(set 'Q '(a b c d e))

(pop (push 'f Q -1)) <span class='arw'>&rarr;</span> a
(pop (push 'g Q -1)) <span class='arw'>&rarr;</span> b

Q <span class='arw'>&rarr;</span>  (c d e f g)
</pre>


<p>Because <tt>push</tt> returns a reference to the modified list,
<tt>pop</tt> can work on it directly.</p>

<p>In the third syntax <tt>push</tt> can be used to change strings. When
<em>int-index</em> is used, it refers to character positions rather than 
byte positions. UTF-8 characters may be multi-byte characters.</p>

<pre>
;; push on strings

(set 'str "abcdefg")

(push "hijk" str -1)  <span class='arw'>&rarr;</span> "abcdefghijk"
str                   <span class='arw'>&rarr;</span> "abcdefghijk"

(push "123" str)  <span class='arw'>&rarr;</span> "123abcdefghijk"
(push "4" str 3)  <span class='arw'>&rarr;</span> "1234abcdefghijk"

(set 'str "\u03b1\u03b2\u03b3")  <span class='arw'>&rarr;</span>  "αβγ"

(push "*" str 1)  <span class='arw'>&rarr;</span>  "α*βγ"

;; push on a string reference

(set 'lst '("abc" "xyz"))

(push x (lst 0)) <span class='arw'>&rarr;</span> "xabc"

lst <span class='arw'>&rarr;</span> ("xabc" "xyz")
</pre>

<p>See also the <a href="#pop">pop</a> function, which is the inverse operation to <tt>push</tt>.
</p>

<br/><br/>

<a name="put-url"></a>
<h2><span class="function">put-url</span></h2>
<h4>syntax: (put-url <em>str-url</em> <em>str-content</em> [<em>str-option</em>] [<em>int-timeout</em> [<em>str-header</em>]])</h4>

<p>
	The HTTP PUT protocol is used to transfer information in <em>str-content</em> 
	to a file specified in <em>str-url</em>. The lesser-known HTTP PUT mode is 
	frequently used for transferring web pages from HTML editors to Web servers.
	In order to use PUT mode, the web server's software must be configured correctly.
	On the Apache web server,
	use the <tt>'Script PUT'</tt> directive in the section where directory access rights are configured.
</p>

<p>If <em>str-url</em> starts with <tt>file://</tt> then <em>str-content</em> is written
to the local file system.</p>

<p>
	Optionally,
	an <em>int-timeout</em> value can be specified in milliseconds as the last parameter.
	<tt>put-url</tt> will return <tt>ERR:
	timeout</tt> when the host gives no response and the timeout expires.
	On other error conditions,
	<tt>put-url</tt> returns a string starting with <tt>ERR:</tt> and the description of the error.
</p>

<p><tt>put-url</tt> requests are also understood by newLISP server nodes, but will
not be served when the server is started in <tt>-http-safe</tt> mode.</p>

<!-- example -->

<pre>
(put-url "http://asite.com/myFile.txt" "Hi there")
(put-url "http://asite.com/myFile.txt" "Hi there" 2000)

(put-url "http://asite.com/webpage.html" 
    (read-file "webpage.html"))

; write /home/joe/newfile.txt on the local file system
(puts-url "file:///home/joe/newfile.txt" "Hello World!")
</pre>


<p>
	The first example creates a file called <tt>myFile.txt</tt> on the target server
	and stores the text string <tt>'Hi there'</tt> in it.
	In the second example,
	the local file <tt>webpage.html</tt> is transferred to <tt>asite.com</tt>.
</p>

<p>
	On an Apache web server,
	the following could be configured in <tt>httpd.conf</tt>.
</p>

<!-- example -->

<pre>
&lt;directory /www/htdocs&gt;
Options All
Script PUT /cgi-bin/put.cgi
&lt;/directory&gt;
</pre>


<p>
	 The script <tt>put.cgi</tt> would contain code to receive content from the web server via STDIN.
	The following is a working <tt>put.cgi</tt> written in newLISP for the Apache web server:
</p>

<!-- example -->
<pre>
#!/usr/home/johndoe/bin/newlisp
#
#
# get PUT method data from CGI STDIN 
# and write data to a file specified
# int the PUT request
# 
#


(print "Content-Type: text/html\n\n")

(set 'cnt 0)
(set 'result "")

(if (= "PUT" (env "REQUEST_METHOD"))
    (begin
      (set 'len (int (env "CONTENT_LENGTH")))

      (while (&lt; cnt len)
          (set 'n (read (device) buffer len))
          (if (not n) 
            (set 'cnt len) 
            (begin 
              (inc cnt n)
              (write result buffer))))
              
      (set 'path (append 
              "/usr/home/johndoe" 
              (env "PATH_TRANSLATED")))

      (write-file path result)
    )
)

(exit)
</pre>


<p>
	Note that the script appends ".txt" to the path to avoid the CGI execution of uploaded malicious scripts.
	Note also that the two lines where the file path is composed may work differently in your web server environment.
	Check environment variables passed by your web server for composition of the right file path.
</p>

<p>
	<tt>put-url</tt> returns content returned by the <tt>put.cgi</tt> script.
</p>

<h3>Additional parameters</h3>
<p>
In <em>str-option</em> can take the same options as <a href="#get-url">get-url</a>
for the returned content. If the <em>int-timeout</em> option is specified, the 
custom header option <em>str-header</em> can be specified, as well. See the 
function <a href="#get-url">get-url</a> for details on all options.
</p>

<p>
See also the functions <a href="#get-url">get-url</a> and <a href="#post-url">post-url</a>,
which can be used to upload files when formatting form data as <tt>multipart/form-data</tt>.
</p>

<br/><br/>

<a name="pv"></a>
<h2><span class="function">pv</span></h2>
<h4>syntax: (pv <em>num-int</em> <em>num-nper</em> <em>num-pmt</em> 
[<em>num-fv</em> [<em>int-type</em>]])</h4>

<p>Calculates the present value of a loan with the constant interest rate 
<em>num-interest</em> and the constant payment <em>num-pmt</em> after 
<em>num-nper</em> number of payments. The future value <em>num-fv</em> 
is assumed to be <tt>0.0</tt> if omitted. If payment is at the end of the 
period, <em>int-type</em> is <tt>0</tt> (zero) or <em>int-type</em> is omitted; 
for payment at the beginning of each period, <em>int-type</em> is 1.</p>

<!-- example -->

<pre>
(pv (div 0.07 12) 240 775.30)  <span class='arw'>&rarr;</span> -100000.1373
</pre>

<p>
	In the example,
	a loan that would be paid off (future value = <tt>0.0</tt>) in 240 payments of $775.30 at a 
	constant interest rate of 7 percent per year would start out at $100,000.14.
</p>

<p>
	See also the <a href="#fv">fv</a>,
	<a href="#irr">irr</a>,
	<a href="#nper">nper</a>,
	<a href="#npv">npv</a>,
	and <a href="#pmt">pmt</a> functions.
</p>

<br/><br/>

<a name="quote"></a>
<h2><span class="function">quote</span></h2>
<h4>syntax: (quote <em>exp</em>)</h4>

<p>Returns <em>exp</em> without evaluating it. The same effect can be obtained by 
prepending a <tt>'</tt> (single quote) to <em>exp</em>. The function <tt>quote</tt> 
is resolved during runtime, the prepended <tt>'</tt> quote is translated into a 
protective envelope (quote cell) during code translation.</p>

<!-- example -->

<pre>
(quote x)         <span class='arw'>&rarr;</span> x
(quote 123)       <span class='arw'>&rarr;</span> 123
(quote (a b c))   <span class='arw'>&rarr;</span> (a b c)
(= (quote x) 'x)  <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="quotep"></a>
<h2><span class="function">quote?</span></h2>
<h4>syntax: (quote? <em>exp</em>)</h4>

<p>
	 Evaluates and tests whether <em>exp</em> is quoted.
	Returns <tt>true</tt> or <tt>nil</tt> depending on the result.
</p>

<!-- example -->

<pre>
(set 'var ''x)  <span class='arw'>&rarr;</span> 'x
(quote? var)    <span class='arw'>&rarr;</span> true
</pre>

<p>
	 Note that in the <tt>set</tt> statement,
	<tt> ''x</tt> is quoted twice because the first quote 
  is lost during the evaluation of the <tt>set</tt> assignment.
</p>

<br/><br/>

<a name="rand"></a>
<h2><span class="function">rand</span></h2>
<h4>syntax: (rand <em>int-range</em> [<em>int-N</em>])</h4>

<p>
	Evaluates the expression in <em>int-range</em> 
	and generates a random number in the range of 
	<tt>0</tt> (zero) to (<em>int-range</em> - 1). 
	When <tt>0</tt> (zero) is passed, 
	the internal random generator 
	is initialized using 
	the current value returned by 
	the C <tt>time()</tt> function. 
	Optionally, a second parameter 
	can be specified to return 
	a list of length <em>int-N</em> 
	of random numbers.
</p>

<!-- example -->

<pre>
(dotimes (x 100) (print (rand 2))) =&gt;
11100000110100111100111101 ... 10111101011101111101001100001000

(rand 3 100)  <span class='arw'>&rarr;</span> (2 0 1 1 2 0 &hellip;)
</pre>


<p>
	The first line in the example 
	prints equally distributed <tt>0</tt>'s and <tt>1</tt>'s, 
	while the second line produces a list 
	of 100 integers with 
	<tt>0</tt>, <tt>1</tt>, and <tt>2</tt> equally distributed. 
	Use the <a href="#random">random</a> 
	and <a href="#normal">normal</a> functions
	to generate floating point
	random numbers, 
	and use <a href="#seed">seed</a> to vary 
	the initial seed 
	for random number generation.
</p>

<br/><br/>

<a name="random"></a>
<h2><span class="function">random</span></h2>
<h4>syntax: (random <em>float-offset</em> <em>float-scale</em> <em>int-n</em>)<br/>
syntax: (random <em>float-offset</em> <em>float-scale</em>)</h4>

<p>
	In the first form, 
	<tt>random</tt> returns a list of <em>int-n</em> 
	evenly distributed floating point numbers 
	scaled (multiplied) by <em>float-scale</em>, 
	with an added offset of <em>float-offset</em>. 
	The starting point of the internal random generator 
	can be seeded using <a href="#seed">seed</a>.
</p>

<!-- example -->

<pre>
(random 0 1 10)
<span class='arw'>&rarr;</span> (0.10898973 0.69823783 0.56434872 0.041507289 0.16516733
    0.81540917 0.68553784 0.76471068 0.82314585 0.95924564)
</pre>


<p>
	When used in the second form, 
	<tt>random</tt> returns a single 
	evenly distributed number:
</p>


<pre>
(random 10 5)  <span class='arw'>&rarr;</span> 11.0971
</pre>

<p> When no parameters are given, <tt>random</tt> assumes a mean of <tt>0.0</tt> 
and a standard deviation of <tt>1.0</tt>.</p>

<p>
	See also the <a href="#normal">normal</a> 
	and <a href="#rand">rand</a> functions.
</p>

<br/><br/>

<a name="randomize"></a>
<h2><span class="function">randomize</span></h2>
<h4>syntax: (randomize <em>list</em> [<em>bool</em>])</h4>

<p>
	Rearranges the order of elements in <em>list</em> 
	into a random order.
</p>

<!-- example -->

<pre>
(randomize '(a b c d e f g))  <span class='arw'>&rarr;</span> (b a c g d e f)
(randomize (sequence 1 5))    <span class='arw'>&rarr;</span> (3 5 4 1 2)
</pre>


<p>
	<tt>randomize</tt> will always return 
	a sequence different from the previous one 
	without the optional <em>bool</em> flag. 
	This may require the function to calculate 
	several sets of reordered elements, 
	which in turn may lead to different processing times 
	with different invocations of the function 
	on the same input list length. 
	To allow for the output to be equal 
	to the input, <tt>true</tt> 
	or any expression evaluating to 
	not <tt>nil</tt> 
	must be specified in <em>bool</em>.
</p>

<p>
	<tt>randomize</tt> uses 
	an internal <em>pseudo random sequence</em> generator 
	that returns the same series of results 
	each time newLISP is started. 
	Use the <a href="#seed">seed</a> function to 
	change this sequence.
</p> 

<br/><br/>

<a name="read-buffer"></a>
<a name="read"></a>
<h2><span class="function">read</span>&nbsp;<a href="#destructive">!</a></h2>

<h4>syntax: (read <em>int-file</em> <em>sym-buffer</em> <em>int-size</em> [<em>str-wait</em>])</h4>

<p>
Reads a maximum of <em>int-size</em> bytes from a file specified in <em>int-file</em> 
into a buffer in <em>sym-buffer</em>.  Any data referenced by the symbol <em>sym-buffer</em> 
prior to the reading is deleted.  The handle in <em>int-file</em> is obtained from a 
previous <a href="#open">open</a> statement. The symbol <em>sym-buffer</em> contains 
data of type string after the read operation.  <em>sym-buffer</em> can also be a default 
functor specified by a context symbol for reference passing in and out of user-defined 
functions.</p>

<p><tt>read</tt> is a shorter writing of <tt>read-buffer</tt>. The longer
form still works but is deprecated and should be avoided in new code.</p>

<p>
	Optionally, 
	a string to be waited for 
	can be specified in <em>str-wait</em>. 
	<tt>read</tt> will read 
	a maximum amount of bytes 
	specified in <em>int-size</em> 
	or return earlier 
	if <em>str-wait</em> was found 
	in the data. 
	The wait-string is part
	of the returned data and must
    not contain binary <tt>0</tt> (zero)
    characters.
</p>

<p>
	Returns the number of bytes read or <tt>nil</tt> 
	when the wait-string was not found. 
	In any case, 
	the bytes read are put into the buffer 
	pointed to by <em>sym-buffer</em>, 
	and the file pointer of the file read 
	is moved forward. 
	If no new bytes have been read, 
	<em>sym-buffer</em> will contain <tt>nil</tt>.
</p>

<!-- example -->

<pre>
(set 'handle (open "aFile.ext" "read"))
(read handle buff 200)
</pre>


<p>
	Reads 200 bytes into the symbol <tt>buff</tt> 
	from the file <tt>aFile.ext</tt>.
</p>


<pre>
(read handle buff 1000 "password:")
</pre>


<p>
	Reads 1000 bytes or until 
	the string <tt>password:</tt> is encountered. 
	The string <tt>password:</tt> 
	will be part of the data returned.
</p>

<p>
	See also the <a href="#write">write</a> function. To start reading at 
	a specific position in the file, use the <a href="#seek">seek</a> function.
</p>

<br/><br/>

<a name="read-char"></a>
<h2><span class="function">read-char</span></h2>

<h4>syntax: (read-char [<em>int-file</em>])</h4>

<p>
Reads a byte from a file specified by the file handle in <em>int-file</em> 
or from the current I/O device - e.g. <em>stdin</em> - when no file handle is specified.
The file handle is obtained from a previous <a href="#open">open</a> operation. 
Each <tt>read-char</tt> advances the file pointer by one byte. 
Once the end of the file is reached, <tt>nil</tt> is returned.
</p>

<!-- example -->

<pre>
(define (slow-file-copy from-file to-file)
    (set 'in-file (open from-file "read"))
    (set 'out-file (open to-file "write"))
    (while (set 'chr (read-char in-file))
        (write-char out-file chr))
    (close in-file)
    (close out-file)
    "finished")
</pre>


<p>
	Use <a href="#read-line">read-line</a> 
	and <a href="#device">device</a> to read 
	whole text lines at a time. 
	Note that newLISP supplies 
	a fast built-in function 
	called <a href="#copy-file">copy-file</a> 
	for copying files.
</p>

<p>
	See also the <a href="#write-char">write-char</a> function.
</p>

<br/><br/>

<a name="read-expr"></a>
<h2><span class="function">read-expr</span></h2>
<h4>syntax: (read-expr <em>str-source</em> [<em>sym-context</em> [<em>exp-error</em> [<em>int-offset</em>]]])</h4>

<p><tt>read-expr</tt> parses the first expressions it finds in <em>str-source</em> and 
returns the translated expression without evaluating it. An optional context in 
<em>sym-context</em> specifies a namespace for the translated expression.</p>

<p>After a call to <tt>read-expr</tt> the system variable <tt>$count</tt> contains the 
number of characters scanned.</p>

<p>If an error occurs when translating <em>str-source</em> the expression in
<em>exp-error</em> is evaluated and the result returned.</p>

<p><em>int-offset</em> specifies an optional offset into <em>str-source</em> where
processing should start. When calling <tt>read-expr</tt> repeatedly this number
can be updated using <tt>$count</tt>, the number of characters processed.</p>

<!-- example -->

<pre>
(set 'code "; a statement\n(define (double x) (+ x x))")

(read-expr code) <span class='arw'>&rarr;</span> (define (double x) (+ x x))

$count <span class='arw'>&rarr;</span> 41

</pre>


<p><tt>read-expr</tt> behaves similar to <a href="#eval-string">eval-string</a>
but without the evaluation step:</p>


<pre>
(read-expr "(+ 3 4)")    <span class='arw'>&rarr;</span> (+ 3 4)

(eval-string "(+ 3 4)")  <span class='arw'>&rarr;</span> 7
</pre>


<p>Using <tt>read-expr</tt> a customized code reader can be programmed
preprocessing expressions before evaluation.</p>

<p>See also <a href="#reader-event">reader-event</a> for preprocessing
expressions event-driven.</p>

<br/><br/>

<a name="read-file"></a>
<h2><span class="function">read-file</span></h2>
<h4>syntax: (read-file <em>str-file-name</em>)</h4>

<p>Reads a file in <em>str-file-name</em> in one swoop and returns a string buffer 
containing the data.</p>

<p>On failure the function returns <tt>nil</tt>. For error information, 
use <a href="#sys-error">sys-error</a> when used on files. When used
on URLs <a href="#net-error">net-error</a> gives more error
information.</p>

<!-- example -->

<pre>
(write-file "myfile.enc" 
    (encrypt (read-file "/home/lisp/myFile") "secret"))
</pre>


<p>
The file <tt>myfile</tt> is read, then encrypted using the password <tt>"secret"</tt> 
before being written back into a new file titled <tt>"myfile.enc"</tt> 
in the current directory.</p>

<p>
<tt>read-file</tt> can take an <tt>http://</tt> 
or <tt>file://</tt> URL in <em>str-file-name</em>. 
When the prefix is <tt>http://</tt>, <tt>read-file</tt> works exactly like 
<a href="#get-url">get-url</a> and can take the same additional parameters.</p>

<!-- example -->

<pre>
(read-file "http://asite.com/somefile.tgz" 10000)
</pre>


<p>
	The file <tt>somefile.tgz</tt> is retrieved from 
	the remote location <tt>http://asite.com</tt>. 
	The file transfer will time out after 10 seconds 
	if it is not finished. 
	In this mode, <tt>read-file</tt> can also be used 
	to transfer files from remote newLISP server nodes.</p>

<p>See also the <a href="#write-file">write-file</a> and 
<a href="#append-file">append-file</a> functions.
</p>

<br/><br/>

<a name="read-key"></a>
<h2><span class="function">read-key</span></h2>
<h4>syntax: (read-key [true])</h4>

<p>
Reads a key from the keyboard and returns an integer value. 
For navigation keys, more than one <tt>read-key</tt> call 
must be made depending of the platform OS. For keys representing 
ASCII characters, the return value is the same on all OSes, except 
for navigation keys and other control sequences like function keys, 
in which case the return values may vary on different OSes and 
configurations.
</p>

<p>When using the <tt>true</tt> flag the <tt>read-key</tt> is non-blocking
and a <tt>0</tt> (zero) is returned when no key has been pressed.
When not using the extra flag, the call to <tt>read-key</tt> is blocking
until a key is pressed.</p>



<!-- example -->

<pre>
(read-key)  <span class='arw'>&rarr;</span> 97  ; after hitting the A key
(read-key)  <span class='arw'>&rarr;</span> 65  ; after hitting the shifted A key
(read-key)  <span class='arw'>&rarr;</span> 10  ; after hitting [enter] on Linux
(read-key)  <span class='arw'>&rarr;</span> 13  ; after hitting [enter] on Windows

(read-key true)  <span class='arw'>&rarr;</span> 0 ; when no key has been pressed

(while (!= (set 'c (read-key)) 1) (println c))
</pre>


<p>
The last example can be used to check return sequences 
from navigation and function keys. To break out of the loop, 
press <tt>Ctrl-A</tt>.
</p>

<p>Note that <tt>read-key</tt> will only work when newLISP is running in a
Unix shell or Windows command shell. It will not work when executed by 
newLISP Unix shared library or newLISP MS Windows DLL (Dynamic Link Library).
These libraries are not listening to STD input.</p>

<br/><br/>

<a name="read-line"></a>

<h2><span class="function">read-line</span></h2>
<h4>syntax: (read-line [<em>int-file</em>])</h4>

<p>
	Reads from the current I/O device a string 
	delimited by a line-feed character (ASCII 10). 
	There is no limit 
	to the length of the string 
	that can be read. 
	The line-feed character is not part of the returned string. 
	The line always breaks on a line-feed, 
	which is then swallowed. 
	A line breaks on a carriage return (ASCII 13) 
	only if followed by a line-feed, 
	in which case both characters are discarded. 
	A carriage return alone only breaks and is swallowed 
	if it is the last character in the stream.
</p>

<p>
	By default, 
	the current <a href="#device">device</a> 
	is the keyboard (<a href="#device">device</a> <tt>0</tt>). 
	Use the built-in function <a href="#device">device</a> 
	to specify a different I/O device (e.g., a file). 
	Optionally, 
	a file handle can be specified 
	in the <em>int-file</em> obtained 
	from a previous <a href="#open">open</a> statement.
</p>

<p>
	The last buffer contents 
	from a read-line operation 
	can be retrieved using <a href="#current-line">current-line</a>.
</p>

<p>When <tt>read-line</tt> is reading from a file or from <em>stdin</em>
in a CGI program or pipe, it will return <tt>nil</tt> when input is exhausted.</p>

<p>When using <tt>read-line</tt> on <em>stdin</em>, line length is limited
to 2048 characters and performance is much faster.</p>

<!-- example -->

<pre>
(print "Enter a num:")
(set 'num (int (read-line)))

(set 'in-file (open "afile.dat" "read"))
(while (read-line in-file)
        (write-line))   
(close in-file)
</pre>


<p>
	The first example reads input from the keyboard 
	and converts it to a number. 
	In the second example, 
	a file is read line-by-line 
	and displayed on the screen. 
	The <tt>write-line</tt> statement 
	takes advantage of the fact 
	that the result from the last 
	<tt>read-line</tt> operation 
	is stored in a system internal buffer. 
	When <a href="#write-line">write-line</a> 
	is used without argument, 
	it writes the contents 
	of the last <tt>read-line</tt> buffer 
	to the screen.
</p>

<p>
	See also the <a href="#current-line">current-line</a> function
	for retrieving this buffer.
</p>

<br/><br/>

<a name="read-utf8"></a>
<h2><span class="function">read-utf8</span></h2>

<h4>syntax: (read-utf8 <em>int-file</em>)</h4>

<p>Reads an UTF-8 character from a file specified by the file handle in <em>int-file</em>. 
The file handle is obtained from a previous <a href="#open">open</a> operation. 
Each <tt>read-utf8</tt> advances the file pointer by the number of bytes contained
in the UTF-8 character.  Once the end of the file is reached, <tt>nil</tt> is returned. </p>

<p>The function returns an integer value which can be converted to a displayable UTF-8
character string using the <a href="#char">char</a> function.</p>

<!-- example -->
<pre>
(set 'fle (open "utf8text.txt" "read"))
(while (setq chr (read-utf8 fle))
	(print (char chr)))
</pre>


<p>The example reads a file containing UTF-8 encoded text and displays it to the
terminal screen.</p>

<br/><br/>


<a name="reader-event"></a>
<h2><span class="function">reader-event</span></h2>
<h4>syntax: (reader-event [<em>sym-event-handler | func-event-handler</em>])<br/>
syntax: (reader-event nil)</h4>

<p>An event handler can be specified to hook between newLISP's reader, 
translation and evaluation process. The function specified in 
<em>sym-event-handler</em> or <em>func-event-handler</em> gets called after 
newLISP translates an expression and before evaluating it. The event handler can do 
transformation on the expression before it gets evaluated.</p>

<p>Specifying <tt>nil</tt> for the event will reset it to the initial default state.</p>

<p>The following one-liner <tt>reader-event</tt> could be used to enhance
the interactive shell with a tracer:</p>

<!-- example -->
<pre>
<b>&gt;</b>(reader-event (lambda (ex) (print " =&gt; " ex)))
$reader-event
<b>&gt; (+ 1 2 3)
 =&gt; (+ 1 2 3)
6
&gt;</b>
</pre>

<p>The expression intercepted passes through unchanged, but output
is enhanced.</p>

<p>The reader event function will be called after each reading of an s-expression
by the <a href="#load">load</a> or <a href="#eval-string">eval-string</a> function.</p>

<p>In versions previous to 10.5.8 <tt>reader-event</tt> was used to define a 
<tt>macro</tt> expansion function in the module file <tt>macro.lsp</tt>. Starting 
version 10.5.8, newLISP has <a href="#macro">macro</a> as a built-in function 
behaving the same, but much faster when loading files and reading source.</p>

<br/><br/>

<a name="real-path"></a>
<h2><span class="function">real-path</span></h2>
<h4>syntax: (real-path [<em>str-path</em>])<br/>
syntax: (real-path <em>str-exec-name</em> true)
</h4>

<p>
In the first syntax <tt>real-path</tt> returns the full path from the relative 
file path given in <em>str-path</em>. If a path is not given, <tt>"."</tt> 
(the current directory) is assumed.</p>

<!-- example -->

<pre>
(real-path)  <span class='arw'>&rarr;</span> "/usr/home/fred"  ; current directory
(real-path "./somefile.txt")
<span class='arw'>&rarr;</span> "/usr/home/fred/somefile.txt"
</pre>

<p>In the second syntax <tt>real-path</tt> returns the full path for an 
executable found given in <em>str-exe-name</em>. This syntax relies on an 
environment variable PATH defined on UNIX and Windows systems.</p>

<pre>
(real-path "make" true) <span class='arw'>&rarr;</span> "/usr/bin/make"
</pre>

<p>The output length is limited by the OS's maximum allowed path length. 
If <tt>real-path</tt> fails (e.g., because of a nonexistent path), 
<tt>nil</tt> is returned.</p>

<br/><br/>

<a name="receive"></a>
<h2><span class="function">receive</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (receive <em>int-pid</em> <em>sym-message</em>)<br/>
syntax: (receive)</h4>

<p>In the first syntax, the function is used for message exchange between 
child processes launched with <a href="#spawn">spawn</a> and their parent 
process. The message received replaces the contents in <em>sym-message.</em></p>

<p>The function reads one message from the receiver queue of <em>int-pid</em>
for each invocation. When the queue is empty, <tt>nil</tt> is returned.</p>

<!-- example -->

<pre>
; sending process
(send spid "hello")  <span class='arw'>&rarr;</span> true

; receiving process
(receive pid msg)    <span class='arw'>&rarr;</span> true
msg                  <span class='arw'>&rarr;</span> "hello"
</pre>

<p>To make <tt>receive</tt> blocking and wait for arriving messages,
use the following form:</p>

<pre>
; wait until a message can be read
(until (receive pid msg))
</pre>

<p>The function will loop until a message can be read from the queue.</p>

<p>In the second syntax, the function returns a list of all child processes
with pending messages for the parent process:</p>

<!-- example -->
<pre>
; read pending messages from child processes
(dolist (pid (receive))
    (receive pid msg)
    (println "received message: " msg " from:" pid)
)
</pre>

<p>The list of child process IDs returned by <tt>(receive)</tt> only
contains PIDs of processes which have unread messages in their 
send queues. The <tt>(receive pid msg)</tt> statement now can
be issued non-blocking, because it always is guaranteed to find
a pending message in a child's message queue.</p>

<p>The <tt>receive</tt> function is not available on MS Windows.</p>

<p>For a more detailed discussion of this function and examples, see the
<a href="#send">send</a> function.</p>

<br/><br/>

<a name="ref"></a>
<h2><span class="function">ref</span></h2>
<h4>syntax: (ref <em>exp-key</em> <em>list</em> [<em>func-compare</em> [true]])</h4>

<p><tt>ref</tt> searches for the key expression <em>exp-key</em> in <em>list</em> and 
returns a list of integer indices or an empty list if <em>exp-key</em> cannot be 
found.  <tt>ref</tt> can work together with <a href="#push">push</a> and 
<a href="pop">pop</a>, both of which can also take lists of indices.</p>

<p>By default, <tt>ref</tt> checks if expressions are equal. With <em>func-compare</em>, 
more complex comparison functions can be used. The comparison function can be a 
previously defined function. Note that this function always takes two arguments,
even if only the second argument is used inside the function.</p>

<p>When the optional <tt>true</tt> parameter is present, the element found
is returned instead of the index vector.</p>

<!-- example -->

<pre>
; get index vectors for list elements

(set 'pList '(a b (c d (x) e)))

(ref 'x pList)    <span class='arw'>&rarr;</span> (2 2 0)

(ref '(x) pList)   <span class='arw'>&rarr;</span> (2 2)

; the key expression is in a variable

(set 'p '(c d (x) e))

(ref p pList)   <span class='arw'>&rarr;</span> (2)

; indexing using the vector returned from ref

(set 'v (ref '(x) pList)) <span class='arw'>&rarr;</span> (2 2)

(pList v) <span class='arw'>&rarr;</span> (x)

; if nothing is found, nil is returned

(ref 'foo plist)  <span class='arw'>&rarr;</span> nil

; not specifying a comparison functor assumes =

(set 'L '(a b (c d (e) f)))

(ref 'e L)      <span class='arw'>&rarr;</span> (2 2 0)
(ref 'e L =)    <span class='arw'>&rarr;</span> (2 2 0)

; a is the first symbol where e is greater

(ref 'e L &gt;)  <span class='arw'>&rarr;</span> (0)

; return the element instead of the index

(ref 'e L &gt; true)  <span class='arw'>&rarr;</span> a

; use an anonymous comparison function

(ref 'e L (fn (x y) (or (= x y) (= y 'd))))      <span class='arw'>&rarr;</span> (2 1)

(ref 'e L (fn (x y) (or (= x y) (= y 'd))) true) <span class='arw'>&rarr;</span> d
</pre>


<p>
The following example shows the use of 
<a href="#match">match</a> and <a href="#unify">unify</a> 
to formulate searches that are as powerful as regular expressions are 
for strings:
</p>


<pre>
(set 'L '((l 3) (a 12) (k 5) (a 10) (z 22)))

; use match as a comparison function

(ref '(a ?) L match) <span class='arw'>&rarr;</span> (1)

; use unify as a comparison function

(set 'L '( ((a b) (c d)) ((e e) (f g)) ))

(ref '(X X) L unify)      <span class='arw'>&rarr;</span> (1 0)

(ref '(X g) L unify)      <span class='arw'>&rarr;</span> (1 1)

(ref '(X g) L unify true) <span class='arw'>&rarr;</span> (f g)
</pre>


<p> The <tt>'(X X)</tt> pattern with <a href="#unify">unify</a> searches for a list pair 
where the two elements are equal.  The <tt>unify</tt> pattern <tt>'(X g)</tt>
searches for a list pair with the symbol <tt>g</tt> as the second member.
The patterns are quoted to protect them from evaluation.</p>

<p>Pass the list as a default functor:</p>

<pre>
(set 'C:C '(a b (c d) e f))

(ref 'd C)  <span class='arw'>&rarr;</span> (2 1)
</pre>

<p>This is suitable when passing lists by reference using a context. See also
the chapter <a href="#pass_big">Passing data by reference</a>.</p>

<p> See also the <a href="#ref-all">ref-all</a> function, which searches for all occurrences
of a key expression in a nested list. </p>

<br/><br/>

<a name="ref-all"></a>
<h2><span class="function">ref-all</span></h2>
<h4>syntax: (ref-all <em>exp-key</em> <em>list</em> [<em>func-compare</em> [true]])</h4>

<p> Works similarly to <a href="#ref">ref</a>, but returns a list of all index vectors found 
for <em>exp-key</em> in <em>list</em>. </p>

<p>When the optional <tt>true</tt> parameter is present, the elements found
is returned of the index vectors.</p>

<p>By default, <tt>ref-all</tt> checks if expressions are equal. 
With <em>func-compare</em>, more complex comparison functions can be used.
</p>

<p>The system variable <tt>$count</tt> counts the number of elements found.</p>

<!-- example -->

<pre>
(set 'L '(a b c (d a f (a h a)) (k a (m n a) (x))))

(ref-all 'a L) <span class='arw'>&rarr;</span> ((0) (3 1) (3 3 0) (3 3 2) (4 1) (4 2 2))
$count <span class='arw'>&rarr;</span> 6

; the index vector returned by ref-all can be used to index the list

(L '(3 1)) <span class='arw'>&rarr;</span> a

; mapped implicit indexing of L 

(map 'L (ref-all 'a L)) <span class='arw'>&rarr;</span> (a a a a a a)

; with comparison operator

(set 'L '(a b c (d f (h l a)) (k a (m n) (x))))

; not specifying a comparison functor assumes =

(ref-all 'c L)       <span class='arw'>&rarr;</span> ((2))
(ref-all 'c L =)     <span class='arw'>&rarr;</span> ((2))

; look for all elements where c is greater

(ref-all 'c L &gt;)       <span class='arw'>&rarr;</span> ((0) (1) (3 2 2) (4 1))
(ref-all 'c L &gt; true)  <span class='arw'>&rarr;</span> (a b a a)


; use an anonymous function to compare

(ref-all 'a L (fn (x y) (or (= x y) (= y 'k))))  
<span class='arw'>&rarr;</span> ((0) (3 2 2) (4 0) (4 1))

; the key is nil because the comparison function only looks at the second argument

(ref-all nil L (fn (x y) (&gt; (length y) 2)))      
<span class='arw'>&rarr;</span> ((3) (3 2) (4))

; define the comparison functions first

(define (is-long? x y) (&gt; (length y) 2)) ; the x gets occupied by 'nil

(ref-all nil L is-long?)    <span class='arw'>&rarr;</span>  ((3) (3 2) (4))

(define (is-it-or-d x y) (or (= x y) (= y 'd)))

(set 'L '(a b (c d (e) f)) )

(ref-all 'e L is-it-or-d)  <span class='arw'>&rarr;</span> ((2 1) (2 2 0))
</pre>


<p>
The comparison function can be a previously defined function. 
Note that the comparison function always takes two arguments, 
even if only the second argument is used 
inside the function (as in the example using <tt>is-long?</tt>).
</p>

<p>
Using the <a href="#match">match</a> and <a href="#unify">unify</a> functions, list 
searches can be formulated that are as powerful as regular expression searches are 
for strings.
</p>


<pre>
(set 'L '((l 3) (a 12) (k 5) (a 10) (z 22)) )

; look for all pairs staring with the symbol a

(ref-all '(a ?) L match)      <span class='arw'>&rarr;</span> ((1) (3))
(ref-all '(a ?) L match true) <span class='arw'>&rarr;</span> ((a 12) (a 10))

; look for all pairs where elements are equal

(set 'L '( ((a b) (c d)) ((e e) (f g)) ((z) (z))))

(ref-all '(X X) L unify)      <span class='arw'>&rarr;</span> ((1 0) (2))
(ref-all '(X X) L unify true) <span class='arw'>&rarr;</span> ((e e) ((z) (z)))

; look for all pairs where the second element is the symbol g

(set 'L '( ((x y z) g) ((a b) (c d)) ((e e) (f g)) ))

(ref-all '(X g) L unify)      <span class='arw'>&rarr;</span> ((0) (2 1))
(ref-all '(X g) L unify true) <span class='arw'>&rarr;</span> (((x y z) g) (f g))
</pre>

<p> See also the <a href="#ref">ref</a> function. </p>

<br/><br/>

<a name="regex"></a>

<h2><span class="function">regex</span></h2>
<h4>syntax: (regex <em>str-pattern</em> <em>str-text</em> [<em>regex-option</em> [<em>int-offset</em>]])</h4>

<p>Performs a Perl Compatible Regular Expression (PCRE) search 
on <em>str-text</em> with the pattern specified in <em>str-pattern</em>. 
The same regular expression pattern matching 
is also supported in the functions <a href="#directory">directory</a>, 
<a href="#find">find</a>, <a href="#find-all">find-all</a>, 
<a href="#parse">parse</a>, <a href="#replace">replace</a>, 
and <a href="#search">search</a> when using these functions on strings.
</p>

<p>
<tt>regex</tt> returns a list with the matched strings and substrings 
and the beginning and length of each string inside the text. 
If no match is found, it returns <tt>nil</tt>.
The offset numbers can be used for subsequent processing.
</p>

<p>Additionally a <em>regex-option</em> can be specified to control certain
regular expression options explained later. Options can be given either by
numbers or letters in a string.</p>

<p>The additional <em>int-offset</em>
parameter tells <tt>regex</tt> to start searching for a match not at the
beginning of the string but at an offset.</p>

<p>When no <em>regex-option</em> is present, the offset and length numbers in 
the <tt>regex</tt> results are given based bytes even when running the UTF-8 
enabled version of newLISP. When specifying the PCRE_UTF8 option in <em>regex-option</em>
only offset and length are reported in UTF8 characters.</p>

<p>
<tt>regex</tt> also sets the variables <tt>$0, $1,</tt> 
and <tt>$2&mdash;</tt> 
to the expression and subexpressions found. 
Just like any other symbol in newLISP, 
these variables or their equivalent expressions 
<tt>($ 0), ($ 1),</tt> and <tt>($ 2)&mdash;</tt> can be used in other 
newLISP expressions for further processing.
</p>

<p>Functions using regular expressions will not reset the <tt>$0, $1 ... $15</tt> 
variables to <tt>nil</tt> when no match is found.</p>

<!-- example -->

<pre>
(regex "b+" "aaaabbbaaaa")  <span class='arw'>&rarr;</span> ("bbb" 4 3)

; case-insensitive search option 1
(regex "b+" "AAAABBBAAAA" 1)  <span class='arw'>&rarr;</span> ("BBB" 4 3) 
; same option given as a string
(regex "b+" "AAAABBBAAAA" "i")  <span class='arw'>&rarr;</span> ("BBB" 4 3) 

(regex "[bB]+" "AAAABbBAAAA" )  <span class='arw'>&rarr;</span> ("BbB" 4 3)

(regex "http://(.*):(.*)" "http://nuevatec.com:80") 
<span class='arw'>&rarr;</span> ("http://nuevatec.com:80" 0 22 "nuevatec.com" 7 12 "80" 20 2)

$0  <span class='arw'>&rarr;</span> "http://nuevatec.com:80"
$1  <span class='arw'>&rarr;</span> "nuevatec.com"
$2  <span class='arw'>&rarr;</span> "80"

(dotimes (i 3) (println ($ i)))
<b>http://nuevatec.com:80
nuevatec.com
80</b>
<span class='arw'>&rarr;</span> "80"
</pre>


<p>
	The second example shows the usage of extra options,
	while the third example demonstrates more complex parsing of two subexpressions
	that were marked by parentheses in the search pattern.
	In the last example,
	the expression and subexpressions are retrieved using the system variables 
	<tt>$0</tt> to <tt>$2</tt> or their equivalent expression <tt>($ 0)</tt> to <tt>($ 2)</tt>.
</p>

<p>
	When <tt>""</tt> (quotes) are used 
	to delimit strings 
	that include literal backslashes, 
	the backslash must be doubled in the regular expression pattern.
	As an alternative, <tt>{ }</tt> (curly brackets) 
	or <tt>[text]</tt> and <tt>[/text]</tt> (text tags) 
	can be used to delimit text strings.
	In these cases, no extra backslashes are required.
</p>

<p>
	Characters escaped by a backslash in newLISP 
	(e.g., the quote <tt>\"</tt> or <tt>\n</tt>) 
	need not to be doubled in a regular expression pattern, 
	which itself is delimited by quotes.
</p>


<pre>
;; double backslash for parentheses and other special char in regex
(regex "\\(abc\\)" "xyz(abc)xyz")  <span class='arw'>&rarr;</span> ("(abc)" 3 5)  
;; double backslash for backslash (special char in regex)
(regex "\\d{1,3}" "qwerty567asdfg")  <span class='arw'>&rarr;</span> ("567" 6 3)

;; one backslash for quotes (special char in newLISP)
(regex "\"" "abc\"def")  <span class='arw'>&rarr;</span> ("\"" 3 1)     

;; brackets as delimiters
(regex {\(abc\)} "xyz(abc)xyz")  <span class='arw'>&rarr;</span> ("(abc)" 3 5)  

;; brackets as delimiters and quote in pattern
(regex {"} "abc\"def")  <span class='arw'>&rarr;</span> ("\"" 3 1)     

;; text tags as delimiters, good for multiline text in CGI
(regex [text]\(abc\)[/text] "xyz(abc)xyz")  <span class='arw'>&rarr;</span> ("(abc)" 3 5)  
(regex [text]"[/text] "abc\"def")           <span class='arw'>&rarr;</span> ("\"" 3 1) 
</pre>


<p>
	When curly brackets or text tags 
	are used to delimit the pattern string 
	instead of quotes, 
	a simple backslash is sufficient. 
	The pattern and string are then passed in raw form 
	to the regular expression routines.
	When curly brackets are used inside a pattern 
	itself delimited by curly brackets,
	the inner brackets must be balanced, as follows:
</p>


<pre>
;; brackets inside brackets are balanced
(regex {\d{1,3}} "qwerty567asdfg")  <span class='arw'>&rarr;</span> ("567" 6 3) 
</pre>



<p>
The following constants can be used for <em>regex-option</em>.
Several options can be combined using a binary or <tt>|</tt> (pipe) operator. 
E.g. <tt>(| 1 4)</tt> would combine options <tt>1</tt> and <tt>4</tt> or <tt>"is"</tt>
when using letters for the two options.</p>

<p>The last two options are specific for newLISP. The REPLACE_ONCE option is only 
to be used in <a href="#replace">replace</a>; it can be combined with other PCRE options.</p>

<p>Multiple options can be combined using a <tt>+</tt> (plus) or <tt>|</tt> (or) operator,
e.g.: <tt>(| PCRE_CASELESS PCRE_DOTALL)</tt> or <tt>"is"</tt> when using letters as options.
</p>

<table width="98%" summary="regex options">
<tr align="left"><th>PCRE name</th><th>no</th><th>description</th></tr>
<tr><td>PCRE_CASELESS</td><td>1 or i</td><td>treat uppercase like lowercase</td></tr>
<tr><td>PCRE_MULTILINE</td><td>2 or m</td><td>limit search at a newline like Perl's /m</td></tr>
<tr><td>PCRE_DOTALL</td><td>4 or s</td><td>. (dot) also matches newline</td></tr>
<tr><td>PCRE_EXTENDED</td><td>8 or x</td><td>ignore whitespace except inside char class</td></tr>
<tr><td>PCRE_ANCHORED</td><td>16 or A</td><td>anchor at the start</td></tr>
<tr><td>PCRE_DOLLAR_ENDONLY</td><td>32 or D</td><td>$ matches at end of string, not before newline</td></tr>
<tr><td>PCRE_EXTRA</td><td>64</td><td>additional functionality currently not used</td></tr>
<tr><td>PCRE_NOTBOL</td><td>128</td><td>first ch, not start of line; ^ shouldn't match</td></tr>
<tr><td>PCRE_NOTEOL</td><td>256</td><td>last char, not end of line; $ shouldn't match</td></tr>
<tr><td>PCRE_UNGREEDY</td><td>512i or U</td><td>invert greediness of quantifiers</td></tr>
<tr><td>PCRE_NOTEMPTY</td><td>1024</td><td>empty string considered invalid</td></tr>
<tr><td>PCRE_UTF8</td><td>2048 or u</td><td>pattern and strings as UTF-8 characters</td></tr>
<tr><td>REPLACE_ONCE</td><td>0x8000</td><td>replace only one occurrence only for use in <a href="#replace">replace</a></td></tr>
<tr><td>PRECOMPILED</td><td>0x10000 or p</td><td>pattern is pre-compiled, can only be combined with RREPLACE_ONCE 0x8000</td></tr>
</table><br/>

<p>The settings of the PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and PCRE_EXTENDED 
options can be changed from within the pattern by a sequence of option letters enclosed 
between "(?" and ")". The option letters are:</p>

<table summary="regex inline options">
<tr><td>i</td><td>for PCRE_CASELESS</td></tr>
<tr><td>m</td><td>for PCRE_MULTILINE</td></tr>
<tr><td>s</td><td>for PCRE_DOTALL</td></tr>
<tr><td>x</td><td>for PCRE_EXTENDED</td></tr>
</table><br/>

<p> Note that regular expression syntax is very complex 
and feature-rich with many special characters and forms.
Please consult a book or the PCRE manual pages for more detail.
Most PERL books or introductions to Linux or Unix 
also contain chapters about regular expressions.
See also <a href="http://www.pcre.org">http://www.pcre.org</a> 
for further references and manual pages.  </p>

<p>Regular expression patterns can be precompiled for higher speed when using 
changing repetitive patterns with <a href="#regex-comp">regex-comp</a>.</p>

<br/><br/>

<a name="regex-comp"></a>
<h2><span class="function">regex-comp</span></h2>
<h4>syntax: (regex-comp <em>str-pattern</em> [<em>int-option</em>])</h4>

<p>newLISP automatically compiles regular expression patterns and caches 
the last compilation to speed up repetitive pattern searches. If patterns
change from one to the next, but are repeated over and over again, then 
the caching of the last pattern is not sufficient. <tt>regex-comp</tt>
can be used to pre-compile repetitive patterns to speed up regular 
expression searches:</p>


<!-- example -->

<pre>
; slower without pre-compilation

(dolist (line page)
	(replace pattern-str1 line repl1 0)
	(replace pattern-str2 line repl2 512)
)

; fast with pre-compilation and option 0x10000

(set 'p1 (regex-comp pattern-str1))
(set 'p2 (regex-comp pattern-str2 512))

(dolist (line page)
	(replace p1 line repl1 0x10000)
	(replace p2 line repl2 0x10000)
)
</pre>


<p>When using pre-compiled patterns in any of the functions using regular
expressions, the option number is set to <tt>0x10000</tt> to signal
that pre-compiled patterns are used. Normal pattern options are specified
 during pre-compilation with <tt>regex-comp</tt> . The <tt>0x10000</tt> option 
can only be combined with <tt>0x8000</tt>, the option used to specify that only 
one replacement should be made when using <a href="#replace">replace</a>.</p>

<p>The function <a href="#ends-with">ends-with</a> should not be used with compiled 
patterns, as it tries to append to an un-compiled pattern internally.</p>

<br/><br/>

<a name="remove-dir"></a>
<h2><span class="function">remove-dir</span></h2>
<h4>syntax: (remove-dir <em>str-path</em>)</h4>

<p>
	Removes the directory
	whose path name is specified in <em>str-path</em>.
	The directory must be empty for <tt>remove-dir</tt> to succeed.
	Returns <tt>nil</tt> on failure.
</p>

<!-- example -->

<pre>
(remove-dir "temp")
</pre>


<p>
	Removes the directory <tt>temp</tt> 
	in the current directory.
</p>

<br/><br/>

<a name="rename-file"></a>
<h2><span class="function">rename-file</span></h2>
<h4>syntax: (rename-file <em>str-path-old</em> <em>str-path-new</em>)</h4>

<p>
Renames a file or directory entry given in the path name <em>str-path-old</em> 
to the name given in <em>str-path-new</em>. Returns <tt>nil</tt> or <tt>true</tt> 
depending on the operation's success.
</p>

<!-- example -->

<pre>
(rename-file "data.lisp" "data.backup")
</pre>

<br/><br/>

<a name="replace"></a>
<h2><span class="function">replace</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (replace <em>exp-key</em> <em>list</em> <em>exp-replacement</em> [<em>func-compare</em>])<br/>
syntax: (replace <em>exp-key</em> <em>list</em>)<br/><br/>

syntax: (replace <em>str-key</em> <em>str-data</em> <em>exp-replacement</em>)<br/>
syntax: (replace <em>str-pattern</em> <em>str-data</em> <em>exp-replacement</em> <em>regex-option</em>)</h4>

<h3>List replacement</h3>

<p>If the second argument is a list, <tt>replace</tt> replaces all elements in
the list <em>list</em> that are equal to the expression in <em>exp-key</em>. The 
element is replaced with <em>exp-replacement</em>. If <em>exp-replacement</em>
is missing, all instances of <em>exp-key</em> will be deleted from <em>list</em>.
</p>

<p>Note that <tt>replace</tt> is 
destructive. It changes the list passed to it and returns the changed list. The 
number of replacements made is contained in the system variable <tt>$count</tt> 
when the function returns. During executions of the replacement expression, the 
anaphoric system variable <tt>$it</tt> is set to the expression to be replaced.
</p>

<p>Optionally, <em>func-compare</em> can specify a comparison operator 
or user-defined function. By default, <em>func-compare</em> is the <tt>=</tt> 
(equals sign).</p>

<!-- example -->

<pre>
;; list replacement

(set 'aList '(a b c d e a b c d))

(replace 'b aList 'B)  <span class='arw'>&rarr;</span> (a B c d e a B c d)
aList  <span class='arw'>&rarr;</span> (a B c d e a B c d)
$count <span class='arw'>&rarr;</span> 2  ; number of replacements

;; list replacement with special compare functor/function

; replace all numbers where 10 &lt; number
(set 'L '(1 4 22 5 6 89 2 3 24))

(replace 10 L 10 &lt;) <span class='arw'>&rarr;</span> (1 4 10 5 6 10 2 3 10)
$count <span class='arw'>&rarr;</span> 3

; same as:

(replace 10 L 10 (fn (x y) (&lt; x y))) <span class='arw'>&rarr;</span> (1 4 10 5 6 10 2 3 10)

; change name-string to symbol, x is ignored as nil

(set 'AL '((john 5 6 4) ("mary" 3 4 7) (bob 4 2 7 9) ("jane" 3)))

(replace nil AL (cons (sym ($it 0)) (rest $it)) 
                (fn (x y) (string? (y 0)))) ; parameter x = nil not used
<span class='arw'>&rarr;</span> ((john 5 6 4) (mary 3 4 7) (bob 4 2 7 9) (jane 3))

; use $count in the replacement expression
(replace 'a '(a b a b a b) (list $count $it) =) <span class='arw'>&rarr;</span>  ((1 a) b (2 a) b (3 a) b)
</pre>


<p>
Using the <a href="#match">match</a> and <a href="#unify">unify</a> functions,
list searches can be formulated that are as powerful as regular expression string 
searches:</p>


<pre>
; calculate the sum in all associations with 'mary

(set 'AL '((john 5 6 4) (mary 3 4 7) (bob 4 2 7 9) (jane 3)))

(replace '(mary *)  AL (list 'mary (apply + (rest $it))) match)
<span class='arw'>&rarr;</span> ((john 5 6 4) (mary 14) (bob 4 2 7 9) (jane 3))
$count <span class='arw'>&rarr;</span> 1

; make sum in all expressions

(set 'AL '((john 5 6 4) (mary 3 4 7) (bob 4 2 7 9) (jane 3)))

(replace '(*) AL (list ($it 0) (apply + (rest $it))) match)
<span class='arw'>&rarr;</span> ((john 15) (mary 14) (bob 22) (jane 3))
$count <span class='arw'>&rarr;</span> 4

; using unify, replace only if elements are equal
(replace '(X X) '((3 10) (2 5) (4 4) (6 7) (8 8)) (list ($it 0) 'double ($it 1)) unify)
<span class='arw'>&rarr;</span> ((3 10) (2 5) (4 double 4) (6 7) (8 double 8))
 </pre>


<h3>List removal</h3>
<p>
The last form of <tt>replace</tt> has only two arguments: the expression <em>exp</em> 
and <em>list</em>. This form removes all <em>exp</em>s found in <em>list</em>.
</p>

<!-- example -->

<pre>
;; removing elements from a list

(set 'lst '(a b a a c d a f g))
(replace 'a lst)  <span class='arw'>&rarr;</span> (b c d f g)
lst               <span class='arw'>&rarr;</span> (b c d f g)

$count <span class='arw'>&rarr;</span> 4
</pre>


<h3>String replacement without regular expression</h3>

<p>If all arguments are strings, <tt>replace</tt> replaces all occurrences 
of <em>str-key</em> in <em>str-data</em> with the evaluated 
<em>exp-replacement</em>, returning the changed string. The expression in 
<em>exp-replacement</em> is evaluated for every replacement. The number of 
replacements made is contained in the system variable <tt>$count</tt>. This 
form of <tt>replace</tt> can also process binary <tt>0</tt>s (zeros).</p>

<!-- example -->

<pre>
;; string replacement
(set 'str "this isa sentence")
(replace "isa" str "is a")  <span class='arw'>&rarr;</span> "this is a sentence"

$count <span class='arw'>&rarr;</span> 1
</pre>


<h3>Regular expression replacement</h3>
<p>
The presence of a fourth parameter indicates that a regular expression search 
should be performed with a regular expression pattern specified in <em>str-pattern</em> 
and an option number specified in <em>regex-option</em> (e.g., <tt>1</tt> (one) or "i" for 
case-insensitive searching or <tt>0</tt> (zero) for a standard Perl Compatible Regular 
Expression (PCRE) search without options). See <a href="#regex">regex</a> above for details.
</p>

<p>
By default, <tt>replace</tt> replaces all occurrences of a search string even if a 
beginning-of-line specification is included in the search pattern. 
After each replace, a new search is started at a new position in <em>str-data</em>.
Setting the option bit to <tt>0x8000</tt> in <em>regex-option</em> will force 
<tt>replace</tt> to replace only the first occurrence. The changed string is returned.
</p>

<p>
<tt>replace</tt> with regular expressions also sets the internal variables 
<tt>$0, $1,</tt> and <tt>$2&mdash;</tt> with the contents of the expressions 
and subexpressions found. The anaphoric system variable <tt>$it</tt> is set to
the same value as <tt>$0</tt>. These can be used to perform replacements 
that depend on the content found during replacement. The symbols <tt>$it, $0, $1,</tt> 
and <tt>$2&mdash;</tt> can be used in expressions just like any other symbols.
If the replacement expression evaluates to something other than a string,
no replacement is made. As an alternative, the contents of these variables can 
also be accessed by using <tt>($ 0), ($ 1), ($ 2),</tt> and so forth.
This method allows indexed access (e.g., <tt>($ i)</tt>,
where <tt>i</tt> is an integer).
</p>

<p>After all replacements are made, the number of replacements 
is contained in the system variable <tt>$count</tt>.</p>

<!-- example -->

<pre>
;; using the option parameter to employ regular expressions

(set 'str "ZZZZZxZZZZyy")     <span class='arw'>&rarr;</span> "ZZZZZxZZZZyy"
(replace "x|y" str "PP" 0)    <span class='arw'>&rarr;</span> "ZZZZZPPZZZZPPPP"
str                           <span class='arw'>&rarr;</span> "ZZZZZPPZZZZPPPP"

;; using system variables for dynamic replacement

(set 'str "---axb---ayb---")
(replace "(a)(.)(b)" str (append $3 $2 $1) 0) 
<span class='arw'>&rarr;</span> "---bxa---bya---"

str  <span class='arw'>&rarr;</span> "---bxa---bya---"

;; using the 'replace once' option bit 0x8000

(replace "a" "aaa" "X" 0)  <span class='arw'>&rarr;</span> "XXX"

(replace "a" "aaa" "X" 0x8000)  <span class='arw'>&rarr;</span> "Xaa"

;; URL translation of hex codes with dynamic replacement

(set 'str "xxx%41xxx%42")
(replace "%([0-9A-F][0-9A-F])" str 
               (char (int (append "0x" $1))) 1)

str    <span class='arw'>&rarr;</span> "xxxAxxxB"

$count <span class='arw'>&rarr;</span> 2
</pre>


<p>
The <a href="#setf">setf</a> function together with <a href="#nth">nth</a>,
<a href="#first">first</a> or <a href="#last">last</a> can also
be used to change elements in a list.</p>

<p>
	See <a href="#directory">directory</a>,
	<a href="#find">find</a>,
	<a href="#find-all">find-all</a>,
	<a href="#parse">parse</a>,
	<a href="#regex">regex</a>, 
	and <a href="#search">search</a> 
	for other functions using regular expressions.
</p>

<br/><br/>

<a name="reset"></a>
<h2><span class="function">reset</span></h2>
<h4>syntax: (reset)<br/>
syntax: (reset true)<br/>
syntax: (reset <em>int-max-cells</em>)</h4>

<p>
In the first syntax, <tt>reset</tt> returns to the top level of evaluation,
switches the <a href="#trace">trace</a> mode off, and switches to the MAIN 
context/namespace. <tt>reset</tt> restores the top-level variable environment 
using the saved variable environments on the stack. It also throws an error 
"user reset - no error" which can be reported with user defined error handlers.
Since version 10.5.5 <tt>reset</tt> also interrupts command line parameter
processing.</p>

<p><tt>reset</tt> walks through the entire cell space, 
which may take a few seconds in a heavily loaded system.</p>

<p><tt>reset</tt> occurs automatically after an error condition.</p>

<p>In the second syntax, <tt>reset</tt> will stop the current process 
and start a new clean newLISP process with the same command-line parameters.
This mode will only work when newLISP was started using its full path-name, 
e.g. <tt>/usr/local/bin/newlisp</tt> instead of only <tt>newlisp</tt>. This mode is 
not available on MS Windows.</p>

<p>In the third syntax. <tt>reset</tt> will change the maximum cell count allowed
in the system. This number is also reported as the second number in the list 
by <a href="#sys-info">sys-info</a>. On 64-bit newLISP one lisp cell occupies
32 bytes, or 16 bytes on the 32-bit version. This does not include
string memory, which may be pointed to by cells.</p>

<p>The minimum cell count is 4095, trying to specify less will set it to 4095.
The program will exit when trying to allocate more.</p>

<pre>
(sys-info)  <span class='arw'>&rarr;</span> (437 576460752303423488 409 1 0 2048 0 60391 10602 1411)

; allocate about 1 Mbyte of cell memory on 64-bit newlisp
(reset 32768) <span class='arw'>&rarr;</span> true

(sys-info)  <span class='arw'>&rarr;</span> (437 32768 409 1 0 2048 0 60392 10602 1411)
</pre>

<p>Resetting the maximum cell count will not restart the system and can be done at any point
in a program. Cell memory is allocated in blocks of 4095 cells, which is also initial 
minimum configuration.</p>

<br/><br/>

<a name="rest"></a>
<h2><span class="function">rest</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>

<h4>syntax: (rest <em>list</em>)<br/>
syntax: (rest <em>array</em>)<br/>
syntax: (rest <em>str</em>)</h4>

<p>Returns all of the items in a list or a string, except for the first.
<tt>rest</tt> is equivalent to <em>cdr</em> or <em>tail</em> in other Lisp dialects.
</p>

<!-- example -->

<pre>
(rest '(1 2 3 4))            <span class='arw'>&rarr;</span> (2 3 4)
(rest '((a b) c d))          <span class='arw'>&rarr;</span> (c d)
(set 'aList '(a b c d e))    <span class='arw'>&rarr;</span> (a b c d e)
(rest aList)                 <span class='arw'>&rarr;</span> (b c d e)
(first (rest aList))         <span class='arw'>&rarr;</span> b
(rest (rest aList))          <span class='arw'>&rarr;</span> (d e)
(rest (first '((a b) c d)))  <span class='arw'>&rarr;</span> (b)

(set 'A (array 2 3 (sequence 1 6)))
<span class='arw'>&rarr;</span> ((1 2) (3 4) (5 6))

(rest A)  <span class='arw'>&rarr;</span> ((3 4) (5 6))

(rest '()) <span class='arw'>&rarr;</span> ()
</pre>


<p>
In the second version, <tt>rest</tt> returns all but the first character 
of the string <em>str</em> in a string.
</p>

<!-- example -->

<pre>
(rest "newLISP")          <span class='arw'>&rarr;</span> "ewLISP"
(first (rest "newLISP"))  <span class='arw'>&rarr;</span> "e"
</pre>


<p>
See also the <a href="#first">first</a> and <a href="#last">last</a> functions.
</p>

<p>
Note that an <em>implicit rest</em> is available for lists.
See the chapter <a href="#implicit_rest_slice">Implicit rest and slice</a>.
</p>

<p>Note that <a href="#rest">rest</a> works on character boundaries rather 
than byte boundaries when the UTF-8&ndash;enabled version of newLISP is used.</p>

<br/><br/>

<a name="reverse"></a>
<h2><span class="function">reverse</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (reverse <em>list</em>)<br/>
syntax: (reverse <em>array</em>)<br/>
syntax: (reverse <em>string</em>)</h4>

<p>In the first and second form, <tt>reverse</tt> reverses and returns the 
<em>list</em> or <em>array</em>. Note that <tt>reverse</tt> is destructive 
and changes the original list or array.</p>

<!-- example -->

<pre>
; reverse a list
(set 'l '(a b c d e f))

(reverse l)  <span class='arw'>&rarr;</span> (f e d c b a)
l            <span class='arw'>&rarr;</span> (f e d c b a)
i
; reverse an array
(set 'a (array 3 2 '(1 2 3 4 5 6))) <span class='arw'>&rarr;</span> ((1 2) (3 4) (5 6))

(reverse a)                         <span class='arw'>&rarr;</span> ((5 6) (3 4) (1 2))
a                                   <span class='arw'>&rarr;</span> ((5 6) (3 4) (1 2))
</pre>


<p>In the third form, <tt>reverse</tt> is used to reverse the order 
of characters in a string.</p>

<!-- example -->

<pre>
; reverse byte character string

(set 'str "newLISP")

(reverse str)  <span class='arw'>&rarr;</span> "PSILwen"
str            <span class='arw'>&rarr;</span> "PSILwen"

; reverse a multibyte character UTF-8 string, explode is UTF-8 sensitive

(join (reverse (explode "ΑΒΓΔΕΖΗΘ")))  <span class='arw'>&rarr;</span> "ΘΗΖΕΔΓΒΑ"
</pre>

<p>See also the <a href="#sort">sort</a> function.</p>

<br/><br/>

<a name="rotate"></a>
<h2><span class="function">rotate</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (rotate <em>list</em> [<em>int-count</em>])<br/>
syntax: (rotate <em>str</em> [<em>int-count</em>])</h4>

<p>Rotates and returns the <em>list</em> or string in <em>str</em>.
A count can be optionally specified in <em>int-count</em> 
to rotate more than one position.  If <em>int-count</em> is positive, 
the rotation is to the right; if <em>int-count</em> is negative, 
the rotation is to the left.  If no <em>int-count</em> is specified, 
<tt>rotate</tt> rotates 1 to the right.  <tt>rotate</tt> is a destructive function 
that changes the contents of the original list or string.</p>

<!-- example -->

<pre>
(set 'l '(1 2 3 4 5 6 7 8 9))

(rotate l)    <span class='arw'>&rarr;</span> (9 1 2 3 4 5 6 7 8)
(rotate l 2)  <span class='arw'>&rarr;</span> (7 8 9 1 2 3 4 5 6)

l  <span class='arw'>&rarr;</span> (7 8 9 1 2 3 4 5 6)

(rotate l -3)  <span class='arw'>&rarr;</span> (1 2 3 4 5 6 7 8 9)

; rotate a byte character string

(set 'str "newLISP")

(rotate str)     <span class='arw'>&rarr;</span> "PnewLIS"
(rotate str 3)   <span class='arw'>&rarr;</span> "LISPnew"
(rotate str -4)  <span class='arw'>&rarr;</span> "newLISP"

; rotate a multibyte character UTF-8 string on character boundaries

(join (rotate (explode "ΑΒΓΔΕΖΗΘ")))  <span class='arw'>&rarr;</span> "ΘΑΒΓΔΕΖΗ"
</pre>


<p>When working on a string, <tt>rotate</tt> works on byte boundaries 
rather than character boundaries.</p>

<br/><br/>

<a name="round"></a>
<h2><span class="function">round</span></h2>
<h4>syntax: (round <em>number</em> [<em>int-digits</em>])</h4>

<p>Rounds the number in <em>number</em> 
to the number of digits given in <em>int-digits</em>. 
When decimals are being rounded, <em>int-digits</em> is negative. 
It is positive when the integer part of a number is being rounded.</p>

<p>If <em>int-digits</em> is omitted, the function rounds to <tt>0</tt> decimal
digits.</p>

<!-- example -->

<pre>
(round 123.49 2)    <span class='arw'>&rarr;</span> 100
(round 123.49 1)    <span class='arw'>&rarr;</span> 120
(round 123.49 0)    <span class='arw'>&rarr;</span> 123
(round 123.49)      <span class='arw'>&rarr;</span> 123
(round 123.49 -1)   <span class='arw'>&rarr;</span> 123.5
(round 123.49 -2)   <span class='arw'>&rarr;</span> 123.49
</pre>


<p>Note that rounding for display purposes is better accomplished using 
<a href="#format">format</a>.</p>

<br/><br/>

<a name="save"></a>
<h2><span class="function">save</span></h2>
<h4>syntax: (save <em>str-file</em>)<br/>
syntax: (save <em>str-file</em> <em>sym-1</em> [<em>sym-2</em> ... ])</h4>

<p>
	In the first syntax, 
	the <tt>save</tt> function writes 
	the contents of the newLISP workspace 
	(in textual form) to the file <em>str-file</em>.
	<tt>save</tt> is the inverse function of <tt>load</tt>. 
	Using <tt>load</tt> on files 
	created with <tt>save</tt> causes 
	newLISP to return to the same state 
	as when <tt>save</tt> was originally invoked.
	System symbols starting with the <tt>$</tt> character 
	(e.g., <tt>$0</tt> from regular expressions 
	or <tt>$main-args</tt> from the command-line), symbols of built-in
	functions and symbols containing <tt>nil</tt> are not saved.
</p>

<p>
	In the second syntax, 
	symbols can be supplied as arguments.
	If <em>sym-n</em> is supplied, 
	only the definition of that symbol is saved.
	If <em>sym-n</em> evaluates to a context,
	all symbols in that context are saved.
	More than one symbol can be specified,
	and symbols and context symbols can be mixed.
	When contexts are saved,
	system variables and symbols starting with the <tt>$</tt> character 
	are not saved.
	Specifying system symbols explicitly 
	causes them to be saved.
</p>

<p>
	Each symbol is saved 
	by means of a <a href="#set">set</a> statement or&mdash;if 
	the symbol contains a lambda or lambda-macro function&mdash;by 
	means of <a href="#define">define</a> 
	or <a href="#define-macro">define-macro</a> statements.
</p>

<p>
	<tt>save</tt> returns <tt>true</tt> on completion.
</p>

<!-- example -->

<pre>
(save "save.lsp")

(save "/home/myself/myfunc.LSP" 'my-func) 
(save "file:///home/myself/myfunc.LSP" 'my-func) 

(save "http://asite.com:8080//home/myself/myfunc.LSP" 'my-func)

(save "mycontext.lsp" 'mycontext) 

;; multiple args
(save "stuff.lsp" 'aContext 'myFunc '$main-args 'Acontext)
</pre>


<p>
	Because all context symbols are part of the context <tt>MAIN</tt>,
	saving <tt>MAIN</tt> saves all contexts.
</p>

<p>
	Saving to a URL 
	will cause an HTTP PUT request to be sent to the URL.
	In this mode, 
	<tt>save</tt> can also be used 
	to push program source 
	to remote newLISP server nodes.
	Note that a double backslash is required 
	when path names are specified 
	relative to the root directory.
	<tt>save</tt> in <tt>HTTP</tt>  mode will 
	observe a 60-second timeout.</p>

<p>
	Symbols made using <a href="#sym">sym</a>
	that are incompatible with the normal syntax rules for symbols
	are serialized using a <a href="#sym">sym</a> statement 
	instead of a <a href="#set">set</a> statement.
</p>

<p>
	<tt>save</tt> serializes contexts and symbols 
	as if the current context is <tt>MAIN</tt>.
	Regardless of the current context,
	<tt>save</tt> will always generate the same output.
</p>

<p>
	See also the functions <a href="#load">load</a> 
	(the inverse operation of <tt>save</tt>) 
	and <a href="#source">source</a>,
	which saves symbols and contexts to a string 
	instead of a file.
</p>

<br/><br/>

<a name="search"></a>
<h2><span class="function">search</span></h2>
<h4>syntax: (search <em>int-file</em> <em>str-search</em> [<em>bool-flag</em> [<em>regex-option</em>]])</h4>

<p>
Searches a file specified by its handle in <em>int-file</em> for a string in <em>str-search</em>.
<em>int-file</em> can be obtained from a previous <a href="#open">open</a> file.  After the search, 
the file pointer is positioned at the beginning  or the end of the searched string or at the end 
of the file if nothing is found.</p>

<p> By default, the file pointer is positioned at the beginning
of the searched string. If <em>bool-flag</em> evaluates to <tt>true</tt>,
then the file pointer is positioned at the end of the searched string.</p>

<p> In <em>regex-option</em>,  the options flags can be specified to perform 
a PCRE regular expression search.  See the function <a href="#regex">regex</a> for details.
If <em>regex-option</em> is not specified a faster, plain string search is performed.
<tt>search</tt> returns the new file position or <tt>nil</tt> if nothing is found.
</p>

<p> When using the regular expression options flag, patterns found are stored in the system variables 
<tt>$0</tt> to <tt>$15</tt>.  </p>

<!-- example -->

<pre>
(set 'file (open "init.lsp" "read"))
(search file "define")
(print (read-line file) "\n")
(close file)

(set 'file (open "program.c" "r"))
(while (search file "#define (.*)" true 0) (println $1))
(close file)
</pre>


<p> The file <tt>init.lsp</tt> is opened and searched for the string <tt>define</tt> and the
line in which the string occurs is printed. </p>

<p>The second example looks for all lines in the file <tt>program.c</tt> which start with
the string <tt>#define</tt> and prints the rest of the line after the string "#define ".</p>

<p>
	For other functions using regular expressions, 
	see <a href="#directory">directory</a>,
	<a href="#find">find</a>,
	<a href="#find-all">find-all</a>, 
	<a href="#parse">parse</a>,
	<a href="#regex">regex</a>, 
	and <a href="#replace">replace</a>.
</p>

<br/><br/>

<a name="seed"></a>
<h2><span class="function">seed</span></h2>
<h4>syntax: (seed <em>int-seed</em>)<br/>
syntax: (seed <em>int-seed</em> <tt>true</tt> [<em>int-pre-N</em>])<br/>
syntax: (seed)</h4>

<p>Seeds the internal random generator that generates numbers for <a href="#amb">amb</a>,
<a href="#normal">normal</a>, <a href="#rand">rand</a>, and <a href="#random">random</a> 
with the number specified in <em>int-seed</em>.  Note that the first syntax uses a 
random generator based on the C-library function <em>rand()</em>.  All randomizing functions 
in newLISP are based on this function.</p>

<p>When using the second syntax, all randomizing functions are based on a random generator
independent of platforms and compilers used to built newLISP. When seeding with the second
syntax all random functions called subsequently like 
<a href="#amb">amb</a>, <a href="#normal">normal</a>, <a href="#rand">rand</a>,
<a href="#random">random</a> and <a href="#randomize">randomize</a> are based on this
platform independent random generator.</p>

<p>The optional <em>int-pre-N</em> specifies the number of random numbers to be pre-
fetched as part of the seeding and initialization procedure. When this parameter is
ommitted <tt>seed</tt> assumes <tt>50</tt>.</p>

<p>Note that the maximum value for <em>int-seed</em> is limited to 16 or 32 bits, 
depending on the operating system used.  Internally, only the 32 least significant 
bits are passed to the random seed function of the OS.</p>

<!-- example -->

<pre>
(seed 12345)

(seed (time-of-day))
</pre>


<p>After using <tt>seed</tt> with the same number, the random generator starts 
the same sequence of numbers.  This facilitates debugging 
when randomized data are involved.  Using <tt>seed</tt>, 
the same random sequences can be generated over and over again.</p>

<p>The second example is useful for guaranteeing 
a different seed any time the program starts.</p>

<p>The following example shows usage of the internal seed state in the built-in
random generator:</p>

<pre>
&gt; (seed 123 true) ; use the true parameter
123
&gt; (random)
0.2788576787704871
&gt; (random)
0.7610070955758016
&gt; (random)
0.2462553424976092
&gt; (random)
0.8135413573186572
&gt; (set 'state (seed)) ; save current state
1747066761
&gt; (random)
0.1895924546707387
&gt; (random)
0.4803856511043318
&gt; (seed state true 0) ; seed with saved state
1747066761
&gt; (random)            ; produces old sequence
0.1895924546707387       
&gt; (random)
0.4803856511043318      
&gt; 
</pre>

<p>In the last syntax <tt>seed</tt> returns the current seed state.</p>

<br/><br/>

<a name="self"></a>
<h2><span class="function">self</span></h2>
<h4>syntax: (self [<em>int-index</em> ... ])</h4>

<p>The function <tt>self</tt> accesses the target object of a FOOP method.
One or more <em>int-index</em> are used to access the object members.
<tt>self</tt> is set by the <a href="#colon">:&nbsp;colon</a> operator.</p>

<p>Objects referenced with <tt>self</tt> are mutable:</p>

<!-- example -->

<pre>
(new Class 'Circle)

(define (Circle:move dx dy)
	(inc (self 1) dx) 
	(inc (self 2) dy))

(set 'aCircle (Circle 1 2 3))
(:move aCircle 10 20)

aCircle <span class='arw'>&rarr;</span> (Circle 11 22 3)

; objects can be anonymous
(set 'circles '((Circle 1 2 3) (Circle 4 5 6)))

(:move (circles 0) 10 20)
(:move (circles 1) 10 20)

circles <span class='arw'>&rarr;</span> ((Circle 11 22 3) (Circle 14 25 6))
</pre>

<p>See also the chapter about programming with FOOP: 
<a href="#foop">Functional object-oriented programming</a></p>

<br/><br/>

<a name="seek"></a>
<h2><span class="function">seek</span></h2>
<h4>syntax: (seek <em>int-file</em> [<em>int-position</em>])</h4>

<p>
Sets the file pointer to the new position <em>int-position</em> in the file 
specified by <em>int-file</em>.The new position is expressed as an offset from 
the beginning of the file, <tt>0</tt> (zero) meaning the beginning of the file.
If no <em>int-position</em> is specified, <tt>seek</tt> returns the current 
position in the file. If <em>int-file</em> is <tt>0</tt> (zero), 
on BSD, <tt>seek</tt> will return the number of characters printed to STDOUT, 
and on Linux and MS Windows, it will return <tt>-1</tt>. On failure, <tt>seek</tt> 
returns <tt>nil</tt>. When <em>int-position</em> is set to <tt>-1</tt>, 
<tt>seek</tt> sets the file pointer to the end of the file.</p>

<p><tt>seek</tt> can set the file position past the current end of the file. Subsequent
writing to this position will extend the file and fill unused positions with zero's.
The blocks of zeros are not actually allocated on disk, so the file takes up less
space and is called a <em>sparse file</em>.</p>

<!-- example -->

<pre>
(set 'file (open "myfile" "read"))  <span class='arw'>&rarr;</span> 5 
(seek file 100)                     <span class='arw'>&rarr;</span> 100
(seek file)                         <span class='arw'>&rarr;</span> 100

(open "newlisp_manual.html" "read")
(seek file -1)  ; seek to EOF
<span class='arw'>&rarr;</span> 593816     

(set 'fle (open "large-file" "read") 
(seek file 30000000000)  <span class='arw'>&rarr;</span> 30000000000
</pre>


<p>
	newLISP supports file position numbers up to 
	9,223,372,036,854,775,807.
</p>

<br/><br/>

<a name="select"></a>
<h2><span class="function">select</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>

<h4>syntax: (select <em>list</em> <em>list-selection</em>)<br/>
syntax: (select <em>list</em> [<em>int-index_i</em> ... ])<br/><br/>

syntax: (select <em>string</em> <em>list-selection</em>)<br/>

syntax: (select <em>string</em> [<em>int-index_i</em> ... ])</h4>

<p>
	In the first two forms,
	<tt>select</tt> picks one or more elements 
	from <em>list</em> using one or more indices 
	specified in <em>list-selection</em> or the <em>int-index_i</em>.
</p>

<!-- example -->

<pre>
(set 'lst '(a b c d e f g))

(select lst '(0 3 2 5 3))  <span class='arw'>&rarr;</span> (a d c f d)

(select lst '(-2 -1 0))  <span class='arw'>&rarr;</span> (f g a)

(select lst -2 -1 0)  <span class='arw'>&rarr;</span> (f g a)
</pre>


<p>
	In the second two forms,
	<tt>select</tt> picks one or more characters 
	from <em>string</em> 
	using one or more indices specified in <em>list-selection</em> 
	or the <em>int-index_i</em>.
</p> 

<!-- example -->

<pre>
(set 'str "abcdefg") 

(select str '(0 3 2 5 3))  <span class='arw'>&rarr;</span> "adcfd"

(select str '(-2 -1 0))  <span class='arw'>&rarr;</span> "fga"

(select str -2 -1 0)  <span class='arw'>&rarr;</span> "fga"
</pre>


<p>
	Selected elements can be repeated and do not have to appear in order,
	although this speeds up processing.
	The order in <em>list-selection</em> or <em>int-index_i</em> 
	can be changed to rearrange elements.
</p>

<br/><br/>



<br/><br/>

<a name="semaphore"></a>
<h2><span class="function">semaphore</span></h2>

<h4>syntax: (semaphore)<br/>
syntax: (semaphore <em>int-id</em>)<br/>
syntax: (semaphore <em>int-id</em> <em>int-wait</em>)<br/>
syntax: (semaphore <em>int-id</em> <em>int-signal</em>)<br/>

syntax: (semaphore <em>int-id</em> <em>0</em>)</h4>

<p>A semaphore is an interprocess synchronization object 
that maintains a count between <tt>0</tt> (zero) and some maximum value.
Useful in controlling access to a shared resource, 
a semaphore is set to signaled when its count is greater than zero 
and to non-signaled when its count is zero.</p>

<p>A semaphore is created using the first syntax.  This returns 
the semaphore ID, an integer used subsequently as <em>int-id</em> 
when the <em>semaphore</em> function is called.  Initially, the 
semaphore has a value of zero, which represents the non-signaled state.
</p>

<p>
If calling <tt>semaphore</tt> with a negative value in <em>int-wait</em> 
causes it to be decremented below zero,
the function call will block until another process
signals the semaphore with a positive value in <em>int-signal</em>.
Calls to the semaphore with <em>int-wait</em> or <em>int-signal</em>
effectively try to increment or decrement the semaphore value 
by a positive or negative value specified in <em>int-signal</em> 
or <em>int-wait</em>.
Because the value of a semaphore must never fall below zero,
the function call will block when this is attempted
(i.e., a semaphore with a value of zero 
will block until another process
increases the value with a positive <em>int-signal</em>).
</p>

<p>The second syntax is used to inquire about the value of a semaphore
 by calling <tt>semaphore</tt> with the <em>int-id</em> only.
This form is not available on MS Windows.</p>

<p>Supplying <tt>0</tt> (zero) as the last argument will release system 
resources for the semaphore, which then becomes unavailable.
Any pending waits on this semaphore in other child processes 
will be released.</p>

<p>On MS Windows, only parent and child processes can share a semaphore.
On Linux/Unix, independent processes can share a semaphore.</p>

<p>On failure the <tt>semaphore</tt> function returns <tt>nil</tt>.
<a href="#sys-error">sys-error</a> can be used to retrieve the error
number and text from the underlying operating system.</p>

<p>The following code examples summarize the different syntax forms:</p>

<!-- example -->

<pre>
;; init semaphores 
(semaphore) 

;; assign a semaphore to sid 
(set 'sid (semaphore))

;; inquire the state of a semaphore (not on Windows OS)
(semaphore sid)

;; put sid semaphore in wait state (-1) 
(semaphore sid -1) 

;; run sid semaphore previously put in wait (always 1) 
(semaphore sid 1) 

;; run sid semaphore with X times a skip (backward or forward) on the function 
(semaphore sid X) 

;; release sid semaphore system-wide (always 0) 
(semaphore sid 0) 
</pre>


<p>The following example shows semaphores controlling a child process:</p> 

<!-- example -->

<pre>
;; counter process output in bold

(define (counter n)
	(println "counter started")
	(dotimes (x n)
		(semaphore sid -1)
		(println x)))

;; hit extra &lt;enter&gt; to make the prompt come back
;; after output to the console from the counter process

&gt; (set 'sid (semaphore))

&gt; (semaphore sid)
<b>0</b>

&gt; (fork (counter 100))

<b>counter started</b>
&gt; (semaphore sid 1)
<b>0</b>
&gt; (semaphore sid 3)
<b>1</b>
<b>2</b>
<b>3</b>
&gt; (semaphore sid 2)
<b>4</b>

<b>5</b>
&gt; _
</pre>


<p>
	After the semaphore is acquired in <tt>sid</tt>, 
	it has a value of <tt>0</tt>
	(the non-signaled state).
	When starting the process <tt>counter</tt>,
	the semaphore will block after the initial start message 
	and will wait in the semaphore call.
	The <tt>-1</tt> is trying to decrement the semaphore,
	which is not possible because its value is already zero.
	In the interactive, main parent process,
	the semaphore is signaled by raising its value by <tt>1</tt>.
	This unblocks the semaphore call in the <tt>counter</tt> process,
	which can now decrement the semaphore from <tt>1</tt> to <tt>0</tt> 
	and execute the <tt>print</tt> statement.
	When the semaphore call is reached again,
	it will block because the semaphore is already in the wait 
	(<tt>0</tt>) state.
</p>

<p>
	Subsequent calls to <tt>semaphore</tt> 
	with numbers greater than <tt>1</tt> 
	give the <tt>counter</tt> process an opportunity 
	to decrement the semaphore several times before blocking.
</p>

<p>
	More than one process can participate in controlling the semaphore, 
	just as more than one semaphore can be created.
	The maximum number of semaphores is controlled 
	by a system-wide kernel setting on Unix-like operating systems.
</p>

<p>Use the <a href="#fork">fork</a> function to start a new process 
and the <a href="#share">share</a> function to share information between 
processes. For a more comprehensive example of using <tt>semaphore</tt> 
to synchronize processes, see the file <tt>prodcons.lsp</tt> example 
in the <tt>examples</tt> directory in the source distribution, 
as well as the examples and modules distributed with newLISP.</p>

<br/><br/>

<a name="send"></a>
<h2><span class="function">send</span></h2>
<h4>syntax: (send <em>int-pid</em> <em>exp</em>)<br/>
syntax: (send)</h4>

<p>The <tt>send</tt> function enables communication between
parent and child processes started with <a href="#spawn">spawn</a>.
Parent processes can send and receive messages to and from
their child processes and child processes can send and receive
messages to and from their parent process. A proxy technique &ndash; shown further
down &ndash; is  employed to communicate between child process
peers. <tt>send</tt> and <a href="#receive">receive</a> do not require 
locks or semaphores. They work on dual send and receive message queues.
</p>

<p>Processes started using <a href="#fork">fork</a> or 
<a href="#process">process</a> can not use <tt>send</tt> and <tt>receive</tt>
message functions. Instead they should use either <a href="#share">share</a> 
with <a href="#semaphore">semaphore</a> or <a href="#pipe">pipe</a> to 
communicate.</p>

<p>The <tt>send</tt> function is not available on MS Windows.</p>

<p>In the first syntax <tt>send</tt> is used to send a message 
from a parent to a child process or a child to a parent process.</p>

<p>The second syntax is only used by parent processes to get a list
of all child processes ready to accept message from the parent in their 
receive queues. If a child's receive queue is full, it will not be part of
the list returned by the <tt>(send)</tt> statement.</p>

<p>The content of a message may be any newLISP expression either
atomic or list expressions: boolean constants <tt>nil</tt> and <tt>true</tt>,
integers, floating point numbers or strings, or any list expression 
in valid newLISP syntax. The size of a message is unlimited.</p>


<p>The <em>exp</em> parameter specifies the data to be sent
to the recipient in <em>int-pid</em>. The recipient can be either a
spawned child process of the current process or the parent
process. If a message queue is full, it can be read from the receiving
end, but a <tt>send</tt> issued on the other side of the queue will
fail and return <tt>nil</tt>.</p>


<pre>
; child process dispatching message to parent

(set 'ppid (sys-info -4)) ; get parent pid

(send ppid "hello") ; send message
</pre>

<p>The targeted recipient of the message is the parent process:</p>

<pre>
; parent process receiving message from child

(receive child-pid msg) <span class='arw'>&rarr;</span> true
msg                     <span class='arw'>&rarr;</span> "hello"
</pre>


<p>When the <tt>send</tt> queue is full, <tt>send</tt> will return
<tt>nil</tt> until enough message content is read on the receiving side
of the queue and the queue is ready to accept new messages from 
<tt>send</tt> statements.</p>

<p>Using the <a href="#until">until</a> looping function, the
message statements can be repeated until they return a value
not <tt>nil</tt>. This way, non-blocking <tt>send</tt> and <tt>receive</tt> 
can be made blocking until they succeed:</p>

<pre>
; blocking sender
(until (send pid msg)) ; true after message is queued up

; blocking receiver
(until (receive pid msg)) ; true after message could be read
</pre>

<p>The sender statement blocks until the message could be deposited
in the recipients queue.</p>

<p>The <tt>receive</tt> statement blocks until a new message can
be fetched from the queue.</p>

<p>As the <tt>until</tt> statements in this example lack body expressions,
the last value of the evaluated conditional expression is the return
value of the <tt>until</tt> loop.</p>

<h3>Blocking message exchange</h3>

<p>The following code shows how a recipient can listen for incoming
messages, and in turn how a sender can retry to deposit a message
into a queue. The example shows 5 child processes constantly delivering 
status data to a parent process which will display the data.
After three data sets have been read, the parent will abort all
child processes and exit:</p>

<!-- example -->

<pre>
#!/usr/local/bin/newlisp

; child process transmits random numbers
(define (child-process)
    (set 'ppid (sys-info -4)) ; get parent pid
    (while true
        (until (send ppid (rand 100))))
)

; parent starts 5  child processes, listens and displays
; the true flag is specified to enable send/receive

(dotimes (i 5) (spawn 'result (child-process) true))

(for (i 1 3)
    (dolist (cpid (sync)) ; iterate thru pending child PIDs
        (until (receive cpid msg))
        (print "pid:" cpid "-&gt;" (format "%-2d  " msg)))
    (println)
)

(abort) ; cancel child-processes
(exit)
</pre>


<p>Running above example produces the following output:</p>

<pre><b>pid:53181->47  pid:53180->61  pid:53179->75  pid:53178->39  pid:53177->3   
pid:53181->59  pid:53180->12  pid:53179->20  pid:53178->77  pid:53177->47  
pid:53181->6   pid:53180->56  pid:53179->96  pid:53178->78  pid:53177->18
</b></pre>

<p>The <tt>(sync)</tt> expression returns a list of all child PIDs,
and <tt>(until (receive cpid msg))</tt> is used to force a wait
until status messages are received for each of the child processes.</p>

<p>A timeout mechanism could be part of an <tt>until</tt> or <tt>while</tt>
loop to stop waiting after certain time has expired.</p>

<p>The examples show messages flowing from a child processes to
a parent process, in the same fashion messages could flow
into the other direction from parent to child processes. In that
case the parent process would use <tt>(send)</tt> to obtain a
list of child processes with place in their message queues.</p>

<h3>Messages containing code for evaluation</h3>

<p>The most powerful feature of the message functions is the ability
to send any newLISP expression, which then can be evaluated by the recipient. 
The recipient uses <a href="#eval">eval</a> to evaluate the received
expression. Symbols contained in the expression are evaluated in the
receivers environment.</p>

<p>The following example shows how a parent process acts like a message 
proxy. The parent receives messages from a child process A and routes them 
to a second child process with ID B. In effect this implements messages 
between child process peers. The implementation relies on the fact that 
the recipient can evaluate expressions contained in messages received.
These expressions can be any valid newLISP statements:</p>

<!-- example -->
<pre>
#!/usr/local/bin/newlisp

; sender child process of the message
(set 'A (spawn 'result 
    (begin
        (dotimes (i 3)
            (set 'ppid (sys-info -4))
            /* the statement in msg will be evaluated in the proxy */
            (set 'msg '(until (send B (string "greetings from " A))))
            (until (send ppid msg)))
        (until (send ppid '(begin 
            (sleep 100) ; make sure all else is printed
            (println "parent exiting ...\n")
            (set 'finished true))))) true)) 

; receiver child process of the message
(set 'B (spawn 'result 
    (begin
        (set 'ppid (sys-info -4))
        (while true
            (until (receive ppid msg))
            (println msg)
            (unless (= msg (string "greetings from " A))
                (println "ERROR in proxy message: " msg)))) true))

(until finished (if (receive A msg) (eval msg))) ; proxy loop

(abort)
(exit)
</pre>

<p>Child process <tt>A</tt> sends three messages to <tt>B</tt>.
As this cannot be done directly <tt>A</tt>  sends <tt>send</tt>
statements to the parent for evaluation. The statement:</p>

<pre>
(until (send pidB (string "greetings from " A)))
</pre>

<p>will be evaluated in the environment of the parent process. Even so the 
variables <tt>A</tt> and <tt>B</tt> are bound to <tt>nil</tt> in 
the sender process <tt>A</tt>, in the parent process they will be
bound to the correct process ID numbers.</p>

<p>After sending the three messages, the statement:</p>

<pre>
(set 'finished true)
</pre>

<p>is sent to the parent process. Once evaluated, it will cause the <tt>until</tt>
loop to finish.</p>

<p>For more details on <tt>send</tt> and <tt>receive</tt> and more examples 
see the <a href="http://www.newlisp.org/CodePatterns.html">Code Patterns</a>
document.</p>

<br/><br/>

<a name="sequence"></a>
<h2><span class="function">sequence</span></h2>
<h4>syntax: (sequence <em>num-start</em> <em>num-end</em> [<em>num-step</em>])</h4>

<p>
	Generates a sequence of numbers 
	from <em>num-start</em> to <em>num-end</em> 
	with an optional step size of <em>num-step</em>.
	When <em>num-step</em> is omitted,
	the value <tt>1</tt> (one) is assumed.
	The generated numbers are of type integer 
	(when no optional step size is specified) 
	or floating point 
	(when the optional step size is present).
</p>

<!-- example -->

<pre>
(sequence 10 5)     <span class='arw'>&rarr;</span> (10 9 8 7 6 5)
(sequence 0 1 0.2)  <span class='arw'>&rarr;</span> (0 0.2 0.4 0.6 0.8 1)
(sequence 2 0 0.3)  <span class='arw'>&rarr;</span> (2 1.7 1.4 1.1 0.8 0.5 0.2)
</pre>


<p>
	Note that the step size must be a positive number,
	even if sequencing from a higher to a lower number.
</p>

<p>
	Use the <a href="#series">series</a> function
	to generate geometric sequences.
</p>

<br/><br/>

<a name="series"></a>
<h2><span class="function">series</span></h2>

<h4>syntax: (series <em>num-start</em> <em>num-factor</em> <em>num-count</em>)<br/>
syntax: (series <em>exp-start</em> <em>func</em> <em>num-count</em>)</h4>

<p>In the first syntax, <tt>series</tt> creates a geometric sequence with <em>num-count</em> 
elements starting with the element in <em>num-start</em>. Each subsequent element 
is multiplied by <em>num-factor</em>. The generated numbers are always floating point 
numbers.</p>

<p>When <em>num-count</em> is less than <tt>1</tt>, then <tt>series</tt>
returns an empty list.</p>

<!-- example -->

<pre>
(series 2 2 5)     <span class='arw'>&rarr;</span> (2 4 8 16 32)
(series 1 1.2 6)   <span class='arw'>&rarr;</span> (1 1.2 1.44 1.728 2.0736 2.48832)
(series 10 0.9 4)  <span class='arw'>&rarr;</span> (10 9 8.1 7.29)
(series 0 0 10)    <span class='arw'>&rarr;</span> (0 0 0 0 0 0 0 0 0 0)
(series 99 1 5)    <span class='arw'>&rarr;</span> (99 99 99 99 99)
</pre>


<p>In the second syntax, <tt>series</tt> uses a function specified in <em>func</em>
to transform the previous expression in to the next expression:</p>


<!-- example -->

<pre>
; embed the function Phi: f(x) = 1 / (1 + x)
; see also http://en.wikipedia.org/wiki/Golden_ratio

(series 1 (fn (x) (div (add 1 x))) 20)  <span class='arw'>&rarr;</span>

(1 0.5 0.6666666 0.6 0.625 0.6153846 0.619047 0.6176470 0.6181818 
 0.6179775 0.6180555 0.6180257 0.6180371 0.6180327 0.6180344 
 0.6180338 0.6180340 0.6180339 0.6180339 0.6180339)

; pre-define the function

(define (oscillate x) 
  (if (&lt; x) 
    (+ (- x) 1) 
    (- (+ x 1)))
)

(series 1 oscillate 20)  <span class='arw'>&rarr;</span> 

(1 -2 3 -4 5 -6 7 -8 9 -10 11 -12 13 -14 15 -16 17 -18 19 -20)

; any data type is accepted as a start expression

(series "a" (fn (c) (char (inc (char c)))) 5) <span class='arw'>&rarr;</span> ("a" "b" "c" "d" "e")

; dependency of the two previous values in this fibonacci generator

(let (x 1) (series x (fn (y) (+ x (swap y x))) 10))  <span class='arw'>&rarr;</span>

(1 2 3 5 8 13 21 34 55 89)
 
</pre>


<p>The first example shows a series converging to the <em>golden ratio, &phi;</em>
(for any starting value). The second example shows how <em>func</em> can be defined
previously for better readability of the <tt>series</tt> statement.</p>

<p>The <tt>series</tt> function also updates the internal list <tt>$idx</tt>
index value, which can be used inside <em>func</em>.</p>


<p>Use the <a href="#sequence">sequence</a> function to generate arithmetic sequences.
</p>

<br/><br/>

<a name="set"></a>
<h2><span class="function">set</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (set <em>sym-1</em> <em>exp-1</em> [<em>sym-2</em> <em>exp-2</em> ... ])</h4>

<p>Evaluates both arguments and then assigns the result of <em>exp</em> 
to the symbol found in <em>sym</em>. The <tt>set</tt> expression 
returns the result of the assignment. The assignment is performed by copying 
the contents of the right side into the symbol. The old contents of the symbol 
are deleted. An error message results when trying to change the contents 
of the symbols <tt>nil</tt>, <tt>true</tt>, or a context symbol.
<tt>set</tt> can take multiple argument pairs.</p>

<!-- example -->

<pre>
(set 'x 123)     <span class='arw'>&rarr;</span> 123
(set 'x 'y)      <span class='arw'>&rarr;</span> y
(set x "hello")  <span class='arw'>&rarr;</span> "hello"

y  <span class='arw'>&rarr;</span> "hello"

(set 'alist '(1 2 3))  <span class='arw'>&rarr;</span> (1 2 3)


(set 'x 1 'y "hello")  <span class='arw'>&rarr;</span> "hello"  ; multiple arguments

x  <span class='arw'>&rarr;</span> 1
y  <span class='arw'>&rarr;</span> "hello"
</pre>


<p>The symbol for assignment could be the result from another newLISP expression.
Expressions can refer to variables in the <tt>set</tt> expression.</p>

<pre>
(set 'lst '(x y z))  <span class='arw'>&rarr;</span> (x y z)

(set (first lst) 123)  <span class='arw'>&rarr;</span> 123

x  <span class='arw'>&rarr;</span> 123

(set 'a 10 'b (+ a a))

a   <span class='arw'>&rarr;</span> 10,  b   <span class='arw'>&rarr;</span> 20
</pre>


<p>
Symbols can be set to lambda or lambda-macro expressions.
This operation is equivalent to using <a href="#define">define</a> 
or <a href="#define-macro">define-macro</a>.</p>


<pre>
(set 'double (lambda (x) (+ x x)))
<span class='arw'>&rarr;</span> (lambda (x) (+ x x))
</pre>


<p>is equivalent to:</p>


<pre>
(define (double x) (+ x x))
<span class='arw'>&rarr;</span> (lambda (x) (+ x x))
</pre>


<p>is equivalent to:</p>


<pre>
(define double (lambda (x) (+ x x)))
<span class='arw'>&rarr;</span> (lambda (x) (+ x x))
</pre>


<p>
Use the <a href="#constant">constant</a> function (which works like <tt>set</tt>)
to protect the symbol from subsequent alteration. Using the <a href="#setq">setq</a> 
or <a href="#setf">setf</a> function eliminates the need to quote the variable symbol.
</p>

<br/><br/>

<a name="set-locale"></a>
<h2><span class="function">set-locale</span></h2>
<h4>syntax: (set-locale [<em>str-locale</em> [<em>int-category</em>]])</h4>

<p>Reports or switches to a different locale on your operating system or platform.
When used without arguments, <em>set-locale</em> reports 
the current locale being used. When <em>str-locale</em> is specified, 
<em>set-locale</em> switches to the locale with all category options turned on 
(<tt>LC_ALL</tt>). Placing an empty string in <em>str-locale</em> 
switches to the default locale used on the current platform.</p>

<p><tt>set-locale</tt> returns either the current locale string and decimal 
point string in a list  or <tt>nil</tt> if the requested change could not 
be performed.</p>

<!-- example -->

<pre>
; report current locale

(set-locale)     

; set default locale of your platform and country
; return value shown when executing on German MS-Windows

(set-locale "")    <span class='arw'>&rarr;</span> ("German_Germany.1252" ",")
(add 1,234 1,234)  <span class='arw'>&rarr;</span> 2,468
</pre>

<p>By default, newLISP &ndash; if not enabled for UTF-8 &ndash; starts up with the POSIX C 
default locale. This guarantees that newLISP's behavior will be identical on any 
platform locale. On UTF-8 enabled versions of newLISP the locale of
the current platform is chosen.</p>

<pre>
; after non-UTF-8 newLISP start up

(set-locale)  <span class='arw'>&rarr;</span> ("C" ".")
</pre>

<p>In <em>int-category</em> integer numbers may be specified as <em>category options</em> 
for fine-tuning certain aspects of the locale, such as number display, date display, 
and so forth. The options valid on your platform can be found in the C include file 
<tt>locale.h</tt> and may be different on each platform. When no <em>int-category</em>
is specified, <tt>LC_ALL</tt> is used to turn on all options for that locale.</p>

<table>
<tr align="left"><th>Category</th><th>macOS, BSDs<br/>&amp; MS Windows</th></tr>
<tr><td>LC_ALL</td><td>0</td></tr>
<tr><td>LC_COLLATE</td><td>1</td></tr>
<tr><td>LC_CTYPE</td><td>2</td></tr>
<tr><td>LC_MONETARY</td><td>3</td></tr>
<tr><td>LC_NUMERIC</td><td>4</td></tr>
<tr><td>LC_TIME</td><td>5</td></tr>
</table>
<br/>

<p>The default C locale uses the decimal dot, but most others use a decimal comma.</p>

<pre>
; with the current locale "en_US.UTF-8", only change the decimal separator 
; to German locale comma on  macOS. LC_NUMERIC is 4 on most platforms 

(set-locale) <span class='arw'>&rarr;</span> ("en_US.UTF-8" ".") 
(set-locale "de_DE.UTF-8" 4) <span class='arw'>&rarr;</span> ("de_DE.UTF-8" ",") 

; mixed locale shows country setting for each category, 4 has changed
(set-locale) <span class='arw'>&rarr;</span> ("en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/de_DE.UTF-8/en_US.UTF-8/en_US.UTF-8" ",")
</pre>

<p>Note that using <tt>set-locale</tt> does not change the behavior 
of regular expressions in newLISP. To localize the behavior of PCRE 
(Perl Compatible Regular Expressions), newLISP must be compiled 
with different character tables. See the file, LOCALIZATION, 
in the newLISP source distribution for details.</p>

<p>
	See also the chapter <a href="#switching">Switching the locale</a>.
</p>

<br/><br/>

<a name="set-ref"></a>
<h2><span class="function">set-ref</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (set-ref <em>exp-key</em> <em>list</em> <em>exp-replacement</em> [<em>func-compare</em>])</h4>


<p>Searches for <em>exp-key</em> in <em>list</em> and replaces the found element with 
<em>exp-replacement</em>. The <em>list</em> can be nested. The system variables 
<tt>$it</tt> contains the expression found and can be used in 
<em>exp-replacement</em>. The function returns the new modified <em>list</em>.</p>


<!-- example -->

<pre>
(set 'data '(fruits (apples 123 44) (oranges 1 5 3)))

(set-ref 'apples data 'Apples)  <span class='arw'>&rarr;</span> (fruits (Apples 123 44) (oranges 1 5 3))

data <span class='arw'>&rarr;</span> (fruits (Apples 123 44) (oranges 1 5 3)))
</pre>


<p><tt>data</tt> could be the context identifier of a default function for passing lists by reference:</p>


<pre>
(set 'db:db '(fruits (apples 123 44) (oranges 1 5 3)))

(define (update ct key value)
	(set-ref key ct value))

(update db 'apples 'Apples)    <span class='arw'>&rarr;</span> (fruits (Apples 123 44) (oranges 1 5 3))
(update db 'oranges 'Oranges)  <span class='arw'>&rarr;</span> (fruits (Apples 123 44) (Oranges 1 5 3))

db:db <span class='arw'>&rarr;</span> (fruits (Apples 123 44) (Oranges 1 5 3))
</pre>


<p>For examples on how to use <em>func-compare</em> see 
<a href="#set-ref-all">set-ref-all</a></p>

<p>For changing all occurrences of an element in a list use 
<a href="#set-ref-all">set-ref-all</a>.</p>

<br/><br/>

<a name="set-ref-all"></a>
<h2><span class="function">set-ref-all</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (set-ref-all <em>exp-key</em> <em>list</em>  <em>exp-replacement</em> [<em>func-compare</em>])</h4>

<p>Searches for <em>exp-key</em> in <em>list</em> and replaces each instance of the found element 
with <em>exp-replacement</em>. The <em>list</em> can be nested. The system variable <tt>$it</tt> 
contains the expression found and can be used in <em>exp-replacement</em>.
The system variable <tt>$count</tt> contains the number of replacements made.
The function returns the new modified <em>list</em>.</p>

<!-- example -->

<pre>
(set 'data '((monday (apples 20 30) (oranges 2 4 9)) (tuesday (apples 5) (oranges 32 1))))

(set-ref-all 'apples data "Apples")
 <span class='arw'>&rarr;</span> ((monday ("Apples" 20 30) (oranges 2 4 9)) (tuesday ("Apples" 5) (oranges 32 1)))

$count <span class='arw'>&rarr;</span> 2
</pre>


<p>Using the default functor in the <tt>(<em>list</em> <em>key</em>)</tt> pattern allows the
list to be passed by reference to a user-defined function containing a <tt>set-ref-all</tt>
statement. This would result in less memory usage and higher speeds in when doing replacements
in large lists:</p>


<pre>
(set 'db:db '((monday (apples 20 30) (oranges 2 4 9)) (tuesday (apples 5) (oranges 32 1))))

(define (foo ctx)
	(set-ref-all 'apples ctx "Apples")
)

(foo db) 
 <span class='arw'>&rarr;</span> ((monday ("Apples" 20 30) (oranges 2 4 9)) (tuesday ("Apples" 5) (oranges 32 1)))
</pre>


<p>When evaluating <tt>(foo db)</tt>, the list in <tt>db:db</tt> will be passed
by reference and <tt>set-ref-all</tt> will make the changes on the original, not on
a copy of <tt>db:db</tt>.</p>

<p>Like with <a href="#find">find</a>, <a href="#replace">replace</a>, 
<a href="#ref">ref</a> and <a href="#ref-all">ref-all</a>, 
complex searches can be expressed using 
<a href="#match">match</a> or <a href="#unify">unify</a> in <em>func-compare</em>:</p>


<pre>
(set 'data '((monday (apples 20 30) (oranges 2 4 9)) (tuesday (apples 5) (oranges 32 1))))

(set-ref-all '(oranges *) data (list (first $it) (apply + (rest $it))) match)
    <span class='arw'>&rarr;</span> ( ... (oranges 15) ... (oranges 33) ... ) 
</pre>


<p>The example sums all numbers found in records starting with 
the symbol <tt>oranges</tt>. The found items appear in <tt>$it</tt></p>

<p>See also <a href="#set-ref">set-ref</a> which replaces only the first element found.</p>

<br/><br/>

<a name="setq"></a> <a name="setf"></a>
<h2><span class="function">setq setf</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (setq <em>place-1</em> <em>exp-1</em> [<em>place-2</em> <em>exp-2</em> ... ])</h4>

<p>
<tt>setq</tt> and <tt>setf</tt> work alike in newLISP and set the contents
of a symbol, list, array or string or of a list, array or string place reference. Like 
<a href="#set">set</a>, <tt>setq</tt> and <tt>setf</tt> can take multiple argument pairs. 
Although both <tt>setq</tt> and <tt>setf</tt> point to the same built-in function internally, 
throughout this manual <tt>setq</tt> is used when setting a symbol reference and <tt>setf</tt> 
is used when setting list or array references.</p>

<!-- example -->

<pre>
(setq x 123)  <span class='arw'>&rarr;</span> 123

; multiple arguments

(setq x 1 y 2 z 3)  <span class='arw'>&rarr;</span> 3 

x  <span class='arw'>&rarr;</span> 1
y  <span class='arw'>&rarr;</span> 2
z  <span class='arw'>&rarr;</span> 3

; with nth or implicit indices
(setq L '(a b (c d) e f g))

(setf (L 1) 'B)      <span class='arw'>&rarr;</span> B
; or the same
(setf (nth 1 L) 'B)
L                    <span class='arw'>&rarr;</span> (a B (c d) e f g)

(setf (L 2 0) 'C)    <span class='arw'>&rarr;</span> C
L                    <span class='arw'>&rarr;</span> (a B (C d) e f g)

(setf (L 2) 'X)   
L                    <span class='arw'>&rarr;</span> (A B X e f g)

; with assoc
(setq L '((a 1) (b 2)))
(setf (assoc 'b L) '(b 3)) <span class='arw'>&rarr;</span> (b 3)
L                          <span class='arw'>&rarr;</span> ((a 1) (b 3))

; with lookup
(setf (lookup 'b L) 30) <span class='arw'>&rarr;</span> 30
L                       <span class='arw'>&rarr;</span> ((a 1) (b 30))

; several list accessors can be nested
(setq L '((a 1) (b 2)))

(push 'b (setf (assoc 'b l) '(b 4))) 'b) <span class='arw'>&rarr;</span> b
L                                        <span class='arw'>&rarr;</span>((a 1) (b b 4)))

; on strings
(set 's "NewISP")

(setf (s 0) "n") <span class='arw'>&rarr;</span> "n"
s <span class='arw'>&rarr;</span> "newISP"

(setf (s 3) "LI") <span class='arw'>&rarr;</span> "LI"
s <span class='arw'>&rarr;</span> "newLISP"
</pre>


<p>Often the new value set is dependent on the old value. <tt>setf</tt> can
use the anaphoric system variable <tt>$it</tt> to refer to the old
value inside the <tt>setf</tt> expression:</p>


<pre>
(setq L '((apples 4) (oranges 1))) 

(setf (L 1 1) (+ $it 1)) <span class='arw'>&rarr;</span> 2

L                        <span class='arw'>&rarr;</span> ((apples 4) (oranges 2))

(set 's "NewLISP")

(setf (s 0) (lower-case $it)) <span class='arw'>&rarr;</span> "n")

s <span class='arw'>&rarr;</span> "newLISP"
</pre>

<br/><br/>

<a name="sgn"></a>

<h2><span class="function">sgn</span></h2>
<h4>syntax: (sgn <em>num</em>)<br/>
syntax: (sgn <em>num</em> <em>exp-1</em> [<em>exp-2</em> [<em>exp-3</em>]])</h4>

<p>
In the first syntax,
the <tt>sgn</tt> function is a logical function 
that extracts the sign of a real number 
according to the following rules:
</p>
<p>
<b><em>
x &gt; <tt>0</tt> : sgn(x) = 1<br/>
x &lt; <tt>0</tt> : sgn(x) = -1<br/>
x = <tt>0</tt> : sgn(x) = <tt>0</tt>
</em></b>
</p>

<!-- example -->

<pre>
(sgn -3.5)  <span class='arw'>&rarr;</span> -1
(sgn 0)     <span class='arw'>&rarr;</span> 0
(sgn 123)   <span class='arw'>&rarr;</span> 1
</pre>

<p>In the second syntax, the result of evaluating 
one of the optional expressions 
<em>exp-1</em>, <em>exp-2</em>, or <em>exp-3</em> is returned, 
instead of <tt>-1</tt>, <tt>0</tt>, or <tt>1</tt>. 
If <em>exp-n</em> is missing for the case triggered,
then <tt>nil</tt> is returned.</p>

<!-- example -->

<pre>
(sgn x -1 0 1)         ; works like (sgn x)
(sgn x -1 1 1)         ; -1 for negative x all others 1
(sgn x nil true true)  ; nil for negative else true
(sgn x (abs x) 0)      ; (abs x) for x &lt; 0, 0 for x = 0, else nil
</pre>


<p>
Any expression or constant can be used for 
<em>exp-1</em>, <em>exp-2</em>, or <em>exp-3</em>.
</p>

<br/><br/>

<a name="share"></a>
<h2><span class="function">share</span></h2>
<h4>syntax: (share)<br/>
syntax: (share <em>int-address-or-handle</em>)<br/>
syntax: (share <em>int-address-or-handle</em> <em>exp-value</em>)<br/><br/>
syntax: (share <em>nil</em> <em>int-address</em>)</h4>

<p>
Accesses shared memory 
for communicating between 
several newLISP processes.
When called without arguments, 
 <tt>share</tt> requests a page of shared memory 
from the operating system. 
This returns a memory address on Linux/Unix 
and a handle on MS Windows, 
which can then be 
assigned to a variable 
for later reference.
This function is not available on OS/2.
</p>

<p>To set the contents of shared memory, use the third syntax of <tt>share</tt>. 
Supply a shared memory address on Linux/Unix or a handle on MS Windows in 
<em>int-address-or-handle</em>, along with an integer, float, string 
expression or any other expression (since v.10.1.0) supplied
in <em>exp-value</em>.  Using this syntax, the value supplied in <em>exp-value</em> 
is also the return value.</p>

<p>To access the contents of shared memory, 
use the second syntax of <tt>share</tt>, 
supplying only the shared memory address or handle.
The return value will be any constant or expression (since v.10.1.0)
written previously into the memory.
If the memory has not been previously set to a value, 
<tt>nil</tt> will be returned.</p>

<p>Only available on Unix-like operating systems, 
the last syntax unmaps a shared memory address.
Note that using a shared address after unmapping it 
will crash the system.</p>

<p>Memory can be shared between parent and child processes,
but not between independent processes.</p>

<p>Since 10.1.0 size of share objects can exceed the shared memory pagesize
of the operating system. For objects bigger than the pagesize, newLISP internally
uses files for sharing. This requires a <tt>/tmp</tt> directory on Unix-like
operating system. On MS Windows systems the environment variable <tt>TEMP</tt>
must be set.</p>

<!-- example -->

<pre>
(set 'mem (share))

(share mem 123)  <span class='arw'>&rarr;</span> 123
(share mem)      <span class='arw'>&rarr;</span> 123

(share mem "hello world") <span class='arw'>&rarr;</span> "hello world"
(share mem)               <span class='arw'>&rarr;</span> "hello world"

(share mem true)  <span class='arw'>&rarr;</span> true
(share mem)       <span class='arw'>&rarr;</span> true

(share mem '(+ 1 2 3 4))  <span class='arw'>&rarr;</span> (+ 1 2 3 4)
(share mem)               <span class='arw'>&rarr;</span> (+ 1 2 3 4)

; expressions received can be evaluated (since v.10.1.0)
(eval (share mem))        <span class='arw'>&rarr;</span> 10 

(share nil mem)   <span class='arw'>&rarr;</span> true  ; unmap only on Unix
</pre>


<p>Expression read from shared memory and evaluated, will be evaluated
in the recipient's process environment.</p>

<p>Note that shared memory access between different processes 
should be synchronized using a <a href="#semaphore">semaphore</a>.
Simultaneous access to shared memory can crash the running process.</p>

<p>For a more comprehensive example of using shared memory in a multi process 
Linux/Unix application, see the file <tt>example/prodcons.lsp</tt> in the
newLISP source distribution.</p>

<br/><br/>

<a name="signal"></a>
<h2><span class="function">signal</span></h2>
<h4>syntax: (signal <em>int-signal</em> <em>sym-event-handler</em> | <em>func-event-handler</em>)<br/>
syntax: (signal <em>int-signal</em> "ignore" | "default" | "reset")<br/>
syntax: (signal <em>int-signal</em>)</h4>

<p>
Sets a user-defined handler in <em>sym-event-handler</em> for a signal specified in <em>int-signal</em>
or sets to a function expression in <em>func-event-handler</em>.</p>

<p>A parameter following <em>int-signal</em> is not evaluated.</p>

<p>If no signal handler is specified any of the string constants <tt>"ignore"</tt>,
<tt>"default"</tt> or <tt>"reset"</tt> can be specified in either lower or upper case
or simply using the first letter of the option string. When signal setup with any
of these three options has been successful, <tt>true</tt> is returned.</p>

<p>Using <tt>"ignore"</tt> will make newLISP ignore the signal. Using <tt>"default"</tt>
will set the handler to the default handler of the underlying platform OS. The <tt>"reset"</tt>
option will restore the handler to newLISP startup state.</p>

<p>On startup, newLISP either specifies an empty newLISP handler or a Ctrl-C handler for 
<tt>SIGINT</tt> and a <tt>waitpipd(-1, 0, WNOHANG)</tt> C-call for <tt>SIGCHLD</tt>.
</p>

<p>Different signals are available on different OS platforms and Linux/Unix flavors.
The numbers to specify in <em>int-signal</em> also differ from platform-to-platform.
Valid values can normally be extracted from a file found in <tt>/usr/include/sys/signal.h</tt> 
or <tt>/usr/include/signal.h</tt>.</p>

<p>Some signals make newLISP exit even after a user-defined handler 
has been specified and executed (e.g., signal SIGKILL).
This behavior may also be different on different platforms.</p>

<!-- example -->

<pre>
(constant 'SIGINT 2)
(define (ctrlC-handler) (println "ctrl-C has been pressed"))

(signal SIGINT 'ctrlC-handler)

; now press ctrl-C
; the following line will appear
; this will only work in an interactive terminal window

ctrl-C has been pressed

; reset treatment of signal 2 to startup conditions

(signal SIGINT "reset")
</pre>


<p> On MS Windows, the above example would execute the handler before exiting newLISP.
On most Linux/Unix systems, newLISP would stay loaded and the prompt would appear 
after hitting the [enter] key.</p>

<p> Instead of specifying a symbol containing the signal handler,
a function can be specified directly.  The signal number is passed as a parameter:
</p>


<pre>
(signal SIGINT exit)  <span class='arw'>&rarr;</span> $signal-2

(signal SIGINT (fn (s) (println "signal " s " occurred")))
</pre>


<p> Note that the signal SIGKILL (9 on most platforms) will always terminate the 
application regardless of an existing signal handler.</p>

<p>The signal could have been sent from another shell on the same computer:</p>


<pre>
kill -s SIGINT 2035
</pre>


<p>In this example, <tt>2035</tt> is the process ID of the running newLISP.</p>

<p>The signal could also have been sent from another newLISP application using 
the function <a href="#destroy">destroy</a>:</p>


<pre>
(destroy 2035) <span class='arw'>&rarr;</span> true
</pre>


<p>If newLISP receives a signal while evaluating another function,
it will still accept the signal and the handler function will be executed:</p>


<pre>
; only on macOS, BSDs and Linux, not on Windows
(constant 'SIGINT 2)
(define (ctrlC-handler) (println "ctrl-C has been pressed"))

(signal SIGINT 'ctrlC-handler)
;; or
(signal SIGINT ctrlC-handler)


(while true (sleep 300) (println "busy"))

;; generates following output
busy
busy
busy
ctrl-C has been pressed
busy
busy
&hellip;
</pre>


<p>Specifying only a signal number will return either the name of 
the currently defined handler function or <tt>nil</tt>.
</p>

<p>The user-defined signal handler can pass the signal number as a parameter.</p>


<pre>
(define (signal-handler sig)
	(println "received signal: " sig))

;; set all signals from 1 to 8 to the same handler	
(for (s 1 8) 
	(signal s 'signal-handler))
</pre>


<p>In this example, all signals from 1 to 8 are set to the same handler.</p>

<br/><br/>

<a name="silent"></a>
<h2><span class="function">silent</span></h2>

<h4>syntax: (silent [<em>exp-1</em> [<em>exp-2</em> ... ]])</h4>

<p>
	Evaluates one or more expressions in <em>exp-1</em>&mdash;.
	<tt>silent</tt> is similar to <a href="#begin">begin</a>,
	but it suppresses console output 
	of the return value 
	and the following prompt.
	It is often used 
	when communicating from 
	a remote application with newLISP 
	(e.g., GUI front-ends 
	or other applications controlling newLISP), 
	and the return value is of no interest.
</p>

<p>
	Silent mode is reset when returning to a prompt.
	This way, 
	it can also be used without arguments 
	in a batch of expressions.
	When in interactive mode, 
	hit [enter] twice after a statement 
	using <tt>silent</tt> 
	to get the prompt back.
</p>


<!-- example -->

<pre>
(silent (my-func))  ; same as next

(silent) (my-func)  ; same effect as previous
</pre>

<br/><br/>

<a name="sin"></a>
<h2><span class="function">sin</span></h2>
<h4>syntax: (sin <em>num-radians</em>)</h4>

<p>
	Calculates the sine function 
	from <em>num-radians</em> 
	and returns the result.
</p>

<!-- example -->

<pre>
(sin 1)                     <span class='arw'>&rarr;</span> 0.8414709838
(set 'pi (mul 2 (acos 0)))  <span class='arw'>&rarr;</span> 3.141592654
(sin (div pi 2))            <span class='arw'>&rarr;</span> 1
</pre>

<br/><br/>

<a name="sinh"></a>
<h2><span class="function">sinh</span></h2>
<h4>syntax: (sinh <em>num-radians</em>)</h4>

<p>Calculates the hyperbolic sine of <em>num-radians</em>. 
The hyperbolic sine is defined mathematically as: <em>(exp (x) - exp (-x)) / 2</em>.
An overflow to <tt>inf</tt> may occur if <em>num-radians</em> is too large.</p>

<!-- example -->

<pre>
(sinh 1)     <span class='arw'>&rarr;</span> 1.175201194
(sinh 10)    <span class='arw'>&rarr;</span> 11013.23287
(sinh 1000)  <span class='arw'>&rarr;</span> inf
(sub (tanh 1) (div (sinh 1) (cosh 1))) <span class='arw'>&rarr;</span> 0
</pre>

<br/><br/>

<a name="sleep"></a>
<h2><span class="function">sleep</span></h2>

<h4>syntax: (sleep <em>num-milliseconds</em>)</h4>

<p>Gives up CPU time to other processes for the amount of 
milliseconds specified in <em>num-milli-seconds</em>.
</p>

<!-- example -->

<pre>
(sleep 1000)  ; sleeps 1 second
(sleep 0.5)   ; sleeps 500 micro seconds
</pre>


<p>On some platforms, <tt>sleep</tt> is only available with a resolution 
of one second. In this case, the parameter <em>int-milli-seconds</em> 
will be rounded to the nearest full second.</p>

<p>A <tt>sleep</tt> may be cut short by a finishing child process started
with <a href="#fork">fork</a> or <a href="#spawn">spawn</a>.</p>

<br/><br/>

<a name="slice"></a>
<h2><span class="function">slice</span></h2>

<h4>syntax: (slice <em>list</em> <em>int-index</em> [<em>int-length</em>])<br/>
syntax: (slice <em>array</em> <em>int-index</em> [<em>int-length</em>])<br/>
syntax: (slice <em>str</em> <em>int-index</em> [<em>int-length</em>])</h4>

<p>In the first form, <tt>slice</tt> copies a sublist 
from a <em>list</em>.  The original list is left unchanged.
The sublist extracted starts at index <em>int-index</em> 
and has a length of <em>int-length</em>.  If <em>int-length</em> is negative,
<tt>slice</tt> will take the parameter as offset counting from the end and copy 
up to but not including that offset.  If the parameter is omitted, 
<tt>slice</tt> copies all of the elements to the end of the list.</p>

<p>
	See also <a href="#indexing">Indexing elements of strings and lists</a>.
</p>

<!-- example -->

<pre>
(slice '(a b c d e f) 3 2)   <span class='arw'>&rarr;</span> (d e)
(slice '(a b c d e f) 2 -2)  <span class='arw'>&rarr;</span> (c d)
(slice '(a b c d e f) 2)     <span class='arw'>&rarr;</span> (c d e f)
(slice '(a b c d e f) -4 3)  <span class='arw'>&rarr;</span> (c d e)

(set 'A (array 3 2 (sequence 1 6))) <span class='arw'>&rarr;</span> ((1 2) (3 4) (5 6))
(slice A 1 2) <span class='arw'>&rarr;</span> ((3 4) (5 6))
</pre>


<p>
In the second form, a part of the string in <em>str</em> 
is extracted.  <em>int-index</em> contains the start index
and <em>int-length</em> contains the length of the substring.
If <em>int-length</em> is not specified, everything to the end of the string is extracted.
<tt>slice</tt> also works on string buffers containing binary data like <tt>0</tt>'s (zeroes). 
It operates on byte boundaries rather than character boundaries.
See also <a href="#indexing">Indexing elements of strings and lists</a>.</p>

<p>Note that <tt>slice</tt> always works on single 8-bit byte boundaries for
offset and length numbers, even when running the UTF-8 enabled version of newLISP.</p>

<!-- example -->

<pre>
(slice "Hello World" 6 2)  <span class='arw'>&rarr;</span> "Wo"
(slice "Hello World" 0 5)  <span class='arw'>&rarr;</span> "Hello"
(slice "Hello World" 6)    <span class='arw'>&rarr;</span> "World"
(slice "newLISP" -4 2)     <span class='arw'>&rarr;</span> "LI"

; UTF-8 strings are converted to a list, then joined again

(join (slice (explode "ΩΨΧΦΥΤΣΣΡΠΟΞΝΜΛΚΙΘΗΖΕΔΓΒΑ") 3 5))  <span class='arw'>&rarr;</span> "ΦΥΤΣΣ" 
</pre>


<p>
	Note that an <em>implicit slice</em> 
	is available for lists.
	See the chapter <a href="#implicit_rest_slice">Implicit rest and slice</a>.
</p>

<p>
	Be aware that <a href="#slice">slice</a> 
	always works on byte boundaries 
	rather than character boundaries 
	in the UTF-8&ndash;enabled version of newLISP.
	As a result, 
	<a href="#slice">slice</a> can be used 
	to manipulate binary content.
</p>

<br/><br/>

<a name="sort"></a>
<h2><span class="function">sort</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (sort <em>list</em> [<em>func-compare</em>])<br/>
syntax: (sort <em>array</em> [<em>func-compare</em>])</h4>

<p>All members in <em>list</em> or <em>array</em> are sorted in ascending order.
Anything may be sorted, regardless of the types.
When members are themselves lists or arrays, each element 
is recursively compared.  If two expressions 
of different types are compared, the lower type is sorted 
before the higher type in the following order:
</p>

<pre>
Atoms: nil, true, integer or float, string, symbol, primitive
Lists: quoted expression, list, lambda, lambda-macro
</pre>

<p>The <tt>sort</tt> is destructive, changing the order of the elements in the
original list or array and returning the sorted list or array. It is a stable
binary merge-sort with approximately <em>O(n log2 n)</em> performance
preserving the order of adjacent elements which are equal. When  
<em>func-compare</em> is used it must work with either <tt>&lt;=</tt> or
<tt>&gt;=</tt> operators to be stable.</p>

<p>An optional comparison operator, user-defined function, 
or anonymous function can be supplied. The functor or operator 
can be given with or without a preceding quote.</p>

<!-- example -->

<pre>
(sort '(v f r t h n m j))     <span class='arw'>&rarr;</span> (f h j m n r t v)
(sort '((3 4) (2 1) (1 10)))  <span class='arw'>&rarr;</span> ((1 10) (2 1) (3 4))
(sort '((3 4) "hi" 2.8 8 b))  <span class='arw'>&rarr;</span> (2.8 8 "hi" b (3 4))

(set 's '(k a l s))
(sort s)  <span class='arw'>&rarr;</span> (a k l s)  

(sort '(v f r t h n m j) &gt;) <span class='arw'>&rarr;</span> (v t r n m j h f)

(sort s &lt;)  <span class='arw'>&rarr;</span> (a k l s)
(sort s &gt;)  <span class='arw'>&rarr;</span> (s l k a)  
s           <span class='arw'>&rarr;</span> (s l k a)

;; define a comparison function
(define (comp x y) 
    (&gt;= (last x) (last y)))
    
(set 'db '((a 3) (g 2) (c 5)))

(sort db comp)  <span class='arw'>&rarr;</span>  ((c 5) (a 3) (g 2))

;; use an anonymous function
(sort db (fn (x y) (&gt;= (last x) (last y))))
</pre>

<br/><br/>

<a name="source"></a>
<h2><span class="function">source</span></h2>
<h4>syntax: (source)<br/>
syntax: (source <em>sym-1</em> [<em>sym-2</em> ... ])</h4>

<p>
	Works almost identically to <a href="#save">save</a>,
	except symbols and contexts get serialized to a string 
	instead of being written to a file.
	Multiple variable symbols,
	definitions, and contexts 
	can be specified.
	If no argument is given,
	<tt>source</tt> serializes the entire 
	newLISP workspace.
	When context symbols are serialized,
	any symbols contained within that context 
	will be serialized, as well.
	Symbols containing <tt>nil</tt> 
	are not serialized.
	System symbols beginning with the <tt>$</tt> (dollar sign) character 
	are only serialized when mentioned explicitly.
</p>

<p>
	Symbols not belonging to the current context
	are written out with their context prefix.
</p>

<!-- example -->

<pre>
(define (double x) (+ x x))

(source 'double)  <span class='arw'>&rarr;</span> "(define (double x)\n  (+ x x))\n\n"
</pre>


<p>
	As with <a href="#save">save</a>,
	the formatting of line breaks 
	and leading spaces or tabs 
	can be controlled using the 
	<a href="#pretty-print">pretty-print</a> function.
</p>

<br/><br/>

<a name="spawn"></a>
<h2><span class="function">spawn</span></h2>
<h4>syntax: (spawn <em>sym</em> <em>exp</em> [true])</h4>

<p>Launches the evaluation of <em>exp</em> as a child process and immediately
returns. The symbol in <em>sym</em> is quoted and receives the result of the 
evaluation when the function <a href="#sync">sync</a> is executed. <tt>spawn</tt>
is used to start parallel evaluation of expressions in concurrent processes.
If newLISP is running on a multi-core CPU, the underlying operating system 
will distribute spawned processes onto different cores, thereby evaluating 
expressions in parallel and speeding up overall processing.</p>

<p>The optional <tt>true</tt> parameter must be set if <a href="#send">send</a>
or <a href="#receive">receive</a> is used to communicated with the child
process spawned.</p>

<p>The function <tt>spawn</tt> is not available on MS Windows.</p>

<p>After successfully starting a child process,  the <tt>spawn</tt> expression
returns the process id of the forked process. The following examples shows
how the calculation of a range of prime numbers can be split up in four sub ranges to
speed up the calculation of the whole range:</p>

<!-- example -->

<pre>
; calculate primes in a range
(define (primes from to)
  (local (plist)
      (for (i from to)
          (if (= 1 (length (factor i)))
              (push i plist -1)))
      plist))

; start child processes
(set 'start (time-of-day))

(spawn 'p1 (primes 1 1000000))
(spawn 'p2 (primes 1000001 2000000))
(spawn 'p3 (primes 2000001 3000000))
(spawn 'p4 (primes 3000001 4000000))

; wait for a maximum of 60 seconds for all tasks to finish
(sync 60000) ; returns true if all finished in time
; p1, p2, p3 and p4 now each contain a lists of primes

(println "time spawn: " (- (time-of-day) start))
(println "time simple: " (time  (primes 1 4000000)))

(exit)
</pre>


<p>On a 1.83 Intel Core 2 Duo processor, the above example will finish
after about 13 seconds. Calculating all primes using <tt>(primes 1 4000000)</tt>
would take about 20 seconds.</p>

<p>The <a href="#sync">sync</a> function will wait for all child processes
to finish and receive the evaluation results in the symbols <tt>p1</tt> to
<tt>p4</tt>. When all results are collected, <tt>sync</tt>
will stop waiting and return <tt>true</tt>. When the time specified was
insufficient , <tt>sync</tt> will return <tt>nil</tt> and  another 
<tt>sync</tt> statement could be given to further wait and collect results. 
A short timeout time can be used to do other processing during waiting:</p>


<pre>
(spawn 'p1 (primes 1 1000000))
(spawn 'p2 (primes 1000001 2000000))
(spawn 'p3 (primes 2000001 3000000))
(spawn 'p4 (primes 3000001 4000000))

; print a dot after each 2 seconds of waiting
(until (sync 2000) (println "."))
</pre>


<p><tt>sync</tt> when used without any parameters, will not wait but immediately
return a list of pending child processes. For the <tt>primes</tt> example, the following
<tt>sync</tt> expression could be used to watch the progress:</p>


<pre>
(spawn 'p1 (primes 1 1000000))
(spawn 'p2 (primes 1000001 2000000))
(spawn 'p3 (primes 2000001 3000000))
(spawn 'p4 (primes 3000001 4000000))

; show a list of pending process ids after each three-tenths of a second
(until (sync 300) (println (sync)))
</pre>


<p>A parameter of <tt>-1</tt> tells <tt>sync</tt> to wait for a very long time
(~ 1193 hours). A better solution would be to wait for a maximum time, 
then <a href="#abort">abort</a>  all pending child processes:</p>


<pre>
(spawn 'p1 (primes 1 1000000))
(spawn 'p2 (primes 1000001 2000000))
(spawn 'p3 (primes 2000001 3000000))
(spawn 'p4 (primes 3000001 4000000))

; wait for one minute, then abort and
; report unfinished PIDs

(if (not (sync 60000))
    (begin
        (println "aborting unfinished: " (sync))
        (abort))
    (println "all finished successfully")
)
</pre>


<p>The three functions <tt>spawn</tt>, <tt>sync</tt> and <tt>abort</tt>
are part of the <a href="http://supertech.csail.mit.edu/cilk/">Cilk</a> API.
The original implementation also does sophisticated scheduling and allocation
of threaded tasks to multiple CPU cores. The newLISP implementation of the Cilk API
lets the operating system of the underlying platform handle process management.
Internally, the API is implemented using the Unix libc functions <tt>fork()</tt>,
<tt>waitpid()</tt> and <tt>kill()</tt>. Intercommunications between processes
and child processes is done using the <a href="#send">send</a> and 
<a href="#receive">receive</a> functions.</p>

<p><tt>spawn</tt> can be called recursively from spawned subtasks:</p>


<pre>
(define (fibo n)
  (local (f1 f2)
    (if(&lt; n 2) 1
       (begin
          (spawn 'f1 (fibo (- n 1)))
          (spawn 'f2 (fibo (- n 2)))
          (sync 10000)
          (+ f1 f2)))))

(fibo 7)  <span class='arw'>&rarr;</span> 21
</pre>


<p>With <tt>(fibo 7)</tt> 41 processes will be generated. Although the above
code shows the working of the Cilk API in a recursive application, 
it would not be practical, as the overhead required to spawn subtasks 
is much higher than the time saved through parallelization.</p>

<p>Since version 10.1 a <a href="#send">send</a> and <a href="#receive">receive</a>
message functions are available for communications between parent and child processes. 
Using these functions any data or expression of any size can be transferred. 
Additionally messaged expressions can be evaluated in the recipient's environment.</p>

<p><a href="#fork">fork</a> and <a href="#process">process</a> are other functions
to start newLISP processes.</p>

<br/><br/>

<a name="sqrt"></a>
<h2><span class="function">sqrt</span></h2>
<h4>syntax: (sqrt <em>num</em>)</h4>

<p>
	Calculates the square root from 
	the expression in <em>num</em> 
	and returns the result.
</p>

<!-- example -->

<pre>
(sqrt 10)  <span class='arw'>&rarr;</span> 3.16227766
(sqrt 25)  <span class='arw'>&rarr;</span> 5
</pre>

<br/><br/>

<a name="ssq"></a>
<h2><span class="function">ssq</span></h2>
<h4>syntax: (ssq <em>list-vector | array-vector</em>)</h4>

<p>Calculates the sum of squares of numbers in a vector in 
<em>list-vector</em> or <em>array-vector</em>.</p>

<!-- example -->

<pre>
(set 'vector (sequence 1 10))
(ssq vector) <span class='arw'>&rarr;</span> 385

(set 'vector (array 10 (sequence 1 10)))
(ssq vector) <span class='arw'>&rarr;</span> 385
</pre>

<br/><br/>

<a name="starts-with"></a>
<h2><span class="function">starts-with</span></h2>
<h4>syntax: (starts-with <em>str</em> <em> str-key</em> [<em>num-option</em>])<br/>
syntax: (starts-with <em>list</em> [<em>exp</em>])</h4>

<p>In the first version, <tt>starts-with</tt> checks if the string <em>str</em> 
starts with a key string in <em>str-key</em> and returns <tt>true</tt> or <tt>nil</tt> 
depending on the outcome.</p>

<p>If a regular expression number is specified in <em>num-option</em>,
<em>str-key</em> contains a regular expression pattern.
See <a href="#regex">regex</a> for valid <em>option</em> numbers. </p>

<!-- example -->

<pre>
(starts-with "this is useful" "this")        <span class='arw'>&rarr;</span> true
(starts-with "this is useful" "THIS")        <span class='arw'>&rarr;</span> nil

;; use regular expressions
(starts-with "this is useful" "THIS" 1)      <span class='arw'>&rarr;</span> true
(starts-with "this is useful" "this|that" 0) <span class='arw'>&rarr;</span> true
</pre>


<p>In the second version, <tt>starts-with</tt> checks to see if a list 
starts with the list element in <em>exp</em>.  <tt>true</tt> or <tt>nil</tt> 
is returned depending on outcome.</p>

<!-- example -->

<pre>
(starts-with '(1 2 3 4 5) 1)             <span class='arw'>&rarr;</span> true
(starts-with '(a b c d e) 'b)            <span class='arw'>&rarr;</span> nil
(starts-with '((+ 3 4) b c d) '(+ 3 4))  <span class='arw'>&rarr;</span> true
</pre>



<p>
	See also the <a href="#ends-with">ends-with</a> function.
</p>

<br/><br/>

<a name="stats"></a>
<h2><span class="function">stats</span></h2>
<h4>syntax: (stats <em>list-vector</em>)</h4>

<p>The functions calculates statistical values of central tendency and distribution moments
of values in <em>list-vector</em>. The following values are returned by <tt>stats</tt>
in a list:</p>

<table>
<tr align="left"><th>name</th><th>description</th></tr>
<tr><td>N</td><td>Number of values</td></tr>
<tr><td>mean</td><td>Mean of values</td></tr>
<tr><td>avdev</td><td>Average deviation from mean value</td></tr>
<tr><td>sdev</td><td>Standard deviation (population estimate)</td></tr>
<tr><td>var</td><td>Variance (population estimate)</td></tr>
<tr><td>skew</td><td>Skew of distribution</td></tr>
<tr><td>kurt</td><td>Kurtosis of distribution</td></tr>
</table>
<br/>

<!-- example -->

<p>The following example uses the list output from the <tt>stats</tt> expression as an
argument for the <a href="#format">format</a> statement:</p>

<pre>
(set 'data '(90 100 130 150 180 200 220 300 350 400))

(println (format [text]
    N        = %5d
    mean     = %8.2f
    avdev    = %8.2f
    sdev     = %8.2f
    var      = %8.2f
    skew     = %8.2f
    kurt     = %8.2f
[/text] (stats data)))

; outputs the following

    N        =    10
    mean     =   212.00
    avdev    =    84.40
    sdev     =   106.12
    var      = 11262.22
    skew     =     0.49
    kurtosis =    -1.34

</pre>


<br/><br/>

<a name="string"></a>
<h2><span class="function">string</span></h2>
<h4>syntax: (string <em>exp-1</em> [<em>exp-2</em> ... ])</h4>

<p>
	Translates into a string anything that results 
	from evaluating <em>exp-1</em>&mdash;.
	If more than one expression is specified, 
	the resulting strings are concatenated.
</p>

<!-- example -->

<pre>
(string 'hello)          <span class='arw'>&rarr;</span> "hello"
(string 1234)            <span class='arw'>&rarr;</span> "1234"
(string '(+ 3 4))        <span class='arw'>&rarr;</span> "(+ 3 4)"
(string (+ 3 4) 8)       <span class='arw'>&rarr;</span> "78"
(string 'hello " " 123)  <span class='arw'>&rarr;</span> "hello 123"
</pre>


<p>
	If a buffer passed to <tt>string</tt> 
	contains <tt>\000</tt>,
	only the string up to the first terminating zero will be copied:
</p>


<pre>
(set 'buff "ABC\000\000\000")  <span class='arw'>&rarr;</span> "ABC\000\000\000"

(length buff)  <span class='arw'>&rarr;</span> 6

(string buff)  <span class='arw'>&rarr;</span> "ABC"

(length (string buff))  <span class='arw'>&rarr;</span> 3
</pre>


<p>
	Use the <a href="#append">append</a> 
	and <a href="#join">join</a> 
	(allows the joining string 
	to be specified) functions 
	to concatenate strings containing zero bytes.
	Use the <a href="#source">source</a> function 
	to convert a lambda expression 
	into its newLISP source string representation.
</p>

<br/><br/>

<a name="stringp"></a>
<h2><span class="function">string?</span></h2>
<h4>syntax: (string? <em>exp</em>)</h4>

<p>
	Evaluates <em>exp</em> and tests 
	to see if it is a string.
	Returns <tt>true</tt> or <tt>nil</tt>
	depending on the result.
</p>

<!-- example -->

<pre>
(set 'var "hello")
(string? var)  <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="struct"></a>
<h2><span class="function">struct</span></h2>
<h4>syntax: (struct <em>symbol</em> [<em>str-data-type</em> ... ])</h4>

<p>The <tt>struct</tt> function can be used to define aggregate data types for 
usage with the extended syntax of <a href="#import">import</a>,
<a href="#pack">pack</a> and <a href="#unpack">unpack</a>, available on all
versions of newLISP compiled with <i>libffi</i>. This allows importing
functions which take C-language <em>struct</em> data types or pointers to these
aggregate data types.</p>

<p>The following example illustrates the usage of <tt>struct</tt> together with
the C data functions <tt>localtime</tt> and <tt>asctime</tt>. The <tt>localtime</tt>
functions works similar to the built-in <a href="#now">now</a> function. The
<tt>asctime</tt> function takes the numerical data output by <tt>localtime</tt>
and formats these to readable text.</p>

<pre>
/* The C function prototypes for the functions to import */

struct tm * localtime(const time_t *clock);

char * asctime(const struct tm *timeptr);

/* the tm struct aggregating different time related values */

struct tm {
    int tm_sec;      /* seconds after the minute [0-60] */
    int tm_min;      /* minutes after the hour [0-59] */
    int tm_hour;     /* hours since midnight [0-23] */
    int tm_mday;     /* day of the month [1-31] */
    int tm_mon;      /* months since January [0-11] */
    int tm_year;     /* years since 1900 */
    int tm_wday;     /* days since Sunday [0-6] */
    int tm_yday;     /* days since January 1 [0-365] */
    int tm_isdst;    /* Daylight Savings Time flag */
    long tm_gmtoff;  /* offset from CUT in seconds */   /*** not on Windows ***/
    char *tm_zone;   /* timezone abbreviation */        /*** not on Windows ***/
};
</pre>

<p>Function import and definition of the structure data type in newLISP:</p>

<pre>
;; for pointers to structs always use void*
;; as a library use msvcrt.dll on Windows or libc.so on Unix.
;; The tm struct type is configured for macOS and Linux.
;; On other OS the tm structure may be different
 
(import "libc.dylib" "asctime" "char*" "void*")
(import "libc.dylib" "localtime" "void*" "void*")

; definition of the struct
(struct 'tm "int" "int" "int" "int" "int" "int" "int" "int" "int" "long" "char*")


;; use import and struct

; todays date number (seconds after 1970 also called Unix epoch time)
(set 'today (date-value))  <span class='arw'>&rarr;</span> 1324134913

;; the time value is passed by it's address
;; localtime retirns a pointer to a tm struct

(set 'ptr (localtime (address today))) <span class='arw'>&rarr;</span> 2896219696

; unpack the tm struct  (7:15:13 on the 17th etc.)
(unpack tm ptr) <span class='arw'>&rarr;</span> (13 15 7 17 11 111 6 350 0 -28800 "PST")

; transform to readable form
(asctime ptr) <span class='arw'>&rarr;</span> "Sat Dec 17 07:15:13 2011\n"

; all in one statement does actually not use struct, pointers are passed directly
(asctime (localtime (address today))) <span class='arw'>&rarr;</span> "Sat Dec 17 07:15:13 2011"

; same as the built-in date function
(date today) <span class='arw'>&rarr;</span> "Sat Dec 17 07:15:13 2011"
</pre>

<p>Care must be taken to pass valid addresses to pointer parameters in imported functions
or when passing address pointers to <a href="#unpack">unpack</a>. Invalid address pointers
can crash newLISP or make it unstable.</p>

<p><tt>struct</tt> definitions can be nested:</p>

<pre>
; the pair aggregate type
(struct 'pair "char" "char") <span class='arw'>&rarr;</span> pair

; nested struct type
(struct 'comp "pair" "int")  <span class='arw'>&rarr;</span> comp

; pack data using the extended pack syntax
; note the insertion of structure alignment bytes after the pair
(pack comp (pack pair 1 2) 3) <span class='arw'>&rarr;</span> "\001\002\000\000\003\000\000\000"

; unpack reverses the process
(unpack comp "\001\002\000\000\003\000\000\000") <span class='arw'>&rarr;</span> ((1 2) 3)
</pre>

<p>Nested structures are unpacked recursively.</p>

<br/><br/>


<a name="sub"></a>
<h2><span class="function">sub</span></h2>
<h4>syntax: (sub <em>num-1</em> [<em>num-2</em> ... ])</h4>

<p>
	Successively subtracts
	the expressions in <em>num-1</em>,
	<em>num-2</em>&mdash;.
	<tt>sub</tt> performs mixed-type arithmetic 
	and handles integers or floating points,
	but it will always return 
	a floating point number.
	If only one argument is supplied,
	its sign is reversed.
	Any floating point calculation 
	with <tt>NaN</tt> also returns <tt>NaN</tt>.
</p>

<!-- example -->

<pre>
(sub 10 8 0.25)  <span class='arw'>&rarr;</span> 1.75
(sub 123)        <span class='arw'>&rarr;</span> -123
</pre>

<br/><br/>

<a name="swap"></a>
<h2><span class="function">swap</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (swap <em>place-1</em> <em>place-2</em>)</h4>

<p>The contents of the two places <em>place-1 and place-2</em> 
are swapped. A <em>place</em> can be the contents of an unquoted symbol or any
list or array references expressed with <a href="#nth">nth</a>, 
 <a href="#first">first</a>, <a href="#lst">last</a> or implicit
 <a href="#indexing">indexing</a> or places referenced by <a href="#assoc">assoc</a>
or <a href="#lookup">lookup</a>.</p>

<p><tt>swap</tt> is a destructive operation that changes the contents of the 
lists, arrays, or symbols involved.</p>

<!-- example -->

<pre>
(set 'lst '(a b c d e f))

(swap (first lst) (last lst)) <span class='arw'>&rarr;</span> a
lst                           <span class='arw'>&rarr;</span> (f b c d e a)

(set 'lst-b '(x y z))

(swap (lst 0) (lst-b -1)) <span class='arw'>&rarr;</span> f
lst                       <span class='arw'>&rarr;</span> (z b c d e a)
lst-b                     <span class='arw'>&rarr;</span> (x y f)

(set 'A (array 2 3 (sequence 1 6)) <span class='arw'>&rarr;</span> ((1 2 3) (4 5 6))

(swap (A 0) (A 1)) <span class='arw'>&rarr;</span> (1 2 3)
A                  <span class='arw'>&rarr;</span> ((4 5 6) (1 2 3))

(set 'x 1 'y 2)

(swap x y)  <span class='arw'>&rarr;</span> 1
x  <span class='arw'>&rarr;</span> 2
y  <span class='arw'>&rarr;</span> 1

(set 'lst '((a 1 2 3) (b 10 20 30)))
(swap (lookup 'a lst -1) (lookup 'b lst 1))
lst <span class='arw'>&rarr;</span> ((a 1 2 10) (b 3 20 30))

(swap (assoc 'a lst) (assoc 'b lst))
lst <span class='arw'>&rarr;</span>  ((b 3 20 30) (a 1 2 10))
</pre>


<p>Any two places can be swept in the same or different objects.</p>

<br/><br/>

<a name="sym"></a>
<h2><span class="function">sym</span></h2>
<h4>syntax: (sym <em>string</em> [<em>sym-context</em> [<em>nil-flag</em>]])<br/>
syntax: (sym <em>number</em> [<em>sym-context</em> [<em>nil-flag</em>]])<br/>
syntax: (sym <em>symbol</em> [<em>sym-context</em> [<em>nil-flag</em>]])</h4>

<p>
	Translates the first argument in <em>string</em>, 
	<em>number</em>, or <em>symbol</em> 
	into a symbol and returns it.
	If the optional context is not specified 
	in <em>sym-context</em>, 
	the current context is used 
	when doing symbol lookup or creation.
	Symbols will be created 
	if they do not already exist.
	When the context does not exist
	and the context is specified by a quoted symbol, 
	the symbol also gets created.
	If the context specification is unquoted, 
	the context is the specified name 
	or the context specification is a variable 
	containing the context.
</p>

<p>
	<tt>sym</tt> can create symbols within the symbol table
	that are not legal symbols in newLISP source code
	(e.g., numbers or names containing special characters
	such as parentheses, colons, etc.).
	This makes <tt>sym</tt> usable 
	as a function for associative memory access, 
	much like <em>hash table</em> access 
	in other scripting languages. 
</p>

<p>
	As a third optional argument,
	<tt>nil</tt> can be specified 
	to suppress symbol creation 
	if the symbol is not found.
	In this case,
	<tt>sym</tt> returns <tt>nil</tt> 
	if the symbol looked up does not exist.
	Using this last form,
	<tt>sym</tt> can be used 
	to check for the existence 
	of a symbol.
</p>

<!-- example -->

<pre>
(sym "some")           <span class='arw'>&rarr;</span> some
(set (sym "var") 345)  <span class='arw'>&rarr;</span> 345
var                    <span class='arw'>&rarr;</span> 345
(sym "aSym" 'MyCTX)    <span class='arw'>&rarr;</span> MyCTX:aSym
(sym "aSym" MyCTX)     <span class='arw'>&rarr;</span> MyCTX:aSym  ; unquoted context

(sym "foo" MyCTX nil)  <span class='arw'>&rarr;</span> nil  ; 'foo does not exist
(sym "foo" MyCTX)      <span class='arw'>&rarr;</span> foo  ; 'foo is created
(sym "foo" MyCTX nil)  <span class='arw'>&rarr;</span> foo  ; foo now exists
</pre>


<p>
	Because the function <tt>sym</tt> 
	returns the symbol looked up or created,
	expressions with <tt>sym</tt> can be embedded 
	directly in other expressions 
	that use symbols as arguments.
	The following example shows 
	the use of <tt>sym</tt> 
	as a hash-like function 
	for associative memory access, 
	as well as symbol configurations 
	that are not legal newLISP symbols:
</p>

<!-- example -->

<pre>
;; using sym for simulating hash tables

(set (sym "John Doe" 'MyDB) 1.234)
(set (sym "(" 'MyDB) "parenthesis open")
(set (sym 12 'MyDB) "twelve")

(eval (sym "John Doe" 'MyDB))  <span class='arw'>&rarr;</span> 1.234
(eval (sym "(" 'MyDB))         <span class='arw'>&rarr;</span> "parenthesis open"
(eval (sym 12 'MyDB))          <span class='arw'>&rarr;</span> "twelve"

;; delete a symbol from a symbol table or hash
(delete (sym "John Doe" 'MyDB))  <span class='arw'>&rarr;</span> true
</pre>


<p>
	The last statement shows 
	how a symbol can be eliminated 
	using <a href="#delete">delete</a>.
</p>

<p>
	The third syntax allows symbols to be used 
	instead of strings for the symbol name 
	in the target context.
	In this case,
	<tt>sym</tt> will extract the name from the symbol 
	and use it as the name string 
	for the symbol in the target context:
</p>

<!-- example -->

<pre>
(sym 'myVar 'FOO)  <span class='arw'>&rarr;</span> FOO:myVar

(define-macro (def-context)
  (dolist (s (rest (args)))
    (sym s (first (args)))))

(def-context foo x y z)

(symbols foo)  <span class='arw'>&rarr;</span> (foo:x foo:y foo:z)
</pre>


<p>The <tt>def-context</tt> macro shows how this could be used 
to create a macro that creates contexts and their variables 
in a dynamic fashion.</p>

<p> A syntax of the <a href="#context">context</a> function can also be used to 
create, set and evaluate symbols.
</p>

<br/><br/>

<a name="symbolp"></a>
<h2><span class="function">symbol?</span></h2>
<h4>syntax: (symbol? <em>exp</em>)</h4>

<p>
	Evaluates the <em>exp</em> expression 
	and returns <tt>true</tt> if the value is a symbol;
	otherwise, it returns <tt>nil</tt>.
</p>

<!-- example -->

<pre>
(set 'x 'y)  <span class='arw'>&rarr;</span> y

(symbol? x)  <span class='arw'>&rarr;</span> true 

(symbol? 123)  <span class='arw'>&rarr;</span> nil

(symbol? (first '(var x y z)))  <span class='arw'>&rarr;</span> true
</pre>


<p>
	The first statement sets the contents of <tt>x</tt> 
	to the symbol <tt>y</tt>.
	The second statement then checks the contents of <tt>x</tt>.
	The last example checks the first element of a list.
</p>

<br/><br/>

<a name="symbols"></a>
<h2><span class="function">symbols</span></h2>
<h4>syntax: (symbols [<em>context</em>])</h4>

<p>
	Returns a sorted list of all symbols 
	in the current context 
	when called without an argument.
	If a context symbol is specified, 
	symbols defined in that context are returned.
</p>

<!-- example -->

<pre>
(symbols)       ; list of all symbols in current context
(symbols 'CTX)  ; list of symbols in context CTX
(symbols CTX)   ; omitting the quote
(set 'ct CTX)   ; assigning context to a variable
(symbols ct)    ; list of symbols in context CTX
</pre>


<p>
	The quote can be omitted 
	because contexts evaluate to themselves.
</p>

<br/><br/>

<a name="sync"></a>
<h2><span class="function">sync</span></h2>
<h4>syntax: (sync <em>int-timeout</em> [<em>func-inlet</em>])<br/>
syntax: (sync)</h4>

<p>When <em>int-timeout</em> in milliseconds is specified, <tt>sync</tt> waits 
for child processes launched with <a href="#spawn">spawn</a> to finish. 
Whenever a child process finishes, <tt>sync</tt> assigns the evaluation result 
of the spawned subtask to the symbol specified in the spawn statement.
The <tt>sync</tt> returns <tt>true</tt> if all child processes have been processed 
or <tt>nil</tt> if the timeout value has been reached and more child processes
are pending.</p>

<p>If <tt>sync</tt> additionally is given with an optional user-defined <em>inlet</em> 
function in <em>func-inlet</em>, this function  will be called with the child process-id
as argument whenever a spawned child process returns. <em>func-inlet</em> can contain 
either a lambda expression or a symbol which defines a function.</p>

<p>Without any parameter, <tt>sync</tt> returns a list of pending child process
PIDs (process identifiers), for which results have not been processed yet.</p>

<p>The function <tt>sync</tt> is not available on MS Windows.</p>

<!-- example -->

<pre>
; wait for 10 seconds and process finished child processes
(sync 10000) 

; wait for the maximum time (~ 1193 hours)
(sync -1) 

(define (report pid)
    (println "process: " pid " has returned"))

; call the report function, when a child returns
(sync 10000 report) ; wait for 10 seconds max

; return a list of pending child processes
(sync)         <span class='arw'>&rarr;</span> (245 246 247 248)

; wait and do something else
(until (true? (sync 10 report) )
    (println (time-of-day)))

</pre>


<p>When <tt>sync</tt> is given with a timeout parameter, it will block
until timeout or until all child processes have returned, whichever
comes earlier. When no parameter is specified or a function is specified,
<tt>sync</tt> returns immediately.</p>


<p>The function <tt>sync</tt> is part of the Cilk API for synchronizing
child processes and process parallelization. See the reference for the
function <a href="#spawn">spawn</a> for a full discussion of the Cilk API.</p>

<br/><br/>


<a name="sys-error"></a>
<h2><span class="function">sys-error</span></h2>
<h4>syntax: (sys-error)<br/>
syntax: (sys-error <em>int-error</em>)<br/>
syntax: (sys-error <tt>0</tt>)</h4>

<p>Reports the last error generated by the underlying OS 
which newLISP is running on.  The error reported 
may differ on the platforms newLISP has been compiled for.
Consult the platform's C library information. The error is
reported as a list of error number and error text.</p>

<p>If no error has occurred or the system error number has
been reset, <tt>nil</tt> is returned.</p>

<p>When <em>int-error</em> is greater <tt>0</tt> (zero) a
list of the number and the error text is returned.</p>

<p>To reset the error specify <tt>0</tt> as the error number.</p>

<p>Whenever a function in newLISP within the system resources area
returns <tt>nil</tt>, <tt>sys-error</tt> can be checked 
for the underlying reason. For file operations, 
<tt>sys-error</tt> may be set for nonexistent files 
or wrong permissions when accessing the resource.
Another cause of error could be the exhaustion of certain system 
resources like file handles or semaphores.</p>

<!-- example -->

<pre>
;; trying to open a nonexistent file
(open "xyz" "r")  <span class='arw'>&rarr;</span> nil

(sys-error)       <span class='arw'>&rarr;</span> (2 "No such file or directory")

;; reset errno
(sys-error 0)     <span class='arw'>&rarr;</span> (0 "Unknown error: 0")
(sys-error)       <span class='arw'>&rarr;</span> nil
</pre>


<p>See also <a href="#last-error">last-error</a> and <a href="#net-error">net-error</a>.</p>

<br/><br/>

<a name="sys-info"></a>
<h2><span class="function">sys-info</span></h2>
<h4>syntax: (sys-info [<em>int-idx</em>])</h4>

<p>Calling <tt>sys-info</tt> without <em>int-idx</em> returns a list of internal 
resource statistics. Ten integers report the following status:</p>

<table width="98%" summary="sys-info offsets">
<tr align="left"><th>offset</th><th>description</th></tr>
<tr><td>0</td><td>Number of Lisp cells</td></tr>
<tr><td>1</td><td>Maximum number of Lisp cells constant</td></tr>
<tr><td>2</td><td>Number of symbols</td></tr>
<tr><td>3</td><td>Evaluation/recursion level</td></tr>
<tr><td>4</td><td>Environment stack level</td></tr>
<tr><td>5</td><td>Maximum call stack constant</td></tr>
<tr><td>6</td><td>Pid of the parent process or 0</td></tr>
<tr><td>7</td><td>Pid of running newLISP process</td></tr>
<tr><td>8</td><td>Version number as an integer constant</td></tr>
<tr><td>9</td><td>Operating system constant:<br/>
linux=1, bsd=2, osx=3, solaris=4,  windows=6, os/2=7, cygwin=8, tru64 unix=9, aix=10, android=11
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;bit 11 will be set for ffilib (extended import/callback API) versions (add 1024)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;bit 10 will be set for IPv6 versions (add 512)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;bit 9 will be set for 64-bit (changeable at runtime) versions (add 256)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;bit 8 will be set for UTF-8 versions (add 128)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;bit 7 will be added for library versions (add 64)</td></tr>
</table><br/>

<p>The numbers from <tt>0</tt> to <tt>9</tt> indicate the optional offset 
in the returned list.</p>

<p>It is recommended to use offsets 0 to 5 to address
up and including "Maximum call stack constant" and to use
negative offsets -1 to -4 to access the last four
entries in the system info list. Future new entries will be inserted
after offset 5. This way older source code does not need to change.</p>

<p> When using <em>int-idx</em>, one element of the list will be returned.
</p>

<!-- example -->

<pre>
(sys-info)     <span class='arw'>&rarr;</span> (429 268435456 402 1 0 2048 0 19453 10406 1155)
(sys-info 3)   <span class='arw'>&rarr;</span> 1 
(sys-info -2)  <span class='arw'>&rarr;</span> 10406 ;; version 10.4.6
</pre>


<p>The number for the maximum of Lisp cells can be changed via the <tt>-m</tt> 
command-line switch. For each megabyte of Lisp cell memory, 
64k memory cells can be allocated. The maximum call stack depth 
can be changed using the <tt>-s</tt> command-line switch. </p>

<br/><br/>

<a name="t-test"></a>
<h2><span class="function">t-test</span></h2>
<h4>syntax: (t-test <em>list-vector</em> <em>number-value</em>)<br/>
syntax: (t-test <em>list-vector-A</em> <em>list-vector-B</em> [<tt>true</tt>])<br/>
syntax: (t-test <em>list-vector-A</em> <em>list-vector-B</em> <em>float-probability</em>)</h4>

<p>In the <b>first syntax</b> the function uses a one sample <em>Student's t</em> 
test to compare the mean value of <em>list-vector</em> to the value in 
<em>number-value</em>:</p>

<!-- example -->
<pre>
; one sample t-test
(t-test '(3 5 4 2 5 7 4 3) 2.5)
<span class='arw'>&rarr;</span> '(4.125 2.5 1.552 0.549 2.960 7 0.021)
</pre>

<p>The following data are returned in a list:</p>

<table>
<tr align="left"><th>name</th><th>description</th></tr>
<tr><td>mean</td><td>mean of data in vector</td></tr>
<tr><td>value</td><td>value to compare</td></tr>
<tr><td>sdev</td><td>standard deviation in data vector</td></tr>
<tr><td>mean-error</td><td>standard error of mean</td></tr>
<tr><td>t</td><td>t between mean and value</td></tr>
<tr><td>df</td><td>degrees of freedom</td></tr>
<tr><td>p</td><td>two tailed probability of t under the null hypothesis</td></tr>
</table>

<p>In above example the difference of the mean value <tt>4.125</tt> from <tt>2.5</tt> is
moderately significant. With a probability <tt>p = 0.021 (2.1%)</tt> the null hypothesis
that the mean is not significantly different, can be rejected.</p>

<p>In the <b>second syntax</b>, the function performs a t-test using the 
<em>Student's t</em> statistic for comparing the means values in <em>list-vector-A</em> 
and <em>list-vector-B</em>. If the <tt>true</tt> flag is not used, both vectors 
in A and B can be of different length and groups represented by A and B are 
not related.</p>

<p>When the optional flag is set to <tt>true</tt>, measurements were taken
from the same group twice, e.g. before and after a procedure.</p>

<p>The following results are returned in a list:</p>

<table>
<tr align="left"><th>name</th><th>description</th></tr>
<tr><td>mean-a</td><td>mean of group A</td></tr>
<tr><td>mean-b</td><td>mean of group B</td></tr>
<tr><td>sdev-a</td><td>standard deviation in group A</td></tr>
<tr><td>sdev-b</td><td>standard deviation in group B</td></tr>
<tr><td>t</td><td>t between mean values</td></tr>
<tr><td>df</td><td>degrees of freedom</td></tr>
<tr><td>p</td><td>two tailed probability of t under the null hypothesis</td></tr>
</table>

<p>The first example studies the effect of different sleep length
before a test on the SCAT (Sam's Cognitive Ability Test):</p>

<!-- example -->
<pre>
; SCAT (Sam's Cognitive Ability Test) 
; two independent sample t-test
(set 'hours-sleep-8 '(5 7 5 3 5 3 3 9))
(set 'hours-sleep-4 '(8 1 4 6 6 4 1 2))

(t-test hours-sleep-8 hours-sleep-4)
 <span class='arw'>&rarr;</span> (5 4 2.138 2.563 0.847 14 0.411)
</pre>

<p>The duration of sleeps before the SCAT does not have a significant
effect with a probability value of <tt>0.411</tt>.</p>

<p>In the second example, the same group of people get tested twice,
before and after a treatment with Prozac depression medication:</p>

<!-- example -->
<pre>
; Effect of an antidepressant on a group of depressed people
; two related samples t-test
(set 'mood-pre '(3 0 6 7 4 3 2 1 4))
(set 'mood-post '(5 1 5 7 10 9 7 11 8))

(t-test mood-pre mood-post true)
<span class='arw'>&rarr;</span> (3.333 7 2.236 3.041 -3.143 8 0.0137)
</pre>

<p>The effect of the antidepressant treatment is moderately significant with a 
<tt>p</tt> of <tt>0.0137</tt>.</p>

<p>In the <b>third syntax</b>, a form of the <em>Student's t</em> called <em>Welch's t-test</em>
is performed. This method is used when the variances observed in both
samples are significantly different. The threshold can be set using the 
<em>float-probability</em> parameter. When this parameter is used the <tt>t-test</tt>
function will perform a F-test to compare the variances in the two data samples.
If the probability of the found <em>F-ratio</em> is below the <em>float-probability</em> 
parameter, the <em>Welch's t-test</em> method will be used. Specifying this value
as <tt>1.0</tt> effectively forces a <em>Welch's t-test</em>:</p>

<!-- example -->
<pre>
; two independent sample t-test using the Welch method
(t-test '(10 4 7 1 1 6 1 8 2 4) '(4 6 9 4 6 8 9 3) 1.0)
<span class='arw'>&rarr;</span> (4.4 6.125 3.239 2.357 -1.307 15 0.211) 

; two independent sample t-test using the normal method
(t-test '(10 4 7 1 1 6 1 8 2 4) '(4 6 9 4 6 8 9 3))
<span class='arw'>&rarr;</span> (4.4 6.125 3.239 2.357 -1.260 16 0.226)
</pre>

<p>There is no significant difference between the means of the two samples.
The <em>Welch</em> method of the t-test is slightly more sensitive in this
case than using the normal t-test method.</p>

<p>Smaller values than <tt>1.0</tt> would trigger the <em>Welch's t-test</em>
method only when the significance of variance difference in the samples reaches
certain value.</p>

<br/><br/>

<a name="tan"></a>
<h2><span class="function">tan</span></h2>
<h4>syntax: (tan <em>num-radians</em>)</h4>

<p>
	Calculates the tangent function from <em>num-radians</em> 
	and returns the result.
</p>

<!-- example -->

<pre>
(tan 1)                     <span class='arw'>&rarr;</span> 1.557407725
(set 'pi (mul 2 (asin 1)))  <span class='arw'>&rarr;</span> 3.141592654
(tan (div pi 4))            <span class='arw'>&rarr;</span> 1
</pre>

<br/><br/>

<a name="tanh"></a>
<h2><span class="function">tanh</span></h2>
<h4>syntax: (tanh <em>num-radians</em>)</h4>

<p>Calculates the hyperbolic tangent of <em>num-radians</em>. 
The hyperbolic tangent is defined mathematically as: <em>sinh (x) / cosh (x)</em>.
</p>

<!-- example -->

<pre>
(tanh 1)     <span class='arw'>&rarr;</span> 0.761594156
(tanh 10)    <span class='arw'>&rarr;</span> 0.9999999959
(tanh 1000)  <span class='arw'>&rarr;</span> 1
(= (tanh 1) (div (sinh 1) (cosh 1)))  <span class='arw'>&rarr;</span> true
</pre>

<br/><br/>

<a name="term"></a>
<h2><span class="function">term</span></h2>
<h4>syntax: (term <em>symbol</em>)</h4>

<p>Returns as a string, the term part of a <em>symbol</em> without the context prefix.</p>

<!-- example -->

<pre>
(set 'ACTX:var 123)
(set 'sm 'ACTX:var)
(string sm)     <span class='arw'>&rarr;</span> "ACTX:var"
(term sm)      <span class='arw'>&rarr;</span> "var"

(set 's 'foo:bar)
(= s (sym (term s) (prefix s)))
</pre>

<p>See also <a href="#prefix">prefix</a> to extract the namespace or
context prefix from a symbol.</p>

<br/><br/>

<a name="throw"></a>
<h2><span class="function">throw</span></h2>
<h4>syntax: (throw <em>exp</em>)</h4>

<p>
	Works together with 
	the <a href="#catch">catch</a> function.
	<tt>throw</tt> forces the return of a previous <tt>catch</tt> statement 
	and puts the <em>exp</em> into the result symbol of <tt>catch</tt>.
</p>


<!-- example -->

<pre>
(define (throw-test)
    (dotimes (x 1000) 
        (if (= x 500) (throw "interrupted"))))

(catch (throw-test) 'result)  <span class='arw'>&rarr;</span> true

result  <span class='arw'>&rarr;</span> "interrupted"

(catch (throw-test))  <span class='arw'>&rarr;</span> "interrupted"
</pre>


<p>
	The last example shows a shorter form of <a href="#catch">catch</a>,
	which returns the <tt>throw</tt> result directly.
</p>

<p>
	<tt>throw</tt> is useful for breaking out of a loop 
	or for early return from user-defined functions 
	or expression blocks.
	In the following example,
	the <tt>begin</tt> block will return <tt>X</tt> 
	if <tt>(foo X)</tt> is <tt>true</tt>; 
	else <tt>Y</tt> will be returned:
</p>


<pre>
(catch (begin
    &hellip;
    (if (foo X) (throw X) Y)
    &hellip;
))
</pre>


<p>
	<tt>throw</tt> will <em>not</em> cause an error exception.
	Use <a href="#throw-error">throw-error</a>
	to throw user error exceptions.
</p> 

<br/><br/>

<a name="throw-error"></a>
<h2><span class="function">throw-error</span></h2>
<h4>syntax: (throw-error <em>exp</em>)</h4>

<p>
	Causes a user-defined error exception 
	with text provided by evaluating <em>exp</em>.
</p>

<!-- example -->

<pre>
(define (foo x y)
    (if (= x 0) (throw-error "first argument cannot be 0"))
    (+ x y))

(foo 1 2)  <span class='arw'>&rarr;</span> 3

(foo 0 2)  ; causes a user error exception
<span class='err'>ERR: user error : first argument cannot be 0
called from user-defined function foo</span>
</pre>


<p>
	The user error can be handled 
	like any other error exception 
	using user-defined error handlers 
	and the <a href="#error-event">error-event</a> function, 
	or the form of <a href="#catch">catch</a> 
	that can capture error exceptions.
</p>

<br/><br/>

<a name="time"></a>
<h2><span class="function">time</span></h2>
<h4>syntax: (time <em>exp</em> [<em>int-count</em>)</h4>

<p>Evaluates the expression in <em>exp</em> and returns the time spent 
on evaluation in floating point milliseconds. Depending on the platform
decimals of milliseconds are shown or not shown.</p>

<!-- example -->

<pre>
(time (myprog x y z))  <span class='arw'>&rarr;</span> 450.340

(time (myprog x y z) 10)  <span class='arw'>&rarr;</span> 4420.021
</pre>


<p>In first the example, 450 milliseconds elapsed 
while evaluating <tt>(myprog x y z)</tt>.  The second example 
returns the time for ten evaluations of  <tt>(myprog x y z)</tt>.
See also <a href="#date">date</a>,
<a href="#date-value">date-value</a>,
<a href="#time-of-day">time-of-day</a>, 
and <a href="#now">now</a>.</p>

<br/><br/>

<a name="time-of-day"></a>
<h2><span class="function">time-of-day</span></h2>

<h4>syntax: (time-of-day)</h4>

<p>Returns the time in milliseconds since the start of the current day. 
</p>

<p>See also the <a href="#date">date</a>,
<a href="#date-value">date-value</a>,
<a href="#time">time</a>, 
and <a href="#now">now</a> functions.</p>

<br/><br/>

<a name="timer"></a>
<h2><span class="function">timer</span></h2>
<h4>syntax: (timer <em>sym-event-handler | func-event-handler</em> <em>num-seconds</em> [<em>int-option</em>])<br/>
syntax: (timer <em>sym-event-handler | func-event-handler</em>)<br/>
syntax: (timer)</h4>

<p>Starts a one-shot timer firing off the Unix signal <tt>SIGALRM</tt>, <tt>SIGVTALRM</tt>, 
or <tt>SIGPROF</tt> after the time in seconds (specified in <em>num-seconds</em>) 
has elapsed. When the timer fires, it calls the user-defined function 
in <em>sym-</em> or <em>func-event-handler</em>.
</p>

<p>On Linux/Unix, an optional <tt>0</tt>, <tt>1</tt>, or <tt>2</tt> can 
be specified to control how the timer counts.  With default option 
<tt>0</tt>, real time is measured.  Option <tt>1</tt> measures the time 
the CPU spends processing in the process owning the timer.
Option <tt>2</tt> is a combination of both called <em>profiling time</em>.
See the Unix man page <tt>setitimer()</tt> for details.
</p>

<p>The event handler can start the timer again to achieve a 
continuous flow of events.  Starting with version 8.5.9,
seconds can be defined as floating point numbers with a fractional 
part (e.g., <tt>0.25</tt> for 250 milliseconds).</p>

<p>Defining <tt>0</tt> (zero) as time shuts the running timer down 
and prevents it from firing.</p>

<p>
When called with <em>sym-</em> or <em>func-event-handler</em>, 
<tt>timer</tt> returns the elapsed time of the timer in progress.
This can be used to program time lines or schedules.</p>

<p>
<tt>timer</tt> called without arguments returns the symbol of the current 
event handler.</p> 


<!-- example -->

<pre>
(define (ticker) 
    (println (date)) (timer 'ticker 1.0))

&gt; (ticker)
<b>Tue Apr 12 20:44:48 2005</b>	; first execution of ticker
<span class='arw'>&rarr;</span> ticker			      ; return value from ticker

&gt; <b>Tue Apr 12 20:44:49 2005</b>	; first timer event
<b>Tue Apr 12 20:44:50 2005</b>	; second timer event ...
<b>Tue Apr 12 20:44:51 2005
Tue Apr 12 20:44:52 2005</b>
</pre>


<p>
	The example shows an event handler, <tt>ticker</tt>,
	which starts the timer again after each event.
</p>

<p>
	Note that a timer cannot interrupt an 
	ongoing built-in function.
	The timer interrupt gets registered by newLISP,
	but a timer handler cannot run 
	until one expression is evaluated 
	and the next one starts.
	To interrupt an ongoing I/O operation with <tt>timer</tt>,
	use the following pattern, 
	which calls <a href="#net-select">net-select</a> 
	to test if a socket is ready for reading:
</p>

<!-- example -->

<pre>
define (interrupt)
    (set 'timeout true))
        
(set 'listen (net-listen 30001))
(set 'socket (net-accept listen))
        
(timer 'interrupt 10)
;; or specifying the function directly
(timer (fn () (set 'timeout true)) 10)
        
(until (or timeout done)
    (if (net-select socket "read" 100000)
        (begin
            (net-receive socket buffer 1024)
            (set 'done true)))
)
                                                                                
(if timeout
    (println "timeout")
    (println buffer))
                                                                              
(exit)
</pre>


<p>
	In this example,
	the <tt>until</tt> loop will run 
	until something can be read from <tt>socket</tt>, 
	or until ten seconds have passed 
	and the <tt>timeout</tt> variable is set.
</p>

<br/><br/>

<a name="title-case"></a>
<h2><span class="function">title-case</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (title-case <em>str</em> <em>[bool]</em>)</h4>

<p>
	Returns a copy of the string in <em>str</em> 
	with the first character converted to uppercase.
	When the optional <em>bool</em> parameter 
	evaluates to any value other than <tt>nil</tt>,
	the rest of the string is converted to lowercase.
</p>

<!-- example -->

<pre>
(title-case "hello")       <span class='arw'>&rarr;</span> "Hello"
(title-case "hELLO" true)  <span class='arw'>&rarr;</span> "Hello"
(title-case "hELLO")       <span class='arw'>&rarr;</span> "HELLO"
</pre>


<p>
	See also the <a href="#lower-case">lower-case</a> 
	and <a href="#upper-case">upper-case</a> functions.
</p>

<br/><br/>

<a name="trace"></a>
<h2><span class="function">trace</span></h2>

<h4>syntax: (trace <em>int-device</em>)<br/>
syntax: (trace <em>true</em>)<br/>
syntax: (trace <em>nil</em>)<br/>
syntax: (trace)</h4>

<p>In the first syntax the parameter is an integer of a device like an opened file.
Output is continuously written to that device. If <em>int-device</em> is 
<tt>1</tt> output is written to <i>stdout</i>. </p> 

<pre>
; write all entries and exits from expressions to trace.txt
(trace (open "trace.txt")) 

; write all entries and exits from expressions to trace.txt
(foo x y)                  
(bar x)              
      
; close the trace.txt file
(trace nil)
</pre>

<p>In the second syntax debugger mode is switched on when the 
parameter evaluates true. When in debugging mode newLISP will stop
after each entry and exit from an expression and wait for user input.
Highlighting is done by bracketing the expression between two # 
(number sign) characters.  This can be changed to a different character 
using <a href="#trace-highlight">trace-highlight</a>.:</p>

<pre>
[-&gt; 2] s|tep n|ext c|ont q|uit &gt;
</pre>

<p>
At the prompt, an <tt>s</tt>, <tt>n</tt>, <tt>c</tt>, 
or <tt>q</tt> can be entered to step into or 
merely execute the next expression.  Any expression can be entered 
at the prompt for evaluation.  Entering the name of a variable,
for example, would evaluate to its contents.
In this way, a variable's contents can be checked during debugging
or set to different values.
</p>

<!-- example -->

<pre>
;; switches newLISP into debugging mode
(trace true)  <span class='arw'>&rarr;</span> true 

;; the debugger will show each step
(my-func a b c)

;; switched newLISP out of debugging mode
(trace nil)  <span class='arw'>&rarr;</span> nil 
</pre>

<p>To set break points where newLISP should interrupt 
normal execution and go into debugging mode,
put <tt>(trace true)</tt> statements into the newLISP 
code where execution should switch on the debugger.</p>

<p>Use the <a href="#debug">debug</a> function as a shortcut 
for the above example:</p>

<pre>
(debug (my-func a b c))
</pre>


<p>In the third syntax <tt>(trace nil)</tt> closes debugger mode or 
the trace file opened.</p>

<p>In the last syntax <tt>(trace)</tt> returns the current mode.</p>

<br/><br/>

<a name="trace-highlight"></a>
<h2><span class="function">trace-highlight</span></h2>
<h4>syntax: (trace-highlight <em>str-pre</em> <em>str-post</em> [<em>str-header</em> <em>str-footer</em>])</h4>

<p>Sets the characters or string of characters used to enclose expressions 
during <a href="#trace">trace</a>.  By default, 
the # (number sign) is used to enclose the expression highlighted 
in <a href="#trace">trace</a> mode.  This can be changed to different characters 
or strings of up to seven characters.  If the console window accepts terminal 
control characters, this can be used to display the expression in a different 
color, bold, reverse, and so forth.</p>

<p>Two more strings can optionally be specified for <em>str-header and str-footer</em>,
which control the separator and prompt. A maximum of 15 characters is allowed 
for the header and 31 for the footer.</p>

<!-- example -->

<pre>
;; active expressions are enclosed in &gt;&gt; and &lt;&lt;

(trace-highlight "&gt;&gt;" "&lt;&lt;") 
             
;; 'bright' color on a VT100 or similar terminal window

(trace-highlight "\027[1m" "\027[0m")   
</pre>


<p>
The first example replaces the default <tt>#</tt> (number sign) 
with a <tt>&gt;&gt;</tt> and <tt>&lt;&lt;</tt>. The second example works 
on most Linux shells.  It may not, however, work in console windows 
under MS Windows or CYGWIN, depending on the configuration of the terminal.</p>

<br/><br/>

<a name="transpose"></a>
<h2><span class="function">transpose</span></h2>
<h4>syntax: (transpose <em>matrix</em>)</h4>

<p>Transposes a <em>matrix</em> by reversing the rows and columns.
Any kind of list-matrix can be transposed. Matrices are made rectangular 
by filling in <tt>nil</tt> for missing elements, omitting elements where 
appropriate, or expanding atoms in rows into lists.
Matrix dimensions are calculated using the number of rows in the original 
matrix for columns and the number of elements in the first row 
as number of rows for the transposed matrix.</p>

<p>The matrix to transpose can contain any data-type.</p>

<p>The dimensions of a matrix are defined by the number of rows 
and the number of elements in the first row.  A matrix can either be a 
nested list or an <a href="#array">array</a>.</p>

<!-- example -->

<pre>
(set 'A '((1 2 3) (4 5 6)))
(transpose A)                      <span class='arw'>&rarr;</span> ((1 4) (2 5) (3 6))
(transpose (list (sequence 1 5)))  <span class='arw'>&rarr;</span> ((1) (2) (3) (4) (5))

; any data type is allowed in the matrix
(transpose '((a b) (c d) (e f)))   <span class='arw'>&rarr;</span> ((a c e) (b d f))

; arrays can be transposed too
(set 'A (array 2 3 (sequence 1 6)))
(set 'M (transpose A)) 
M <span class='arw'>&rarr;</span> ((1 4) (2 5) (3 6))
</pre>


<p>The number of columns in a matrix is defined by the number of elements 
in the first row of the matrix.  If other rows have fewer elements, 
<tt>transpose</tt> will assume <tt>nil</tt> for those missing elements.
Superfluous elements in a row will be ignored.</p>


<pre>
(set 'A '((1 2 3) (4 5) (7 8 9)))

(transpose A)  <span class='arw'>&rarr;</span> ((1 4 7) (2 5 8) (3 nil 9))
</pre>


<p>If a row is any other data type besides a list,
the transposition treats it like an entire row of elements 
of that data type:</p>


<pre>
(set 'A '((1 2 3) X (7 8 9)))

(transpose A)  <span class='arw'>&rarr;</span> ((1 X 7) (2 X 8) (3 X 9))
</pre>


<p>All operations shown here on lists can also be performed on arrays.
</p>

<p>
	See also the matrix operations 
	<a href="#det">det</a>, <a href="#invert">invert</a>,
    <a href="#mat">mat</a> and <a href="#multiply">multiply</a>.
</p>

<br/><br/>

<a name="trim"></a>
<h2><span class="function">trim</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (trim <em>str</em>)<br/>
syntax: (trim <em>str</em> <em>str-char</em>)<br/>
syntax: (trim <em>str</em> <em>str-left-char</em> <em>str-right-char</em>)</h4>

<p>Using the first syntax, all white-space characters are trimmed from both
sides of <em>str</em>.</p>

<p>The second syntax trims the string <em>str</em> from both sides, 
stripping the leading and trailing characters as given 
in <em>str-char</em>. If <em>str-char</em> contains no character,
the space character is assumed. <tt>trim</tt> returns the new string.
</p>

<p>The third syntax can either trim different characters from both sides 
or trim only one side if an empty string is specified 
for the other.  </p>

<!-- example -->

<pre>
(trim "   hello \n ")            <span class='arw'>&rarr;</span> "hello"
(trim "   h e l l o   ")         <span class='arw'>&rarr;</span> "h e l l o")
(trim "----hello-----" "-")      <span class='arw'>&rarr;</span> "hello"
(trim "00012340" "0" "")         <span class='arw'>&rarr;</span> "12340"
(trim "1234000" "" "0")          <span class='arw'>&rarr;</span> "1234"
(trim "----hello=====" "-" "=")  <span class='arw'>&rarr;</span> "hello"
</pre>

<p>For more complex cases <a href="#replace">replace</a> can be used. When
possible, the much faster <tt>trim</tt> is preferred.</p>

<br/><br/>

<a name="truep"></a>
<h2><span class="function">true?</span></h2>
<h4>syntax: (true? <em>exp</em>)</h4>

<p>If the expression in <em>exp</em> 
evaluates to anything other than <tt>nil</tt>
or the empty list <tt>()</tt>, 
<tt>true?</tt> returns <tt>true</tt>; 
otherwise, it returns <tt>nil</tt>.</p>

<!-- example -->

<pre>
(map true? '(x 1 "hi" (a b c) nil ()))
<span class='arw'>&rarr;</span> (true true true true nil nil)
(true? nil)  <span class='arw'>&rarr;</span> nil
(true? '())  <span class='arw'>&rarr;</span> nil
</pre>


<p><tt>true?</tt> behaves like <a href="#if">if</a>
and rejects the empty list <tt>()</tt>.</p>

<br/><br/> 


<a name="unicode"></a>
<h2><span class="function">unicode</span></h2>

<h4>syntax: (unicode <em>str-utf8</em>)</h4>

<p>Converts ASCII/UTF-8 character strings in <em>str</em> 
to UCS-4&ndash;encoded Unicode of 4-byte integers per character.
The string is terminated with a 4-byte integer <tt>0</tt>.
This function is only available on UTF-8&ndash;enabled versions 
of newLISP.</p>

<!-- example -->

<pre>
(unicode "new") 
<span class='arw'>&rarr;</span> "n\000\000\000e\000\000\000w\000\000\000\000\000\000\000"

(utf8 (unicode "new"))  <span class='arw'>&rarr;</span> "new"
</pre>


<p>On <em>big endian</em> CPU architectures, the byte order will 
be reversed from high to low. The <tt>unicode</tt> and 
<a href="#utf8">utf8</a> functions are the inverse of each other.
These functions are only necessary if UCS-4 Unicode is in use.
Most systems use UTF-8 encoding only.</p>

<br/><br/>

<a name="unify"></a>
<h2><span class="function">unify</span></h2>
<h4>syntax: (unify <em>exp-1</em> <em>exp-2</em> [<em>list-env</em>])</h4>

<p>Evaluates and matches <em>exp-1</em> and <em>exp-2</em>.
Expressions match if they are equal or if one of the expressions is 
an unbound variable (which would then be bound to the other expression).
If expressions are lists, they are matched by comparing subexpressions.
Unbound variables start with an uppercase character 
to distinguish them from symbols.  <tt>unify</tt> returns <tt>nil</tt> 
when the unification process fails,
or it returns a list of variable associations on success.
When no variables were bound, but the match is still successful,
<tt>unify</tt> returns an empty list.
newLISP uses a modified <em>J. Alan Robinson</em> unification algorithm
with correctly applied <em>occurs check</em>. 
See also <em>Peter Norvig</em>'s paper about a common 
<a href="http://norvig.com/unify-bug.pdf">unification algorithm bug</a>, which 
is not present in this implementation.
</p>

<p>Since version 10.4.0 the underscore symbol <tt>_</tt> (ASCII 95) matches any atom,
list or unbound variable and never binds.</p>

<p>
    Like <a href="#match">match</a>, <tt>unify</tt> is frequently 
    employed as a parameter functor in <a href="#find">find</a>,
    <a href="#ref">ref</a>,  <a href="#ref-all">ref-all</a> and 
    <a href="#replace">replace</a>.
</p>


<!-- example -->

<pre>
(unify 'A 'A)  <span class='arw'>&rarr;</span> ()  ; tautology

(unify 'A 123)  <span class='arw'>&rarr;</span> ((A 123))  ; A bound to 123

(unify '(A B) '(x y))  <span class='arw'>&rarr;</span> ((A x) (B y))  ; A bound to x, B bound to y

(unify '(A B) '(B abc))  <span class='arw'>&rarr;</span> ((A abc) (B abc))  ; B is alias for A

(unify 'abc 'xyz)  <span class='arw'>&rarr;</span> nil  ; fails because symbols are different

(unify '(A A) '(123 456))  <span class='arw'>&rarr;</span> nil  ; fails because A cannot be bound to different values

(unify '(f A) '(f B))  <span class='arw'>&rarr;</span> ((A B))  ; A and B are aliases

(unify '(f A) '(g B))  <span class='arw'>&rarr;</span> nil  ; fails because heads of terms are different

(unify '(f A) '(f A B))  <span class='arw'>&rarr;</span> nil  ; fails because terms are of different arity

(unify '(f (g A)) '(f B))  <span class='arw'>&rarr;</span> ((B (g A)))  ; B bound to (g A)

(unify '(f (g A) A) '(f B xyz))  <span class='arw'>&rarr;</span> ((B (g xyz)) (A xyz))  ; B bound to (g xyz) A to xyz

(unify '(f A) 'A)  <span class='arw'>&rarr;</span> nil  ; fails because of infinite unification (f(f(f &hellip;)))

(unify '(A xyz A) '(abc X X))  <span class='arw'>&rarr;</span>  nil ; indirect alias A to X doesn't match bound terms

(unify '(p X Y a) '(p Y X X))  <span class='arw'>&rarr;</span> '((Y a) (X a)))  ; X alias Y and binding to 'a

(unify '(q (p X Y) (p Y X)) '(q Z Z))  <span class='arw'>&rarr;</span> ((Y X) (Z (p X X)))  ; indirect alias

(unify '(A b _) '(x G z)) <span class='arw'>&rarr;</span> ((A x) (G b)) ; _ matches atom z 

(unify '(A b c _) '(x G _ z)) <span class='arw'>&rarr;</span> ((A x) (G b)) ; _ never binds, matches c and z

(unify '(A b _) '(x G (x y z))) <span class='arw'>&rarr;</span> ((A x) (G b)) ; _ matches list (x y z)

;; some examples taken from <a href="http://en.wikipedia.org/wiki/Unification_(computer_science)">http://en.wikipedia.org/wiki/Unification_(computer_science)</a>
</pre>


<p>
	<tt>unify</tt> can take an optional binding 
	or association list in <em>list-env</em>.
	This is useful when chaining <tt>unify</tt> expressions 
	and the results of previous <tt>unify</tt> bindings 
	must be included:
</p>

<!-- example -->

<pre>
(unify '(f X) '(f 123))  <span class='arw'>&rarr;</span> ((X 123))

(unify '(A B) '(X A) '((X 123)))
<span class='arw'>&rarr;</span> ((X 123) (A 123) (B 123))
</pre>


<p>
	In the previous example,
	<tt>X</tt> was bound to <tt>123</tt> earlier 
	and is included in the second statement 
	to pre-bind <tt>X</tt>.
</p> 


<h3>Use <tt>unify</tt> with <tt>expand</tt></h3>

<p>
	Note that variables are not actually bound 
	as a newLISP assignment. Rather,
	an association list is returned 
	showing the logical binding.
	A special syntax of <a href="#expand">expand</a> 
	can be used to actually replace bound variables 
	with their terms:
</p>


<pre>
(set 'bindings (unify '(f (g A) A) '(f B xyz)))
<span class='arw'>&rarr;</span> ((B (g xyz)) (A xyz))

(expand '(f (g A) A) bindings)  <span class='arw'>&rarr;</span> (f (g xyz) xyz)

; or in one statement
(expand '(f (g A) A) (unify '(f (g A) A) '(f B xyz)))
<span class='arw'>&rarr;</span> (f (g xyz) xyz)
</pre>

 
<h3>Use <tt>unify</tt> with <tt>bind</tt> for de-structuring</h3>

<p>The function <a href="#bind">bind</a> can be used to set unified
variables:</p>

<pre>
(bind (unify '(f (g A) A) '(f B xyz)))

A <span class='arw'>&rarr;</span> xyz 
B <span class='arw'>&rarr;</span> (g xyz)
</pre>

<p>This can be used for de-structuring:</p>

<pre>
(set 'structure '((one "two") 3 (four (x y z))))
(set 'pattern '((A B) C (D E)))
(bind (unify pattern structure))

A <span class='arw'>&rarr;</span> one
B <span class='arw'>&rarr;</span> "two"
C <span class='arw'>&rarr;</span> 3
D <span class='arw'>&rarr;</span> four
E <span class='arw'>&rarr;</span> (x y z)
</pre>

<p><tt>unify</tt> returns an association list and <tt>bind</tt> binds the associations.</p>

<h3>Model propositional logic with <tt>unify</tt></h3>

<p>
	The following example shows how propositional logic 
	can be modeled using <tt>unify</tt> 
	and <a href="#expand">expand</a>:
</p>


<pre>
; if somebody is human, he is mortal -&gt; (X human) :- (X mortal)
; socrates is human -&gt; (socrates human)
; is socrates mortal? -&gt; ?  (socrates mortal)

(expand '(X mortal) 
         (unify '(X human) '(socrates human))) 
<span class='arw'>&rarr;</span> (socrates mortal)
</pre>


<p>
	The following is a more complex example 
	showing a small, working PROLOG (Programming in Logic) 
	implementation.
</p>


<pre>
;; a small PROLOG implementation

(set 'facts '(
    (socrates philosopher)
    (socrates greek)
    (socrates human)
    (einstein german)
    (einstein (studied physics))
    (einstein human)
))

(set 'rules '(
    ((X mortal) &lt;- (X human))
    ((X (knows physics)) &lt;- (X physicist))
    ((X physicist) &lt;- (X (studied physics)))
))


(define (query trm)
    (or  (when (find trm facts) true) (catch (prove-rule trm))))

(define (prove-rule trm)
    (dolist (r rules)
        (when (list? (set 'e (unify trm (first r))))
            (when (query (expand (last r) e))
                (throw true))))
    nil
)

; try it

&gt; (query '(socrates human))
<b>true</b>
&gt; (query '(socrates (knows physics)))
<b>nil</b>
&gt; (query '(einstein (knows physics)))
<b>true</b>
</pre>


<p>
	The program handles a database of <tt>facts</tt> 
	and a database of simple 
	<em>A is a fact if B is a fact</em> <tt>rules</tt>.
	A fact is proven true 
	if it either can be found in the <tt>facts</tt> database 
	or if it can be proven using a rule.
	Rules can be nested:
	for example, to prove that somebody <tt>(knows physics)</tt>, 
	it must be proved true that somebody is a <tt>physicist</tt>.
	But somebody is only a physicist 
	if that person <tt>studied physics</tt>.
	The <tt>&lt;-</tt> symbol 
	separating the left and right terms of the rules 
	is not required 
	and is only added to make the rules database 
	more readable.
</p>

<p>
	This implementation does not handle multiple terms 
	in the right premise part of the rules,
	but it does handle backtracking of the <tt>rules</tt> database 
	to try out different matches.
	It does not handle backtracking 
	in multiple premises of the rule.
	For example,
	if in the following rule <tt>A if B and C and D</tt>, 
	the premises <tt>B</tt> and <tt>C</tt> succeed 
	and <tt>D</tt> fails,
	a backtracking mechanism might need to go back 
	and reunify the <tt>B</tt> or <tt>A</tt> terms 
	with different facts or rules 
	to make <tt>D</tt> succeed.
</p>

<p>
	The above algorithm could be written differently 
	by omitting <a href="#expand">expand</a> 
	from the definition of <tt>prove-rule</tt>
	and by passing the environment, <tt>e</tt>,
	as an argument to the <tt>unify</tt> and <tt>query</tt> functions.
</p>

<p>
	A <em>learning</em> of proven facts 
	can be implemented by appending them 
	to the <tt>facts</tt> database 
	once they are proven.
	This would speed up subsequent queries.
</p>

<p>
	Larger PROLOG implementations 
	also allow the evaluation of terms in rules. 
	This makes it possible to implement functions
	for doing other work 
	while processing rule terms.
	<tt>prove-rule</tt> could accomplish this testing 
	for the symbol <tt>eval</tt> in each rule term.
</p>

<br/><br/>

<a name="union"></a>
<h2><span class="function">union</span></h2>
<h4>syntax: (union <em>list-1</em> <em>list-2</em> [<em>list-3</em> ... ])</h4>

<p><tt>union</tt> returns a unique collection list of distinct elements found in two 
or more lists.</p>

<pre>
(union '(1 3 1 4 4 3) '(2 1 5 6 4))  <span class='arw'>&rarr;</span>  (1 3 4 2 5 6)
</pre>

<p>Like the other set functions <a href="#difference">difference</a>, 
<a href="#intersect">intersect</a> and <a href="#unique">unique</a>,
<tt>union</tt> maintains the order of elements as found in the original
lists.</p>

<br/><br/>

<a name="unique"></a>
<h2><span class="function">unique</span></h2>
<h4>syntax: (unique <em>list</em>)</h4>

<p>
	Returns a unique version of <em>list</em> 
	with all duplicates removed.
</p>

<!-- example -->

<pre>
(unique '(2 3 4 4 6 7 8 7))  <span class='arw'>&rarr;</span> (2 3 4 6 7 8)
</pre>


<p>
	Note that the list does not need to be sorted,
	but a sorted list makes <tt>unique</tt> perform faster.
</p>

<p>
	Other <em>set</em> functions are <a href="#difference">difference</a>,
	<a href="#intersect">intersect</a> and <a href="#union">union</a>.
</p>

<br/><br/>

<a name="unless"></a>
<h2><span class="function">unless</span></h2>
<h4>syntax: (unless <em>exp-condition</em> <em>body</em>)</h4>

<p>The statements in <em>body</em> are only evaluated if <em>exp-condition</em>
evaluates to <tt>nil</tt> or the empty list <tt>()</tt>. The result
of the last expression in <em>body</em> is returned or the return value
of <em>exp-condition</em> if <em>body</em> was not executed.</p>

<p>Because <tt>unless</tt> does not have an <em>else</em> condition as in
<a href="#if">if</a>, <!-- or <a href="#if-not">if-not</a> -->the statements in <em>body</em> need 
not to be grouped with <a href="#begin">begin</a>:</p>

<pre>
(unless (starts-with (read-line) "quit")
	(process (current-line))
	...
	(finish)
)	
</pre>

<p>See also the function <a href="#when">when</a>.</p>

<br/><br/>

<a name="unpack"></a> 

<h2><span class="function">unpack</span>&nbsp;
<a href="#shared-lib"><font size="+2">&#x26A0;</font></a></h2> 

<h4>syntax: (unpack <em>str-format</em> <em>str-addr-packed</em>)<br/>
syntax: (unpack <em>str-format</em> <em>num-addr-packed</em>)<br/><br/>
syntax: (unpack <em>struct</em> <em>num-addr-packed</em>)<br/> 
syntax: (unpack <em>struct</em> <em>str-addr-packed</em>)</h4> 

<p>When the first parameter is a string, <tt>unpack</tt> unpacks a binary structure 
in <em>str-addr-packed</em> or pointed to by <em>num-addr-packed</em> into newLISP 
variables using the format in <em>str-format</em>. <tt>unpack</tt> is the reverse 
operation of <tt>pack</tt>.  Using <em>num-addr-packed</em> facilitates the unpacking 
of structures returned from imported, shared library functions.</p>

<p>If the number specified in <em>num-addr-packed</em> is not a valid memory 
address, a system bus error or segfault can occur and crash newLISP or leave 
it in an unstable state.</p>

<p>When the first parameter is the symbol of a <a href="#struct">struct</a> definition, 
<tt>unpack</tt> uses the format as specified in <em>struct</em>. 
While <tt>unpack</tt> with <em>str-format</em> literally unpacks as specified,
<tt>unpack</tt> with <em>struct</em> will skip structure aligning pad-bytes
depending on data type, order of elements and CPU architecture.
Refer to the description of the <a href="#struct">struct</a> function for more detail.</p>

<p>When unpacking structures containing <tt>NULL</tt> pointers, an error will be
thrown when <tt>unpack</tt> tries to convert the pointer to a string. If <tt>NULL</tt>
pointers are to be expected, <tt>void*</tt> should be used in the structure definition.</p>

<p>The following characters may define a format:</p>
 
<table width="98%" summary="format chracters in pack">

<tr align="left" valign="bottom"><th>format</th><th>description</th></tr>

<tr>
<td><tt>c </tt></td>
<td>a signed 8-bit number</td>
</tr>

<tr>
<td><tt>b </tt></td>
<td>an unsigned 8-bit number</td>
</tr>

<tr>
<td><tt>d </tt></td>
<td>a signed 16-bit short number</td>
</tr>

<tr>
<td><tt>u </tt></td>
<td>an unsigned 16-bit short number</td>
</tr>

<tr>
<td><tt>ld</tt></td>
<td>a signed 32-bit long number</td>
</tr>

<tr>
<td><tt>lu</tt></td>
<td>an unsigned 32-bit long number</td>
</tr>

<tr>
<td><tt>Ld</tt></td>
<td>a signed 64-bit long number</td>
</tr>

<tr>
<td><tt>Lu</tt></td>
<td>an unsigned 64-bit long number</td>
</tr>

<tr>
<td><tt>f </tt></td>
<td>a float in 32-bit representation</td>

</tr>

<tr>
<td><tt>lf</tt></td>
<td>a double float in 64-bit representation</td>
</tr>

<tr>
<td><tt>sn</tt></td>
<td>a string of <em>n</em> null padded ASCII characters</td>
</tr>

<tr>

<td><tt>nn</tt></td>
<td><em>n</em> null characters</td>
</tr>

<tr>
<td><tt>&gt;</tt></td>
<td>switches to big endian byte order</td>
</tr>

<tr>
<td><tt>&lt;</tt></td>
<td>switches to little endian byte order</td>
</tr>

</table><br/>

<br/>
<!-- example -->

<pre>
(pack "c c c" 65 66 67)  <span class='arw'>&rarr;</span> "ABC"
(unpack "c c c" "ABC")   <span class='arw'>&rarr;</span> (65 66 67)

(set 's (pack "c d u" 10 12345 56789))
(unpack "c d u" s)  <span class='arw'>&rarr;</span> (10 12345 56789)

(set 's (pack "s10 f" "result" 1.23))
(unpack "s10 f" s)  <span class='arw'>&rarr;</span> ("result\000\000\000\000" 1.230000019)

(set 's (pack "s3 lf" "result" 1.23))
(unpack "s3 f" s)  <span class='arw'>&rarr;</span> ("res" 1.23)

(set 's (pack "c n7 c" 11 22))
(unpack "c n7 c" s)  <span class='arw'>&rarr;</span> (11 22))
</pre>


<p>
	The <tt>&gt;</tt> and <tt>&lt;</tt> specifiers 
	can be used to switch between 
	<em>little endian</em> and <em>big endian</em> byte order 
	when packing or unpacking:
</p>



<pre>
;; on a little endian system (e.g., Intel CPUs)
(set 'buff (pack "d" 1))  <span class='arw'>&rarr;</span> "\001\000" 

(unpack "d" buff)   <span class='arw'>&rarr;</span> (1)
(unpack "&gt;d" buff)  <span class='arw'>&rarr;</span> (256)
</pre>



<p>
	Switching the byte order 
	will affect all number formats 
	with 16-, 32-, or 64-bit sizes.
</p>


<p>
	The <tt>pack</tt> and <tt>unpack</tt> format 
	need not be the same,
	as in the following example:
</p>


<pre>
(set 's (pack "s3" "ABC"))
(unpack "c c c" s)  <span class='arw'>&rarr;</span> (65 66 67)
</pre>


<p>
	The examples show spaces between the format specifiers.
	Although not required, they can improve readability.
</p>

<p>
If the buffer's size at a memory address 
is smaller than the formatting string specifies, 
some formatting characters may be left unused.</p>

<p>
	See also the <a href="#address">address</a>,
	<a href="#get-int">get-int</a>,
	<a href="#get-long">get-long</a>,
	<a href="#get-char">get-char</a>,
	<a href="#get-string">get-string</a>, 
	and <a href="#pack">pack</a> functions.
</p>

<br/><br/>

<a name="until"></a>
<h2><span class="function">until</span></h2>
<h4>syntax: (until <em>exp-condition</em> [<em>body</em>])</h4>

<p> Evaluates the condition in <em>exp-condition</em>.
If the result is <tt>nil</tt> or the empty list <tt>()</tt>, 
the expressions in <em>body</em> are evaluated.
Evaluation is repeated until the exp-condition results in a value 
other than <tt>nil</tt> or the empty list.
The result of the last expression evaluated in <em>body</em>
is the return value of the <tt>until</tt> expression. If
<em>body</em> is empty, the result of last <em>exp-condition</em>
is returned. <tt>until</tt> works like 
(<a href="#while">while</a> (<a href="#not">not</a> &hellip;)).
</p>

<p><tt>until</tt> also updates the system iterator symbol <tt>$idx</tt>.</p>

<!-- example -->

<pre>
(device (open "somefile.txt" "read"))
(set 'line-count 0)
(until (not (read-line)) (inc line-count))
(close (device))
(print "the file has " line-count " lines\n")
</pre>


<p>
	Use the <a href="#do-until">do-until</a> function 
	to test the condition <em>after</em> evaluation 
	of the body expressions.
</p>

<br/><br/>

<a name="upper-case"></a>
<h2><span class="function">upper-case</span>&nbsp;<font size="-1"><a href="#utf8_capable">utf8</a></font></h2>
<h4>syntax: (upper-case <em>str</em>)</h4>

<p>Returns a copy of the string in <em>str</em> converted to uppercase.
International characters are converted correctly.</p>

<!-- example -->

<pre>
(upper-case "hello world")  <span class='arw'>&rarr;</span> "HELLO WORLD"
</pre>


<p>
	See also the <a href="#lower-case">lower-case</a> 
	and <a href="#title-case">title-case</a> functions.
</p>

<br/><br/>

<a name="utf8"></a>
<h2><span class="function">utf8</span></h2>
<h4>syntax: (utf8 <em>str-unicode</em>)</h4>

<p>Converts a UCS-4, 4-byte, Unicode-encoded string (<em>str</em>)
into UTF-8.  This function is only available on UTF-8&ndash;enabled 
versions of newLISP.</p>

<!-- example -->

<pre>
(unicode "new") 
<span class='arw'>&rarr;</span> "n\000\000\000e\000\000\000w\000\000\000\000\000\000\000"

(utf8 (unicode "new"))  <span class='arw'>&rarr;</span> "new"
</pre>


<p>The <tt>utf8</tt> function can also be used 
to test for the presence of UTF-8&ndash;enabled newLISP:</p>


<pre>
(if utf8 (do-utf8-version-of-code) (do-ascii-version-of-code))
</pre>


<p>
On <em>big endian</em> CPU architectures, the byte order will be reversed 
from highest to lowest. The <tt>utf8</tt> and <a href="#unicode">unicode</a> 
functions are the inverse of each other. These functions are only necessary 
if UCS-4 Unicode is in use. Most systems use UTF-8 Unicode encoding only.
</p>

<br/><br/>

<a name="utf8len"></a>
<h2><span class="function">utf8len</span></h2>
<h4>syntax: (utf8len <em>str</em>)</h4>

<p>Returns the number of characters in a UTF-8&ndash;encoded string. 
UTF-8 characters can be encoded in more than one 8-bit byte. 
<tt>utf8len</tt> returns the number of UTF-8 characters in a string. 
This function is only available on UTF-8&ndash;enabled versions of newLISP.</p>

<!-- example -->

<pre>
(utf8len "我能吞下玻璃而不伤身体。")    <span class='arw'>&rarr;</span> 12
(length "我能吞下玻璃而不伤身体。")      <span class='arw'>&rarr;</span> 36
</pre>


<p>See also the <a href="#unicode">unicode</a> and <a href="#utf8">utf8</a> functions.
Above Chinese text from <a href="http://www.columbia.edu/kermit/utf8.html">UTF-8 Sampler</a>.</p>

<br/><br/>

<a name="uuid"></a>
<h2><span class="function">uuid</span></h2>
<h4>syntax: (uuid [<em>str-node</em>])</h4>

<p>
	Constructs and returns
	a UUID (Universally Unique IDentifier).
	Without a node spec in <em>str-node</em>, 
	a type 4 UUID random generated byte number 
	is returned.
	When the optional <em>str-node</em> parameter is used, 
	a type 1 UUID is returned.
	The string in <em>str-node</em> 
	specifies a valid MAC (Media Access Code) 
	from a network adapter installed on the node
	or a random node ID.
	When a random node ID is specified,
	the least significant bit of the first node byte 
	should be set to 1 
	to avoid clashes with real MAC identifiers.
	UUIDs of type 1 with node ID 
	are generated from a timestamp and other data.
	See <a href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a> 
	for details on UUID generation.
</p>

<!-- example -->

<pre>
;; type 4 UUID for any system

(uuid)  <span class='arw'>&rarr;</span> "493AAD61-266F-48A9-B99A-33941BEE3607"

;; type 1 UUID preferred for distributed systems

;; configure node ID for ether 00:14:51:0a:e0:bc
(set 'id (pack "cccccc" 0x00 0x14 0x51 0x0a 0xe0 0xbc))

(uuid  id)  <span class='arw'>&rarr;</span> "0749161C-2EC2-11DB-BBB2-0014510AE0BC"
</pre>


<p>
	Each invocation of the <tt>uuid</tt> function 
	will yield a new unique UUID.
	The UUIDs are generated without system-wide 
	shared stable store (see RFC 4122).
	If the system generating the UUIDs 
	is distributed over several nodes, 
	then type 1 generation should be used 
	with a different node ID on each node.
	For several processes on the same node, 
	valid UUIDs are guaranteed 
	even if requested at the same time.
	This is because the process ID 
	of the generating newLISP process 
	is part of the seed 
	for the random number generator.
	When type 4 IDs are used on a distributed system, 
	two identical UUID's are still highly unlikely 
	and impossible for type 1 IDs 
	if real MAC addresses are used.
</p>

<br/><br/>

<a name="wait-pid"></a>
<h2><span class="function">wait-pid</span></h2>
<h4>syntax: (wait-pid <em>int-pid</em> [<em>int-options</em> | <tt>nil</tt>])</h4>

<p>
Waits for a child process specified in <em>int-pid</em> to end.  The child process was 
previously started with <a href="#process">process</a> or <a href="#fork">fork</a>.
When the child process specified in <em>int-pid</em> ends, a list of pid and status value is 
returned.  The status value describes the reason for termination of the child process.
The interpretation of the returned status value differs between Linux and other flavors 
of Unix.  Consult the Linux/Unix man pages for the <tt>waitpid</tt> command (without the hyphen 
used in newLISP) for further information.
</p>

<p>
When <tt>-1</tt> is specified for <em>int-pid</em>, 
pid and  status information of any child process started by the parent are returned.
When <tt>0</tt> is specified, <tt>wait-pid</tt> only watches child processes in the 
same process group as the calling process.  Any other negative value for <em>int-pid</em> 
reports child processes in the same process group as specified with a negative sign 
in <em>int-pid</em>.
</p>

<p>
An option can be specified in <em>int-option</em>.  See Linux/Unix documentation 
for details on integer values for <em>int-options</em>. As an alternative, <tt>nil</tt>
can be specified. This option causes <tt>wait-pid</tt> to be non-blocking, returning 
right away with a <tt>0</tt> in the pid of the list returned. This option used together with
 an <em>int-pid</em> parameter of <tt>-1</tt> can be used to continuously loop and act 
on returned child processes.</p>

<p>
This function is only available on macOS, Linux and other Unix-like operating systems.
</p>

<!-- example -->
<pre>
(set 'pid (fork (my-process))) <span class='arw'>&rarr;</span> 8596

(set 'ret (wait-pid pid))  <span class='arw'>&rarr;</span> (8596 0) ; child has exited

(println "process: " pid " has finished with status: " (last ret))
</pre>

<p>
	The process <tt>my-process</tt> is started, 
	then the main program blocks 
	in the <tt>wait-pid</tt> call 
	until <tt>my-process</tt> has finished.
</p>

<br/><br/>

<a name="when"></a>
<h2><span class="function">when</span></h2>
<h4>syntax: (when <em>exp-condition</em> <em>body</em>)</h4>

<p>The statements in <em>body</em> are only evaluated if <em>exp-condition</em>
evaluates to anything not <tt>nil</tt> and not the empty list <tt>()</tt>. The result
of the last expression in <em>body</em> is returned or <tt>nil</tt> or the empty
list <tt>()</tt> if <em>body</em> was not executed.</p>

<p>Because <tt>when</tt> does not have an <em>else</em> condition as in
<a href="#if">if</a>, the statements in <em>body</em> need not to be grouped with
<a href="#begin">begin</a>:</p>

<!-- example -->
<pre>
(when (read-line)
	(set 'result (analyze (current-line)))
	(report result)
	(finish)
)	
</pre>

<p>See also the function <a href="#unless">unless</a>.</p>

<br/><br/>

<a name="while"></a>
<h2><span class="function">while</span></h2>
<h4>syntax: (while <em>exp-condition</em> <em>body</em>)</h4>

<p> Evaluates the condition in <em>exp-condition</em>.
If the result is not <tt>nil</tt> or the empty list <tt>()</tt>, 
the expressions in <em>body</em> are evaluated.
Evaluation is repeated until an <em>exp-condition</em> results 
in <tt>nil</tt> or the empty list <tt>()</tt>.
The result of the body's last evaluated expression
is the return value of the <tt>while</tt> expression.
</p>

<p><tt>while</tt> also updates the system iterator symbol <tt>$idx</tt>.</p>

<!-- example -->

<pre>
(device (open "somefile.txt" "read"))
(set 'line-count 0)
(while (read-line) (inc line-count))
(close (device))
(print "the file has " line-count " lines\n")
</pre>


<p>
Use the <a href="#do-while">do-while</a> function to evaluate the condition 
<em>after</em> evaluating the body of expressions.
</p>

<br/><br/>

<a name="write"></a>
<a name="write-buffer"></a>

<h2><span class="function">write</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (write)<br/>
syntax: (write <em>int-file</em> <em>str-buffer</em> [<em>int-size</em>])<br/>
syntax: (write <em>str</em> <em>str-buffer</em> [<em>int-size</em>])</h4>


<p>In the second syntax <tt>write</tt> writes <em>int-size</em> bytes 
from a buffer in <em>str-buffer</em> to a file specified in <em>int-file</em>, 
previously obtained from a file <tt>open</tt> operation. If <em>int-size</em> 
is not specified, all data in <em>sym-buffer</em> or <em>str-buffer</em> is written.
<tt>write</tt> returns the number of bytes written or <tt>nil</tt> on failure.</p>

<p>If all parameters are omitted, <tt>write</tt> writes the contents from the
last <a href="#read-line">read-line</a> to standard out (STDOUT).</p>

<p><tt>write</tt> is a shorter writing of <tt>write-buffer</tt>. The longer
form still works but is deprecated and should be avoided in new code.</p>

<!-- example -->

<pre>
(set 'handle (open "myfile.ext" "write"))
(write handle data 100)   
(write handle "a quick message\n")
</pre>

<p>The code in the example writes 100 bytes to the file <tt>myfile.ext</tt> 
from the contents in <tt>data</tt>.</p>

<p>In the third syntax, <tt>write</tt> can be used for destructive
string appending:</p>

<pre>
(set 'str "")
(write str "hello world")

str   <span class='arw'>&rarr;</span> "hello world"
</pre>

<p>See also the <a href="#read">read</a> function.</p>

<br/><br/>

<a name="write-char"></a>
<h2><span class="function">write-char</span></h2>
<h4>syntax: (write-char <em>int-file</em> <em>int-byte1</em> [<em>int-byte2</em> ... ])</h4>

<p>
Writes a byte specified in <em>int-byte</em> to a file specified by the file 
handle in <em>int-file</em>. The file handle is obtained from a previous 
<tt>open</tt> operation. Each <tt>write-char</tt> advances the file pointer 
by one 8-bit byte.</p>

<p><tt>write-char</tt> returns the number of bytes written.</p>

<!-- example -->

<pre>
(define (slow-file-copy from-file to-file)
    (set 'in-file (open from-file "read"))
    (set 'out-file (open to-file "write"))
    (while (set 'chr (read-char in-file))
        (write-char out-file chr))
     (close in-file)
    (close out-file)
    "finished")
</pre>


<p>
Use the <a href="#print">print</a> 
and <a href="#device">device</a> functions 
to write larger portions of data at a time.
Note that newLISP already supplies a faster 
built-in function called 
<a href="#copy-file">copy-file</a>.
</p>

<p>
	See also the <a href="#read-char">read-char</a> function.
</p>

<br/><br/>

<a name="write-file"></a>
<h2><span class="function">write-file</span></h2>
<h4>syntax: (write-file <em>str-file-name</em> <em>str-buffer</em>)</h4>

<p>Writes a file in <em>str-file-name</em> with contents in <em>str-buffer</em> 
in one swoop and returns the number of bytes written.</p>

<p>On failure the function returns <tt>nil</tt>. For error information, 
use <a href="#sys-error">sys-error</a> when used on files. When used
on URLs <a href="#net-error">net-error</a> gives more error
information.</p>

<!-- example -->

<pre>
(write-file "myfile.enc"
    (encrypt (read-file "/home/lisp/myFile") "secret"))
</pre>


<p>The file <tt>myfile</tt> is read, <a href="#encrypt">encrypted</a> using the 
password <tt>secret</tt>, and written back into the new file <tt>myfile.enc</tt>
in the current directory.</p>

<p>
<tt>write-file</tt> can take an <tt>http://</tt> or <tt>file://</tt> URL 
in <em>str-file-name</em>. When the prefix <tt>http://</tt> is used, 
<tt>write-file</tt> works exactly like <a href="#put-url">put-url</a> 
and can take the same additional parameters:</p>

<!-- example -->

<pre>
(write-file "http://asite.com/message.txt" "This is a message" )
</pre>


<p>The file <tt>message.txt</tt> is created and written at a remote location, 
<tt>http://asite.com</tt>, with the contents of <em>str-buffer</em>.
In this mode, <tt>write-file</tt> can also be used to transfer files 
to remote newLISP server nodes.</p>

<p>See also the <a href="#append-file">append-file</a> 
and <a href="#read-file">read-file</a> functions.</p>

<br/><br/>

<a name="write-line"></a>
<h2><span class="function">write-line</span>&nbsp;<a href="#destructive">!</a></h2>
<h4>syntax: (write-line [<em>int-file</em> [<em>str</em>]])<br/>
syntax: (write-line <em>str-out</em> [<em>str</em>]])</h4>

<p>The string in <em>str</em> and the line termination character(s) 
are written to the device specified in <em>int-file</em>.
When the string argument is omitted <tt>write-line</tt> writes the 
contents of the last <a href="#read-line">read-line</a> to <em>int-file</em>
If the first argument is omitted too then it writes to  to standard out 
(STDOUT) or to whatever device is set by <a href="#device">device</a>.</p> 

<p>In the second syntax lines are appended to a string in <em>str-out</em>.</p>

<p><tt>write-line</tt> returns the number of bytes written.</p>

<!-- example -->

<pre>
(set 'out-file (open "myfile" "write"))
(write-line out-file "hello there")
(close out-file)

(set 'myFile (open "init.lsp" "read")
(while (read-line myFile) (write-line))

(set 'str "")
(write-line str "hello")
(write-line str "world")

str  <span class='arw'>&rarr;</span>  "hello\nworld\n"
</pre>


<p>
The first example opens/creates a file, writes a line to it, 
and closes the file. The second example shows the usage of <tt>write-line</tt> 
without arguments. The contents of <tt>init.lsp</tt> are written to the console 
screen.</p>

<p>See also the function <a href="#write">write</a> for writing
to a device without the line-terminating character.</p>

<br/><br/>

<a name="xfer-event"></a>
<h2><span class="function">xfer-event</span></h2>
<h4>syntax: (xfer-event <em>sym-event-handler</em> | <em>func-event-handler</em>)
syntax: (xfer-event nil)</h4>

<p>Registers a function in symbol <em>sym-event-handler</em> or in lambda function 
<em>func-event-handler</em>
to monitor HTTP byte transfers initiated by <a href="#get-url">get-url</a>,
<a href="#post-url">post-url</a> or <a href="#put-url">put-url</a> or initiated
by file functions which can take URLs like <a href="#load">load</a>,
<a href="#save">save</a>, <a href="#read-file">read-file</a>,
<a href="#write-file">write-file</a> and <a href="#append-file">append-file</a>.
</p>

<p>E.g. whenever a block of data requested with <a href="#get-url">get-url</a>
arrives, the function in <em>sym</em> or <em>func</em> will be called with
the number of bytes transferred. Likewise when sending data with
<a href="#post-url">post-url</a> or any of the other data sending 
functions, <em>sym</em> or <em>func</em> will be called with the number of
bytes transferred for each block of data transferred.</p>

<p>Specifying <tt>nil</tt> for the event will reset it to the initial default state.</p>

<!-- example -->

<pre>
(xfer-event (fn (n) (println "-&gt;" n)))
(length (get-url "http://newlisp.org"))
<b>
-&gt;73
-&gt;799
-&gt;1452
-&gt;351
-&gt;1093
-&gt;352
-&gt;211
-&gt;885
-&gt;564
-&gt;884
-&gt;561
-&gt;75
-&gt;812
-&gt;638
-&gt;1452
-&gt;801
-&gt;5
-&gt;927
11935
</b>
</pre>


<p>The computer output is shown in bold. Whenever a block of data is received
its byte size is printed. Instead of defining the handler
function directory with a lambda function in <em>func</em>, a symbol
containing a function definition could have been used:</p>


<pre>
(define (report n) (println "-&gt;" n))
(xfer-event 'report)
</pre>


<p> This can be used to monitor the progress of longer
lasting byte transfers in HTTP uploads or downloads.</p>

<br/><br/>

<a name="xml-error"></a>
<h2><span class="function">xml-error</span></h2>
<h4>syntax: (xml-error)</h4>

<p>
	Returns a list of error information 
	from the last <a href="#xml-parse">xml-parse</a> operation; 
	otherwise, returns <tt>nil</tt>
	if no error occurred.
	The first element contains text 
	describing the error, 
	and the second element is a number indicating 
	the last scan position in the source XML text, 
	starting at <tt>0</tt> (zero).
</p>

<!-- example -->

<pre>
(xml-parse "&lt;atag&gt;hello&lt;/atag&gt;&lt;fin")  <span class='arw'>&rarr;</span> nil

(xml-error)  <span class='arw'>&rarr;</span> ("expected closing tag: &gt;" 18)
</pre>

<br/><br/>

<a name="xml-parse"></a>
<h2><span class="function">xml-parse</span></h2>
<h4>syntax: (xml-parse <em>string-xml</em> [<em>int-options</em> [<em>sym-context</em> [<em>func-callback</em>]]])</h4>

<p>
Parses a string containing XML 1.0 compliant, <em>well-formed</em> XML.
<tt>xml-parse</tt> does not perform DTD validation. 
It skips DTDs (Document Type Declarations) and processing instructions.
Nodes of type ELEMENT, TEXT, CDATA, and COMMENT are parsed, and 
a newLISP list structure is returned.  When an element node does not have 
attributes or child nodes, it instead contains an empty list.
Attributes are returned as association lists, 
which can be accessed using <a href="#assoc">assoc</a>.
When <tt>xml-parse</tt> fails due to malformed XML, <tt>nil</tt> is returned 
and <a href="#xml-error">xml-error</a> can be used to access error information.
</p>

<!-- example -->

<pre>
(set 'xml 
  "&lt;person name='John Doe' tel='555-1212'&gt;nice guy&lt;/person&gt;")

(xml-parse xml) 
<span class='arw'>&rarr;</span> (("ELEMENT" "person" 
    (("name" "John Doe") 
     ("tel" "555-1212"))
    (("TEXT" "nice guy"))))
</pre>


<h3>Modifying the translation process.</h3>
<p>
Optionally, the <em>int-options</em> parameter can be specified 
to suppress whitespace, empty attribute lists, and comments.
It can also be used to transform tags from strings into symbols.
Another function, <a href="#xml-type-tags">xml-type-tags</a>, 
serves for translating the XML tags.
The following option numbers can be used:
</p>

<table  summary="option numbers for xml-parse">
<tr align="left" valign="bottom"><th>option</th><th>description</th></tr>
<tr><td>1</td><td>suppress whitespace text tags</td></tr>
<tr><td>2</td><td>suppress empty attribute lists</td></tr>
<tr><td>4</td><td>suppress comment tags</td></tr>
<tr><td>8</td><td>translate string tags into symbols</td></tr>

<tr><td>16</td><td>add SXML (S-expression XML) attribute tags (@ ...)</td></tr>
</table><br/>

<p>
Options can be combined by adding the numbers
(e.g., <tt>3</tt> would combine the options 
for suppressing whitespace text tags/info 
and empty attribute lists).
</p>
 
<p>
The following examples show how the different options can be used:
</p>
<br/>
<b>XML source:</b>


<pre>
&lt;?xml version="1.0" ?&gt;
&lt;DATABASE name="example.xml"&gt;
&lt;!--This is a database of fruits--&gt;
    &lt;FRUIT&gt;
        &lt;NAME&gt;apple&lt;/NAME&gt;
        &lt;COLOR&gt;red&lt;/COLOR&gt;
        &lt;PRICE&gt;0.80&lt;/PRICE&gt;
    &lt;/FRUIT&gt;

    &lt;FRUIT&gt;
        &lt;NAME&gt;orange&lt;/NAME&gt;
        &lt;COLOR&gt;orange&lt;/COLOR&gt;
        &lt;PRICE&gt;1.00&lt;/PRICE&gt;
    &lt;/FRUIT&gt;

    &lt;FRUIT&gt;
       &lt;NAME&gt;banana&lt;/NAME&gt;
       &lt;COLOR&gt;yellow&lt;/COLOR&gt;
       &lt;PRICE&gt;0.60&lt;/PRICE&gt;
    &lt;/FRUIT&gt;
&lt;/DATABASE&gt;
</pre>


<h3>Parsing without any options:</h3>


<pre>
(xml-parse (read-file "example.xml"))
<span class='arw'>&rarr;</span> (("ELEMENT" "DATABASE" (("name" "example.xml")) (("TEXT" "\r\n\t") 
    ("COMMENT" "This is a database of fruits") 
    ("TEXT" "\r\n\t") 
    ("ELEMENT" "FRUIT" () (("TEXT" "\r\n\t\t") ("ELEMENT" "NAME" () 
       (("TEXT" "apple"))) 
      ("TEXT" "\r\n\t\t") 
      ("ELEMENT" "COLOR" () (("TEXT" "red"))) 
      ("TEXT" "\r\n\t\t") 
      ("ELEMENT" "PRICE" () (("TEXT" "0.80"))) 
      ("TEXT" "\r\n\t"))) 
    ("TEXT" "\r\n\r\n\t") 
    ("ELEMENT" "FRUIT" () (("TEXT" "\r\n\t\t") ("ELEMENT" "NAME" () 
       (("TEXT" "orange"))) 
      ("TEXT" "\r\n\t\t") 
      ("ELEMENT" "COLOR" () (("TEXT" "orange"))) 
      ("TEXT" "\r\n\t\t") 
      ("ELEMENT" "PRICE" () (("TEXT" "1.00"))) 
      ("TEXT" "\r\n\t"))) 
    ("TEXT" "\r\n\r\n\t") 
    ("ELEMENT" "FRUIT" () (("TEXT" "\r\n\t\t") ("ELEMENT" "NAME" () 
       (("TEXT" "banana"))) 
      ("TEXT" "\r\n\t\t") 
      ("ELEMENT" "COLOR" () (("TEXT" "yellow"))) 
      ("TEXT" "\r\n\t\t") 
      ("ELEMENT" "PRICE" () (("TEXT" "0.60"))) 
      ("TEXT" "\r\n\t"))) 
    ("TEXT" "\r\n"))))
</pre>


<p>
The <tt>TEXT</tt> elements containing only whitespace make the output very confusing.
As the database in <tt>example.xml</tt> only contains data,
we can suppress whitespace, empty attribute lists and comments with 
option <tt>(+ 1 2 4)</tt>:</p>

<h3>Filtering whitespace TEXT, COMMENT tags, and empty attribute lists:</h3>


<pre>
(xml-parse (read-file "example.xml") (+ 1 2 4))
<span class='arw'>&rarr;</span> (("ELEMENT" "DATABASE" (("name" "example.xml")) ( 
     ("ELEMENT" "FRUIT" (
       ("ELEMENT" "NAME" (("TEXT" "apple"))) 
       ("ELEMENT" "COLOR" (("TEXT" "red"))) 
       ("ELEMENT" "PRICE" (("TEXT" "0.80"))))) 
     ("ELEMENT" "FRUIT" (
       ("ELEMENT" "NAME" (("TEXT" "orange"))) 
       ("ELEMENT" "COLOR" (("TEXT" "orange"))) 
       ("ELEMENT" "PRICE" (("TEXT" "1.00"))))) 
     ("ELEMENT" "FRUIT" (
       ("ELEMENT" "NAME" (("TEXT" "banana"))) 
       ("ELEMENT" "COLOR" (("TEXT" "yellow"))) 
       ("ELEMENT" "PRICE" (("TEXT" "0.60"))))))))
</pre>


<p>
The resulting output looks much more readable, but it can still be improved 
by using symbols instead of strings for the tags "FRUIT", "NAME", "COLOR", and "PRICE",
as well as by suppressing the XML type tags "ELEMENT" and "TEXT" completely 
using the <a href="#xml-type-tags">xml-type-tags</a> directive.
</p>

<h3>Suppressing XML type tags with <a href="#xml-type-tags">xml-type-tags</a> 
and translating string tags into symbol tags:</h3>


<pre>
;; suppress all XML type tags for TEXT and ELEMENT
;; instead of "CDATA", use cdata and instead of "COMMENT", use !--

(xml-type-tags nil 'cdata '!-- nil) 

;; turn on all options for suppressing whitespace and empty
;; attributes, translate tags to symbols

(xml-parse (read-file "example.xml") (+ 1 2 8))
<span class='arw'>&rarr;</span> ((DATABASE (("name" "example.xml")) 
     (!-- "This is a database of fruits") 
     (FRUIT (NAME "apple") (COLOR "red") (PRICE "0.80")) 
     (FRUIT (NAME "orange") (COLOR "orange") (PRICE "1.00")) 
     (FRUIT (NAME "banana") (COLOR "yellow") (PRICE "0.60"))))
</pre>


<p>
When tags are translated into symbols by using option <tt>8</tt>, 
a context can be specified in <em>sym-context</em>. 
If no context is specified, all symbols will be created inside the current context.
</p>


<pre>
(xml-type-tags nil nil nil nil)
(xml-parse "&lt;msg&gt;Hello World&lt;/msg&gt;" (+ 1 2 4 8 16) 'CTX)
<span class='arw'>&rarr;</span> ((CTX:msg "Hello World"))
</pre>


<p>
Specifying <tt>nil</tt> for the XML type tags TEXT and ELEMENT 
makes them disappear.  At the same time, 
parentheses of the child node list are removed so that 
child nodes now appear as members of the list, 
starting with the tag symbol translated from the string tags 
"FRUIT", "NAME", etcetera.
</p>

<h3>Parsing into SXML (S-expressions XML) format:</h3>
<p>
Using <a href="#xml-type-tags">xml-type-tags</a> to suppress 
all XML-type tags&mdash;along with the option numbers 
<tt>1</tt>, <tt>2</tt>, <tt>4</tt>, <tt>8</tt>, and <tt>16</tt>&mdash;SXML 
formatted output can be generated:
</p>


<pre>
(xml-type-tags nil nil nil nil)
(xml-parse (read-file "example.xml") (+ 1 2 4 8 16))
<span class='arw'>&rarr;</span> ((DATABASE (@ (name "example.xml")) 
    (FRUIT (NAME "apple") (COLOR "red") (PRICE "0.80")) 
    (FRUIT (NAME "orange") (COLOR "orange") (PRICE "1.00")) 
    (FRUIT (NAME "banana") (COLOR "yellow") (PRICE "0.60"))))
</pre>

<p>If the original XML tags contain a namespace part separated by a <tt>:</tt>,
that colon will be translated into a <tt>.</tt> dot in the resulting newLISP
symbol.</p>


<p>
Note that using option number <tt>16</tt> 
causes an <tt>@</tt> (at symbol) to be added to attribute lists.
</p>

<p>
See also the <a href="#xml-type-tags">xml-type-tags</a> function 
for further information on XML parsing.
</p>

<h3>Parsing into a specified context</h3>

<p>When parsing XML expressions, XML tags are translated into newLISP symbols,
when option 8 is specified.  The <i>sym-context</i> option specifies the target 
context for the symbol creation:</p>

<pre>
(xml-type-tags nil nil nil nil)
(xml-parse (read-file "example.xml") (+ 1 2 4 8 16) 'CTX)
<span class='arw'>&rarr;</span>((CTX:DATABASE (@ (CTX:name "example.xml")) 
    (CTX:FRUIT (CTX:NAME "apple") (CTX:COLOR "red") (CTX:PRICE "0.80")) 
    (CTX:FRUIT (CTX:NAME "orange") (CTX:COLOR "orange") (CTX:PRICE "1.00")) 
    (CTX:FRUIT (CTX:NAME "banana") (CTX:COLOR "yellow") (CTX:PRICE "0.60"))))
</pre>

<p>If the context does not exist, it will be created. If it exists, the quote can 
be omitted or the context can be referred to by a variable.</p>

<h3>Using a call back function</h3>
<p>Normally, <tt>xml-parse</tt> will not return until all parsing has finished.
Using the <em>func-callback</em> option, <tt>xml-parse</tt> will call back after
each tag closing with the generated S-expression and a start position and
length in the source XML:</p>


<pre>
;; demo callback feature
(define (xml-callback s-expr start size)
    (if (or (= (s-expr 0) 'NAME) (= (s-expr 0) 'COLOR) (= (s-expr 0) 'PRICE))
        (begin
            (print "parsed expression:" s-expr)
            (println ", source:" (start size example-xml))
        )
    )
)

(xml-type-tags nil 'cdata '!-- nil)
(xml-parse  (read-file "example.xml") (+ 1 2 8) MAIN xml-callback)
</pre>


<p>The following output will be generated by the callback function <tt>xml-callback</tt>:</p>

<pre>
parsed expression:(NAME "apple"), source:&lt;NAME&gt;apple&lt;/NAME&gt;
parsed expression:(COLOR "red"), source:&lt;COLOR&gt;red&lt;/COLOR&gt;
parsed expression:(PRICE "0.80"), source:&lt;PRICE&gt;0.80&lt;/PRICE&gt;
parsed expression:(NAME "orange"), source:&lt;NAME&gt;orange&lt;/NAME&gt;
parsed expression:(COLOR "orange"), source:&lt;COLOR&gt;orange&lt;/COLOR&gt;
parsed expression:(PRICE "1.00"), source:&lt;PRICE&gt;1.00&lt;/PRICE&gt;
parsed expression:(NAME "banana"), source:&lt;NAME&gt;banana&lt;/NAME&gt;
parsed expression:(COLOR "yellow"), source:&lt;COLOR&gt;yellow&lt;/COLOR&gt;
parsed expression:(PRICE "0.60"), source:&lt;PRICE&gt;0.60&lt;/PRICE&gt;
</pre>


<p>The example callback handler function filters the tags of interest and processes
them as they occur.</p>

<br/><br/>

<a name="xml-type-tags"></a>
<h2><span class="function">xml-type-tags</span></h2>

<h4>syntax: (xml-type-tags [<em>exp-text-tag</em> <em>exp-cdata-tag</em> <em>exp-comment-tag</em> <em>exp-element-tags</em>])</h4>

<p>
Can suppress completely or replace the XML type tags 
"TEXT", "CDATA", "COMMENT", and "ELEMENT" with something else specified 
in the parameters.
</p>

<p>
Note that <tt>xml-type-tags</tt> only suppresses or translates the tags themselves 
but does not suppress or modify the tagged information.  The latter would be done 
using option numbers in <a href="#xml-parse">xml-parse</a>.
</p>

<p>
Using <tt>xml-type-tags</tt> without arguments 
returns the current type tags:
</p>

<!-- example -->

<pre>
(xml-type-tags)  <span class='arw'>&rarr;</span> ("TEXT" "CDATA" "COMMENT" "ELEMENT")

(xml-type-tags nil 'cdata '!-- nil)
</pre>


<p>
The first example just shows the currently used type tags.
The second example specifies suppression of the "TEXT" and "ELEMENT" tags 
and shows <tt>cdata</tt> and <tt>!--</tt> instead of 
"CDATA" and "COMMENT".
</p>

<br/><br/>

<a name="zerop"></a>
<h2><span class="function">zero?</span>&nbsp;
<a href="#big_int"><font size="-1">bigint</font></a></h2>
<h4>syntax: (zero? <em>exp</em>)</h4>

<p>
Checks the evaluation of <em>exp</em> to see if it equals <tt>0</tt> (zero).
</p>

<!-- example -->

<pre>
(set 'value 1.2)
(set 'var 0)
(zero? value)  <span class='arw'>&rarr;</span> nil
(zero? var)    <span class='arw'>&rarr;</span> true

(map zero? '(0 0.0 3.4 4))  <span class='arw'>&rarr;</span> (true true nil nil)

(map zero? '(nil true 0 0.0 "" ()))  <span class='arw'>&rarr;</span> (nil nil true true nil nil)
</pre>


<p>
	<tt>zero?</tt> will return <tt>nil</tt> 
	on data types other than numbers.
</p>

<br/><br/>

<center style="font-size: 150%">
<span class="divider">(&nbsp;<font color="#7ba9d4">&part;</font>&nbsp;)</span>
</center>

<br/><br/>

<hr/>

<br/><br/>

<a name="appendix"></a>
<center><h2>newLISP APPENDIX</h2></center>

<a name="error_codes"></a>
<h2>Error codes</h2>

<table summary="Error codes">
<tr align="left"><th>description</th><th>no</th></tr>
<tr><td>not enough memory</td><td>1</td></tr>
<tr><td>environment stack overflow</td><td>2</td></tr>
<tr><td>call stack overflow</td><td>3</td></tr>
<tr><td>problem accessing file</td><td>4</td></tr>
<tr><td>not an expression</td><td>5</td></tr>
<tr><td>missing parenthesis</td><td>6</td></tr>
<tr><td>string token too long</td><td>7</td></tr>
<tr><td>missing argument</td><td>8</td></tr>
<tr><td>number or string expected</td><td>9</td></tr>
<tr><td>value expected</td><td>10</td></tr>
<tr><td>string expected</td><td>11</td></tr>
<tr><td>symbol expected</td><td>12</td></tr>
<tr><td>context expected</td><td>13</td></tr>
<tr><td>symbol or context expected</td><td>14</td></tr>
<tr><td>list expected</td><td>15</td></tr>
<tr><td>list or array expected</td><td>16</td></tr>
<tr><td>list or symbol expected</td><td>17</td></tr>
<tr><td>list or string expected</td><td>18</td></tr>
<tr><td>list or number expected</td><td>19</td></tr>
<tr><td>array expected</td><td>20</td></tr>
<tr><td>array, list or string expected</td><td>21</td></tr>
<tr><td>lambda expected</td><td>22</td></tr>
<tr><td>lambda-macro expected</td><td>23</td></tr>
<tr><td>invalid function</td><td>24</td></tr>
<tr><td>invalid lambda expression</td><td>25</td></tr>
<tr><td>invalid macro expression</td><td>26</td></tr>
<tr><td>invalid let parameter list</td><td>27</td></tr>
<tr><td>problem saving file</td><td>28</td></tr>
<tr><td>division by zero</td><td>29</td></tr>
<tr><td>matrix expected</td><td>30</td></tr>
<tr><td>wrong dimensions</td><td>31</td></tr>
<tr><td>matrix is singular</td><td>32</td></tr>
<tr><td>syntax in regular expression</td><td>33</td></tr>
<tr><td>throw without catch</td><td>34</td></tr>
<tr><td>problem loading library</td><td>35</td></tr>
<tr><td>import function not found</td><td>36</td></tr>
<tr><td>symbol is protected</td><td>37</td></tr>
<tr><td>error number too high</td><td>38</td></tr>
<tr><td>regular expression</td><td>39</td></tr>
<tr><td>missing end of text [/text]</td><td>40</td></tr>
<tr><td>mismatch in number of arguments</td><td>41</td></tr>
<tr><td>problem in format string</td><td>42</td></tr>
<tr><td>data type and format don't match</td><td>43</td></tr>
<tr><td>invalid parameter</td><td>44</td></tr>
<tr><td>invalid parameter: 0.0</td><td>45</td></tr>
<tr><td>invalid parameter: NaN</td><td>46</td></tr>
<tr><td>invalid UTF8 string</td><td>47</td></tr>
<tr><td>illegal parameter type</td><td>48</td></tr>
<tr><td>symbol not in MAIN context</td><td>49</td></tr>
<tr><td>symbol not in current context</td><td>50</td></tr>
<tr><td>target cannot be MAIN</td><td>51</td></tr>
<tr><td>list index out of bounds</td><td>52</td></tr>
<tr><td>array index out of bounds</td><td>53</td></tr>
<tr><td>string index out of bounds</td><td>54</td></tr>
<tr><td>nesting level too deep</td><td>55</td></tr>
<tr><td>list reference changed</td><td>56</td></tr>
<tr><td>invalid syntax</td><td>57</td></tr>
<tr><td>user error</td><td>58</td></tr>
<tr><td>user reset -</td><td>59</td></tr>
<tr><td>received SIGINT -</td><td>60</td></tr>
<tr><td>function is not reentrant</td><td>61</td></tr>
<tr><td>local symbol is protected</td><td>62</td></tr>
<tr><td>no reference found</td><td>63</td></tr>
<tr><td>list is empty</td><td>64</td></tr>
<tr><td>I/O error</td><td>65</td></tr>
<tr><td>working directory not found</td><td>66</td></tr>
<tr><td>invalid PID</td><td>67</td></tr>
<tr><td>cannot open socket pair</td><td>68</td></tr>
<tr><td>cannot fork process</td><td>69</td></tr>
<tr><td>no comm channel found</td><td>70</td></tr>

<tr><td>ffi preparation failed</td><td>71</td></tr>
<tr><td>invalid ffi type</td><td>72</td></tr>
<tr><td>ffi struct expected</td><td>73</td></tr>

<tr><td>bigint type not applicable</td><td>74</td></tr>
<tr><td>not a number or infinite</td><td>75</td></tr>
<tr><td>cannot convert NULL to string</td><td>76</td></tr>
</table><br/>

<br/><br/><br/>

<a name="tcpip_error_codes"></a>
<h2>TCP/IP and UDP Error Codes</h2>

<table summary="tcp/ip error codes">
<tr align="left"><th>no</th><th>description</th></tr>
<tr><td>1</td><td>Cannot open socket</td></tr>
<tr><td>2</td><td>DNS resolution failed</td></tr>
<tr><td>3</td><td>Not a valid service</td></tr>
<tr><td>4</td><td>Connection failed</td></tr>
<tr><td>5</td><td>Accept failed</td></tr>
<tr><td>6</td><td>Connection closed</td></tr>
<tr><td>7</td><td>Connection broken</td></tr>
<tr><td>8</td><td>Socket send() failed</td></tr>
<tr><td>9</td><td>Socket recv() failed</td></tr>
<tr><td>10</td><td>Cannot bind socket</td></tr>
<tr><td>11</td><td>Too many sockets in net-select</td></tr>
<tr><td>12</td><td>Listen failed</td></tr>
<tr><td>13</td><td>Badly formed IP</td></tr>
<tr><td>14</td><td>Select failed</td></tr>
<tr><td>15</td><td>Peek failed</td></tr>
<tr><td>16</td><td>Not a valid socket</td></tr>
<tr><td>17</td><td>Cannot unblock socket</td></tr>
<tr><td>18</td><td>Operation timed out</td></tr>
<tr><td>19</td><td>HTTP bad formed URL</td></tr>
<tr><td>20</td><td>HTTP file operation failed</td></tr>
<tr><td>21</td><td>HTTP transfer failed</td></tr>
<tr><td>22</td><td>HTTP invalid response from server</td></tr>
<tr><td>23</td><td>HTTP no response from server</td></tr>
<tr><td>24</td><td>HTTP document empty</td></tr>
<tr><td>25</td><td>HTTP error in header</td></tr>
<tr><td>26</td><td>HTTP error in chunked format</td></tr>
</table><br/>

<br/><br/><br/>

<a name="system_symbols"></a>
<h2>System Symbols and Constants</h2>

<h3>Variables changed by the system</h3>

<p>newLISP maintains several internal symbol variables. All of them are global
and can be used by the programmer. Some have write protection, others
are user settable. Some will change when used in a sub-expression of the
enclosing expression using it. Others are safe when using reentrant in nested 
functions or expressions.</p>

<p>All symbols starting with the <tt>$</tt> character will not be serialized
when using the <a href="#save">save</a> or <a href="#source">source</a> functions.
</p>

<table width="98%" summary="system vars">
<tr><th>&nbsp;variable&nbsp;name</th><th>purpose</th><th>&nbsp;protected&nbsp;</th><th>&nbsp;reentrant&nbsp;</th></tr>
<tr><td><tt>&nbsp;$0 - $15</tt></td><td>Used primarily in regular expressions. <tt>$0</tt>
is also used to record the last state or count of execution of some functions.</td>
<td>&nbsp;no</td><td>&nbsp;no</td></tr>

<tr><td><tt>&nbsp;$args</tt></td><td>Contains the list parameters not bound to local
variables. Normally the function <a href="#args">args</a> is used to retrieve
the contents of this variable.</td><td>&nbsp;yes</td><td>&nbsp;yes</td></tr>

<tr><td><tt>&nbsp;$count</tt></td><td>The count of elements matching when using
<a href="#find-all">find-all</a>, <a href="#replace">replace</a>,
<a href="#ref-all">ref-all</a> and <a href="#set-ref-all">set-ref-all</a> or the 
count of characters processed by <a href="#read-expr">read-expr</a>.</td>
<td>&nbsp;yes</td><td>&nbsp;no</td></tr>

<tr><td><tt>&nbsp;$idx</tt></td><td>The function <a href="#dolist">dolist</a>
maintains this as a list index or offset. The functions 
<a href="#map">map</a>, <a href="#series">series</a>,
<a href="#while">while</a>, <a href="#until">until</a>,
<a href="#do-while">do-while</a> and <a href="#do-until">do-until</a>
maintain this variable as an iteration counter starting with 0 (zero) for
the first iteration.</td>
<td>&nbsp;yes</td><td>&nbsp;yes</td></tr>

<tr><td><tt>&nbsp;$it</tt></td><td>The <em>anaphoric</em> <tt>$it</tt> refers to the
result inside an executing expression, i.e. in self referential assignments. 
<tt>$it</tt> is only available inside the function expression setting it, and
is set to <tt>nil</tt> on exit of that expression. The following functions use it:
<a href="#if">if</a>,
<a href="#hash">hashes</a>, <a href="#find-all">find-all</a>,
<a href="#replace">replace</a>, <a href="#set-ref">set-ref</a>, 
<a href="#set-ref-all">set-ref-all</a> and <a href="#setf">setf setq</a>.</td>
<td>&nbsp;yes</td><td>&nbsp;no</td></tr>

<tr><td><tt>&nbsp;$main-args</tt></td><td>Contains the list of command line arguments
passed by the OS to newLISP when it was started. Normally the function
<a href="#main-args">main-args</a> is used to retrieve the contents.</td>
<td>&nbsp;yes</td><td>&nbsp;n/a</td></tr>

</table><br/>

<h3>Predefined variables and functions.</h3>

<p>These are preset symbol constants. Two of them are used as namespace templates,
one two write platform independent code.</p> 

<table  width="98%" summary="preset vars">
<tr><th>name</th><th>purpose</th><th>&nbsp;protected&nbsp;</th><th>&nbsp;reentrant&nbsp;</th></tr>

<tr><td><tt>&nbsp;Class</tt></td><td>Is the predefined general FOOP class constructor
which can be used together with <tt>new</tt> to create new FOOP classes, e.g:
<tt>(new Class 'Rectangle)</tt> would create a class and object constructor for
a user class <tt>Rectangle</tt>. See the 
<a href="#newlisp_classes">FOOP classes and constructors</a> chapter in the users 
manual for details.</td>
<td>&nbsp;no</td><td>&nbsp;n/a</td></tr>

<tr><td><tt>&nbsp;ostype</tt></td><td>Contains a string identifying the OS-Platform
for which the running newLISP version has been compiled. See the reference section for 
<a href="#ostype">details</a></td>
<td>&nbsp;yes</td><td>&nbsp;n/a</td></tr>

<tr><td><tt>&nbsp;Tree</tt></td><td>Is a predefined namespace to serve as a hash like
dictionary. Instead of writing <tt>(define Foo:Foo)</tt> to create a <tt>Foo</tt>
dictionary, the expression <tt>(new Tree 'Foo)</tt> can be used as well. See the chapter
<a href="#hash">Hash functions and dictionaries</a> foe details.</td>
<td>&nbsp;no</td><td>&nbsp;n/a</td></tr>

<tr><td><tt>&nbsp;module</tt></td><td>Is a predefined function to load modules. 
Instead of using <tt>load</tt> together with the <tt>NEWLISPDIR</tt> environment 
variable, the <tt>module</tt> function loads automatically from 
<tt>$NEWLISPDIR/modules/</tt>.</td>
<td>&nbsp;no</td><td>&nbsp;n/a</td></tr>

</table><br/>

<p>The symbols <tt>Class</tt>, <tt>Tree</tt> and <tt>module</tt> are predefined as follows:</p>

<pre>
; built-in template for FOOP constructors
; usage: (new Class 'MyClass)
(define (Class:Class) 
    (cons (context) (args)))

; built-in template for hashes
; usage: (new Tree 'MyHash)
(context 'Tree) 
  (constant 'Tree:Tree) 
(context MAIN)"

; load modules from standard path
; usage (module "mymodule.lsp")
(define (module $x)
  (load (append (env "NEWLISPDIR") "/modules/" $x)))

(global 'module)
</pre>

<p>These symbols are not protected and can be redefined by the user.
The <tt>$x</tt> variable is built-in and protected against deletion.
This <tt>$x</tt> variable is also used in <a href="#curry">curry</a> expressions.</p>

<br/><br/>

<center style="font-size: 150%">
<span class="divider">(&nbsp;<font color="#7ba9d4">&part;</font>&nbsp;)</span>
</center>
<br/><br/>

<hr/>


<div class="license">
<a name="GNUFDL"></a>
<center>
<h2><span class="gnu">GNU Free Documentation License</span></h2>
<p>Version 1.2, November 2002</p>

<p>
Copyright (C) 2000,2001,2002  Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
</p>
</center>

<br/><br/>

<h4>0. PREAMBLE</h4>

<p>The purpose of this License is to make a manual, textbook, or other
functional and useful document "free" in the sense of freedom: to
assure everyone the effective freedom to copy and redistribute it,
with or without modifying it, either commercially or noncommercially.
Secondarily, this License preserves for the author and publisher a way
to get credit for their work, while not being considered responsible
for modifications made by others.
</p>
<p>This License is a kind of "copyleft", which means that derivative
works of the document must themselves be free in the same sense. It
complements the GNU General Public License, which is a copyleft
license designed for free software.
</p>
<p>We have designed this License in order to use it for manuals for
free
software, because free software needs free documentation: a free
program should come with manuals providing the same freedoms that the
software does. But this License is not limited to software manuals;
it can be used for any textual work, regardless of subject matter or
whether it is published as a printed book. We recommend this License
principally for works whose purpose is instruction or reference.
</p>
<h4>1. APPLICABILITY AND DEFINITIONS</h4>
<p>This License applies to any manual or other work, in any medium,
that
contains a notice placed by the copyright holder saying it can be
distributed under the terms of this License. Such a notice grants a
world-wide, royalty-free license, unlimited in duration, to use that
work under the conditions stated herein. The "Document", below,
refers to any such manual or work. Any member of the public is a
licensee, and is addressed as "you". You accept the license if you
copy, modify or distribute the work in a way requiring permission
under copyright law.
</p>
<p>A "Modified Version" of the Document means any work containing the
Document or a portion of it, either copied verbatim, or with
modifications and/or translated into another language.
</p>
<p>A "Secondary Section" is a named appendix or a front-matter section
of
the Document that deals exclusively with the relationship of the
publishers or authors of the Document to the Document's overall subject
(or to related matters) and contains nothing that could fall directly
within that overall subject. (Thus, if the Document is in part a
textbook of mathematics, a Secondary Section may not explain any
mathematics.) The relationship could be a matter of historical
connection with the subject or with related matters, or of legal,
commercial, philosophical, ethical or political position regarding
them.
</p>
<p>The "Invariant Sections" are certain Secondary Sections whose titles
are designated, as being those of Invariant Sections, in the notice
that says that the Document is released under this License. If a
section does not fit the above definition of Secondary then it is not
allowed to be designated as Invariant. The Document may contain zero
Invariant Sections. If the Document does not identify any Invariant
Sections then there are none.
</p>
<p>The "Cover Texts" are certain short passages of text that are
listed,
as Front-Cover Texts or Back-Cover Texts, in the notice that says that
the Document is released under this License. A Front-Cover Text may
be at most 5 words, and a Back-Cover Text may be at most 25 words.
</p>
<p>A "Transparent" copy of the Document means a machine-readable copy,
represented in a format whose specification is available to the
general public, that is suitable for revising the document
straightforwardly with generic text editors or (for images composed of
pixels) generic paint programs or (for drawings) some widely available
drawing editor, and that is suitable for input to text formatters or
for automatic translation to a variety of formats suitable for input
to text formatters. A copy made in an otherwise Transparent file
format whose markup, or absence of markup, has been arranged to thwart
or discourage subsequent modification by readers is not Transparent.
An image format is not Transparent if used for any substantial amount
of text. A copy that is not "Transparent" is called "Opaque".
</p>
<p>Examples of suitable formats for Transparent copies include plain
ASCII without markup, Texinfo input format, LaTeX input format, SGML
or XML using a publicly available DTD, and standard-conforming simple
HTML, PostScript or PDF designed for human modification. Examples of
transparent image formats include PNG, XCF and JPG. Opaque formats
include proprietary formats that can be read and edited only by
proprietary word processors, SGML or XML for which the DTD and/or
processing tools are not generally available, and the
machine-generated HTML, PostScript or PDF produced by some word
processors for output purposes only.
</p>
<p>The "Title Page" means, for a printed book, the title page itself,
plus such following pages as are needed to hold, legibly, the material
this License requires to appear in the title page. For works in
formats which do not have any title page as such, "Title Page" means
the text near the most prominent appearance of the work's title,
preceding the beginning of the body of the text.
</p>
<p>A section "Entitled XYZ" means a named subunit of the Document whose
title either is precisely XYZ or contains XYZ in parentheses following
text that translates XYZ in another language. (Here XYZ stands for a
specific section name mentioned below, such as "Acknowledgements",
"Dedications", "Endorsements", or "History".) To "Preserve the Title"
of such a section when you modify the Document means that it remains a
section "Entitled XYZ" according to this definition.
</p>
<p>The Document may include Warranty Disclaimers next to the notice
which
states that this License applies to the Document. These Warranty
Disclaimers are considered to be included by reference in this
License, but only as regards disclaiming warranties: any other
implication that these Warranty Disclaimers may have is void and has
no effect on the meaning of this License.
</p>
<h4>2. VERBATIM COPYING</h4>
<p>You may copy and distribute the Document in any medium, either
commercially or noncommercially, provided that this License, the
copyright notices, and the license notice saying this License applies
to the Document are reproduced in all copies, and that you add no other
conditions whatsoever to those of this License. You may not use
technical measures to obstruct or control the reading or further
copying of the copies you make or distribute. However, you may accept
compensation in exchange for copies. If you distribute a large enough
number of copies you must also follow the conditions in section 3.
</p>
<p>You may also lend copies, under the same conditions stated above,
and
you may publicly display copies.
</p>
<h4>3. COPYING IN QUANTITY</h4>
<p>If you publish printed copies (or copies in media that commonly have
printed covers) of the Document, numbering more than 100, and the
Document's license notice requires Cover Texts, you must enclose the
copies in covers that carry, clearly and legibly, all these Cover
Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
the back cover. Both covers must also clearly and legibly identify
you as the publisher of these copies. The front cover must present
the full title with all words of the title equally prominent and
visible. You may add other material on the covers in addition.
Copying with changes limited to the covers, as long as they preserve
the title of the Document and satisfy these conditions, can be treated
as verbatim copying in other respects.
</p>
<p>If the required texts for either cover are too voluminous to fit
legibly, you should put the first ones listed (as many as fit
reasonably) on the actual cover, and continue the rest onto adjacent
pages.
</p>
<p>If you publish or distribute Opaque copies of the Document numbering
more than 100, you must either include a machine-readable Transparent
copy along with each Opaque copy, or state in or with each Opaque copy
a computer-network location from which the general network-using
public has access to download using public-standard network protocols
a complete Transparent copy of the Document, free of added material.
If you use the latter option, you must take reasonably prudent steps,
when you begin distribution of Opaque copies in quantity, to ensure
that this Transparent copy will remain thus accessible at the stated
location until at least one year after the last time you distribute an
Opaque copy (directly or through your agents or retailers) of that
edition to the public.
</p>
<p>It is requested, but not required, that you contact the authors of
the
Document well before redistributing any large number of copies, to give
them a chance to provide you with an updated version of the Document.
</p>
<h4>4. MODIFICATIONS</h4>
<p>You may copy and distribute a Modified Version of the Document under
the conditions of sections 2 and 3 above, provided that you release
the Modified Version under precisely this License, with the Modified
Version filling the role of the Document, thus licensing distribution
and modification of the Modified Version to whoever possesses a copy
of it. In addition, you must do these things in the Modified Version:
</p>
<blockquote>
  <p><b>A.</b> Use in the Title Page (and on the covers, if
any) a title distinct from that of the Document, and from those of
previous versions (which should, if there were any, be listed in the
History section of the Document). You may use the same title as a
previous version if the original publisher of that version gives
permission.</p>
  <p><b>B.</b> List on the Title Page, as authors, one or
more persons or entities responsible for authorship of the
modifications in the Modified Version, together with at least five of
the principal authors of the Document (all of its principal authors, if
it has fewer than five), unless they release you from this requirement.</p>
  <p><b>C.</b> State on the Title page the name of the
publisher of the Modified Version, as the publisher.</p>
  <p><b>D.</b> Preserve all the copyright notices of the
Document.</p>
  <p><b>E.</b> Add an appropriate copyright notice for your
modifications adjacent to the other copyright notices.</p>
  <p><b>F.</b> Include, immediately after the copyright
notices, a license notice giving the public permission to use the
Modified Version under the terms of this License, in the form shown in
the Addendum below.</p>
  <p><b>G.</b> Preserve in that license notice the full
lists of Invariant Sections and required Cover Texts given in the
Document's license notice.</p>
  <p><b>H.</b> Include an unaltered copy of this License.</p>
  <p><b>I.</b> Preserve the section Entitled "History",
Preserve its Title, and add to it an item stating at least the title,
year, new authors, and publisher of the Modified Version as given on
the Title Page. If there is no section Entitled "History" in the
Document, create one stating the title, year, authors, and publisher of
the Document as given on its Title Page, then add an item describing
the Modified Version as stated in the previous sentence.</p>
  <p><b>J.</b> Preserve the network location, if any, given
in the Document for public access to a Transparent copy of the
Document, and likewise the network locations given in the Document for
previous versions it was based on. These may be placed in the "History"
section. You may omit a network location for a work that was published
at least four years before the Document itself, or if the original
publisher of the version it refers to gives permission.</p>
  <p><b>K.</b> For any section Entitled "Acknowledgements"
or "Dedications", Preserve the Title of the section, and preserve in
the section all the substance and tone of each of the contributor
acknowledgements and/or dedications given therein.</p>
  <p><b>L.</b> Preserve all the Invariant Sections of the
Document, unaltered in their text and in their titles. Section numbers
or the equivalent are not considered part of the section titles.</p>
  <p><b>M.</b> Delete any section Entitled "Endorsements".
Such a section may not be included in the Modified Version.</p>
  <p><b>N.</b> Do not retitle any existing section to be
Entitled "Endorsements" or to conflict in title with any Invariant
Section.</p>
  <p><b>O.</b> Preserve any Warranty Disclaimers.</p>
</blockquote>
<p>
If the Modified Version includes new front-matter sections or
appendices that qualify as Secondary Sections and contain no material
copied from the Document, you may at your option designate some or all
of these sections as invariant. To do this, add their titles to the
list of Invariant Sections in the Modified Version's license notice.
These titles must be distinct from any other section titles.
</p>
<p>You may add a section Entitled "Endorsements", provided it contains
nothing but endorsements of your Modified Version by various
parties--for example, statements of peer review or that the text has
been approved by an organization as the authoritative definition of a
standard.
</p>
<p>You may add a passage of up to five words as a Front-Cover Text, and
a
passage of up to 25 words as a Back-Cover Text, to the end of the list
of Cover Texts in the Modified Version. Only one passage of
Front-Cover Text and one of Back-Cover Text may be added by (or
through arrangements made by) any one entity. If the Document already
includes a cover text for the same cover, previously added by you or
by arrangement made by the same entity you are acting on behalf of,
you may not add another; but you may replace the old one, on explicit
permission from the previous publisher that added the old one.
</p>
<p>The author(s) and publisher(s) of the Document do not by this
License
give permission to use their names for publicity for or to assert or
imply endorsement of any Modified Version.
</p>
<h4>5. COMBINING DOCUMENTS</h4>
<p>You may combine the Document with other documents released under
this
License, under the terms defined in section 4 above for modified
versions, provided that you include in the combination all of the
Invariant Sections of all of the original documents, unmodified, and
list them all as Invariant Sections of your combined work in its
license notice, and that you preserve all their Warranty Disclaimers.
</p>
<p>The combined work need only contain one copy of this License, and
multiple identical Invariant Sections may be replaced with a single
copy. If there are multiple Invariant Sections with the same name but
different contents, make the title of each such section unique by
adding at the end of it, in parentheses, the name of the original
author or publisher of that section if known, or else a unique number.
Make the same adjustment to the section titles in the list of
Invariant Sections in the license notice of the combined work.
</p>
<p>In the combination, you must combine any sections Entitled "History"
in the various original documents, forming one section Entitled
"History"; likewise combine any sections Entitled "Acknowledgements",
and any sections Entitled "Dedications". You must delete all sections
Entitled "Endorsements."
</p>
<h4>6. COLLECTIONS OF DOCUMENTS</h4>
<p>You may make a collection consisting of the Document and other
documents
released under this License, and replace the individual copies of this
License in the various documents with a single copy that is included in
the collection, provided that you follow the rules of this License for
verbatim copying of each of the documents in all other respects.
</p>
<p>You may extract a single document from such a collection, and
distribute
it individually under this License, provided you insert a copy of this
License into the extracted document, and follow this License in all
other respects regarding verbatim copying of that document.
</p>
<h4>7. AGGREGATION WITH INDEPENDENT WORKS</h4>
<p>A compilation of the Document or its derivatives with other separate
and independent documents or works, in or on a volume of a storage or
distribution medium, is called an "aggregate" if the copyright
resulting from the compilation is not used to limit the legal rights
of the compilation's users beyond what the individual works permit.
When the Document is included in an aggregate, this License does not
apply to the other works in the aggregate which are not themselves
derivative works of the Document.
</p>
<p>If the Cover Text requirement of section 3 is applicable to these
copies of the Document, then if the Document is less than one half of
the entire aggregate, the Document's Cover Texts may be placed on
covers that bracket the Document within the aggregate, or the
electronic equivalent of covers if the Document is in electronic form.
Otherwise they must appear on printed covers that bracket the whole
aggregate.
</p>
<h4>8. TRANSLATION</h4>
<p>Translation is considered a kind of modification, so you may
distribute translations of the Document under the terms of section 4.
Replacing Invariant Sections with translations requires special
permission from their copyright holders, but you may include
translations of some or all Invariant Sections in addition to the
original versions of these Invariant Sections. You may include a
translation of this License, and all the license notices in the
Document, and any Warranty Disclaimers, provided that you also include
the original English version of this License and the original versions
of those notices and disclaimers. In case of a disagreement between
the translation and the original version of this License or a notice
or disclaimer, the original version will prevail.
</p>
<p>If a section in the Document is Entitled "Acknowledgements",
"Dedications", or "History", the requirement (section 4) to Preserve
its Title (section 1) will typically require changing the actual
title.
</p>
<h4>9. TERMINATION</h4>
<p>You may not copy, modify, sublicense, or distribute the Document
except
as expressly provided for under this License. Any other attempt to
copy, modify, sublicense or distribute the Document is void, and will
automatically terminate your rights under this License. However,
parties who have received copies, or rights, from you under this
License will not have their licenses terminated so long as such
parties remain in full compliance.
</p>
<h4>10. FUTURE REVISIONS OF THIS LICENSE</h4>
<p>The Free Software Foundation may publish new, revised versions
of the GNU Free Documentation License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns. See
http://www.gnu.org/copyleft/.
</p>
<p>Each version of the License is given a distinguishing version
number.
If the Document specifies that a particular numbered version of this
License "or any later version" applies to it, you have the option of
following the terms and conditions either of that specified version or
of any later version that has been published (not as a draft) by the
Free Software Foundation. If the Document does not specify a version
number of this License, you may choose any version ever published (not
as a draft) by the Free Software Foundation.
</p>
<br/><br/>

<hr/>

<br/><br/>

<br/><br/>

<a name="GNUGPL"></a>
<center>
<h2><span class="gnu">GNU GENERAL PUBLIC LICENSE</span></h2>
		      <p>Version 3, 29 June 2007</p>
</center>

<p>
 Copyright (C) 2007 Free Software Foundation, Inc. http://fsf.org/
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.
</p>

<center><h4>Preamble</h4></center>

<p>
  The GNU General Public License is a free, copyleft license for
software and other kinds of works.
</p>
<p>
  The licenses for most software and other practical works are designed
to take away your freedom to share and change the works.  By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.  We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors.  You can apply it to
your programs, too.
</p>
<p>
  When we speak of free software, we are referring to freedom, not
price.  Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
</p>
<p>
  To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights.  Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
</p>
<p>
  For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received.  You must make sure that they, too, receive
or can get the source code.  And you must show them these terms so they
know their rights.
</p>
<p>
  Developers that use the GNU GPL protect your rights with two steps:
</p>
<p>
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
</p>
<p>
  For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software.  For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
</p>
<p>
  Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so.  This is fundamentally incompatible with the aim of
protecting users' freedom to change the software.  The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable.  Therefore, we
have designed this version of the GPL to prohibit the practice for those
products.  If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
</p>
<p>
  Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary.  To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
</p>
<p>
  The precise terms and conditions for copying, distribution and
modification follow.
</p>
<center><h4>TERMS AND CONDITIONS</h4></center>

<h4>0. Definitions.</h4>
<p>
  "This License" refers to version 3 of the GNU General Public License.
</p>
<p>
  "Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
</p>
<p>
  "The Program" refers to any copyrightable work licensed under this
License.  Each licensee is addressed as "you".  "Licensees" and
"recipients" may be individuals or organizations.
</p>
<p>
  To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy.  The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
</p>
<p>
  A "covered work" means either the unmodified Program or a work based
on the Program.
</p>
<p>
  To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy.  Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
</p>
<p>
  To "convey" a work means any kind of propagation that enables other
parties to make or receive copies.  Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
</p>
<p>
  An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License.  If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.</p>

<h4>1. Source Code.</h4>

<p>
  The "source code" for a work means the preferred form of the work
for making modifications to it.  "Object code" means any non-source
form of a work.
</p>
<p>
  A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
</p>
<p>
  The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form.  A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
</p>
<p>
  The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities.  However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work.  For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
</p>
<p>
  The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
</p>
<p>
  The Corresponding Source for a work in source code form is that
same work.
</p>
<h4>2. Basic Permissions.</h4>
<p>
  All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met.  This License explicitly affirms your unlimited
permission to run the unmodified Program.  The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work.  This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
</p>
<p>
  You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force.  You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright.  Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
</p>
<p>
  Conveying under any other circumstances is permitted solely under
the conditions stated below.  Sublicensing is not allowed; section 10
makes it unnecessary.
</p>
<h4>3. Protecting Users' Legal Rights From Anti-Circumvention Law.</h4>
<p>
  No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
</p>
<p>
  When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
</p>
<h4>4. Conveying Verbatim Copies.</h4>
<p>
 You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
</p>
<p>
  You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
</p>
<h4>5. Conveying Modified Source Versions.</h4>
<p>
  You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
</p>
<blockquote>
    a) The work must carry prominent notices stating that you modified
    it, and giving a relevant date.
</blockquote>
<blockquote>
    b) The work must carry prominent notices stating that it is
    released under this License and any conditions added under section
    7.  This requirement modifies the requirement in section 4 to
    "keep intact all notices".
</blockquote>
<blockquote>
    c) You must license the entire work, as a whole, under this
    License to anyone who comes into possession of a copy.  This
    License will therefore apply, along with any applicable section 7
    additional terms, to the whole of the work, and all its parts,
    regardless of how they are packaged.  This License gives no
    permission to license the work in any other way, but it does not
    invalidate such permission if you have separately received it.
</blockquote>
<blockquote>
    d) If the work has interactive user interfaces, each must display
    Appropriate Legal Notices; however, if the Program has interactive
    interfaces that do not display Appropriate Legal Notices, your
    work need not make them do so.
</blockquote>
<p>
  A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit.  Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
</p>
<h4>6. Conveying Non-Source Forms.</h4>
<p>
  You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
</p>
<blockquote>
    a) Convey the object code in, or embodied in, a physical product
    (including a physical distribution medium), accompanied by the
    Corresponding Source fixed on a durable physical medium
    customarily used for software interchange.
</blockquote>
<blockquote>
    b) Convey the object code in, or embodied in, a physical product
    (including a physical distribution medium), accompanied by a
    written offer, valid for at least three years and valid for as
    long as you offer spare parts or customer support for that product
    model, to give anyone who possesses the object code either (1) a
    copy of the Corresponding Source for all the software in the
    product that is covered by this License, on a durable physical
    medium customarily used for software interchange, for a price no
    more than your reasonable cost of physically performing this
    conveying of source, or (2) access to copy the
    Corresponding Source from a network server at no charge.
</blockquote>
<blockquote>
    c) Convey individual copies of the object code with a copy of the
    written offer to provide the Corresponding Source.  This
    alternative is allowed only occasionally and noncommercially, and
    only if you received the object code with such an offer, in accord
    with subsection 6b.
</blockquote>
<blockquote>
    d) Convey the object code by offering access from a designated
    place (gratis or for a charge), and offer equivalent access to the
    Corresponding Source in the same way through the same place at no
    further charge.  You need not require recipients to copy the
    Corresponding Source along with the object code.  If the place to
    copy the object code is a network server, the Corresponding Source
    may be on a different server (operated by you or a third party)
    that supports equivalent copying facilities, provided you maintain
    clear directions next to the object code saying where to find the
    Corresponding Source.  Regardless of what server hosts the
    Corresponding Source, you remain obligated to ensure that it is
    available for as long as needed to satisfy these requirements.
</blockquote>
<blockquote>
    e) Convey the object code using peer-to-peer transmission, provided
    you inform other peers where the object code and Corresponding
    Source of the work are being offered to the general public at no
    charge under subsection 6d.
</blockquote>
<p>
  A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
</p>
<p>
  A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling.  In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage.  For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product.  A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
</p>
<p>
  "Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source.  The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
</p>
<p>
  If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information.  But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
</p>
<p>
  The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed.  Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
</p>
<p>
  Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
</p>
<h4>7. Additional Terms.</h4>
<p>
  "Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law.  If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
</p>
<p>
  When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it.  (Additional permissions may be written to require their own
removal in certain cases when you modify the work.)  You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
</p>
<p>
  Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
</p>
<blockquote>
    a) Disclaiming warranty or limiting liability differently from the
    terms of sections 15 and 16 of this License; or
</blockquote>
<blockquote>
    b) Requiring preservation of specified reasonable legal notices or
    author attributions in that material or in the Appropriate Legal
    Notices displayed by works containing it; or
</blockquote>
<blockquote>
    c) Prohibiting misrepresentation of the origin of that material, or
    requiring that modified versions of such material be marked in
    reasonable ways as different from the original version; or
</blockquote>
<blockquote>
    d) Limiting the use for publicity purposes of names of licensors or
    authors of the material; or
</blockquote>
<blockquote>
    e) Declining to grant rights under trademark law for use of some
    trade names, trademarks, or service marks; or
</blockquote>
<blockquote>
    f) Requiring indemnification of licensors and authors of that
    material by anyone who conveys the material (or modified versions of
    it) with contractual assumptions of liability to the recipient, for
    any liability that these contractual assumptions directly impose on
    those licensors and authors.
</blockquote>
<p>
  All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10.  If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term.  If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
</p>
<p>
  If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
</p>
<p>
  Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
</p>
<h4>8. Termination.</h4>
<p>
  You may not propagate or modify a covered work except as expressly
provided under this License.  Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
</p>
<p>
  However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
</p>
<p>
  Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
</p>
<p>
  Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License.  If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
</p>
<h4>9. Acceptance Not Required for Having Copies.</h4>
<p>
  You are not required to accept this License in order to receive or
run a copy of the Program.  Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance.  However,
nothing other than this License grants you permission to propagate or
modify any covered work.  These actions infringe copyright if you do
not accept this License.  Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
</p>
<h4>10. Automatic Licensing of Downstream Recipients.</h4>
<p>
  Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License.  You are not responsible
for enforcing compliance by third parties with this License.
</p>
<p>
  An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations.  If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
</p>
<p>
  You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License.  For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
</p>
<h4>11. Patents.</h4>
<p>
  A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based.  The
work thus licensed is called the contributor's "contributor version".
</p>
<p>
  A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version.  For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
</p>
<p>
  Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
</p>
<p>
  In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement).  To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
</p>
<p>
  If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients.  "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
</p>
<p>
  If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
</p>
<p>
  A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License.  You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
</p>
<p>
  Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
</p>
<h4>12. No Surrender of Others' Freedom.</h4>
<p>
  If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License.  If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all.  For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
</p>
<h4>13. Use with the GNU Affero General Public License.</h4>
<p>
  Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work.  The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
</p>
<h4>14. Revised Versions of this License.</h4>
<p>
  The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time.  Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
</p>
<p>
  Each version is given a distinguishing version number.  If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation.  If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
</p>
<p>
  If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
</p>
<p>
  Later license versions may give you additional or different
permissions.  However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
</p>
<h4>15. Disclaimer of Warranty.</h4>
<p>
  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
</p>
<h4>16. Limitation of Liability.</h4>
<p>
  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
</p>
<h4>17. Interpretation of Sections 15 and 16.</h4>
<p>
  If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
</p>
<br/>
<center><h4>END OF TERMS AND CONDITIONS</h4></center>
</div>
<br/><br/>
<center style="font-size: 150%">
<span class="divider">(&nbsp;<font color="#7ba9d4">&part;</font>&nbsp;)</span>
</center>
<br/><br/>

</body>
</html>