-
Notifications
You must be signed in to change notification settings - Fork 1
/
arc.lisp
1659 lines (1322 loc) · 42.2 KB
/
arc.lisp
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
(in-package :arc-compat.internal)
(in-readtable :common-lisp)
; Main Arc lib. Ported to Scheme version Jul 06.
; don't like names of conswhen and consif
; need better way of generating strings; too many calls to string
; maybe strings with escape char for evaluation
; make foo~bar equiv of foo:~bar (in expand-ssyntax)
; add sigs of ops defined in ac.scm
; get hold of error types within arc
; does macex have to be defined in scheme instead of using def below?
; write disp, read, write in arc
; could I get all of macros up into arc.arc?
; warn when shadow a global name
; some simple regexp/parsing plan
; compromises in this implementation:
; no objs in code
; (mac testlit args (listtab args)) breaks when called
; separate string type
; (= (cdr (cdr str)) "foo") couldn't work because no way to get str tail
; not sure this is a mistake; strings may be subtly different from
; lists of chars
#|(assign do (annotate 'mac
(fn args `((fn () ,@args)))))|#
#|(assign safeset (annotate 'mac
(fn (var val)
`(do (if (bound ',var)
(do (disp "*** redefining " (stderr))
(disp ',var (stderr))
(disp #\newline (stderr))))
(assign ,var ,val)))))|#
#|(assign def (annotate 'mac
(fn (name parms . body)
`(do (sref sig ',parms ',name)
(safeset ,name (fn ,parms ,@body))))))|#
(def no (x) (is x nil))
(def acons (x) (is (type x) 'cons))
;(def atom (x) (no (acons x)))
; Can return to this def once Rtm gets ac to make all rest args
; nil-terminated lists.
; (def list args args)
(def copylist (xs)
(if (no xs)
nil
(cons (car xs) (copylist (cdr xs)))))
(def list args (copylist args))
(def idfn (x) x)
; Maybe later make this internal. Useful to let xs be a fn?
(def map1 (f xs)
(if (no xs)
nil
(cons (f (car xs)) (map1 f (cdr xs)))))
#|(def pair (xs (o f list))
(if (no xs)
nil
(no (cdr xs))
(list (list (car xs)))
(cons (f (car xs) (cadr xs))
(pair (cddr xs) f))))|#
#|(assign mac (annotate 'mac
(fn (name parms . body)
`(do (sref sig ',parms ',name)
(safeset ,name (annotate 'mac (fn ,parms ,@body)))))))|#
#|(mac and args
(if args
(if (cdr args)
`(if ,(car args) (and ,@(cdr args)))
(car args))
't))|#
(def assoc (key al)
(if (atom al)
nil
(and (acons (car al)) (is (caar al) key))
(car al)
(assoc key (cdr al))))
(def alref (al key) (cadr (assoc key al)))
#|(mac with (parms . body)
`((fn ,(map1 car (pair parms))
,@body)
,@(map1 cadr (pair parms))))|#
(mac with (parms . body)
`(funcall (fn ,(map1 #'car (pair parms))
,@body)
,@(map1 #'cadr (pair parms))))
(mac let (var val . body)
`(with (,var ,val) ,@body))
(mac withs (parms . body)
(if (no parms)
`(do ,@body)
`(let ,(car parms) ,(cadr parms)
(withs ,(cddr parms) ,@body))))
; Rtm prefers to overload + to do this
(def join args
(if (no args)
nil
(let a (car args)
(if (no a)
(apply join (cdr args))
(cons (car a) (apply join (cdr a) (cdr args)))))))
; Need rfn for use in macro expansions.
#|(mac rfn (name parms . body)
`(let ,name nil
(assign ,name (fn ,parms ,@body))))|#
#|(mac afn (parms . body)
`(let self nil
(assign self (fn ,parms ,@body))))|#
; Ac expands x:y:z into (compose x y z), ~x into (complement x)
; Only used when the call to compose doesn't occur in functional position.
; Composes in functional position are transformed away by ac.
#|(mac compose args
(let g (uniq)
`(fn ,g
,((afn (fs)
(if (cdr fs)
(list (car fs) (self (cdr fs)))
`(apply ,(if (car fs) (car fs) 'idfn) ,g)))
args))))|#
(mac compose args
(let g (uniq)
`(fn ,g
,((afn (fs)
(if (cdr fs)
(list (car fs) (self (cdr fs)))
`(apply ,(if (car fs) (car fs) 'idfn) ,g)))
args))))
; Ditto: complement in functional position optimized by ac.
(mac complement (f)
(let g (uniq)
`(fn ,g (no (apply ,f ,g)))))
#|(def rev (xs)
((afn (xs acc)
(if (no xs)
acc
(self (cdr xs) (cons (car xs) acc))))
xs nil))|#
(def isnt (x y) (no (is x y)))
#|(mac w/uniq (names . body)
(if (acons names)
`(with ,(apply + nil (map1 (fn (n) (list n '(uniq)))
names))
,@body)
`(let ,names (uniq) ,@body)))|#
(mac w/uniq (names . body)
(if (acons names)
`(with ,(apply join nil (map1 (fn (n) (list n '(uniq)))
names))
,@body)
`(let ,names (uniq) ,@body)))
(mac or args
(and args
(w/uniq g
`(let ,g ,(car args)
(if ,g ,g (or ,@(cdr args)))))))
(def alist (x) (or (no x) (is (type x) 'cons)))
(mac in (x . choices)
(w/uniq g
`(let ,g ,x
(or ,@(map1 (fn (c) `(is ,g ,c)) choices)))))
; Could take n args, but have never once needed that.
(def iso (x y)
(or (is x y)
(and (acons x)
(acons y)
(iso (car x) (car y))
(iso (cdr x) (cdr y)))))
(mac when (test . body)
`(if ,test (do ,@body)))
(mac unless (test . body)
`(if (no ,test) (do ,@body)))
(mac while (test . body)
(w/uniq (gf gp)
`((rfn ,gf (,gp)
(when ,gp ,@body (,gf ,test)))
,test)))
(def empty (seq)
(or (no seq)
(and (no (acons seq)) (is (len seq) 0))))
(def reclist (f xs)
(and xs (or (f xs) (reclist f (cdr xs)))))
#|(def recstring (test s (o start 0))
(let n (len s)
((afn (i)
(and (< i (len s))
(or (test i)
(self (+ i 1)))))
start)))|#
#|(def testify (x)
(if (isa x 'fn) x [is _ x]))|#
#|(def some (test seq)
(let f (testify test)
(if (alist seq)
(reclist f:car seq)
(recstring f:seq seq))))|#
(def all (test seq)
(~some (complement (testify test)) seq))
(def mem (test seq)
(let f (testify test)
(reclist [if (f:car _) _] seq)))
(def find (test seq)
(let f (testify test)
(if (alist seq)
(reclist [if (f:car _) (car _)] seq)
(recstring [if (f:seq _) (seq _)] seq))))
(def isa (x y) (is (type x) y))
; Possible to write map without map1, but makes News 3x slower.
;(def map (f . seqs)
; (if (some1 no seqs)
; nil
; (no (cdr seqs))
; (let s1 (car seqs)
; (cons (f (car s1))
; (map f (cdr s1))))
; (cons (apply f (map car seqs))
; (apply map f (map cdr seqs)))))
(def map (f . seqs)
(if (some [isa _ 'string] seqs)
(withs (n (apply min (map len seqs))
new (newstring n))
((afn (i)
(if (is i n)
new
(do (sref new (apply f (map [_ i] seqs)) i)
(self (+ i 1)))))
0))
(no (cdr seqs))
(map1 f (car seqs))
((afn (seqs)
(if (some no seqs)
nil
(cons (apply f (map1 car seqs))
(self (map1 cdr seqs)))))
seqs)))
(def mappend (f . args)
(apply + nil (apply map f args)))
(def firstn (n xs)
(if (no n) xs
(and (> n 0) xs) (cons (car xs) (firstn (- n 1) (cdr xs)))
nil))
(def nthcdr (n xs)
(if (no n) xs
(> n 0) (nthcdr (- n 1) (cdr xs))
xs))
; Generalization of pair: (tuples x) = (pair x)
(def tuples (xs (o n 2))
(if (no xs)
nil
(cons (firstn n xs)
(tuples (nthcdr n xs) n))))
; If ok to do with =, why not with def? But see if use it.
(mac defs args
`(do ,@(map [cons 'def _] (tuples args 3))))
(def caris (x val)
(and (acons x) (is (car x) val)))
(def warn (msg . args)
(disp (+ "Warning: " msg ". "))
(map [do (write _) (disp " ")] args)
(disp #\newline))
(mac atomic body
`(atomic-invoke (fn () ,@body)))
(mac atlet args
`(atomic (let ,@args)))
(mac atwith args
`(atomic (with ,@args)))
(mac atwiths args
`(atomic (withs ,@args)))
; setforms returns (vars get set) for a place based on car of an expr
; vars is a list of gensyms alternating with expressions whose vals they
; should be bound to, suitable for use as first arg to withs
; get is an expression returning the current value in the place
; set is an expression representing a function of one argument
; that stores a new value in the place
; A bit gross that it works based on the *name* in the car, but maybe
; wrong to worry. Macros live in expression land.
; seems meaningful to e.g. (push 1 (pop x)) if (car x) is a cons.
; can't in cl though. could I define a setter for push or pop?
(assign setter (table))
(mac defset (name parms . body)
(w/uniq gexpr
`(sref setter
(fn (,gexpr)
(let ,parms (cdr ,gexpr)
,@body))
',name)))
(defset car (x)
(w/uniq g
(list (list g x)
`(car ,g)
`(fn (val) (scar ,g val)))))
(defset cdr (x)
(w/uniq g
(list (list g x)
`(cdr ,g)
`(fn (val) (scdr ,g val)))))
(defset caar (x)
(w/uniq g
(list (list g x)
`(caar ,g)
`(fn (val) (scar (car ,g) val)))))
(defset cadr (x)
(w/uniq g
(list (list g x)
`(cadr ,g)
`(fn (val) (scar (cdr ,g) val)))))
(defset cddr (x)
(w/uniq g
(list (list g x)
`(cddr ,g)
`(fn (val) (scdr (cdr ,g) val)))))
; Note: if expr0 macroexpands into any expression whose car doesn't
; have a setter, setforms assumes it's a data structure in functional
; position. Such bugs will be seen only when the code is executed, when
; sref complains it can't set a reference to a function.
(def setforms (expr0)
(let expr (macex expr0)
(if (isa expr 'sym)
(if (ssyntax expr)
(setforms (ssexpand expr))
(w/uniq (g h)
(list (list g expr)
g
`(fn (,h) (assign ,expr ,h)))))
; make it also work for uncompressed calls to compose
(and (acons expr) (metafn (car expr)))
(setforms (expand-metafn-call (ssexpand (car expr)) (cdr expr)))
(and (acons expr) (acons (car expr)) (is (caar expr) 'get))
(setforms (list (cadr expr) (cadr (car expr))))
(let f (setter (car expr))
(if f
(f expr)
; assumed to be data structure in fn position
(do (when (caris (car expr) 'fn)
(warn "Inverting what looks like a function call"
expr0 expr))
(w/uniq (g h)
(let argsyms (map [uniq] (cdr expr))
(list (+ (list g (car expr))
(mappend list argsyms (cdr expr)))
`(,g ,@argsyms)
`(fn (,h) (sref ,g ,h ,(car argsyms))))))))))))
(def metafn (x)
(or (ssyntax x)
(and (acons x) (in (car x) 'compose 'complement))))
(def expand-metafn-call (f args)
(if (is (car f) 'compose)
((afn (fs)
(if (caris (car fs) 'compose) ; nested compose
(self (join (cdr (car fs)) (cdr fs)))
(cdr fs)
(list (car fs) (self (cdr fs)))
(cons (car fs) args)))
(cdr f))
(is (car f) 'no)
(err "Can't invert " (cons f args))
(cons f args)))
(def expand= (place val)
(if (and (isa place 'sym) (~ssyntax place))
`(assign ,place ,val)
(let (vars prev setter) (setforms place)
(w/uniq g
`(atwith ,(+ vars (list g val))
(,setter ,g))))))
(def expand=list (terms)
`(do ,@(map (fn ((p v)) (expand= p v)) ; [apply expand= _]
(pair terms))))
(mac = args
(expand=list args))
(mac loop (start test update . body)
(w/uniq (gfn gparm)
`(do ,start
((rfn ,gfn (,gparm)
(if ,gparm
(do ,@body ,update (,gfn ,test))))
,test))))
(mac for (v init max . body)
(w/uniq (gi gm)
`(with (,v nil ,gi ,init ,gm (+ ,max 1))
(loop (assign ,v ,gi) (< ,v ,gm) (assign ,v (+ ,v 1))
,@body))))
(mac repeat (n . body)
`(for ,(uniq) 1 ,n ,@body))
; could bind index instead of gensym
(mac each (var expr . body)
(w/uniq (gseq g)
`(let ,gseq ,expr
(if (alist ,gseq)
((afn (,g)
(when (acons ,g)
(let ,var (car ,g) ,@body)
(self (cdr ,g))))
,gseq)
(isa ,gseq 'table)
(maptable (fn (,g ,var) ,@body)
,gseq)
(for ,g 0 (- (len ,gseq) 1)
(let ,var (,gseq ,g) ,@body))))))
; (nthcdr x y) = (cut y x).
(def cut (seq start (o end))
(let end (if (no end) (len seq)
(< end 0) (+ (len seq) end)
end)
(if (isa seq 'string)
(let s2 (newstring (- end start))
(for i 0 (- end start 1)
(= (s2 i) (seq (+ start i))))
s2)
(firstn (- end start) (nthcdr start seq)))))
(mac ontable (k v h . body)
`(maptable (fn (,k ,v) ,@body) ,h))
(mac whilet (var test . body)
(w/uniq (gf gp)
`((rfn ,gf (,gp)
(let ,var ,gp
(when ,var ,@body (,gf ,test))))
,test)))
(def last (xs)
(if (cdr xs)
(last (cdr xs))
(car xs)))
(def rem (test seq)
(let f (testify test)
(if (alist seq)
((afn (s)
(if (no s) nil
(f (car s)) (self (cdr s))
(cons (car s) (self (cdr s)))))
seq)
(coerce (rem test (coerce seq 'cons)) 'string))))
(def keep (test seq)
(rem (complement (testify test)) seq))
;(def trues (f seq)
; (rem nil (map f seq)))
(def trues (f xs)
(and xs
(let fx (f (car xs))
(if fx
(cons fx (trues f (cdr xs)))
(trues f (cdr xs))))))
(mac do1 args
(w/uniq g
`(let ,g ,(car args)
,@(cdr args)
,g)))
; Would like to write a faster case based on table generated by a macro,
; but can't insert objects into expansions in Mzscheme.
(mac caselet (var expr . args)
(let ex (afn (args)
(if (no (cdr args))
(car args)
`(if (is ,var ',(car args))
,(cadr args)
,(self (cddr args)))))
`(let ,var ,expr ,(ex args))))
(mac case (expr . args)
`(caselet ,(uniq) ,expr ,@args))
(mac push (x place)
(w/uniq gx
(let (binds val setter) (setforms place)
`(let ,gx ,x
(atwiths ,binds
(,setter (cons ,gx ,val)))))))
(mac swap (place1 place2)
(w/uniq (g1 g2)
(with ((binds1 val1 setter1) (setforms place1)
(binds2 val2 setter2) (setforms place2))
`(atwiths ,(+ binds1 (list g1 val1) binds2 (list g2 val2))
(,setter1 ,g2)
(,setter2 ,g1)))))
(mac rotate places
(with (vars (map [uniq] places)
forms (map setforms places))
`(atwiths ,(mappend (fn (g (binds val setter))
(+ binds (list g val)))
vars
forms)
,@(map (fn (g (binds val setter))
(list setter g))
(+ (cdr vars) (list (car vars)))
forms))))
(mac pop (place)
(w/uniq g
(let (binds val setter) (setforms place)
`(atwiths ,(+ binds (list g val))
(do1 (car ,g)
(,setter (cdr ,g)))))))
(def adjoin (x xs (o test iso))
(if (some [test x _] xs)
xs
(cons x xs)))
(mac pushnew (x place . args)
(w/uniq gx
(let (binds val setter) (setforms place)
`(atwiths ,(+ (list gx x) binds)
(,setter (adjoin ,gx ,val ,@args))))))
(mac pull (test place)
(w/uniq g
(let (binds val setter) (setforms place)
`(atwiths ,(+ (list g test) binds)
(,setter (rem ,g ,val))))))
(mac ++ (place (o i 1))
(if (isa place 'sym)
`(= ,place (+ ,place ,i))
(w/uniq gi
(let (binds val setter) (setforms place)
`(atwiths ,(+ binds (list gi i))
(,setter (+ ,val ,gi)))))))
(mac -- (place (o i 1))
(if (isa place 'sym)
`(= ,place (- ,place ,i))
(w/uniq gi
(let (binds val setter) (setforms place)
`(atwiths ,(+ binds (list gi i))
(,setter (- ,val ,gi)))))))
; E.g. (inc x) equiv to (zap + x 1)
(mac zap (op place . args)
(with (gop (uniq)
gargs (map [uniq] args)
mix (afn seqs
(if (some no seqs)
nil
(+ (map car seqs)
(apply self (map cdr seqs))))))
(let (binds val setter) (setforms place)
`(atwiths ,(+ binds (list gop op) (mix gargs args))
(,setter (,gop ,val ,@gargs))))))
; Can't simply mod pr to print strings represented as lists of chars,
; because empty string will get printed as nil. Would need to rep strings
; as lists of chars annotated with 'string, and modify car and cdr to get
; the rep of these. That would also require hacking the reader.
(def pr args
(map1 disp args)
(car args))
(def prn args
(do1 (apply pr args)
(writec #\newline)))
(mac wipe args
`(do ,@(map (fn (a) `(= ,a nil)) args)))
(mac set args
`(do ,@(map (fn (a) `(= ,a t)) args)))
; Destructuring means ambiguity: are pat vars bound in else? (no)
(mac iflet (var expr then . rest)
(w/uniq gv
`(let ,gv ,expr
(if ,gv (let ,var ,gv ,then) ,@rest))))
(mac whenlet (var expr . body)
`(iflet ,var ,expr (do ,@body)))
(mac aif (expr . body)
`(let it ,expr
(if it
,@(if (cddr body)
`(,(car body) (aif ,@(cdr body)))
body))))
(mac awhen (expr . body)
`(let it ,expr (if it (do ,@body))))
(mac aand args
(if (no args)
't
(no (cdr args))
(car args)
`(let it ,(car args) (and it (aand ,@(cdr args))))))
(mac accum (accfn . body)
(w/uniq gacc
`(withs (,gacc nil ,accfn [push _ ,gacc])
,@body
(rev ,gacc))))
; Repeatedly evaluates its body till it returns nil, then returns vals.
(mac drain (expr (o eof nil))
(w/uniq (gacc gdone gres)
`(with (,gacc nil ,gdone nil)
(while (no ,gdone)
(let ,gres ,expr
(if (is ,gres ,eof)
(= ,gdone t)
(push ,gres ,gacc))))
(rev ,gacc))))
; For the common C idiom while x = snarfdata != stopval.
; Rename this if use it often.
(mac whiler (var expr endval . body)
(w/uniq gf
`(withs (,var nil ,gf (testify ,endval))
(while (no (,gf (= ,var ,expr)))
,@body))))
;(def macex (e)
; (if (atom e)
; e
; (let op (and (atom (car e)) (eval (car e)))
; (if (isa op 'mac)
; (apply (rep op) (cdr e))
; e))))
(def consif (x y) (if x (cons x y) y))
(def string args
(apply + "" (map [coerce _ 'string] args)))
(def flat (x)
((afn (x acc)
(if (no x) acc
(atom x) (cons x acc)
(self (car x) (self (cdr x) acc))))
x nil))
(mac check (x test (o alt))
(w/uniq gx
`(let ,gx ,x
(if (,test ,gx) ,gx ,alt))))
(def pos (test seq (o start 0))
(let f (testify test)
(if (alist seq)
((afn (seq n)
(if (no seq)
nil
(f (car seq))
n
(self (cdr seq) (+ n 1))))
(nthcdr start seq)
start)
(recstring [if (f (seq _)) _] seq start))))
(def even (n) (is (mod n 2) 0))
(def odd (n) (no (even n)))
(mac after (x . ys)
`(protect (fn () ,x) (fn () ,@ys)))
(let expander
(fn (f var name body)
`(let ,var (,f ,name)
(after (do ,@body) (close ,var))))
(mac w/infile (var name . body)
(expander 'infile var name body))
(mac w/outfile (var name . body)
(expander 'outfile var name body))
(mac w/instring (var str . body)
(expander 'instring var str body))
(mac w/socket (var port . body)
(expander 'open-socket var port body))
)
(mac w/outstring (var . body)
`(let ,var (outstring) ,@body))
; what happens to a file opened for append if arc is killed in
; the middle of a write?
(mac w/appendfile (var name . body)
`(let ,var (outfile ,name 'append)
(after (do ,@body) (close ,var))))
; rename this simply "to"? - prob not; rarely use
(mac w/stdout (str . body)
`(call-w/stdout ,str (fn () ,@body)))
(mac w/stdin (str . body)
`(call-w/stdin ,str (fn () ,@body)))
(mac tostring body
(w/uniq gv
`(w/outstring ,gv
(w/stdout ,gv ,@body)
(inside ,gv))))
(mac fromstring (str . body)
(w/uniq gv
`(w/instring ,gv ,str
(w/stdin ,gv ,@body))))
(def readstring1 (s (o eof nil)) (w/instring i s (read i eof)))
(def read ((o x (stdin)) (o eof nil))
(if (isa x 'string) (readstring1 x eof) (sread x eof)))
; inconsistency between names of readfile[1] and writefile
(def readfile (name) (w/infile s name (drain (read s))))
(def readfile1 (name) (w/infile s name (read s)))
(def readall (src (o eof nil))
((afn (i)
(let x (read i eof)
(if (is x eof)
nil
(cons x (self i)))))
(if (isa src 'string) (instring src) src)))
(def writefile (val file)
(let tmpfile (+ file ".tmp")
(w/outfile o tmpfile (write val o))
(mvfile tmpfile file))
val)
(def sym (x) (coerce x 'sym))
(def int (x (o b 10)) (coerce x 'int b))
(mac rand-choice exprs
`(case (rand ,(len exprs))
,@(let key -1
(mappend [list (++ key) _]
exprs))))
(mac n-of (n expr)
(w/uniq ga
`(let ,ga nil
(repeat ,n (push ,expr ,ga))
(rev ,ga))))
; rejects bytes >= 248 lest digits be overrepresented
(def rand-string (n)
"Generates a random string of alphanumerics of length n."
(let c "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
(with (nc 62 s (newstring n) i 0 salt (get-internal-real-time))
(while (< i n)
(let x (mod (+ salt (cl:random 256)) 256)
(unless (> x 247)
(setf (elt s i) (elt c (mod x nc)))
(++ i))))
s)))
(mac forlen (var s . body)
`(for ,var 0 (- (len ,s) 1) ,@body))
(mac on (var s . body)
(if (is var 'index)
(err "Can't use index as first arg to on.")
(w/uniq gs
`(let ,gs ,s
(forlen index ,gs
(let ,var (,gs index)
,@body))))))
(def best (f seq)
(if (no seq)
nil
(let wins (car seq)
(each elt (cdr seq)
(if (f elt wins) (= wins elt)))
wins)))
(def max args (best > args))
(def min args (best < args))
; (mac max2 (x y)
; (w/uniq (a b)
; `(with (,a ,x ,b ,y) (if (> ,a ,b) ,a ,b))))
(def most (f seq)
(unless (no seq)
(withs (wins (car seq) topscore (f wins))
(each elt (cdr seq)
(let score (f elt)
(if (> score topscore) (= wins elt topscore score))))
wins)))
; Insert so that list remains sorted. Don't really want to expose
; these but seem to have to because can't include a fn obj in a
; macroexpansion.
(def insert-sorted (test elt seq)
(if (no seq)
(list elt)
(test elt (car seq))
(cons elt seq)
(cons (car seq) (insert-sorted test elt (cdr seq)))))
(mac insort (test elt seq)
`(zap [insert-sorted ,test ,elt _] ,seq))
(def reinsert-sorted (test elt seq)
(if (no seq)
(list elt)
(is elt (car seq))
(reinsert-sorted test elt (cdr seq))
(test elt (car seq))
(cons elt (rem elt seq))
(cons (car seq) (reinsert-sorted test elt (cdr seq)))))
(mac insortnew (test elt seq)
`(zap [reinsert-sorted ,test ,elt _] ,seq))
; Could make this look at the sig of f and return a fn that took the
; right no of args and didn't have to call apply (or list if 1 arg).
(def memo (f)
(with (cache (table) nilcache (table))
(fn args
(or (cache args)
(and (no (nilcache args))
(aif (apply f args)
(= (cache args) it)
(do (set (nilcache args))
nil)))))))
(mac defmemo (name parms . body)
`(safeset ,name (memo (fn ,parms ,@body))))
(def <= args
(or (no args)
(no (cdr args))
(and (no (> (car args) (cadr args)))
(apply <= (cdr args)))))
(def >= args
(or (no args)
(no (cdr args))
(and (no (< (car args) (cadr args)))
(apply >= (cdr args)))))
(def whitec (c)
(in c #\space #\newline #\tab #\return))
(def nonwhite (c) (no (whitec c)))
(def letter (c) (or (<= #\a c #\z) (<= #\A c #\Z)))
(def digit (c) (<= #\0 c #\9))
(def alphadig (c) (or (letter c) (digit c)))
(def punc (c)
(in c #\. #\, #\; #\: #\! #\?))
(def readline ((o str (stdin)))
(awhen (readc str)
(tostring
(writec it)
(whiler c (readc str) [in _ nil #\newline]
(writec c)))))
; Don't currently use this but suspect some code could.
(mac summing (sumfn . body)
(w/uniq (gc gt)
`(let ,gc 0
(let ,sumfn (fn (,gt) (if ,gt (++ ,gc)))
,@body)
,gc)))
(def sum (f xs)
(let n 0
(each x xs (++ n (f x)))
n))
(def treewise (f base tree)
(if (atom tree)
(base tree)
(f (treewise f base (car tree))
(treewise f base (cdr tree)))))
(def carif (x) (if (atom x) x (car x)))