-
Notifications
You must be signed in to change notification settings - Fork 0
/
arc.arc
3719 lines (3309 loc) · 111 KB
/
arc.arc
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
; Main Arc lib. Ported to Scheme version Jul 06.
; Rewritten Oct 08 for Arc-F
; optimize ~foo in functional position in ac, like compose
; make foo~bar equiv of foo:~bar (in expand-ssyntax)
; rename assert
; (10 x) for (= x 10)?
; should (= x) mean (= x t)?
; add sigs of ops defined in ac.scm
; get hold of error types within arc
; why is macex defined in scheme instead of using def below?
; write disp, read, write in arc
; could prob write rmfile and dir in terms of system
; could I get all of macros up into arc.arc?
; warn when shadow a global name
; permanent objs that live on disk and are updated when modified
; problem: serialization of shared structure, especially cycles
; (need eq?-tables!!)
; way to spec default 0 rather than nil for hts
; do in access call or when ht created? simply have ++ nil -> 1?
; 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
(in-package arc)
; NOTE! Do not modify. We can't modify this
; without potentially breaking existing code.
; Instead use <arc>v3-exp for new stuff.
; If it's good, it'll be added into <arc>v4
;-----DO NOT MODIFY BEGINS
; separated, because not all Arc implementations
; might support threads (or, like SNAP, may
; support an incompatible sort of concurrency)
(interface <arc>v3-thread
thread new-thread kill-thread break-thread
dead)
; similar: might not be supported by all Arc
; implementations
(interface <arc>v3-sync
pipe pipe-len thread-local
sema sema-wait sema-post
sync synct
lock w/lock)
; almkglor: controversial? LOL
(interface <arc>v3-reductor
l-reductor t-reductor r-reductor)
(interface <arc>v3-polymorph
polymorph)
; potentially rarely useful
(interface <arc>v3-testify
testify)
; almkglor: controversial? LOL
(interface <arc>v3-scanner
scanner unscan
each each-skip each-early-out each-skip-early-out
w/collect)
; all sorts of stuff
(interface <arc>v3-sort-internals
insert-sorted insort reinsert-sorted insortnew
mergesort)
; alternative multimethods
(interface <arc>v3-dispatcher-method
dispatcher-method add-dispatcher-method)
; almkglor: in my opinion, $ should be
; reserved for implementation-specific
; "hooks"
(interface <arc>v3-on-mzscheme
$)
; because you sometimes need to
; differentiate between what would
; have been there, versus what is
; really there
(interface <arc>v3-IDEAL
IDEAL REAL)
; to gain access to hidden help stuff
(interface <arc>v3-help
help helpsearch helpstr fns)
; packages
(interface <arc>v3-packages
cxt pkg pkg-of pkg-name unpkg
cxt-ref-d)
(interface <arc>v3
; types
bool cons sym fn char string
int num table output input
socket exception re thread
thread-local sema
; maths
+ - * / mod quotient expt sqrt
trunc exact
abs signop positive
round roundup to-nearest
avg
multiple
; range
range
; comparison
is isnt < > <= >=
iso
in empty
caris prefix mismatch
compare
single
len< len>
; character classification punc
whitec nonwhite alphadig
; running code
apply eval
; higher-order functions
compose complement >> andf orf
memo
par
only
; definitional structures
def mac defm defcall
defmemo
redef
; code grouping
do do1 let with withs given givens
; cons creation and manipulation
cons list
makeproper
; string creation and manipulation
newstring tostring fromstring
; string playing
ellipsize
; table creation and manipulation
table maptable fill-table
w/table memtable
keys vals
; table read and write
load-table read-table
load-tables save-table
write-table
safe-load-table
; expression reading
readstring1 read readfile readfile1
readall
saferead
; scanner traversal
car cdr caar cadr cdar cadr cddr
carif
nthcdr last
each
reclist
noisy-each
some all ;note the equivalency of these
ormap andmap
mem find
; mutation
scar scdr sref
; assignment
= defset
push swap rotate pop
pushnew pull
++ --
zap
wipe assert
; control structures
and or nor
when unless
while until
loop for repeat n-of
forlen on
case
whilet caselet iflet whenlet
drain whiler
check
; simple functions
no idfn nilfn
even odd
; user types
annotate rep type
; type checks
; - should be less useful given generics
; - also least-consistently-named part of ArcN
isa acons atom alist number dotted
; other traversal structures
recstring trav trav+
ontable
index
; collecting structures
w/collect w/scanner-collect-each
w/collect-on
accum accums
summing
intersperse
counts
; filtering and choosing
keep rem trues
best max min
most least
bestn firstn-that
union
count
before
dedup
commonest
; scanner/sequence manipulation
map1 map mappend pair tuples mapeach
butlast cut firstn
split ; list only
ssplit ; string only
join
rev len
at ; list and string only
adjoin consif conswhen ; list only
flat
copy-seq seq-to-list
sort
merge ; list only
atend ; len-able sequence only
; scanner/sequence copying
copy-seq seq-to-list
; scanner reduction
reduce rreduce
; cons cells as trees
tree-subst treewise ontree
; tables as objects
obj inst deftem addtem
templatize
temread temload temloadall
tems
; queues
queue enq deq qlen qlist enq-limit
; hooks
hook defhook
; association lists
assoc alref
tablist listtab
; bracket functions
; - needed if say some macro syntax would appreciate []
make-br-fn _
; anaphoric functions
rfn afn
; anaphoric structures
awhile aif aand awhen
; anaphora
self it break collect throw
; continuations and weirder control structures
ccc breakable point
protect dynamic-wind
after
; gensyms
uniq w/uniq
; printing
disp write pr prn
prall prs
prf
warn ero
writefile1
writefileraw
bar* ; global variable, not very good T.T
w/bars
; error
err on-err details
errsafe
; i/o
infile outfile instring outstring
w/infile w/outfile w/instring w/outstring w/appendfile append
inside
close
stdout stdin sterr
call-w/stdout call-w/stdin
w/stdout w/stdin
readc writec peekc
readb writeb
readline
; sockets
open-socket socket-accept
client-ip
connect-socket flush-socket
w/socket
; conversion
string sym coerce
downcase upcase
; GIL operations
atomic-invoke atomic atlet atwith atwiths
; random
rand-string rand rand-choice random-elt
; filesystem and system
dir file-exists dir-exists
rmfile
mkdir ensure-dir
system pipe-from
which-os
; time
sleep msec seconds
datetbl
current-process-milliseconds
current-gc-milliseconds
since minutes-since hours-since
days-since
date
; timed cache
cache
; profiling
time jtime time10
; global variable queries
bound varif
; PG wierdness
copy
; loading sources
load require arc-installation-path
; getting out of a lousy fork of Arc
quit
; macro playing
expand macex macex1
; help facilities
help fns
docstring)
;-----DO NOT MODIFY ENDS
; NOTE! add new structures and functions here, *not*
; in any of the above interfaces. Users of this
; interface are warned that it could break their
; code at any random time.
; if you want to propose a new interface for <arc>v4,
; provide it first in the form <arc>v3-your-interface-exp
; :s/your-interface/whatever you want/
; i.e. with the -exp tag to denote its experimental
; status for 3F
(interface <arc>v3-bitops-exp
bit-and bit-or bit-not bit-xor bit-shift)
(interface <arc>v3-your-interface-exp)
(interface <arc>v3-exp
<arc>v3
<arc>v3-bitops-exp
; mathematics
log
; sequence checking
pos
; number representation
pad
; control structures
catch always)
; NOTE! THIS INTERFACE EXISTS ONLY FOR DOCUMENTATION PURPOSES
; IT IS NOT INTENDED FOR ACTUAL USAGE
(interface <base>v3
; maths
<base>+ <base>- <base>negate <base>*
<base>/ <base>reciprocal <base>mod
<base>quotient
; comparison
<base>is <base><
; higher-order functions
<base>andf <base>orf
; scanner efficiency overrides
<base>each <base>collect-on)
(set help* (table))
(set current-load-file* "arc.arc")
(set source-file* (table))
(set safeset (annotate 'mac
(fn (var val)
`((fn ()
(if (bound ',var)
((fn ()
(disp "*** redefining ")
(disp ',var)
(writec #\newline))))
(set ,var ,val))))))
(set mac (annotate 'mac
(fn (name parms . body)
`((fn ()
(sref sig ',parms ',name)
; Document the macro, including the docstring if present
(if (<base>is (type ',(car body)) 'string)
(sref help* '(mac ,(car body)) ',name)
(sref help* '(mac nil) ',name))
(sref source-file* current-load-file* ',name)
(safeset ,name (annotate 'mac (fn ,parms ,@body))))))))
;documentation for mac itself
(sref help*
'(mac
" Defines a macro, a special function which transforms code.
You may specify a docstring to be displayed by `help' by
placing it as the first string in `body'
See also [[def]] [[docstring]] ")
'mac)
(sref sig
'(name parms . body)
'mac)
(sref source-file*
current-load-file*
'mac)
(mac do args
" Evaluates each expression in sequence and returns the result of the
last expression.
See also [[do1]] [[after]] "
`((fn () ,@args)))
(mac IDEAL (ideal real-tag real)
" Used to differentiate between a form that
we would prefer to use ideally, versus a
form that exists solely for real-world
efficiency. "
(if (<base>is real-tag 'REAL)
()
(err "syntax error in IDEAL form"))
real)
; It would be nice if multiple strings counted as multiple docstring lines.
(mac def (name parms . body)
" Defines a function with the given `name', `parms', and `body'.
You may specify a docstring to be displayed by `help' by
placing it as the first string in `body'
See also [[fn]] [[mac]] [[defm]] [[docstring]] "
`(do (sref sig ',parms ',name)
; Document the function, including the docstring if present
,(if (<base>is (type (car body)) 'string)
`(sref help* '(fn ,(car body)) ',name)
`(sref help* '(fn nil) ',name))
(sref source-file* current-load-file* ',name)
(safeset ,name (fn ,parms ,@body))))
(def docstring (name type parms docstring)
" A function which creates a docstring for
a given `name' symbol. `type' and `parms'
are the type of the `name' and the
parameters to that name. `docstring' is
the actual docstring for use with `help'.
See also [[help]] [[def]] [[mac]] "
(sref help* `(,type ,docstring) name)
(sref sig parms name)
(sref source-file* current-load-file* name)
t)
(mac let (var val . body)
" Assigns a local variable for the given `body'.
See also [[with]] [[withs]] [[fn]] [[do]] "
`((fn (,var)
,@body)
,val))
(def caar (xs) " Equivalent to (car (car xs)) " (car (car xs)))
(def cadr (xs) " Equivalent to (car (cdr xs)) " (car (cdr xs)))
(def cdar (xs) " Equivalent to (cdr (car xs)) " (cdr (car xs)))
(def cddr (xs) " Equivalent to (cdr (cdr xs)) " (cdr (cdr xs)))
(def no (x) " Determines if `x' is `nil'. " (<base>is x nil))
(def acons (x)
" Determines if `x' is a `cons' cell or list.
Unlike 'alist, this function will return nil if given an empty list
See also [[atom]] [[alist]] [[dotted]] [[isa]] [[cons]] [[list]] "
(<base>is (type x) 'cons))
(def atom (x)
" Determines if `x' is an atom
See also [[acons]] [[isa]] "
(no (acons x)))
(def list args
" Creates a list from the given parameters.
See also [[cons]] [[acons]] "
args)
(def idfn (x)
" Identity function - just returns its argument.
See also [[nilfn]] "
x)
(def nilfn args
" Takes any number of arguments and returns nil.
See also [[idfn]] "
nil)
; define reductors
(IDEAL
(do ; ideally, this is the definition for l-reductor
; however, due to speed issues, l-reductors have
; been made into special types internally on
; the mzscheme side
; other implementations are free to use this
; definition, or to imitate this implementation
; and create special objects for l-reductors
; (provided that they are indistinguishable
; by Arc from true functions)
(def l-reductor (f0 f1 f2)
" Creates a function which accepts any number of
parameters. If the function is given no
parameters, f0 is executed, if given 1 parameter
f1 is executed, if given 2 parameters f2 is
executed. For more than 2 parameters, parameters
are paired left-to-right:
(= f (l-reductor f0 f1 f2))
(f) == (f0)
(f a) == (f1 a)
(f a b) == (f2 a b)
(f a b c) == (f2 (f2 a b) c)
See also [[r-reductor]] [[t-reductor]] "
(fn rest
(if (no rest) (f0)
(no:cdr rest) (f1:car rest)
(no:cdr:cdr rest) (f2 (car rest) (cadr rest))
(l-reduction f2 (f2 (car rest) (cadr rest)) (cdr:cdr rest)))))
; this function is *not* part of the <arc>v3
; interface, or any interface; portable arc
; programs should not rely on its presence
; *or* absence.
; This also means that other implementations
; are free to include various internal
; functions in arc.arc, provided they are not
; included in a standard interface
(def l-reduction (f acc args)
(if args
(l-reduction f (f acc (car args)) (cdr args))
acc)))
REAL
; l-reductor is defined scheme-side
(docstring 'l-reductor 'fn '(f0 f1 f2)
" Creates a function which accepts any number of
parameters. If the function is given no
parameters, f0 is executed, if given 1 parameter
f1 is executed, if given 2 parameters f2 is
executed. For more than 2 parameters, parameters
are paired left-to-right.
(= f (l-reductor f0 f1 f2))
(f) == (f0)
(f a) == (f1 a)
(f a b) == (f2 a b)
(f a b c) == (f2 (f2 a b) c)
See also [[r-reductor]] [[t-reductor]] "))
; use wrapper functions to allow overriding of
; basic math functions
(set + (l-reductor
(fn () (err "'+ requires at least one parameter"))
(fn (x) x)
(fn (a b) (<base>+ a b))))
(docstring '+ 'fn 'args
" Adds numbers together, or concatenates
strings and lists.
See also [[-]] [[*]] [[/]] [[join]] ")
(set - (l-reductor
(fn () (err "'- requires at least one parameter"))
(fn (x) (<base>negate x))
(fn (a b) (<base>- a b))))
(docstring '- 'fn 'args
" Subtracts other numbers from the first given
number, or negates a single number.
(- 10 5 4) => (10 - 5) - 4 == 1
(- 10) => -10
See also [[-]] [[*]] [[/]] ")
(set * (l-reductor
(fn () (err "'* requires at least one parameter"))
(fn (x) x)
(fn (a b) (<base>* a b))))
(docstring '* 'fn 'args
" Multiplies numbers together.
See also [[+]] [[-]] [[/]] ")
(set / (l-reductor
(fn () (err "'/ requires at least one parameter"))
(fn (x) (<base>reciprocal x))
(fn (a b) (<base>/ a b))))
(docstring '/ 'fn 'args
" Divides a number by succeeding numbers,
or takes the reciprocal of a number.
(/ 60 3 2) => (60 / 3) / 2 == 10
(/ 0.25) => 4.0
See also [[+]] [[-]] [[*]] [[mod]] [[quotient]] ")
(set mod
(l-reductor
(fn () (err "'mod requires at least two parameters"))
(fn (x) (err "'mod requires at least two parameters"))
(fn (a b) (<base>mod a b))))
(docstring 'mod 'fn 'args
" Divides a number by another number,
returning the remainder (or
'modulo'); the result is further
divided and the remainder computed
if more than two parameters are
given. All numbers must be integers.
(mod 59 10) => 9
(mod 59 10 4) => (59 mod 10) mod 4
=> 9 mod 4
=> 1
See also [[quotient]] [[/]] ")
(set quotient
(l-reductor
(fn () (err "'quotient requires at least two parameters"))
(fn (x) (err "'quotient requires at least two parameters"))
(fn (a b) (<base>quotient a b))))
(docstring 'quotient 'fn 'args
" Divides a number by another number,
returning the quotient, rounded
towards zero to the nearest integer;
the result is further divided and
the quotient rounded if more than
two parameters are given. All
numbers must be integers.
(quotient 42 8) => 5
(quotient 42 8 2) => round(round(42 / 8) / 2)
=> round(5 / 2)
=> 2
See also [[mod]] [[/]] ")
(set compose
(l-reductor
(fn () (err "'compose requires at least one parameter"))
(fn (x) x)
(fn (a b) (<base>compose a b))))
(docstring 'compose 'fn 'args
" Connects several functions so that the
result of the rightmost function is
given to the function to the left,
and so on.
((compose a b) x) => (a (b x))
See also [[>>]] [[complement]] ")
(IDEAL
(def <base>compose (a b)
(fn rest
(a (apply b rest))))
REAL
())
(def complement (f)
" Arc expands ~x into (complement x)
whenever the function returns true this returns false.
See also [[no]] [[isnt]] [[compose]]"
(<base>compose no f))
(IDEAL
(do ; like l-reductor, this is handled
; by the base ac.scm for speed
(def r-reductor (f0 f1 f2)
" Creates a function which accepts any number of
parameters. If the function is given no
parameters, f0 is executed, if given 1 parameter
f1 is executed, if given 2 parameters f2 is
executed. For more than 2 parameters, parameters
are paired right-to-left:
(= f (r-reductor f0 f1 f2))
(f) == (f0)
(f a) == (f1 a)
(f a b) == (f2 a b)
(f a b c) == (f2 a (f2 b c))
See also [[l-reductor]] [[t-reductor]] "
(fn rest
(if (no rest) (f0)
(no:cdr rest) (f1:car args)
(no:cdr:cdr rest) (f2 (car args) (cadr args))
(f2 (car rest) (r-reduction f2 (cdr rest))))))
(def r-reduction (f2 rest)
(if (no:cdr rest)
(car rest)
(f2 (car rest) (r-reduction f2 (cdr rest))))))
REAL
(docstring 'r-reductor 'fn '(f0 f1 f2)
" Creates a function which accepts any number of
parameters. If the function is given no
parameters, f0 is executed, if given 1 parameter
f1 is executed, if given 2 parameters f2 is
executed. For more than 2 parameters, parameters
are paired right-to-left:
(= f (r-reductor f0 f1 f2))
(f) == (f0)
(f a) == (f1 a)
(f a b) == (f2 a b)
(f a b c) == (f2 a (f2 b c))
See also [[l-reductor]] [[t-reductor]] "))
; reduce right so that pairing is thus:
; (<base>andf a (<base>andf b c))
; this is slightly more efficient in the
; case of a function returning nil early
(set andf
(r-reductor
(fn () (fn (x) t))
(fn (a) (fn (x) (a x)))
(fn (a b) (<base>andf a b))))
(docstring 'andf 'fn 'fns
" Creates a function which returns true on its argument if all of the
given `fns' return true on that argument.
The created function accepts a single argument.
See also [[and]] [[orf]] ")
(def <base>andf (a b)
(fn (x)
(if (a x) (b x))))
(set orf
(r-reductor
(fn () (fn (x) t))
(fn (a) (fn (x) (a x)))
(fn (a b) (<base>orf a b))))
(docstring 'orf 'fn 'fns
" Creates a function which returns true on its argument if any of the
given `fns' return true on that argument.
The created function accepts a single argument.
See also [[or]] [[andf]] ")
(def <base>orf (a b)
(fn (x)
((fn (rv)
(if rv rv
(b x)))
(a x))))
(IDEAL
(do ; efficiency concern, efficiency concern
(def t-reductor (f0 f1 f2)
" Creates a function which accepts any number of
parameters. If the function is given no
parameters, f0 is executed, if given 1 parameter
f1 is executed, if given 2 parameters f2 is
executed. For more than 2 parameters, parameters
are paired left-to-right, and execution continues
only if f2 on the previous pair returns true:
(= f (t-reductor f0 f1 f2))
(f) == (f0)
(f a) == (f1 a)
(f a b) == (f2 a b)
(f a b c) == (if (f2 a b) (f2 b c))
See also [[l-reductor]] [[r-reductor]] "
(fn rest
(if (no rest) (f0)
(no:cdr rest) (f1:car rest)
(no:cdr:cdr rest) (f2 (car rest) (cadr rest))
; notice only cdr
(t-reduction f2 (f2 (car rest) (cadr rest)) (cdr rest)))))
(def t-reduction (f2 result rest)
(if result
(if (cdr:cdr rest)
(t-reduction f2 (f2 (car rest) (cadr rest)) (cdr rest))
(f2 (car rest) (cadr rest))))))
REAL
(docstring 't-reductor 'fn '(f0 f1 f2)
" Creates a function which accepts any number of
parameters. If the function is given no
parameters, f0 is executed, if given 1 parameter
f1 is executed, if given 2 parameters f2 is
executed. For more than 2 parameters, parameters
are paired left-to-right, and execution continues
only if f2 on the previous pair returns true:
(= f (t-reductor f0 f1 f2))
(f) == (f0)
(f a) == (f1 a)
(f a b) == (f2 a b)
(f a b c) == (if (f2 a b) (f2 b c))
See also [[l-reductor]] [[r-reductor]] "))
(set is
(t-reductor
(fn () (err "'is expects at least one argument"))
(fn (a) (fn (b) (is a b)))
(fn (a b) (<base>is a b))))
(docstring 'is 'fn 'args
" Determines if all arguments are
equal.
If given only one argument, returns
a function that compares its
argument to that value.
(is 1 2) => nil
(is 1 1) => t
(= f (is 0))
(f 2) => nil
(f 0) => t
See also [[isnt]] [[iso]] [[<]] [[>]] [[<=]] [[>=]] ")
(set isnt
(t-reductor
(fn () (err "'isnt expects at least one argument"))
(fn (a) (fn (b) (isnt a b)))
(fn (a b) (no (<base>is a b)))))
(docstring 'isnt 'fn 'args
" Determines if all arguments are not
equal.
If given only one argument, returns
a function that compares its
argument to that value.
(isnt 1 2) => t
(isnt 1 1) => nil
(= f (isnt 0))
(f 2) => t
(f 0) => nil
See also [[no]] [[is]] [[<]] [[>]] [[<=]] [[>=]] ")
(set <
(t-reductor
(fn () (err "'< expects at least one argument"))
(fn (a) (fn (b) (< a b)))
(fn (a b) (<base>< a b))))
(docstring '< 'fn 'args
" Determines if arguments are in order
from least to highest.
If given only one argument, returns
a function that compares its
argument to that value.
(< 1 2) => t
(< 1 1) => nil
(< 1 2 3) => t
(< 1 3 2) => nil
(= f (< 0))
(f 1) => t
(f -1) => nil
See also [[<=]] [[>]] [[>=]] [[is]] [[isnt]] ")
(set >
(t-reductor
(fn () (err "'> expects at least one argument"))
(fn (a) (fn (b) (> a b)))
(fn (a b) (if (<base>< a b)
nil
(no:<base>is a b)))))
(docstring '> 'fn 'args
" Determines if arguments are in order
from highest to lowest.
If given only one argument, returns
a function that compares its
argument to that value.
(> 1 2) => nil
(> 1 1) => nil
(> 1 2 3) => nil
(> 3 2 1) => t
(= f (> 0))
(f 1) => nil
(f -1) => t
See also [[>=]] [[<=]] [[<]] [[is]] [[isnt]] ")
(set <=
(t-reductor
(fn () (err "'<= expects at least one argument"))
(fn (a) (fn (b) (<= a b)))
(fn (a b) ((fn (rv)
(if rv
rv
(<base>is a b)))
(<base>< a b)))))
(docstring '<= 'fn 'args
" Determines if arguments are not
decreasing.
If given only one argument, returns
a function that compares its
argument to that value.
(<= 1 2) => t
(<= 3 2) => nil
(<= 1 1 2) => t
(<= 3 3 2) => nil
(= f (<= 0))
(f 1) => t
(f -1) => nil
See also [[<]] [[>=]] [[>]] [[is]] [[isnt]] ")
(set >=
(t-reductor
(fn () (err "'>= expect at least one argument"))
(fn (a) (fn (b) (>= a b)))
(fn (a b) (if (<base>< a b)
(<base>is a b)
t))))
(docstring '>= 'fn 'args
" Determines if arguments are not
increasing.
If given only one argument, returns
a function that compares its
argument to that value.
(>= 1 2) => nil
(>= 3 2) => t
(>= 1 1 2) => nil
(>= 3 3 2) => t
(= f (>= 0))
(f 1) => nil
(f -1) => t
See also [[>]] [[<=]] [[<]] [[is]] [[isnt]] ")
(mac and args
" Evaluates arguments till false is found else returns the last one.
See also [[or]] [[aand]] [[andf]] [[andmap]] "
(if args
(if (cdr args)
`(if ,(car args) (and ,@(cdr args)))
(car args))
't))
(mac defm (name parms . body)
" Defines or redefines a method, with type
annotations. A generic function must be
defined with 'def before using this form.
Example:
(defm sref ((t c container) value)
(scar (rep c) value))
See also [[def]] [[defcall]]"
(let (real-parms parmstl
types typestl
self) nil
; withs not defined yet ^^
(let add-parm
(fn (p)
(if real-parms
(set parmstl (scdr parmstl (list p)))
(set real-parms (set parmstl (list p)))))
(let add-type
(fn (tt)
(if types
(set typestl (scdr typestl (list tt)))
(set types (set typestl (list tt)))))
(let rest-parm
(fn (p)
(if real-parms
(scdr parmstl p)
(set real-parms p)))
(set self
(fn (l)
(if (is (type l) 'cons)
(let parm (car l)
(if (and (is (type parm) 'cons)
(is (car parm) t))
(let (spec real-parm typ) parm
(add-parm real-parm)
(add-type typ))
(do (add-parm parm)
(add-type nil)))
(self:cdr l))
(rest-parm l))))
(self parms)
`(set ,name
(symeval!polymorph
',types (fn ,real-parms ,@body)
,name)))))))
; bootstrap ends
(mac w/collect body
" Creates a new list, providing a `collect' function
within `body' which collects its argument into the
list.
Collection is in the correct order.
See also [[accum]] [[w/collect-on]] [[summing]] "
`(symeval!w/collect-f (fn (collect) ,@body)))
(def w/collect-f (bf)
(let (hd tl collect) nil
(set collect
(fn (e)
(if hd (set tl (scdr tl (cons e nil)))
(set tl (set hd (cons e nil))))))
(bf collect)
hd))
; defined here, but <base>each is defined later
(mac each (var expr . body)
" Performs `body' for each element of the sequence returned by `expr',
with each element assigned to `var'.
See also [[forlen]] [[on]] [[map]] [[mapeach]] [[ontable]]
[[each-skip-early-out]] "
`(symeval!<base>each ,expr 0 (fn (,var) ,@body t)))
(mac each-skip-early-out (start var expr . body)
" Performs `body' for each element of the sequence returned by `expr',
starting at `start'. If the last expression in `body' returns nil,
ends iteration.
See also [[each]] [[each-skip]] [[each-early-out]] "
`(symeval!<base>each ,expr ,start (fn (,var) ,@body)))
(mac each-skip (start var expr . body)
" Performs `body' for each element of the sequence returned by `expr',
starting at `start'.
See also [[each]] [[each-skip-early-out]] [[each-early-out]] "
`(symeval!<base>each ,expr ,start (fn (,var) ,@body t)))
(mac each-early-out (var expr . body)
" Performs `body' for each element of the sequence returend by `expr'.
If the last epression in `body' returns nil, ends iteration,
See also [[each]] [[each-skip]] [[each-skip-early-out]] "
`(symeval!<base>each ,expr 0 (fn (,var) ,@body)))
(mac w/collect-on (seq . body)
" Constructs a new sequence with the same type as `seq',
and provides a `collect' function which collects its
argument into the sequence. The original `seq' is not
modified, and is used only as a template.
See also [[w/collect]] "
`(symeval!<base>collect-on ,seq (fn (collect) ,@body)))
(def nthcdr (n xs)
" Returns the result of applying `cdr' repeatedly
by `n' times on `xs'.
See also [[cut]] [[firstn]] "