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
|
;;; cl.el --- Common-Lisp extensions for GNU Emacs Lisp.
;; Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
;; Author: Cesar Quiroz <quiroz@cs.rochester.edu>
;; Keywords: extensions
(defvar cl-version "3.0 07-February-1993")
;; This file is part of GNU Emacs.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY. No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing. Refer to the GNU Emacs General Public
;; License for full details.
;; Everyone is granted permission to copy, modify and redistribute
;; GNU Emacs, but only under the conditions described in the
;; GNU Emacs General Public License. A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities. It should be in a
;; file named COPYING. Among other things, the copyright notice
;; and this notice must be preserved on all copies.
;;; Commentary:
;;; Notes from Rob Austein on his mods
;; yaya:/usr/u/sra/cl/cl.el, 5-May-1991 16:01:34, sra
;;
;; Slightly hacked copy of cl.el 2.0 beta 27.
;;
;; Various minor performance improvements:
;; a) Don't use MAPCAR when we're going to discard its results.
;; b) Make various macros a little more clever about optimizing
;; generated code in common cases.
;; c) Fix DEFSETF to expand to the right code at compile-time.
;; d) Make various macros cleverer about generating reasonable
;; code when compiled, particularly forms like DEFSTRUCT which
;; are usually used at top-level and thus are only compiled if
;; you use Hallvard Furuseth's hacked bytecomp.el.
;;
;; New features: GETF, REMF, and REMPROP.
;;
;; Notes:
;; 1) I'm sceptical about the FBOUNDP checks in SETF. Why should
;; the SETF expansion fail because the SETF method isn't defined
;; at compile time? Lisp is going to check for a binding at run-time
;; anyway, so maybe we should just assume the user's right here.
;;;; These are extensions to Emacs Lisp that provide some form of
;;;; Common Lisp compatibility, beyond what is already built-in
;;;; in Emacs Lisp.
;;;;
;;;; When developing them, I had the code spread among several files.
;;;; This file 'cl.el' is a concatenation of those original files,
;;;; minus some declarations that became redundant. The marks between
;;;; the original files can be found easily, as they are lines that
;;;; begin with four semicolons (as this does). The names of the
;;;; original parts follow the four semicolons in uppercase, those
;;;; names are GLOBAL, SYMBOLS, LISTS, SEQUENCES, CONDITIONALS,
;;;; ITERATIONS, MULTIPLE VALUES, ARITH, SETF and DEFSTRUCT. If you
;;;; add functions to this file, you might want to put them in a place
;;;; that is compatible with the division above (or invent your own
;;;; categories).
;;;;
;;;; To compile this file, make sure you load it first. This is
;;;; because many things are implemented as macros and now that all
;;;; the files are concatenated together one cannot ensure that
;;;; declaration always precedes use.
;;;;
;;;; Bug reports, suggestions and comments,
;;;; to quiroz@cs.rochester.edu
;;;; GLOBAL
;;;; This file provides utilities and declarations that are global
;;;; to Common Lisp and so might be used by more than one of the
;;;; other libraries. Especially, I intend to keep here some
;;;; utilities that help parsing/destructuring some difficult calls.
;;;;
;;;;
;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986
;;;; (quiroz@cs.rochester.edu)
;;; Too many pieces of the rest of this package use psetq. So it is unwise to
;;; use here anything but plain Emacs Lisp! There is a neater recursive form
;;; for the algorithm that deals with the bodies.
;;; Code:
;;; This version is due to Hallvard Furuseth (hallvard@ifi.uio.no, 6 Jul 91)
(defmacro psetq (&rest args)
"(psetq {VARIABLE VALUE}...): In parallel, set each VARIABLE to its VALUE.
All the VALUEs are evaluated, and then all the VARIABLEs are set.
Aside from order of evaluation, this is the same as `setq'."
;; check there is a reasonable number of forms
(if (/= (% (length args) 2) 0)
(error "Odd number of arguments to `psetq'"))
(setq args (copy-sequence args)) ;for safety below
(prog1 (cons 'setq args)
(while (progn (if (not (symbolp (car args)))
(error "`psetq' expected a symbol, found '%s'."
(prin1-to-string (car args))))
(cdr (cdr args)))
(setcdr args (list (list 'prog1 (nth 1 args)
(cons 'setq
(setq args (cdr (cdr args))))))))))
;;; utilities
;;;
;;; pair-with-newsyms takes a list and returns a list of lists of the
;;; form (newsym form), such that a let* can then bind the evaluation
;;; of the forms to the newsyms. The idea is to guarantee correct
;;; order of evaluation of the subforms of a setf. It also returns a
;;; list of the newsyms generated, in the corresponding order.
(defun pair-with-newsyms (oldforms)
"PAIR-WITH-NEWSYMS OLDFORMS
The top-level components of the list oldforms are paired with fresh
symbols, the pairings list and the newsyms list are returned."
(do ((ptr oldforms (cdr ptr))
(bindings '())
(newsyms '()))
((endp ptr) (values (nreverse bindings) (nreverse newsyms)))
(let ((newsym (gentemp)))
(setq bindings (cons (list newsym (car ptr)) bindings))
(setq newsyms (cons newsym newsyms)))))
(defun zip-lists (evens odds)
"Merge two lists EVENS and ODDS, taking elts from each list alternatingly.
EVENS and ODDS are two lists. ZIP-LISTS constructs a new list, whose
even numbered elements (0,2,...) come from EVENS and whose odd
numbered elements (1,3,...) come from ODDS.
The construction stops when the shorter list is exhausted."
(do* ((p0 evens (cdr p0))
(p1 odds (cdr p1))
(even (car p0) (car p0))
(odd (car p1) (car p1))
(result '()))
((or (endp p0) (endp p1))
(nreverse result))
(setq result
(cons odd (cons even result)))))
(defun unzip-list (list)
"Extract even and odd elements of LIST into two separate lists.
The argument LIST is separated in two strands, the even and the odd
numbered elements. Numbering starts with 0, so the first element
belongs in EVENS. No check is made that there is an even number of
elements to start with."
(do* ((ptr list (cddr ptr))
(this (car ptr) (car ptr))
(next (cadr ptr) (cadr ptr))
(evens '())
(odds '()))
((endp ptr)
(values (nreverse evens) (nreverse odds)))
(setq evens (cons this evens))
(setq odds (cons next odds))))
(defun reassemble-argslists (argslists)
"(reassemble-argslists ARGSLISTS) => a list of lists
ARGSLISTS is a list of sequences. Return a list of lists, the first
sublist being all the entries coming from ELT 0 of the original
sublists, the next those coming from ELT 1 and so on, until the
shortest list is exhausted."
(let* ((minlen (apply 'min (mapcar 'length argslists)))
(result '()))
(dotimes (i minlen (nreverse result))
;; capture all the elements at index i
(setq result
(cons (mapcar (function (lambda (sublist) (elt sublist i)))
argslists)
result)))))
;;; Checking that a list of symbols contains no duplicates is a common
;;; task when checking the legality of some macros. The check for 'eq
;;; pairs can be too expensive, as it is quadratic on the length of
;;; the list. I use a 4-pass, linear, counting approach. It surely
;;; loses on small lists (less than 5 elements?), but should win for
;;; larger lists. The fourth pass could be eliminated.
;;; 10 dec 1986. Emacs Lisp has no REMPROP, so I just eliminated the
;;; 4th pass.
;;;
;;; [22 April 1991, sra] REMPROP now in library, so restored 4th pass.
(defun duplicate-symbols-p (list)
"Find all symbols appearing more than once in LIST.
Return a list of all such duplicates; `nil' if there are no duplicates."
(let ((duplicates '()) ;result built here
(propname (gensym)) ;we use a fresh property
)
;; check validity
(unless (and (listp list)
(every 'symbolp list))
(error "a list of symbols is needed"))
;; pass 1: mark
(dolist (x list)
(put x propname 0))
;; pass 2: count
(dolist (x list)
(put x propname (1+ (get x propname))))
;; pass 3: collect
(dolist (x list)
(if (> (get x propname) 1)
(setq duplicates (cons x duplicates))))
;; pass 4: unmark.
(dolist (x list)
(remprop x propname))
;; return result
duplicates))
;;;; end of cl-global.el
;;;; SYMBOLS
;;;; This file provides the gentemp function, which generates fresh
;;;; symbols, plus some other minor Common Lisp symbol tools.
;;;;
;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986
;;;; (quiroz@cs.rochester.edu)
;;; Keywords. There are no packages in Emacs Lisp, so this is only a
;;; kludge around to let things be "as if" a keyword package was around.
(defmacro defkeyword (x &optional docstring)
"Make symbol X a keyword (symbol whose value is itself).
Optional second argument is a documentation string for it."
(cond ((symbolp x)
(list 'defconst x (list 'quote x) docstring))
(t
(error "`%s' is not a symbol" (prin1-to-string x)))))
(defun keywordp (sym)
"t if SYM is a keyword."
(if (and (symbolp sym) (char-equal (aref (symbol-name sym) 0) ?\:))
;; looks like one, make sure value is right
(set sym sym)
nil))
(defun keyword-of (sym)
"Return a keyword that is naturally associated with symbol SYM.
If SYM is keyword, the value is SYM.
Otherwise it is a keyword whose name is `:' followed by SYM's name."
(cond ((keywordp sym)
sym)
((symbolp sym)
(let ((newsym (intern (concat ":" (symbol-name sym)))))
(set newsym newsym)))
(t
(error "expected a symbol, not `%s'" (prin1-to-string sym)))))
;;; Temporary symbols.
;;;
(defvar *gentemp-index* 0
"Integer used by gentemp to produce new names.")
(defvar *gentemp-prefix* "T$$_"
"Names generated by gentemp begin with this string by default.")
(defun gentemp (&optional prefix oblist)
"Generate a fresh interned symbol.
There are 2 optional arguments, PREFIX and OBLIST. PREFIX is the
string that begins the new name, OBLIST is the obarray used to search for
old names. The defaults are just right, YOU SHOULD NEVER NEED THESE
ARGUMENTS IN YOUR OWN CODE."
(if (null prefix)
(setq prefix *gentemp-prefix*))
(if (null oblist)
(setq oblist obarray)) ;default for the intern functions
(let ((newsymbol nil)
(newname))
(while (not newsymbol)
(setq newname (concat prefix *gentemp-index*))
(setq *gentemp-index* (+ *gentemp-index* 1))
(if (not (intern-soft newname oblist))
(setq newsymbol (intern newname oblist))))
newsymbol))
(defvar *gensym-index* 0
"Integer used by gensym to produce new names.")
(defvar *gensym-prefix* "G$$_"
"Names generated by gensym begin with this string by default.")
(defun gensym (&optional prefix)
"Generate a fresh uninterned symbol.
There is an optional argument, PREFIX. PREFIX is the
string that begins the new name. Most people take just the default,
except when debugging needs suggest otherwise."
(if (null prefix)
(setq prefix *gensym-prefix*))
(let ((newsymbol nil)
(newname ""))
(while (not newsymbol)
(setq newname (concat prefix *gensym-index*))
(setq *gensym-index* (+ *gensym-index* 1))
(if (not (intern-soft newname))
(setq newsymbol (make-symbol newname))))
newsymbol))
;;;; end of cl-symbols.el
;;;; CONDITIONALS
;;;; This file provides some of the conditional constructs of
;;;; Common Lisp. Total compatibility is again impossible, as the
;;;; 'if' form is different in both languages, so only a good
;;;; approximation is desired.
;;;;
;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986
;;;; (quiroz@cs.rochester.edu)
;;; indentation info
(put 'case 'lisp-indent-hook 1)
(put 'ecase 'lisp-indent-hook 1)
(put 'when 'lisp-indent-hook 1)
(put 'unless 'lisp-indent-hook 1)
;;; WHEN and UNLESS
;;; These two forms are simplified ifs, with a single branch.
(defmacro when (condition &rest body)
"(when CONDITION . BODY) => evaluate BODY if CONDITION is true."
(list* 'if (list 'not condition) '() body))
(defmacro unless (condition &rest body)
"(unless CONDITION . BODY) => evaluate BODY if CONDITION is false."
(list* 'if condition '() body))
;;; CASE and ECASE
;;; CASE selects among several clauses, based on the value (evaluated)
;;; of a expression and a list of (unevaluated) key values. ECASE is
;;; the same, but signals an error if no clause is activated.
(defmacro case (expr &rest cases)
"(case EXPR . CASES) => evals EXPR, chooses from CASES on that value.
EXPR -> any form
CASES -> list of clauses, non empty
CLAUSE -> HEAD . BODY
HEAD -> t = catch all, must be last clause
-> otherwise = same as t
-> nil = illegal
-> atom = activated if (eql EXPR HEAD)
-> list of atoms = activated if (memq EXPR HEAD)
BODY -> list of forms, implicit PROGN is built around it.
EXPR is evaluated only once."
(let* ((newsym (gentemp))
(clauses (case-clausify cases newsym)))
;; convert case into a cond inside a let
(list 'let
(list (list newsym expr))
(list* 'cond (nreverse clauses)))))
(defmacro ecase (expr &rest cases)
"(ecase EXPR . CASES) => like `case', but error if no case fits.
`t'-clauses are not allowed."
(let* ((newsym (gentemp))
(clauses (case-clausify cases newsym)))
;; check that no 't clause is present.
;; case-clausify would put one such at the beginning of clauses
(if (eq (caar clauses) t)
(error "no clause-head should be `t' or `otherwise' for `ecase'"))
;; insert error-catching clause
(setq clauses
(cons
(list 't (list 'error
"ecase on %s = %s failed to take any branch"
(list 'quote expr)
(list 'prin1-to-string newsym)))
clauses))
;; generate code as usual
(list 'let
(list (list newsym expr))
(list* 'cond (nreverse clauses)))))
(defun case-clausify (cases newsym)
"CASE-CLAUSIFY CASES NEWSYM => clauses for a 'cond'
Converts the CASES of a [e]case macro into cond clauses to be
evaluated inside a let that binds NEWSYM. Returns the clauses in
reverse order."
(do* ((currentpos cases (cdr currentpos))
(nextpos (cdr cases) (cdr nextpos))
(curclause (car cases) (car currentpos))
(result '()))
((endp currentpos) result)
(let ((head (car curclause))
(body (cdr curclause)))
;; construct a cond-clause according to the head
(cond ((null head)
(error "case clauses cannot have null heads: `%s'"
(prin1-to-string curclause)))
((or (eq head 't)
(eq head 'otherwise))
;; check it is the last clause
(if (not (endp nextpos))
(error "clause with `t' or `otherwise' head must be last"))
;; accept this clause as a 't' for cond
(setq result (cons (cons 't body) result)))
((atom head)
(setq result
(cons (cons (list 'eql newsym (list 'quote head)) body)
result)))
((listp head)
(setq result
(cons (cons (list 'memq newsym (list 'quote head)) body)
result)))
(t
;; catch-all for this parser
(error "don't know how to parse case clause `%s'"
(prin1-to-string head)))))))
;;;; end of cl-conditionals.el
;;;; ITERATIONS
;;;; This file provides simple iterative macros (a la Common Lisp)
;;;; constructed on the basis of let, let* and while, which are the
;;;; primitive binding/iteration constructs of Emacs Lisp
;;;;
;;;; The Common Lisp iterations use to have a block named nil
;;;; wrapped around them, and allow declarations at the beginning
;;;; of their bodies and you can return a value using (return ...).
;;;; Nothing of the sort exists in Emacs Lisp, so I haven't tried
;;;; to imitate these behaviors.
;;;;
;;;; Other than the above, the semantics of Common Lisp are
;;;; correctly reproduced to the extent this was reasonable.
;;;;
;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986
;;;; (quiroz@cs.rochester.edu)
;;; some lisp-indentation information
(put 'do 'lisp-indent-hook 2)
(put 'do* 'lisp-indent-hook 2)
(put 'dolist 'lisp-indent-hook 1)
(put 'dotimes 'lisp-indent-hook 1)
(put 'do-symbols 'lisp-indent-hook 1)
(put 'do-all-symbols 'lisp-indent-hook 1)
(defmacro do (stepforms endforms &rest body)
"(do STEPFORMS ENDFORMS . BODY): Iterate BODY, stepping some local variables.
STEPFORMS must be a list of symbols or lists. In the second case, the
lists must start with a symbol and contain up to two more forms. In
the STEPFORMS, a symbol is the same as a (symbol). The other 2 forms
are the initial value (def. NIL) and the form to step (def. itself).
The values used by initialization and stepping are computed in parallel.
The ENDFORMS are a list (CONDITION . ENDBODY). If the CONDITION
evaluates to true in any iteration, ENDBODY is evaluated and the last
form in it is returned.
The BODY (which may be empty) is evaluated at every iteration, with
the symbols of the STEPFORMS bound to the initial or stepped values."
;; check the syntax of the macro
(and (check-do-stepforms stepforms)
(check-do-endforms endforms))
;; construct emacs-lisp equivalent
(let ((initlist (extract-do-inits stepforms))
(steplist (extract-do-steps stepforms))
(endcond (car endforms))
(endbody (cdr endforms)))
(cons 'let (cons initlist
(cons (cons 'while (cons (list 'not endcond)
(append body steplist)))
(append endbody))))))
(defmacro do* (stepforms endforms &rest body)
"`do*' is to `do' as `let*' is to `let'.
STEPFORMS must be a list of symbols or lists. In the second case, the
lists must start with a symbol and contain up to two more forms. In
the STEPFORMS, a symbol is the same as a (symbol). The other 2 forms
are the initial value (def. NIL) and the form to step (def. itself).
Initializations and steppings are done in the sequence they are written.
The ENDFORMS are a list (CONDITION . ENDBODY). If the CONDITION
evaluates to true in any iteration, ENDBODY is evaluated and the last
form in it is returned.
The BODY (which may be empty) is evaluated at every iteration, with
the symbols of the STEPFORMS bound to the initial or stepped values."
;; check the syntax of the macro
(and (check-do-stepforms stepforms)
(check-do-endforms endforms))
;; construct emacs-lisp equivalent
(let ((initlist (extract-do-inits stepforms))
(steplist (extract-do*-steps stepforms))
(endcond (car endforms))
(endbody (cdr endforms)))
(cons 'let* (cons initlist
(cons (cons 'while (cons (list 'not endcond)
(append body steplist)))
(append endbody))))))
;;; DO and DO* share the syntax checking functions that follow.
(defun check-do-stepforms (forms)
"True if FORMS is a valid stepforms for the do[*] macro (q.v.)"
(if (nlistp forms)
(error "init/step form for do[*] should be a list, not `%s'"
(prin1-to-string forms))
(mapcar
(function
(lambda (entry)
(if (not (or (symbolp entry)
(and (listp entry)
(symbolp (car entry))
(< (length entry) 4))))
(error "init/step must be %s, not `%s'"
"symbol or (symbol [init [step]])"
(prin1-to-string entry)))))
forms)))
(defun check-do-endforms (forms)
"True if FORMS is a valid endforms for the do[*] macro (q.v.)"
(if (nlistp forms)
(error "termination form for do macro should be a list, not `%s'"
(prin1-to-string forms))))
(defun extract-do-inits (forms)
"Returns a list of the initializations (for do) in FORMS
--a stepforms, see the do macro--. FORMS is assumed syntactically valid."
(mapcar
(function
(lambda (entry)
(cond ((symbolp entry)
(list entry nil))
((listp entry)
(list (car entry) (cadr entry))))))
forms))
;;; There used to be a reason to deal with DO differently than with
;;; DO*. The writing of PSETQ has made it largely unnecessary.
(defun extract-do-steps (forms)
"EXTRACT-DO-STEPS FORMS => an s-expr
FORMS is the stepforms part of a DO macro (q.v.). This function
constructs an s-expression that does the stepping at the end of an
iteration."
(list (cons 'psetq (select-stepping-forms forms))))
(defun extract-do*-steps (forms)
"EXTRACT-DO*-STEPS FORMS => an s-expr
FORMS is the stepforms part of a DO* macro (q.v.). This function
constructs an s-expression that does the stepping at the end of an
iteration."
(list (cons 'setq (select-stepping-forms forms))))
(defun select-stepping-forms (forms)
"Separate only the forms that cause stepping."
(let ((result '()) ;ends up being (... var form ...)
(ptr forms) ;to traverse the forms
entry ;to explore each form in turn
)
(while ptr ;(not (endp entry)) might be safer
(setq entry (car ptr))
(cond ((and (listp entry) (= (length entry) 3))
(setq result (append ;append in reverse order!
(list (caddr entry) (car entry))
result))))
(setq ptr (cdr ptr))) ;step in the list of forms
(nreverse result)))
;;; Other iterative constructs
(defmacro dolist (stepform &rest body)
"(dolist (VAR LIST [RESULTFORM]) . BODY): do BODY for each elt of LIST.
The RESULTFORM defaults to nil. The VAR is bound to successive
elements of the value of LIST and remains bound (to the nil value) when the
RESULTFORM is evaluated."
;; check sanity
(cond
((nlistp stepform)
(error "stepform for `dolist' should be (VAR LIST [RESULT]), not `%s'"
(prin1-to-string stepform)))
((not (symbolp (car stepform)))
(error "first component of stepform should be a symbol, not `%s'"
(prin1-to-string (car stepform))))
((> (length stepform) 3)
(error "too many components in stepform `%s'"
(prin1-to-string stepform))))
;; generate code
(let* ((var (car stepform))
(listform (cadr stepform))
(resultform (caddr stepform))
(listsym (gentemp)))
(nconc
(list 'let (list var (list listsym listform))
(nconc
(list 'while listsym
(list 'setq
var (list 'car listsym)
listsym (list 'cdr listsym)))
body))
(and resultform
(cons (list 'setq var nil)
(list resultform))))))
(defmacro dotimes (stepform &rest body)
"(dotimes (VAR COUNTFORM [RESULTFORM]) . BODY): Repeat BODY, counting in VAR.
The COUNTFORM should return a positive integer. The VAR is bound to
successive integers from 0 to COUNTFORM-1 and the BODY is repeated for
each of them. At the end, the RESULTFORM is evaluated and its value
returned. During this last evaluation, the VAR is still bound, and its
value is the number of times the iteration occurred. An omitted RESULTFORM
defaults to nil."
;; check sanity
(cond
((nlistp stepform)
(error "stepform for `dotimes' should be (VAR COUNT [RESULT]), not `%s'"
(prin1-to-string stepform)))
((not (symbolp (car stepform)))
(error "first component of stepform should be a symbol, not `%s'"
(prin1-to-string (car stepform))))
((> (length stepform) 3)
(error "too many components in stepform `%s'"
(prin1-to-string stepform))))
;; generate code
(let* ((var (car stepform))
(countform (cadr stepform))
(resultform (caddr stepform))
(testsym (if (consp countform) (gentemp) countform)))
(nconc
(list
'let (cons (list var -1)
(and (not (eq countform testsym))
(list (list testsym countform))))
(nconc
(list 'while (list '< (list 'setq var (list '1+ var)) testsym))
body))
(and resultform (list resultform)))))
(defmacro do-symbols (stepform &rest body)
"(do_symbols (VAR [OBARRAY [RESULTFORM]]) . BODY)
The VAR is bound to each of the symbols in OBARRAY (def. obarray) and
the BODY is repeatedly performed for each of those bindings. At the
end, RESULTFORM (def. nil) is evaluated and its value returned.
During this last evaluation, the VAR is still bound and its value is nil.
See also the function `mapatoms'."
;; check sanity
(cond
((nlistp stepform)
(error "stepform for `do-symbols' should be (VAR OBARRAY [RESULT]), not `%s'"
(prin1-to-string stepform)))
((not (symbolp (car stepform)))
(error "first component of stepform should be a symbol, not `%s'"
(prin1-to-string (car stepform))))
((> (length stepform) 3)
(error "too many components in stepform `%s'"
(prin1-to-string stepform))))
;; generate code
(let* ((var (car stepform))
(oblist (cadr stepform))
(resultform (caddr stepform)))
(list 'progn
(list 'mapatoms
(list 'function
(cons 'lambda (cons (list var) body)))
oblist)
(list 'let
(list (list var nil))
resultform))))
(defmacro do-all-symbols (stepform &rest body)
"(do-all-symbols (VAR [RESULTFORM]) . BODY)
Is the same as (do-symbols (VAR obarray RESULTFORM) . BODY)."
(list*
'do-symbols
(list (car stepform) 'obarray (cadr stepform))
body))
(defmacro loop (&rest body)
"(loop . BODY) repeats BODY indefinitely and does not return.
Normally BODY uses `throw' or `signal' to cause an exit.
The forms in BODY should be lists, as non-lists are reserved for new features."
;; check that the body doesn't have atomic forms
(if (nlistp body)
(error "body of `loop' should be a list of lists or nil")
;; ok, it is a list, check for atomic components
(mapcar
(function (lambda (component)
(if (nlistp component)
(error "components of `loop' should be lists"))))
body)
;; build the infinite loop
(cons 'while (cons 't body))))
;;;; end of cl-iterations.el
;;;; LISTS
;;;; This file provides some of the lists machinery of Common-Lisp
;;;; in a way compatible with Emacs Lisp. Especially, see the the
;;;; typical c[ad]*r functions.
;;;;
;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986
;;;; (quiroz@cs.rochester.edu)
;;; Synonyms for list functions
(defsubst first (x)
"Synonym for `car'"
(car x))
(defsubst second (x)
"Return the second element of the list LIST."
(nth 1 x))
(defsubst third (x)
"Return the third element of the list LIST."
(nth 2 x))
(defsubst fourth (x)
"Return the fourth element of the list LIST."
(nth 3 x))
(defsubst fifth (x)
"Return the fifth element of the list LIST."
(nth 4 x))
(defsubst sixth (x)
"Return the sixth element of the list LIST."
(nth 5 x))
(defsubst seventh (x)
"Return the seventh element of the list LIST."
(nth 6 x))
(defsubst eighth (x)
"Return the eighth element of the list LIST."
(nth 7 x))
(defsubst ninth (x)
"Return the ninth element of the list LIST."
(nth 8 x))
(defsubst tenth (x)
"Return the tenth element of the list LIST."
(nth 9 x))
(defsubst rest (x)
"Synonym for `cdr'"
(cdr x))
(defsubst endp (x)
"t if X is nil, nil if X is a cons; error otherwise."
(if (listp x)
(null x)
(error "endp received a non-cons, non-null argument `%s'"
(prin1-to-string x))))
(defun last (x)
"Returns the last link in the list LIST."
(if (nlistp x)
(error "arg to `last' must be a list"))
(do ((current-cons x (cdr current-cons))
(next-cons (cdr x) (cdr next-cons)))
((endp next-cons) current-cons)))
(defun list-length (x) ;taken from CLtL sect. 15.2
"Returns the length of a non-circular list, or `nil' for a circular one."
(do ((n 0) ;counter
(fast x (cddr fast)) ;fast pointer, leaps by 2
(slow x (cdr slow)) ;slow pointer, leaps by 1
(ready nil)) ;indicates termination
(ready n)
(cond ((endp fast)
(setq ready t)) ;return n
((endp (cdr fast))
(setq n (+ n 1))
(setq ready t)) ;return n+1
((and (eq fast slow) (> n 0))
(setq n nil)
(setq ready t)) ;return nil
(t
(setq n (+ n 2)))))) ;just advance counter
(defun butlast (list &optional n)
"Return a new list like LIST but sans the last N elements.
N defaults to 1. If the list doesn't have N elements, nil is returned."
(if (null n) (setq n 1))
(nreverse (nthcdr n (reverse list)))) ;optim. due to macrakis@osf.org
;;; This version due to Aaron Larson (alarson@src.honeywell.com, 26 Jul 91)
(defun list* (arg &rest others)
"Return a new list containing the first arguments consed onto the last arg.
Thus, (list* 1 2 3 '(a b)) returns (1 2 3 a b)."
(if (null others)
arg
(let* ((others (cons arg (copy-sequence others)))
(a others))
(while (cdr (cdr a))
(setq a (cdr a)))
(setcdr a (car (cdr a)))
others)))
(defun adjoin (item list)
"Return a list which contains ITEM but is otherwise like LIST.
If ITEM occurs in LIST, the value is LIST. Otherwise it is (cons ITEM LIST).
When comparing ITEM against elements, `eql' is used."
(if (memq item list)
list
(cons item list)))
(defun ldiff (list sublist)
"Return a new list like LIST but sans SUBLIST.
SUBLIST must be one of the links in LIST; otherwise the value is LIST itself."
(do ((result '())
(curcons list (cdr curcons)))
((or (endp curcons) (eq curcons sublist))
(reverse result))
(setq result (cons (car curcons) result))))
;;; The popular c[ad]*r functions and other list accessors.
;;; To implement this efficiently, a new byte compile handler is used to
;;; generate the minimal code, saving one function call.
(defsubst caar (X)
"Return the car of the car of X."
(car (car X)))
(defsubst cadr (X)
"Return the car of the cdr of X."
(car (cdr X)))
(defsubst cdar (X)
"Return the cdr of the car of X."
(cdr (car X)))
(defsubst cddr (X)
"Return the cdr of the cdr of X."
(cdr (cdr X)))
(defsubst caaar (X)
"Return the car of the car of the car of X."
(car (car (car X))))
(defsubst caadr (X)
"Return the car of the car of the cdr of X."
(car (car (cdr X))))
(defsubst cadar (X)
"Return the car of the cdr of the car of X."
(car (cdr (car X))))
(defsubst cdaar (X)
"Return the cdr of the car of the car of X."
(cdr (car (car X))))
(defsubst caddr (X)
"Return the car of the cdr of the cdr of X."
(car (cdr (cdr X))))
(defsubst cdadr (X)
"Return the cdr of the car of the cdr of X."
(cdr (car (cdr X))))
(defsubst cddar (X)
"Return the cdr of the cdr of the car of X."
(cdr (cdr (car X))))
(defsubst cdddr (X)
"Return the cdr of the cdr of the cdr of X."
(cdr (cdr (cdr X))))
(defsubst caaaar (X)
"Return the car of the car of the car of the car of X."
(car (car (car (car X)))))
(defsubst caaadr (X)
"Return the car of the car of the car of the cdr of X."
(car (car (car (cdr X)))))
(defsubst caadar (X)
"Return the car of the car of the cdr of the car of X."
(car (car (cdr (car X)))))
(defsubst cadaar (X)
"Return the car of the cdr of the car of the car of X."
(car (cdr (car (car X)))))
(defsubst cdaaar (X)
"Return the cdr of the car of the car of the car of X."
(cdr (car (car (car X)))))
(defsubst caaddr (X)
"Return the car of the car of the cdr of the cdr of X."
(car (car (cdr (cdr X)))))
(defsubst cadadr (X)
"Return the car of the cdr of the car of the cdr of X."
(car (cdr (car (cdr X)))))
(defsubst cdaadr (X)
"Return the cdr of the car of the car of the cdr of X."
(cdr (car (car (cdr X)))))
(defsubst caddar (X)
"Return the car of the cdr of the cdr of the car of X."
(car (cdr (cdr (car X)))))
(defsubst cdadar (X)
"Return the cdr of the car of the cdr of the car of X."
(cdr (car (cdr (car X)))))
(defsubst cddaar (X)
"Return the cdr of the cdr of the car of the car of X."
(cdr (cdr (car (car X)))))
(defsubst cadddr (X)
"Return the car of the cdr of the cdr of the cdr of X."
(car (cdr (cdr (cdr X)))))
(defsubst cddadr (X)
"Return the cdr of the cdr of the car of the cdr of X."
(cdr (cdr (car (cdr X)))))
(defsubst cdaddr (X)
"Return the cdr of the car of the cdr of the cdr of X."
(cdr (car (cdr (cdr X)))))
(defsubst cdddar (X)
"Return the cdr of the cdr of the cdr of the car of X."
(cdr (cdr (cdr (car X)))))
(defsubst cddddr (X)
"Return the cdr of the cdr of the cdr of the cdr of X."
(cdr (cdr (cdr (cdr X)))))
;;; some inverses of the accessors are needed for setf purposes
(defsubst setnth (n list newval)
"Set (nth N LIST) to NEWVAL. Returns NEWVAL."
(rplaca (nthcdr n list) newval))
(defun setnthcdr (n list newval)
"(setnthcdr N LIST NEWVAL) => NEWVAL
As a side effect, sets the Nth cdr of LIST to NEWVAL."
(when (< n 0)
(error "N must be 0 or greater, not %d" n))
(while (> n 0)
(setq list (cdr list)
n (- n 1)))
;; here only if (zerop n)
(rplaca list (car newval))
(rplacd list (cdr newval))
newval)
;;; A-lists machinery
(defsubst acons (key item alist)
"Return a new alist with KEY paired with ITEM; otherwise like ALIST.
Does not copy ALIST."
(cons (cons key item) alist))
(defun pairlis (keys data &optional alist)
"Return a new alist with each elt of KEYS paired with an elt of DATA;
optional 3rd arg ALIST is nconc'd at the end. KEYS and DATA must
have the same length."
(unless (= (length keys) (length data))
(error "keys and data should be the same length"))
(do* ;;collect keys and data in front of alist
((kptr keys (cdr kptr)) ;traverses the keys
(dptr data (cdr dptr)) ;traverses the data
(key (car kptr) (car kptr)) ;current key
(item (car dptr) (car dptr)) ;current data item
(result alist))
((endp kptr) result)
(setq result (acons key item result))))
;;;; end of cl-lists.el
;;;; SEQUENCES
;;;; Emacs Lisp provides many of the 'sequences' functionality of
;;;; Common Lisp. This file provides a few things that were left out.
;;;;
(defkeyword :test "Used to designate positive (selection) tests.")
(defkeyword :test-not "Used to designate negative (rejection) tests.")
(defkeyword :key "Used to designate component extractions.")
(defkeyword :predicate "Used to define matching of sequence components.")
(defkeyword :start "Inclusive low index in sequence")
(defkeyword :end "Exclusive high index in sequence")
(defkeyword :start1 "Inclusive low index in first of two sequences.")
(defkeyword :start2 "Inclusive low index in second of two sequences.")
(defkeyword :end1 "Exclusive high index in first of two sequences.")
(defkeyword :end2 "Exclusive high index in second of two sequences.")
(defkeyword :count "Number of elements to affect.")
(defkeyword :from-end "T when counting backwards.")
(defkeyword :initial-value "For the syntax of #'reduce")
(defun some (pred seq &rest moreseqs)
"Test PREDICATE on each element of SEQUENCE; is it ever non-nil?
Extra args are additional sequences; PREDICATE gets one arg from each
sequence and we advance down all the sequences together in lock-step.
A sequence means either a list or a vector."
(let ((args (reassemble-argslists (list* seq moreseqs))))
(do* ((ready nil) ;flag: return when t
(result nil) ;resulting value
(applyval nil) ;result of applying pred once
(remaining args
(cdr remaining)) ;remaining argument sets
(current (car remaining) ;current argument set
(car remaining)))
((or ready (endp remaining)) result)
(setq applyval (apply pred current))
(when applyval
(setq ready t)
(setq result applyval)))))
(defun every (pred seq &rest moreseqs)
"Test PREDICATE on each element of SEQUENCE; is it always non-nil?
Extra args are additional sequences; PREDICATE gets one arg from each
sequence and we advance down all the sequences together in lock-step.
A sequence means either a list or a vector."
(let ((args (reassemble-argslists (list* seq moreseqs))))
(do* ((ready nil) ;flag: return when t
(result t) ;resulting value
(applyval nil) ;result of applying pred once
(remaining args
(cdr remaining)) ;remaining argument sets
(current (car remaining) ;current argument set
(car remaining)))
((or ready (endp remaining)) result)
(setq applyval (apply pred current))
(unless applyval
(setq ready t)
(setq result nil)))))
(defun notany (pred seq &rest moreseqs)
"Test PREDICATE on each element of SEQUENCE; is it always nil?
Extra args are additional sequences; PREDICATE gets one arg from each
sequence and we advance down all the sequences together in lock-step.
A sequence means either a list or a vector."
(let ((args (reassemble-argslists (list* seq moreseqs))))
(do* ((ready nil) ;flag: return when t
(result t) ;resulting value
(applyval nil) ;result of applying pred once
(remaining args
(cdr remaining)) ;remaining argument sets
(current (car remaining) ;current argument set
(car remaining)))
((or ready (endp remaining)) result)
(setq applyval (apply pred current))
(when applyval
(setq ready t)
(setq result nil)))))
(defun notevery (pred seq &rest moreseqs)
"Test PREDICATE on each element of SEQUENCE; is it sometimes nil?
Extra args are additional sequences; PREDICATE gets one arg from each
sequence and we advance down all the sequences together in lock-step.
A sequence means either a list or a vector."
(let ((args (reassemble-argslists (list* seq moreseqs))))
(do* ((ready nil) ;flag: return when t
(result nil) ;resulting value
(applyval nil) ;result of applying pred once
(remaining args
(cdr remaining)) ;remaining argument sets
(current (car remaining) ;current argument set
(car remaining)))
((or ready (endp remaining)) result)
(setq applyval (apply pred current))
(unless applyval
(setq ready t)
(setq result t)))))
;;; More sequence functions that don't need keyword arguments
(defun concatenate (type &rest sequences)
"(concatenate TYPE &rest SEQUENCES) => a sequence
The sequence returned is of type TYPE (must be 'list, 'string, or 'vector) and
contains the concatenation of the elements of all the arguments, in the order
given."
(let ((sequences (append sequences '(()))))
(case type
(list
(apply (function append) sequences))
(string
(apply (function concat) sequences))
(vector
(apply (function vector) (apply (function append) sequences)))
(t
(error "type for concatenate `%s' not 'list, 'string or 'vector"
(prin1-to-string type))))))
(defun map (type function &rest sequences)
"(map TYPE FUNCTION &rest SEQUENCES) => a sequence
The FUNCTION is called on each set of elements from the SEQUENCES \(stopping
when the shortest sequence is terminated\) and the results are possibly
returned in a sequence of type TYPE \(one of 'list, 'vector, 'string, or nil\)
giving NIL for TYPE gets rid of the values."
(if (not (memq type (list 'list 'string 'vector nil)))
(error "type for map `%s' not 'list, 'string, 'vector or nil"
(prin1-to-string type)))
(let ((argslists (reassemble-argslists sequences))
results)
(if (null type)
(while argslists ;don't bother accumulating
(apply function (car argslists))
(setq argslists (cdr argslists)))
(setq results (mapcar (function (lambda (args) (apply function args)))
argslists))
(case type
(list
results)
(string
(funcall (function concat) results))
(vector
(apply (function vector) results))))))
;;; an inverse of elt is needed for setf purposes
(defun setelt (seq n newval)
"In SEQUENCE, set the Nth element to NEWVAL. Returns NEWVAL.
A sequence means either a list or a vector."
(let ((l (length seq)))
(if (or (< n 0) (>= n l))
(error "N(%d) should be between 0 and %d" n l)
;; only two cases need be considered valid, as strings are arrays
(cond ((listp seq)
(setnth n seq newval))
((arrayp seq)
(aset seq n newval))
(t
(error "SEQ should be a sequence, not `%s'"
(prin1-to-string seq)))))))
;;; Testing with keyword arguments.
;;;
;;; Many of the sequence functions use keywords to denote some stylized
;;; form of selecting entries in a sequence. The involved arguments
;;; are collected with a &rest marker (as Emacs Lisp doesn't have a &key
;;; marker), then they are passed to build-klist, who
;;; constructs an association list. That association list is used to
;;; test for satisfaction and matching.
;;; DON'T USE MEMBER, NOR ANY FUNCTION THAT COULD TAKE KEYWORDS HERE!!!
(defun build-klist (argslist acceptable &optional allow-other-keys)
"Decode a keyword argument list ARGSLIST for keywords in ACCEPTABLE.
ARGSLIST is a list, presumably the &rest argument of a call, whose
even numbered elements must be keywords.
ACCEPTABLE is a list of keywords, the only ones that are truly acceptable.
The result is an alist containing the arguments named by the keywords
in ACCEPTABLE, or an error is signalled, if something failed.
If the third argument (an optional) is non-nil, other keys are acceptable."
;; check legality of the arguments, then destructure them
(unless (and (listp argslist)
(evenp (length argslist)))
(error "build-klist: odd number of keyword-args"))
(unless (and (listp acceptable)
(every 'keywordp acceptable))
(error "build-klist: second arg should be a list of keywords"))
(multiple-value-bind
(keywords forms)
(unzip-list argslist)
(unless (every 'keywordp keywords)
(error "build-klist: expected keywords, found `%s'"
(prin1-to-string keywords)))
(unless (or allow-other-keys
(every (function (lambda (keyword)
(memq keyword acceptable)))
keywords))
(error "bad keyword[s]: %s not in %s"
(prin1-to-string (mapcan (function (lambda (keyword)
(if (memq keyword acceptable)
nil
(list keyword))))
keywords))
(prin1-to-string acceptable)))
(do* ;;pick up the pieces
((auxlist ;auxiliary a-list, may
(pairlis keywords forms)) ;contain repetitions and junk
(ptr acceptable (cdr ptr)) ;pointer in acceptable
(this (car ptr) (car ptr)) ;current acceptable keyword
(auxval nil) ;used to move values around
(alist '())) ;used to build the result
((endp ptr) alist)
;; if THIS appears in auxlist, use its value
(when (setq auxval (assq this auxlist))
(setq alist (cons auxval alist))))))
(defun extract-from-klist (klist key &optional default)
"(extract-from-klist KLIST KEY [DEFAULT]) => value of KEY or DEFAULT
Extract value associated with KEY in KLIST (return DEFAULT if nil)."
(let ((retrieved (cdr (assq key klist))))
(or retrieved default)))
(defun keyword-argument-supplied-p (klist key)
"(keyword-argument-supplied-p KLIST KEY) => nil or something
NIL if KEY (a keyword) does not appear in the KLIST."
(assq key klist))
(defun add-to-klist (key item klist)
"(ADD-TO-KLIST KEY ITEM KLIST) => new KLIST
Add association (KEY . ITEM) to KLIST."
(setq klist (acons key item klist)))
(defun elt-satisfies-test-p (item elt klist)
"(elt-satisfies-test-p ITEM ELT KLIST) => t or nil
KLIST encodes a keyword-arguments test, as in CH. 14 of CLtL.
True if the given ITEM and ELT satisfy the test."
(let ((test (extract-from-klist klist :test))
(test-not (extract-from-klist klist :test-not))
(keyfn (extract-from-klist klist :key 'identity)))
(cond (test
(funcall test item (funcall keyfn elt)))
(test-not
(not (funcall test-not item (funcall keyfn elt))))
(t ;should never happen
(error "neither :test nor :test-not in `%s'"
(prin1-to-string klist))))))
(defun elt-satisfies-if-p (item klist)
"(elt-satisfies-if-p ITEM KLIST) => t or nil
True if an -if style function was called and ITEM satisfies the
predicate under :predicate in KLIST."
(let ((predicate (extract-from-klist klist :predicate))
(keyfn (extract-from-klist klist :key 'identity)))
(funcall predicate (funcall keyfn item))))
(defun elt-satisfies-if-not-p (item klist)
"(elt-satisfies-if-not-p ITEM KLIST) => t or nil
KLIST encodes a keyword-arguments test, as in CH. 14 of CLtL.
True if an -if-not style function was called and ITEM does not satisfy
the predicate under :predicate in KLIST."
(let ((predicate (extract-from-klist klist :predicate))
(keyfn (extract-from-klist klist :key 'identity)))
(not (funcall predicate (funcall keyfn item)))))
(defun elts-match-under-klist-p (e1 e2 klist)
"(elts-match-under-klist-p E1 E2 KLIST) => t or nil
KLIST encodes a keyword-arguments test, as in CH. 14 of CLtL.
True if elements E1 and E2 match under the tests encoded in KLIST."
(let ((test (extract-from-klist klist :test))
(test-not (extract-from-klist klist :test-not))
(keyfn (extract-from-klist klist :key 'identity)))
(if (and test test-not)
(error "both :test and :test-not in `%s'"
(prin1-to-string klist)))
(cond (test
(funcall test (funcall keyfn e1) (funcall keyfn e2)))
(test-not
(not (funcall test-not (funcall keyfn e1) (funcall keyfn e2))))
(t ;should never happen
(error "neither :test nor :test-not in `%s'"
(prin1-to-string klist))))))
;;; This macro simplifies using keyword args. It is less clumsy than using
;;; the primitives build-klist, etc... For instance, member could be written
;;; this way:
;;; (defun member (item list &rest kargs)
;;; (with-keyword-args kargs (test test-not (key 'identity))
;;; ...))
;;; Suggested by Robert Potter (potter@cs.rochester.edu, 15 Nov 1989)
(defmacro with-keyword-args (keyargslist vardefs &rest body)
"(WITH-KEYWORD-ARGS KEYARGSLIST VARDEFS . BODY)
KEYARGSLIST can be either a symbol or a list of one or two symbols.
In the second case, the second symbol is either T or NIL, indicating whether
keywords other than the mentioned ones are tolerable.
VARDEFS is a list. Each entry is either a VAR (symbol) or matches
\(VAR [DEFAULT [KEYWORD]]). Just giving VAR is the same as giving
\(VAR nil :VAR).
The BODY is executed in an environment where each VAR (a symbol) is bound to
the value present in the KEYARGSLIST provided, or to the DEFAULT. The value
is searched by using the keyword form of VAR (i.e., :VAR) or the optional
keyword if provided.
Notice that this macro doesn't distinguish between a default value given
explicitly by the user and one provided by default. See also the more
primitive functions build-klist, add-to-klist, extract-from-klist,
keyword-argument-supplied-p, elt-satisfies-test-p, elt-satisfies-if-p,
elt-satisfies-if-not-p, elts-match-under-klist-p. They provide more complete,
if clumsier, control over this feature."
(let (allow-other-keys)
(if (listp keyargslist)
(if (> (length keyargslist) 2)
(error
"`%s' should be SYMBOL, (SYMBOL), or (SYMBOL t-OR-nil)"
(prin1-to-string keyargslist))
(setq allow-other-keys (cadr keyargslist)
keyargslist (car keyargslist))
(if (not (and
(symbolp keyargslist)
(memq allow-other-keys '(t nil))))
(error
"first subform should be SYMBOL, (SYMBOL), or (SYMBOL t-OR-nil)"
)))
(if (symbolp keyargslist)
(setq allow-other-keys nil)
(error
"first subform should be SYMBOL, (SYMBOL), or (SYMBOL t-OR-nil)")))
(let (vars defaults keywords forms
(klistname (gensym "KLIST_")))
(mapcar (function (lambda (entry)
(if (symbolp entry) ;defaulty case
(setq entry (list entry nil (keyword-of entry))))
(let* ((l (length entry))
(v (car entry))
(d (cadr entry))
(k (caddr entry)))
(if (or (< l 1) (> l 3))
(error
"`%s' must match (VAR [DEFAULT [KEYWORD]])"
(prin1-to-string entry)))
(if (or (null v) (not (symbolp v)))
(error
"bad variable `%s': must be non-null symbol"
(prin1-to-string v)))
(setq vars (cons v vars))
(setq defaults (cons d defaults))
(if (< l 3)
(setq k (keyword-of v)))
(if (and (= l 3)
(or (null k)
(not (keywordp k))))
(error
"bad keyword `%s'" (prin1-to-string k)))
(setq keywords (cons k keywords))
(setq forms (cons (list v (list 'extract-from-klist
klistname
k
d))
forms)))))
vardefs)
(append
(list 'let* (nconc (list (list klistname
(list 'build-klist keyargslist
(list 'quote keywords)
allow-other-keys)))
(nreverse forms)))
body))))
(put 'with-keyword-args 'lisp-indent-hook 1)
;;; REDUCE
;;; It is here mostly as an example of how to use KLISTs.
;;;
;;; First of all, you need to declare the keywords (done elsewhere in this
;;; file):
;;; (defkeyword :from-end "syntax of sequence functions")
;;; (defkeyword :start "syntax of sequence functions")
;;; etc...
;;;
;;; Then, you capture all the possible keyword arguments with a &rest
;;; argument. You can pass that list downward again, of course, but
;;; internally you need to parse it into a KLIST (an alist, really). One uses
;;; (build-klist REST-ARGS ACCEPTABLE-KEYWORDS [ALLOW-OTHER]). You can then
;;; test for presence by using (keyword-argument-supplied-p KLIST KEY) and
;;; extract a value with (extract-from-klist KLIST KEY [DEFAULT]).
(defun reduce (function sequence &rest kargs)
"Apply FUNCTION (a function of two arguments) to successive pairs of elements
from SEQUENCE. Some keyword arguments are valid after FUNCTION and SEQUENCE:
:from-end If non-nil, process the values backwards
:initial-value If given, prefix it to the SEQUENCE. Suffix, if :from-end
:start Restrict reduction to the subsequence from this index
:end Restrict reduction to the subsequence BEFORE this index.
If the sequence is empty and no :initial-value is given, the FUNCTION is
called on zero (not two) arguments. Otherwise, if there is exactly one
element in the combination of SEQUENCE and the initial value, that element is
returned."
(let* ((klist (build-klist kargs '(:from-end :start :end :initial-value)))
(length (length sequence))
(from-end (extract-from-klist klist :from-end))
(initial-value-given (keyword-argument-supplied-p
klist :initial-value))
(start (extract-from-klist kargs :start 0))
(end (extract-from-klist kargs :end length)))
(setq sequence (cl$subseq-as-list sequence start end))
(if from-end
(setq sequence (reverse sequence)))
(if initial-value-given
(setq sequence (cons (extract-from-klist klist :initial-value)
sequence)))
(if (null sequence)
(funcall function) ;only use of 0 arguments
(let* ((result (car sequence))
(sequence (cdr sequence)))
(while sequence
(setq result (if from-end
(funcall function (car sequence) result)
(funcall function result (car sequence)))
sequence (cdr sequence)))
result))))
(defun cl$subseq-as-list (sequence start end)
"(cl$subseq-as-list SEQUENCE START END) => a list"
(let ((list (append sequence nil))
(length (length sequence))
result)
(if (< start 0)
(error "start should be >= 0, not %d" start))
(if (> end length)
(error "end should be <= %d, not %d" length end))
(if (and (zerop start) (= end length))
list
(let ((i start)
(vector (apply 'vector list)))
(while (/= i end)
(setq result (cons (elt vector i) result))
(setq i (+ i 1)))
(nreverse result)))))
;;;; end of cl-sequences.el
;;;; Some functions with keyword arguments
;;;;
;;;; Both list and sequence functions are considered here together. This
;;;; doesn't fit any more with the original split of functions in files.
(defun cl-member (item list &rest kargs)
"Look for ITEM in LIST; return first tail of LIST the car of whose first
cons cell tests the same as ITEM. Admits arguments :key, :test, and
:test-not."
(if (null kargs) ;treat this fast for efficiency
(memq item list)
(let* ((klist (build-klist kargs '(:test :test-not :key)))
(test (extract-from-klist klist :test))
(testnot (extract-from-klist klist :test-not))
(key (extract-from-klist klist :key 'identity)))
;; another workaround allegedly for speed, BLAH
(if (and (or (eq test 'eq) (eq test 'eql)
(eq test (symbol-function 'eq))
(eq test (symbol-function 'eql)))
(null testnot)
(or (eq key 'identity) ;either by default or so given
(eq key (function identity)) ;could this happen?
(eq key (symbol-function 'identity)) ;sheer paranoia
))
(memq item list)
(if (and test testnot)
(error ":test and :test-not both specified for member"))
(if (not (or test testnot))
(setq test 'eql))
;; final hack: remove the indirection through the function names
(if testnot
(if (symbolp testnot)
(setq testnot (symbol-function testnot)))
(if (symbolp test)
(setq test (symbol-function test))))
(if (symbolp key)
(setq key (symbol-function key)))
;; ok, go for it
(let ((ptr list)
(done nil)
(result '()))
(if testnot
(while (not (or done (endp ptr)))
(cond ((not (funcall testnot item (funcall key (car ptr))))
(setq done t)
(setq result ptr)))
(setq ptr (cdr ptr)))
(while (not (or done (endp ptr)))
(cond ((funcall test item (funcall key (car ptr)))
(setq done t)
(setq result ptr)))
(setq ptr (cdr ptr))))
result)))))
;;;; MULTIPLE VALUES
;;;; This package approximates the behavior of the multiple-values
;;;; forms of Common Lisp.
;;;;
;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986
;;;; (quiroz@cs.rochester.edu)
;;; Lisp indentation information
(put 'multiple-value-bind 'lisp-indent-hook 2)
(put 'multiple-value-setq 'lisp-indent-hook 2)
(put 'multiple-value-list 'lisp-indent-hook nil)
(put 'multiple-value-call 'lisp-indent-hook 1)
(put 'multiple-value-prog1 'lisp-indent-hook 1)
;;; Global state of the package is kept here
(defvar *mvalues-values* nil
"Most recently returned multiple-values")
(defvar *mvalues-count* nil
"Count of multiple-values returned, or nil if the mechanism was not used")
;;; values is the standard multiple-value-return form. Must be the
;;; last thing evaluated inside a function. If the caller is not
;;; expecting multiple values, only the first one is passed. (values)
;;; is the same as no-values returned (unaware callers see nil). The
;;; alternative (values-list <list>) is just a convenient shorthand
;;; and complements multiple-value-list.
(defun values (&rest val-forms)
"Produce multiple values (zero or more). Each arg is one value.
See also `multiple-value-bind', which is one way to examine the
multiple values produced by a form. If the containing form or caller
does not check specially to see multiple values, it will see only
the first value."
(setq *mvalues-values* val-forms)
(setq *mvalues-count* (length *mvalues-values*))
(car *mvalues-values*))
(defun values-list (&optional val-forms)
"Produce multiple values (zero or more). Each element of LIST is one value.
This is equivalent to (apply 'values LIST)."
(cond ((nlistp val-forms)
(error "Argument to values-list must be a list, not `%s'"
(prin1-to-string val-forms))))
(setq *mvalues-values* val-forms)
(setq *mvalues-count* (length *mvalues-values*))
(car *mvalues-values*))
;;; Callers that want to see the multiple values use these macros.
(defmacro multiple-value-list (form)
"Execute FORM and return a list of all the (multiple) values FORM produces.
See `values' and `multiple-value-bind'."
(list 'progn
(list 'setq '*mvalues-count* nil)
(list 'let (list (list 'it '(gensym)))
(list 'set 'it form)
(list 'if '*mvalues-count*
(list 'copy-sequence '*mvalues-values*)
(list 'progn
(list 'setq '*mvalues-count* 1)
(list 'setq '*mvalues-values*
(list 'list (list 'symbol-value 'it)))
(list 'copy-sequence '*mvalues-values*))))))
(defmacro multiple-value-call (function &rest args)
"Call FUNCTION on all the values produced by the remaining arguments.
(multiple-value-call '+ (values 1 2) (values 3 4)) is 10."
(let* ((result (gentemp))
(arg (gentemp)))
(list 'apply (list 'function (eval function))
(list 'let* (list (list result '()))
(list 'dolist (list arg (list 'quote args) result)
(list 'setq result
(list 'append
result
(list 'multiple-value-list
(list 'eval arg)))))))))
(defmacro multiple-value-bind (vars form &rest body)
"Bind VARS to the (multiple) values produced by FORM, then do BODY.
VARS is a list of variables; each is bound to one of FORM's values.
If FORM doesn't make enough values, the extra variables are bound to nil.
(Ordinary forms produce only one value; to produce more, use `values'.)
Extra values are ignored.
BODY (zero or more forms) is executed with the variables bound,
then the bindings are unwound."
(let* ((vals (gentemp)) ;name for intermediate values
(clauses (mv-bind-clausify ;convert into clauses usable
vars vals))) ; in a let form
(list* 'let*
(cons (list vals (list 'multiple-value-list form))
clauses)
body)))
(defmacro multiple-value-setq (vars form)
"Set VARS to the (multiple) values produced by FORM.
VARS is a list of variables; each is set to one of FORM's values.
If FORM doesn't make enough values, the extra variables are set to nil.
(Ordinary forms produce only one value; to produce more, use `values'.)
Extra values are ignored."
(let* ((vals (gentemp)) ;name for intermediate values
(clauses (mv-bind-clausify ;convert into clauses usable
vars vals))) ; in a setq (after append).
(list 'let*
(list (list vals (list 'multiple-value-list form)))
(cons 'setq (apply (function append) clauses)))))
(defmacro multiple-value-prog1 (form &rest body)
"Evaluate FORM, then BODY, then produce the same values FORM produced.
Thus, (multiple-value-prog1 (values 1 2) (foobar)) produces values 1 and 2.
This is like `prog1' except that `prog1' would produce only one value,
which would be the first of FORM's values."
(let* ((heldvalues (gentemp)))
(cons 'let*
(cons (list (list heldvalues (list 'multiple-value-list form)))
(append body (list (list 'values-list heldvalues)))))))
;;; utility functions
;;;
;;; mv-bind-clausify makes the pairs needed to have the variables in
;;; the variable list correspond with the values returned by the form.
;;; vals is a fresh symbol that intervenes in all the bindings.
(defun mv-bind-clausify (vars vals)
"MV-BIND-CLAUSIFY VARS VALS => Auxiliary list
Forms a list of pairs `(,(nth i vars) (nth i vals)) for i from 0 to
the length of VARS (a list of symbols). VALS is just a fresh symbol."
(if (or (nlistp vars)
(notevery 'symbolp vars))
(error "expected a list of symbols, not `%s'"
(prin1-to-string vars)))
(let* ((nvars (length vars))
(clauses '()))
(dotimes (n nvars clauses)
(setq clauses (cons (list (nth n vars)
(list 'nth n vals)) clauses)))))
;;;; end of cl-multiple-values.el
;;;; ARITH
;;;; This file provides integer arithmetic extensions. Although
;;;; Emacs Lisp doesn't really support anything but integers, that
;;;; has still to be made to look more or less standard.
;;;;
;;;;
;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986
;;;; (quiroz@cs.rochester.edu)
(defsubst plusp (number)
"True if NUMBER is strictly greater than zero."
(> number 0))
(defsubst minusp (number)
"True if NUMBER is strictly less than zero."
(< number 0))
(defsubst oddp (number)
"True if INTEGER is not divisible by 2."
(/= (% number 2) 0))
(defsubst evenp (number)
"True if INTEGER is divisible by 2."
(= (% number 2) 0))
(defsubst abs (number)
"Return the absolute value of NUMBER."
(if (< number 0)
(- number)
number))
(defsubst signum (number)
"Return -1, 0 or 1 according to the sign of NUMBER."
(cond ((< number 0)
-1)
((> number 0)
1)
(t ;exactly zero
0)))
(defun gcd (&rest integers)
"Return the greatest common divisor of all the arguments.
The arguments must be integers. With no arguments, value is zero."
(let ((howmany (length integers)))
(cond ((= howmany 0)
0)
((= howmany 1)
(abs (car integers)))
((> howmany 2)
(apply (function gcd)
(cons (gcd (nth 0 integers) (nth 1 integers))
(nthcdr 2 integers))))
(t ;howmany=2
;; essentially the euclidean algorithm
(when (zerop (* (nth 0 integers) (nth 1 integers)))
(error "a zero argument is invalid for `gcd'"))
(do* ((absa (abs (nth 0 integers))) ; better to operate only
(absb (abs (nth 1 integers))) ;on positives.
(dd (max absa absb)) ; setup correct order for the
(ds (min absa absb)) ;successive divisions.
;; intermediate results
(q 0)
(r 0)
;; final results
(done nil) ; flag: end of iterations
(result 0)) ; final value
(done result)
(setq q (/ dd ds))
(setq r (% dd ds))
(cond ((zerop r) (setq done t) (setq result ds))
(t (setq dd ds) (setq ds r))))))))
(defun lcm (integer &rest more)
"Return the least common multiple of all the arguments.
The arguments must be integers and there must be at least one of them."
(let ((howmany (length more))
(a integer)
(b (nth 0 more))
prod ; intermediate product
(yetmore (nthcdr 1 more)))
(cond ((zerop howmany)
(abs a))
((> howmany 1) ; recursive case
(apply (function lcm)
(cons (lcm a b) yetmore)))
(t ; base case, just 2 args
(setq prod (* a b))
(cond
((zerop prod)
0)
(t
(/ (abs prod) (gcd a b))))))))
(defun isqrt (number)
"Return the integer square root of NUMBER.
NUMBER must not be negative. Result is largest integer less than or
equal to the real square root of the argument."
;; The method used here is essentially the Newtonian iteration
;; x[n+1] <- (x[n] + Number/x[n]) / 2
;; suitably adapted to integer arithmetic.
;; Thanks to Philippe Schnoebelen <phs@lifia.imag.fr> for suggesting the
;; termination condition.
(cond ((minusp number)
(error "argument to `isqrt' (%d) must not be negative"
number))
((zerop number)
0)
(t ;so (>= number 0)
(do* ((approx 1) ;any positive integer will do
(new 0) ;init value irrelevant
(done nil))
(done (if (> (* approx approx) number)
(- approx 1)
approx))
(setq new (/ (+ approx (/ number approx)) 2)
done (or (= new approx) (= new (+ approx 1)))
approx new)))))
(defun cl-floor (number &optional divisor)
"Divide DIVIDEND by DIVISOR, rounding toward minus infinity.
DIVISOR defaults to 1. The remainder is produced as a second value."
(cond ((and (null divisor) ; trivial case
(numberp number))
(values number 0))
(t ; do the division
(multiple-value-bind
(q r s)
(safe-idiv number divisor)
(cond ((zerop s)
(values 0 0))
((plusp s)
(values q r))
(t ;opposite-signs case
(if (zerop r)
(values (- q) 0)
(let ((q (- (+ q 1))))
(values q (- number (* q divisor)))))))))))
(defun cl-ceiling (number &optional divisor)
"Divide DIVIDEND by DIVISOR, rounding toward plus infinity.
DIVISOR defaults to 1. The remainder is produced as a second value."
(cond ((and (null divisor) ; trivial case
(numberp number))
(values number 0))
(t ; do the division
(multiple-value-bind
(q r s)
(safe-idiv number divisor)
(cond ((zerop s)
(values 0 0))
((plusp s)
(values (+ q 1) (- r divisor)))
(t
(values (- q) (+ number (* q divisor)))))))))
(defun cl-truncate (number &optional divisor)
"Divide DIVIDEND by DIVISOR, rounding toward zero.
DIVISOR defaults to 1. The remainder is produced as a second value."
(cond ((and (null divisor) ; trivial case
(numberp number))
(values number 0))
(t ; do the division
(multiple-value-bind
(q r s)
(safe-idiv number divisor)
(cond ((zerop s)
(values 0 0))
((plusp s) ;same as floor
(values q r))
(t ;same as ceiling
(values (- q) (+ number (* q divisor)))))))))
(defun cl-round (number &optional divisor)
"Divide DIVIDEND by DIVISOR, rounding to nearest integer.
DIVISOR defaults to 1. The remainder is produced as a second value."
(cond ((and (null divisor) ; trivial case
(numberp number))
(values number 0))
(t ; do the division
(multiple-value-bind
(q r s)
(safe-idiv number divisor)
(setq r (abs r))
;; adjust magnitudes first, and then signs
(let ((other-r (- (abs divisor) r)))
(cond ((> r other-r)
(setq q (+ q 1)))
((and (= r other-r)
(oddp q))
;; round to even is mandatory
(setq q (+ q 1))))
(setq q (* s q))
(setq r (- number (* q divisor)))
(values q r))))))
;;; These two functions access the implementation-dependent representation of
;;; the multiple value returns.
(defun cl-mod (number divisor)
"Return remainder of X by Y (rounding quotient toward minus infinity).
That is, the remainder goes with the quotient produced by `cl-floor'.
Emacs Lisp hint:
If you know that both arguments are positive, use `%' instead for speed."
(cl-floor number divisor)
(cadr *mvalues-values*))
(defun rem (number divisor)
"Return remainder of X by Y (rounding quotient toward zero).
That is, the remainder goes with the quotient produced by `cl-truncate'.
Emacs Lisp hint:
If you know that both arguments are positive, use `%' instead for speed."
(cl-truncate number divisor)
(cadr *mvalues-values*))
;;; internal utilities
;;;
;;; safe-idiv performs an integer division with positive numbers only.
;;; It is known that some machines/compilers implement weird remainder
;;; computations when working with negatives, so the idea here is to
;;; make sure we know what is coming back to the caller in all cases.
;;; Signum computation fixed by mad@math.keio.JUNET (MAEDA Atusi)
(defun safe-idiv (a b)
"SAFE-IDIV A B => Q R S
Q=|A|/|B|, S is the sign of A/B, R is the rest A - S*Q*B."
;; (unless (and (numberp a) (numberp b))
;; (error "arguments to `safe-idiv' must be numbers"))
;; (when (zerop b)
;; (error "cannot divide %d by zero" a))
(let* ((q (/ (abs a) (abs b)))
(s (* (signum a) (signum b)))
(r (- a (* s q b))))
(values q r s)))
;;;; end of cl-arith.el
;;;; SETF
;;;; This file provides the setf macro and friends. The purpose has
;;;; been modest, only the simplest defsetf forms are accepted.
;;;; Use it and enjoy.
;;;;
;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986
;;;; (quiroz@cs.rochester.edu)
(defkeyword :setf-update-fn
"Property, its value is the function setf must invoke to update a
generalized variable whose access form is a function call of the
symbol that has this property.")
(defkeyword :setf-update-doc
"Property of symbols that have a `defsetf' update function on them,
installed by the `defsetf' from its optional third argument.")
(defmacro setf (&rest pairs)
"Generalized `setq' that can set things other than variable values.
A use of `setf' looks like (setf {PLACE VALUE}...).
The behavior of (setf PLACE VALUE) is to access the generalized variable
at PLACE and store VALUE there. It returns VALUE. If there is more
than one PLACE and VALUE, each PLACE is set from its VALUE before
the next PLACE is evaluated."
(let ((nforms (length pairs)))
;; check the number of subforms
(cond ((/= (% nforms 2) 0)
(error "odd number of arguments to `setf'"))
((= nforms 0)
nil)
((> nforms 2)
;; this is the recursive case
(cons 'progn
(do* ;collect the place-value pairs
((args pairs (cddr args))
(place (car args) (car args))
(value (cadr args) (cadr args))
(result '()))
((endp args) (nreverse result))
(setq result
(cons (list 'setf place value)
result)))))
(t ;i.e., nforms=2
;; this is the base case (SETF PLACE VALUE)
(let* ((place (car pairs))
(value (cadr pairs))
(head nil)
(updatefn nil))
;; dispatch on the type of the PLACE
(cond ((symbolp place)
(list 'setq place value))
((and (listp place)
(setq head (car place))
(symbolp head)
(setq updatefn (get head :setf-update-fn)))
;; dispatch on the type of update function
(cond ((and (consp updatefn) (eq (car updatefn) 'lambda))
(cons 'funcall
(cons (list 'function updatefn)
(append (cdr place) (list value)))))
((and (symbolp updatefn)
(fboundp updatefn)
(let ((defn (symbol-function updatefn)))
(or (subrp defn)
(and (consp defn)
(or (eq (car defn) 'lambda)
(eq (car defn) 'macro))))))
(cons updatefn (append (cdr place) (list value))))
(t
(multiple-value-bind
(bindings newsyms)
(pair-with-newsyms
(append (cdr place) (list value)))
;; this let gets new symbols to ensure adequate
;; order of evaluation of the subforms.
(list 'let
bindings
(cons updatefn newsyms))))))
(t
(error "no `setf' update-function for `%s'"
(prin1-to-string place)))))))))
(defmacro defsetf (accessfn updatefn &optional docstring)
"Define how `setf' works on a certain kind of generalized variable.
A use of `defsetf' looks like (defsetf ACCESSFN UPDATEFN [DOCSTRING]).
ACCESSFN is a symbol. UPDATEFN is a function or macro which takes
one more argument than ACCESSFN does. DEFSETF defines the translation
of (SETF (ACCESFN . ARGS) NEWVAL) to be a form like (UPDATEFN ARGS... NEWVAL).
The function UPDATEFN must return its last arg, after performing the
updating called for."
;; reject ill-formed requests. too bad one can't test for functionp
;; or macrop.
(when (not (symbolp accessfn))
(error "first argument of `defsetf' must be a symbol, not `%s'"
(prin1-to-string accessfn)))
;; update properties
(list 'progn
(list 'eval-and-compile
(list 'put (list 'quote accessfn)
:setf-update-fn (list 'function updatefn)))
(list 'put (list 'quote accessfn) :setf-update-doc docstring)
;; any better thing to return?
(list 'quote accessfn)))
;;; This section provides the "default" setfs for Common-Emacs-Lisp
;;; The user will not normally add anything to this, although
;;; defstruct will introduce new ones as a matter of fact.
;;;
;;; Apply is a special case. The Common Lisp
;;; standard makes the case of apply be useful when the user writes
;;; something like (apply #'name ...), Emacs Lisp doesn't have the #
;;; stuff, but it has (function ...). Notice that V18 includes a new
;;; apply: this file is compatible with V18 and pre-V18 Emacses.
;;; INCOMPATIBILITY: the SETF macro evaluates its arguments in the
;;; (correct) left to right sequence *before* checking for apply
;;; methods (which should really be an special case inside setf). Due
;;; to this, the lambda expression defsetf'd to apply will succeed in
;;; applying the right function even if the name was not quoted, but
;;; computed! That extension is not Common Lisp (nor is particularly
;;; useful, I think).
(defsetf apply
(lambda (&rest args)
;; disassemble the calling form
;; "(((quote fn) x1 x2 ... xn) val)" (function instead of quote, too)
(let* ((fnform (car args)) ;functional form
(applyargs (append ;arguments "to apply fnform"
(apply 'list* (butlast (cdr args)))
(last args)))
(newupdater nil)) ; its update-fn, if any
(if (and (symbolp fnform)
(setq newupdater (get fnform :setf-update-fn)))
(apply newupdater applyargs)
(error "can't `setf' to `%s'"
(prin1-to-string fnform)))))
"`apply' is a special case for `setf'")
(defsetf aref
aset
"`setf' inversion for `aref'")
(defsetf nth
setnth
"`setf' inversion for `nth'")
(defsetf nthcdr
setnthcdr
"`setf' inversion for `nthcdr'")
(defsetf elt
setelt
"`setf' inversion for `elt'")
(defsetf first
(lambda (list val) (setnth 0 list val))
"`setf' inversion for `first'")
(defsetf second
(lambda (list val) (setnth 1 list val))
"`setf' inversion for `second'")
(defsetf third
(lambda (list val) (setnth 2 list val))
"`setf' inversion for `third'")
(defsetf fourth
(lambda (list val) (setnth 3 list val))
"`setf' inversion for `fourth'")
(defsetf fifth
(lambda (list val) (setnth 4 list val))
"`setf' inversion for `fifth'")
(defsetf sixth
(lambda (list val) (setnth 5 list val))
"`setf' inversion for `sixth'")
(defsetf seventh
(lambda (list val) (setnth 6 list val))
"`setf' inversion for `seventh'")
(defsetf eighth
(lambda (list val) (setnth 7 list val))
"`setf' inversion for `eighth'")
(defsetf ninth
(lambda (list val) (setnth 8 list val))
"`setf' inversion for `ninth'")
(defsetf tenth
(lambda (list val) (setnth 9 list val))
"`setf' inversion for `tenth'")
(defsetf rest
(lambda (list val) (setcdr list val))
"`setf' inversion for `rest'")
(defsetf car setcar "Replace the car of a cons")
(defsetf cdr setcdr "Replace the cdr of a cons")
(defsetf caar
(lambda (list val) (setcar (nth 0 list) val))
"`setf' inversion for `caar'")
(defsetf cadr
(lambda (list val) (setcar (cdr list) val))
"`setf' inversion for `cadr'")
(defsetf cdar
(lambda (list val) (setcdr (car list) val))
"`setf' inversion for `cdar'")
(defsetf cddr
(lambda (list val) (setcdr (cdr list) val))
"`setf' inversion for `cddr'")
(defsetf caaar
(lambda (list val) (setcar (caar list) val))
"`setf' inversion for `caaar'")
(defsetf caadr
(lambda (list val) (setcar (cadr list) val))
"`setf' inversion for `caadr'")
(defsetf cadar
(lambda (list val) (setcar (cdar list) val))
"`setf' inversion for `cadar'")
(defsetf cdaar
(lambda (list val) (setcdr (caar list) val))
"`setf' inversion for `cdaar'")
(defsetf caddr
(lambda (list val) (setcar (cddr list) val))
"`setf' inversion for `caddr'")
(defsetf cdadr
(lambda (list val) (setcdr (cadr list) val))
"`setf' inversion for `cdadr'")
(defsetf cddar
(lambda (list val) (setcdr (cdar list) val))
"`setf' inversion for `cddar'")
(defsetf cdddr
(lambda (list val) (setcdr (cddr list) val))
"`setf' inversion for `cdddr'")
(defsetf caaaar
(lambda (list val) (setcar (caaar list) val))
"`setf' inversion for `caaaar'")
(defsetf caaadr
(lambda (list val) (setcar (caadr list) val))
"`setf' inversion for `caaadr'")
(defsetf caadar
(lambda (list val) (setcar (cadar list) val))
"`setf' inversion for `caadar'")
(defsetf cadaar
(lambda (list val) (setcar (cdaar list) val))
"`setf' inversion for `cadaar'")
(defsetf cdaaar
(lambda (list val) (setcdr (caar list) val))
"`setf' inversion for `cdaaar'")
(defsetf caaddr
(lambda (list val) (setcar (caddr list) val))
"`setf' inversion for `caaddr'")
(defsetf cadadr
(lambda (list val) (setcar (cdadr list) val))
"`setf' inversion for `cadadr'")
(defsetf cdaadr
(lambda (list val) (setcdr (caadr list) val))
"`setf' inversion for `cdaadr'")
(defsetf caddar
(lambda (list val) (setcar (cddar list) val))
"`setf' inversion for `caddar'")
(defsetf cdadar
(lambda (list val) (setcdr (cadar list) val))
"`setf' inversion for `cdadar'")
(defsetf cddaar
(lambda (list val) (setcdr (cdaar list) val))
"`setf' inversion for `cddaar'")
(defsetf cadddr
(lambda (list val) (setcar (cdddr list) val))
"`setf' inversion for `cadddr'")
(defsetf cddadr
(lambda (list val) (setcdr (cdadr list) val))
"`setf' inversion for `cddadr'")
(defsetf cdaddr
(lambda (list val) (setcdr (caddr list) val))
"`setf' inversion for `cdaddr'")
(defsetf cdddar
(lambda (list val) (setcdr (cddar list) val))
"`setf' inversion for `cdddar'")
(defsetf cddddr
(lambda (list val) (setcdr (cddr list) val))
"`setf' inversion for `cddddr'")
(defsetf get put "`setf' inversion for `get' is `put'")
(defsetf symbol-function fset
"`setf' inversion for `symbol-function' is `fset'")
(defsetf symbol-plist setplist
"`setf' inversion for `symbol-plist' is `setplist'")
(defsetf symbol-value set
"`setf' inversion for `symbol-value' is `set'")
(defsetf point goto-char
"To set (point) to N, use (goto-char N)")
;; how about defsetfing other Emacs forms?
;;; Modify macros
;;;
;;; It could be nice to implement define-modify-macro, but I don't
;;; think it really pays.
(defmacro incf (ref &optional delta)
"(incf REF [DELTA]) -> increment the g.v. REF by DELTA (default 1)"
(if (null delta)
(setq delta 1))
(list 'setf ref (list '+ ref delta)))
(defmacro decf (ref &optional delta)
"(decf REF [DELTA]) -> decrement the g.v. REF by DELTA (default 1)"
(if (null delta)
(setq delta 1))
(list 'setf ref (list '- ref delta)))
(defmacro push (item ref)
"(push ITEM REF) -> cons ITEM at the head of the g.v. REF (a list)"
(list 'setf ref (list 'cons item ref)))
(defmacro pushnew (item ref)
"(pushnew ITEM REF): adjoin ITEM at the head of the g.v. REF (a list)"
(list 'setf ref (list 'adjoin item ref)))
(defmacro pop (ref)
"(pop REF) -> (prog1 (car REF) (setf REF (cdr REF)))"
(let ((listname (gensym)))
(list 'let (list (list listname ref))
(list 'prog1
(list 'car listname)
(list 'setf ref (list 'cdr listname))))))
;;; PSETF
;;;
;;; Psetf is the generalized variable equivalent of psetq. The right
;;; hand sides are evaluated and assigned (via setf) to the left hand
;;; sides. The evaluations are done in an environment where they
;;; appear to occur in parallel.
(defmacro psetf (&rest body)
"(psetf {var value }...) => nil
Like setf, but all the values are computed before any assignment is made."
(let ((length (length body)))
(cond ((/= (% length 2) 0)
(error "psetf needs an even number of arguments, %d given"
length))
((null body)
'())
(t
(list 'prog1 nil
(let ((setfs '())
(bodyforms (reverse body)))
(while bodyforms
(let* ((value (car bodyforms))
(place (cadr bodyforms)))
(setq bodyforms (cddr bodyforms))
(if (null setfs)
(setq setfs (list 'setf place value))
(setq setfs (list 'setf place
(list 'prog1 value
setfs))))))
setfs))))))
;;; SHIFTF and ROTATEF
;;;
(defmacro shiftf (&rest forms)
"(shiftf PLACE1 PLACE2... NEWVALUE)
Set PLACE1 to PLACE2, PLACE2 to PLACE3...
Each PLACE is set to the old value of the following PLACE,
and the last PLACE is set to the value NEWVALUE.
Returns the old value of PLACE1."
(unless (> (length forms) 1)
(error "`shiftf' needs more than one argument"))
(let ((places (butlast forms))
(newvalue (car (last forms))))
;; the places are accessed to fresh symbols
(multiple-value-bind
(bindings newsyms)
(pair-with-newsyms places)
(list 'let bindings
(cons 'setf
(zip-lists places
(append (cdr newsyms) (list newvalue))))
(car newsyms)))))
(defmacro rotatef (&rest places)
"(rotatef PLACE...) sets each PLACE to the old value of the following PLACE.
The last PLACE is set to the old value of the first PLACE.
Thus, the values rotate through the PLACEs. Returns nil."
(if (null places)
nil
(multiple-value-bind
(bindings newsyms)
(pair-with-newsyms places)
(list
'let bindings
(cons 'setf
(zip-lists places
(append (cdr newsyms) (list (car newsyms)))))
nil))))
;;; GETF, REMF, and REMPROP
;;;
(defun getf (place indicator &optional default)
"Return PLACE's PROPNAME property, or DEFAULT if not present."
(while (and place (not (eq (car place) indicator)))
(setq place (cdr (cdr place))))
(if place
(car (cdr place))
default))
(defmacro getf$setf$method (place indicator default &rest newval)
"SETF method for GETF. Not for public use."
(case (length newval)
(0 (setq newval default default nil))
(1 (setq newval (car newval)))
(t (error "Wrong number of arguments to (setf (getf ...)) form")))
(let ((psym (gentemp)) (isym (gentemp)) (vsym (gentemp)))
(list 'let (list (list psym place)
(list isym indicator)
(list vsym newval))
(list 'while
(list 'and psym
(list 'not
(list 'eq (list 'car psym) isym)))
(list 'setq psym (list 'cdr (list 'cdr psym))))
(list 'if psym
(list 'setcar (list 'cdr psym) vsym)
(list 'setf place
(list 'nconc place (list 'list isym newval))))
vsym)))
(defsetf getf
getf$setf$method)
(defmacro remf (place indicator)
"Remove from the property list at PLACE its PROPNAME property.
Returns non-nil if and only if the property existed."
(let ((psym (gentemp)) (isym (gentemp)))
(list 'let (list (list psym place) (list isym indicator))
(list 'cond
(list (list 'eq isym (list 'car psym))
(list 'setf place (list 'cdr (list 'cdr psym)))
t)
(list t
(list 'setq psym (list 'cdr psym))
(list 'while
(list 'and (list 'cdr psym)
(list 'not
(list 'eq (list 'car (list 'cdr psym))
isym)))
(list 'setq psym (list 'cdr (list 'cdr psym))))
(list 'cond
(list (list 'cdr psym)
(list 'setcdr psym
(list 'cdr
(list 'cdr (list 'cdr psym))))
t)))))))
(defun remprop (symbol indicator)
"Remove SYMBOL's PROPNAME property, returning non-nil if it was present."
(remf (symbol-plist symbol) indicator))
;;;; STRUCTS
;;;; This file provides the structures mechanism. See the
;;;; documentation for Common-Lisp's defstruct. Mine doesn't
;;;; implement all the functionality of the standard, although some
;;;; more could be grafted if so desired. More details along with
;;;; the code.
;;;;
;;;;
;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986
;;;; (quiroz@cs.rochester.edu)
(defkeyword :include "Syntax of `defstruct'")
(defkeyword :named "Syntax of `defstruct'")
(defkeyword :conc-name "Syntax of `defstruct'")
(defkeyword :copier "Syntax of `defstruct'")
(defkeyword :predicate "Syntax of `defstruct'")
(defkeyword :print-function "Syntax of `defstruct'")
(defkeyword :type "Syntax of `defstruct'")
(defkeyword :initial-offset "Syntax of `defstruct'")
(defkeyword :structure-doc "Documentation string for a structure.")
(defkeyword :structure-slotsn "Number of slots in structure")
(defkeyword :structure-slots "List of the slot's names")
(defkeyword :structure-indices "List of (KEYWORD-NAME . INDEX)")
(defkeyword :structure-initforms "List of (KEYWORD-NAME . INITFORM)")
(defkeyword :structure-includes
"() or list of a symbol, that this struct includes")
(defkeyword :structure-included-in
"List of the structs that include this")
(defmacro defstruct (&rest args)
"(defstruct NAME [DOC-STRING] . SLOTS) define NAME as structure type.
NAME must be a symbol, the name of the new structure. It could also
be a list (NAME . OPTIONS).
Each option is either a symbol, or a list of a keyword symbol taken from the
list \{:conc-name, :copier, :constructor, :predicate, :include,
:print-function, :type, :initial-offset\}. The meanings of these are as in
CLtL, except that no BOA-constructors are provided, and the options
\{:print-function, :type, :initial-offset\} are ignored quietly. All these
structs are named, in the sense that their names can be used for type
discrimination.
The DOC-STRING is established as the `structure-doc' property of NAME.
The SLOTS are one or more of the following:
SYMBOL -- meaning the SYMBOL is the name of a SLOT of NAME
list of SYMBOL and VALUE -- meaning that VALUE is the initial value of
the slot.
`defstruct' defines functions `make-NAME', `NAME-p', `copy-NAME' for the
structure, and functions with the same name as the slots to access
them. `setf' of the accessors sets their values."
(multiple-value-bind
(name options docstring slotsn slots initlist)
(parse$defstruct$args args)
;; Names for the member functions come from the options. The
;; slots* stuff collects info about the slots declared explicitly.
(multiple-value-bind
(conc-name constructor copier predicate
moreslotsn moreslots moreinits included)
(parse$defstruct$options name options slots)
;; The moreslots* stuff refers to slots gained as a consequence
;; of (:include clauses). -- Oct 89: Only one :include tolerated
(when (and (numberp moreslotsn)
(> moreslotsn 0))
(setf slotsn (+ slotsn moreslotsn))
(setf slots (append moreslots slots))
(setf initlist (append moreinits initlist)))
(unless (> slotsn 0)
(error "%s needs at least one slot"
(prin1-to-string name)))
(let ((dups (duplicate-symbols-p slots)))
(when dups
(error "`%s' are duplicates"
(prin1-to-string dups))))
(setq initlist (simplify$inits slots initlist))
(let (properties functions keywords accessors alterators returned)
;; compute properties of NAME
(setq properties
(append
(list
(list 'put (list 'quote name) :structure-doc
docstring)
(list 'put (list 'quote name) :structure-slotsn
slotsn)
(list 'put (list 'quote name) :structure-slots
(list 'quote slots))
(list 'put (list 'quote name) :structure-initforms
(list 'quote initlist))
(list 'put (list 'quote name) :structure-indices
(list 'quote (extract$indices initlist))))
;; If this definition :includes another defstruct,
;; modify both property lists.
(cond (included
(list
(list 'put
(list 'quote name)
:structure-includes
(list 'quote included))
(list 'pushnew
(list 'quote name)
(list 'get (list 'quote (car included))
:structure-included-in))))
(t
(list
(let ((old (gensym)))
(list 'let
(list (list old
(list 'car
(list 'get
(list 'quote name)
:structure-includes))))
(list 'when old
(list 'put
old
:structure-included-in
(list 'delq
(list 'quote name)
;; careful with destructive
;;manipulation!
(list
'append
(list
'get
old
:structure-included-in)
'())
)))))
(list 'put
(list 'quote name)
:structure-includes
'()))))
;; If this definition used to be :included in another, warn
;; that things make break. On the other hand, the redefinition
;; may be trivial, so don't call it an error.
(let ((old (gensym)))
(list
(list 'let
(list (list old (list 'get
(list 'quote name)
:structure-included-in)))
(list 'when old
(list 'message
"`%s' redefined. Should redefine `%s'?"
(list 'quote name)
(list 'prin1-to-string old))))))))
;; Compute functions associated with NAME. This is not
;; handling BOA constructors yet, but here would be the place.
(setq functions
(list
(list 'fset (list 'quote constructor)
(list 'function
(list 'lambda (list '&rest 'args)
(list 'make$structure$instance
(list 'quote name)
'args))))
(list 'fset (list 'quote copier)
(list 'function 'copy-sequence))
(let ((typetag (gensym)))
(list 'fset (list 'quote predicate)
(list
'function
(list
'lambda (list 'thing)
(list 'and
(list 'vectorp 'thing)
(list 'let
(list (list typetag
(list 'elt 'thing 0)))
(list 'or
(list
'and
(list 'eq
typetag
(list 'quote name))
(list '=
(list 'length 'thing)
(1+ slotsn)))
(list
'memq
typetag
(list 'get
(list 'quote name)
:structure-included-in))))))
)))))
;; compute accessors for NAME's slots
(multiple-value-setq
(accessors alterators keywords)
(build$accessors$for name conc-name predicate slots slotsn))
;; generate returned value -- not defined by the standard
(setq returned
(list
(cons 'vector
(mapcar
(function (lambda (x) (list 'quote x)))
(cons name slots)))))
;; generate code
(cons 'progn
(nconc properties functions keywords
accessors alterators returned))))))
(defun parse$defstruct$args (args)
"(parse$defstruct$args ARGS) => NAME OPTIONS DOCSTRING SLOTSN SLOTS INITLIST
NAME=symbol, OPTIONS=list of, DOCSTRING=string, SLOTSN=count of slots,
SLOTS=list of their names, INITLIST=alist (keyword . initform)."
(let (name ;args=(symbol...) or ((symbol...)...)
options ;args=((symbol . options) ...)
(docstring "") ;args=(head docstring . slotargs)
slotargs ;second or third cdr of args
(slotsn 0) ;number of slots
(slots '()) ;list of slot names
(initlist '())) ;list of (slot keyword . initform)
;; extract name and options
(cond ((symbolp (car args)) ;simple name
(setq name (car args)
options '()))
((and (listp (car args)) ;(name . options)
(symbolp (caar args)))
(setq name (caar args)
options (cdar args)))
(t
(error "first arg to `defstruct' must be symbol or (symbol ...)")))
(setq slotargs (cdr args))
;; is there a docstring?
(when (stringp (car slotargs))
(setq docstring (car slotargs)
slotargs (cdr slotargs)))
;; now for the slots
(multiple-value-bind
(slotsn slots initlist)
(process$slots slotargs)
(values name options docstring slotsn slots initlist))))
(defun process$slots (slots)
"(process$slots SLOTS) => SLOTSN SLOTSLIST INITLIST
Converts a list of symbols or lists of symbol and form into the last 3
values returned by PARSE$DEFSTRUCT$ARGS."
(let ((slotsn (length slots)) ;number of slots
slotslist ;(slot1 slot2 ...)
initlist) ;((:slot1 . init1) ...)
(do*
((ptr slots (cdr ptr))
(this (car ptr) (car ptr)))
((endp ptr))
(cond ((symbolp this)
(setq slotslist (cons this slotslist))
(setq initlist (acons (keyword-of this) nil initlist)))
((and (listp this)
(symbolp (car this)))
(let ((name (car this))
(form (cadr this)))
;; this silently ignores any slot options. bad...
(setq slotslist (cons name slotslist))
(setq initlist (acons (keyword-of name) form initlist))))
(t
(error "slot should be symbol or (symbol ...), not `%s'"
(prin1-to-string this)))))
(values slotsn (nreverse slotslist) (nreverse initlist))))
(defun parse$defstruct$options (name options slots)
"(parse$defstruct$options name OPTIONS SLOTS) => many values
A defstruct named NAME, with options list OPTIONS, has already slots SLOTS.
Parse the OPTIONS and return the updated form of the struct's slots and other
information. The values returned are:
CONC-NAME is the string to use as prefix/suffix in the methods,
CONST is the name of the official constructor,
COPIER is the name of the structure copier,
PRED is the name of the type predicate,
MORESLOTSN is the number of slots added by :include,
MORESLOTS is the list of slots added by :include,
MOREINITS is the list of initialization forms added by :include,
INCLUDED is nil, or the list of the symbol added by :include"
(let* ((namestring (symbol-name name))
;; to build the return values
(conc-name (concat namestring "-"))
(const (intern (concat "make-" namestring)))
(copier (intern (concat "copy-" namestring)))
(pred (intern (concat namestring "-p")))
(moreslotsn 0)
(moreslots '())
(moreinits '())
;; auxiliaries
option-head ;When an option is not a plain
option-second ; keyword, it must be a list of
option-rest ; the form (head second . rest)
these-slotsn ;When :include is found, the
these-slots ; info about the included
these-inits ; structure is added here.
included ;NIL or (list INCLUDED)
)
;; Values above are the defaults. Now we read the options themselves
(dolist (option options)
;; 2 cases arise, as options must be a keyword or a list
(cond
((keywordp option)
(case option
(:named
) ;ignore silently
(t
(error "can't recognize option `%s'"
(prin1-to-string option)))))
((and (listp option)
(keywordp (setq option-head (car option))))
(setq option-second (second option))
(setq option-rest (nthcdr 2 option))
(case option-head
(:conc-name
(setq conc-name
(cond
((stringp option-second)
option-second)
((null option-second)
"")
(t
(error "`%s' is invalid as `conc-name'"
(prin1-to-string option-second))))))
(:copier
(setq copier
(cond
((and (symbolp option-second)
(null option-rest))
option-second)
(t
(error "can't recognize option `%s'"
(prin1-to-string option))))))
(:constructor ;no BOA-constructors allowed
(setq const
(cond
((and (symbolp option-second)
(null option-rest))
option-second)
(t
(error "can't recognize option `%s'"
(prin1-to-string option))))))
(:predicate
(setq pred
(cond
((and (symbolp option-second)
(null option-rest))
option-second)
(t
(error "can't recognize option `%s'"
(prin1-to-string option))))))
(:include
(unless (symbolp option-second)
(error "arg to `:include' should be a symbol, not `%s'"
(prin1-to-string option-second)))
(setq these-slotsn (get option-second :structure-slotsn)
these-slots (get option-second :structure-slots)
these-inits (get option-second :structure-initforms))
(unless (and (numberp these-slotsn)
(> these-slotsn 0))
(error "`%s' is not a valid structure"
(prin1-to-string option-second)))
(if included
(error "`%s' already includes `%s', can't include `%s' too"
name (car included) option-second)
(push option-second included))
(multiple-value-bind
(xtra-slotsn xtra-slots xtra-inits)
(process$slots option-rest)
(when (> xtra-slotsn 0)
(dolist (xslot xtra-slots)
(unless (memq xslot these-slots)
(error "`%s' is not a slot of `%s'"
(prin1-to-string xslot)
(prin1-to-string option-second))))
(setq these-inits (append xtra-inits these-inits)))
(setq moreslotsn (+ moreslotsn these-slotsn))
(setq moreslots (append these-slots moreslots))
(setq moreinits (append these-inits moreinits))))
((:print-function :type :initial-offset)
) ;ignore silently
(t
(error "can't recognize option `%s'"
(prin1-to-string option)))))
(t
(error "can't recognize option `%s'"
(prin1-to-string option)))))
;; Return values found
(values conc-name const copier pred
moreslotsn moreslots moreinits
included)))
(defun simplify$inits (slots initlist)
"(simplify$inits SLOTS INITLIST) => new INITLIST
Removes from INITLIST - an ALIST - any shadowed bindings."
(let ((result '()) ;built here
key ;from the slot
)
(dolist (slot slots)
(setq key (keyword-of slot))
(setq result (acons key (cdr (assoc key initlist)) result)))
(nreverse result)))
(defun extract$indices (initlist)
"(extract$indices INITLIST) => indices list
Kludge. From a list of pairs (keyword . form) build a list of pairs
of the form (keyword . position in list from 0). Useful to precompute
some of the work of MAKE$STRUCTURE$INSTANCE."
(let ((result '())
(index 0))
(dolist (entry initlist (nreverse result))
(setq result (acons (car entry) index result)
index (+ index 1)))))
(defun build$accessors$for (name conc-name predicate slots slotsn)
"(build$accessors$for NAME PREDICATE SLOTS SLOTSN) => FSETS DEFSETFS KWDS
Generate the code for accesors and defsetfs of a structure called
NAME, whose slots are SLOTS. Also, establishes the keywords for the
slots names."
(do ((i 0 (1+ i))
(accessors '())
(alterators '())
(keywords '())
(canonic "")) ;slot name with conc-name prepended
((>= i slotsn)
(values
(nreverse accessors) (nreverse alterators) (nreverse keywords)))
(setq canonic (intern (concat conc-name (symbol-name (nth i slots)))))
(setq accessors
(cons
(list 'fset (list 'quote canonic)
(list 'function
(list 'lambda (list 'object)
(list 'cond
(list (list predicate 'object)
(list 'aref 'object (1+ i)))
(list 't
(list 'error
"`%s' is not a struct %s"
(list 'prin1-to-string
'object)
(list 'prin1-to-string
(list 'quote
name))))))))
accessors))
(setq alterators
(cons
(list 'defsetf canonic
(list 'lambda (list 'object 'newval)
(list 'cond
(list (list predicate 'object)
(list 'aset 'object (1+ i) 'newval))
(list 't
(list 'error
"`%s' not a `%s'"
(list 'prin1-to-string
'object)
(list 'prin1-to-string
(list 'quote
name)))))))
alterators))
(setq keywords
(cons (list 'defkeyword (keyword-of (nth i slots)))
keywords))))
(defun make$structure$instance (name args)
"(make$structure$instance NAME ARGS) => new struct NAME
A struct of type NAME is created, some slots might be initialized
according to ARGS (the &rest argument of MAKE-name)."
(unless (symbolp name)
(error "`%s' is not a possible name for a structure"
(prin1-to-string name)))
(let ((initforms (get name :structure-initforms))
(slotsn (get name :structure-slotsn))
(indices (get name :structure-indices))
initalist ;pairlis'd on initforms
initializers ;definitive initializers
)
;; check sanity of the request
(unless (and (numberp slotsn)
(> slotsn 0))
(error "`%s' is not a defined structure"
(prin1-to-string name)))
(unless (evenp (length args))
(error "slot initializers `%s' not of even length"
(prin1-to-string args)))
;; analyze the initializers provided by the call
(multiple-value-bind
(speckwds specvals) ;keywords and values given
(unzip-list args) ; by the user
;; check that all the arguments are introduced by keywords
(unless (every (function keywordp) speckwds)
(error "all of the names in `%s' should be keywords"
(prin1-to-string speckwds)))
;; check that all the keywords are known
(dolist (kwd speckwds)
(unless (numberp (cdr (assoc kwd indices)))
(error "`%s' is not a valid slot name for %s"
(prin1-to-string kwd) (prin1-to-string name))))
;; update initforms
(setq initalist
(pairlis speckwds
(do* ;;protect values from further evaluation
((ptr specvals (cdr ptr))
(val (car ptr) (car ptr))
(result '()))
((endp ptr) (nreverse result))
(setq result
(cons (list 'quote val)
result)))
(copy-sequence initforms)))
;; compute definitive initializers
(setq initializers
(do* ;;gather the values of the most definitive forms
((ptr indices (cdr ptr))
(key (caar ptr) (caar ptr))
(result '()))
((endp ptr) (nreverse result))
(setq result
(cons (eval (cdr (assoc key initalist))) result))))
;; do real initialization
(apply (function vector)
(cons name initializers)))))
;;;; end of cl-structs.el
;;; For lisp-interaction mode, so that multiple values can be seen when passed
;;; back. Lies every now and then...
(defvar - nil "form currently under evaluation")
(defvar + nil "previous -")
(defvar ++ nil "previous +")
(defvar +++ nil "previous ++")
(defvar / nil "list of values returned by +")
(defvar // nil "list of values returned by ++")
(defvar /// nil "list of values returned by +++")
(defvar * nil "(first) value of +")
(defvar ** nil "(first) value of ++")
(defvar *** nil "(first) value of +++")
(defun cl-eval-print-last-sexp ()
"Evaluate sexp before point; print value\(s\) into current buffer.
If the evaled form returns multiple values, they are shown one to a line.
The variables -, +, ++, +++, *, **, ***, /, //, /// have their usual meaning.
It clears the multiple-value passing mechanism, and does not pass back
multiple values. Use this only if you are debugging cl.el and understand well
how the multiple-value stuff works, because it can be fooled into believing
that multiple values have been returned when they actually haven't, for
instance
\(identity \(values nil 1\)\)
However, even when this fails, you can trust the first printed value to be
\(one of\) the returned value\(s\)."
(interactive)
;; top level call, can reset mvalues
(setq *mvalues-count* nil
*mvalues-values* nil)
(setq - (car (read-from-string
(buffer-substring
(let ((stab (syntax-table)))
(unwind-protect
(save-excursion
(set-syntax-table emacs-lisp-mode-syntax-table)
(forward-sexp -1)
(point))
(set-syntax-table stab)))
(point)))))
(setq *** **
** *
* (eval -))
(setq /// //
// /
/ *mvalues-values*)
(setq +++ ++
++ +
+ -)
(cond ((or (null *mvalues-count*) ;mvalues mechanism not used
(not (eq * (car *mvalues-values*))))
(print * (current-buffer)))
((null /) ;no values returned
(terpri (current-buffer)))
(t ;more than zero mvalues
(terpri (current-buffer))
(mapcar (function (lambda (value)
(prin1 value (current-buffer))
(terpri (current-buffer))))
/)))
(setq *mvalues-count* nil ;make sure
*mvalues-values* nil))
;;;; More LISTS functions
;;;;
;;; Some mapping functions on lists, commonly useful.
;;; They take no extra sequences, to go along with Emacs Lisp's MAPCAR.
(defun mapc (function list)
"(MAPC FUNCTION LIST) => LIST
Apply FUNCTION to each element of LIST, return LIST.
Like mapcar, but called only for effect."
(let ((args list))
(while args
(funcall function (car args))
(setq args (cdr args))))
list)
(defun maplist (function list)
"(MAPLIST FUNCTION LIST) => list'ed results of FUNCTION on cdrs of LIST
Apply FUNCTION to successive sublists of LIST, return the list of the results"
(let ((args list)
results '())
(while args
(setq results (cons (funcall function args) results)
args (cdr args)))
(nreverse results)))
(defun mapl (function list)
"(MAPL FUNCTION LIST) => LIST
Apply FUNCTION to successive cdrs of LIST, return LIST.
Like maplist, but called only for effect."
(let ((args list))
(while args
(funcall function args)
(setq args (cdr args)))
list))
(defun mapcan (function list)
"(MAPCAN FUNCTION LIST) => nconc'd results of FUNCTION on LIST
Apply FUNCTION to each element of LIST, nconc the results.
Beware: nconc destroys its first argument! See copy-list."
(let ((args list)
(results '()))
(while args
(setq results (nconc (funcall function (car args)) results)
args (cdr args)))
(nreverse results)))
(defun mapcon (function list)
"(MAPCON FUNCTION LIST) => nconc'd results of FUNCTION on cdrs of LIST
Apply FUNCTION to successive sublists of LIST, nconc the results.
Beware: nconc destroys its first argument! See copy-list."
(let ((args list)
(results '()))
(while args
(setq results (nconc (funcall function args) results)
args (cdr args)))
(nreverse results)))
;;; Copiers
(defsubst copy-list (list)
"Build a copy of LIST"
(append list '()))
(defun copy-tree (tree)
"Build a copy of the tree of conses TREE
The argument is a tree of conses, it is recursively copied down to
non conses. Circularity and sharing of substructure are not
necessarily preserved."
(if (consp tree)
(cons (copy-tree (car tree))
(copy-tree (cdr tree)))
tree))
;;; reversals, and destructive manipulations of a list's spine
(defun revappend (x y)
"does what (append (reverse X) Y) would, only faster"
(if (endp x)
y
(revappend (cdr x) (cons (car x) y))))
(defun nreconc (x y)
"does (nconc (nreverse X) Y) would, only faster
Destructive on X, be careful."
(if (endp x)
y
;; reuse the first cons of x, making it point to y
(nreconc (cdr x) (prog1 x (rplacd x y)))))
(defun nbutlast (list &optional n)
"Side-effected LIST truncated N+1 conses from the end.
This is the destructive version of BUTLAST. Returns () and does not
modify the LIST argument if the length of the list is not at least N."
(when (null n) (setf n 1))
(let ((length (list-length list)))
(cond ((null length)
list)
((< length n)
'())
(t
(setnthcdr (- length n) list nil)
list))))
;;; Substitutions
(defun subst (new old tree)
"NEW replaces OLD in a copy of TREE
Uses eql for the test."
(subst-if new (function (lambda (x) (eql x old))) tree))
(defun subst-if-not (new test tree)
"NEW replaces any subtree or leaf that fails TEST in a copy of TREE"
;; (subst-if new (function (lambda (x) (not (funcall test x)))) tree)
(cond ((not (funcall test tree))
new)
((atom tree)
tree)
(t ;no match so far
(let ((head (subst-if-not new test (car tree)))
(tail (subst-if-not new test (cdr tree))))
;; If nothing changed, return originals. Else use the new
;; components to assemble a new tree.
(if (and (eql head (car tree))
(eql tail (cdr tree)))
tree
(cons head tail))))))
(defun subst-if (new test tree)
"NEW replaces any subtree or leaf that satisfies TEST in a copy of TREE"
(cond ((funcall test tree)
new)
((atom tree)
tree)
(t ;no match so far
(let ((head (subst-if new test (car tree)))
(tail (subst-if new test (cdr tree))))
;; If nothing changed, return originals. Else use the new
;; components to assemble a new tree.
(if (and (eql head (car tree))
(eql tail (cdr tree)))
tree
(cons head tail))))))
(defun sublis (alist tree)
"Use association list ALIST to modify a copy of TREE
If a subtree or leaf of TREE is a key in ALIST, it is replaced by the
associated value. Not exactly Common Lisp, but close in spirit and
compatible with the native Emacs Lisp ASSOC, which uses EQUAL."
(let ((toplevel (assoc tree alist)))
(cond (toplevel ;Bingo at top
(cdr toplevel))
((atom tree) ;Give up on this
tree)
(t
(let ((head (sublis alist (car tree)))
(tail (sublis alist (cdr tree))))
(if (and (eql head (car tree))
(eql tail (cdr tree)))
tree
(cons head tail)))))))
(defun member-if (predicate list)
"PREDICATE is applied to the members of LIST. As soon as one of them
returns true, that tail of the list if returned. Else NIL."
(catch 'found-member-if
(while (not (endp list))
(if (funcall predicate (car list))
(throw 'found-member-if list)
(setq list (cdr list))))
nil))
(defun member-if-not (predicate list)
"PREDICATE is applied to the members of LIST. As soon as one of them
returns false, that tail of the list if returned. Else NIL."
(catch 'found-member-if-not
(while (not (endp list))
(if (funcall predicate (car list))
(setq list (cdr list))
(throw 'found-member-if-not list)))
nil))
(defun tailp (sublist list)
"(tailp SUBLIST LIST) => True if SUBLIST is a sublist of LIST."
(catch 'tailp-found
(while (not (endp list))
(if (eq sublist list)
(throw 'tailp-found t)
(setq list (cdr list))))
nil))
;;; Suggestion of phr%widow.Berkeley.EDU@lilac.berkeley.edu
(defmacro declare (&rest decls)
"Ignore a Common-Lisp declaration."
"declarations are ignored in this implementation")
(defun proclaim (&rest decls)
"Ignore a Common-Lisp proclamation."
"declarations are ignored in this implementation")
(defmacro the (type form)
"(the TYPE FORM) macroexpands to FORM
No checking is even attempted. This is just for compatibility with
Common-Lisp codes."
form)
;;; Due to Aaron Larson (alarson@src.honeywell.com, 26 Jul 91)
(put 'progv 'common-lisp-indent-hook '(4 4 &body))
(defmacro progv (vars vals &rest body)
"progv vars vals &body forms
bind vars to vals then execute forms.
If there are more vars than vals, the extra vars are unbound, if
there are more vals than vars, the extra vals are just ignored."
(` (progv$runtime (, vars) (, vals) (function (lambda () (,@ body))))))
;;; To do this efficiently, it really needs to be a special form...
(defun progv$runtime (vars vals body)
(eval (let ((vars-n-vals nil)
(unbind-forms nil))
(do ((r vars (cdr r))
(l vals (cdr l)))
((endp r))
(push (list (car r) (list 'quote (car l))) vars-n-vals)
(if (null l)
(push (` (makunbound '(, (car r)))) unbind-forms)))
(` (let (, vars-n-vals) (,@ unbind-forms) (funcall '(, body)))))))
(provide 'cl)
;;;; end of cl.el
|