-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprelude.lisp
233 lines (181 loc) · 4.33 KB
/
prelude.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
(define null? not)
(define err? (lambda (x) (eq? x 'ERR)))
(define number? (lambda (x) (eq? (* 0 x) 0)))
(define pair? (lambda (x) (not (err? (cdr x)))))
(define symbol?
(lambda (x)
(and
x
(not (err? x))
(not (number? x))
(not (pair? x)))))
(define atom?
(lambda (x)
(or
(not x)
(symbol? x))))
(define list?
(lambda (x)
(if (not x)
#t
(if (pair? x)
(list? (cdr x))
()))))
(define equal?
(lambda (x y)
(or
(eq? x y)
(and
(pair? x)
(pair? y)
(equal? (car x) (car y))
(equal? (cdr x) (cdr y))))))
(define negate (lambda (n) (- 0 n)))
(define > (lambda (x y) (< y x)))
(define <= (lambda (x y) (not (< y x))))
(define >= (lambda (x y) (not (< x y))))
(define = (lambda (x y) (eq? (- x y) 0)))
(define list (lambda args args))
(define cadr (lambda (x) (car (cdr x))))
(define caddr (lambda (x) (car (cdr (cdr x)))))
(define begin (lambda (x . args) (if args (begin . args) x)))
(define length
(lambda (t)
(if t
(+ 1 (length (cdr t)))
0)))
(define append1
(lambda (s t)
(if s
(cons (car s) (append1 (cdr s) t))
t)))
(define append
(lambda (t . args)
(if args
(append1 t (append . args))
t)))
(define nthcdr
(lambda (t n)
(if (eq? n 0)
t
(nthcdr (cdr t) (- n 1)))))
(define nth (lambda (t n) (car (nthcdr t n))))
(define rev1
(lambda (r t)
(if t
(rev1 (cons (car t) r) (cdr t))
r)))
(define reverse (lambda (t) (rev1 () t)))
(define member
(lambda (x t)
(if t
(if (equal? x (car t))
t
(member x (cdr t)))
t)))
(define foldr
(lambda (f x t)
(if t
(f (car t) (foldr f x (cdr t)))
x)))
(define foldl
(lambda (f x t)
(if t
(foldl f (f (car t) x) (cdr t))
x)))
(define min
(lambda args
(foldl
(lambda (x y)
(if (< x y)
x
y))
9.999999999e99
args)))
(define max
(lambda args
(foldl (lambda (x y)
(if (< x y)
y
x))
-9.999999999e99
args)))
(define filter
(lambda (f t)
(if t
(if (f (car t))
(cons (car t) (filter f (cdr t)))
(filter f (cdr t)))
())))
(define all?
(lambda (f t)
(if t
(and
(f (car t))
(all? f (cdr t)))
#t)))
(define any?
(lambda (f t)
(if t
(or
(f (car t))
(any? f (cdr t)))
())))
(define mapcar
(lambda (f t)
(if t
(cons (f (car t)) (mapcar f (cdr t)))
())))
(define map
(lambda (f . args)
(if (any? null? args)
()
(let*
(x (mapcar car args))
(t (mapcar cdr args))
(cons (f . x) (map f . t))))))
(define zip (lambda args (map list . args)))
(define seq
(lambda (n m)
(if (< n m)
(cons n (seq (+ n 1) m))
())))
(define seqby
(lambda (n m k)
(if (< 0 (* k (- m n)))
(cons n (seqby (+ n k) m k))
())))
(define range
(lambda (n m . args)
(if args
(seqby n m (car args))
(seq n m))))
(define abs
(lambda (n)
(if (< n 0)
(- 0 n)
n)))
(define frac (lambda (n) (- n (int n))))
(define truncate int)
(define floor
(lambda (n)
(int
(if (< n 0)
(- n 1)
n))))
(define ceiling (lambda (n) (- 0 (floor (- 0 n)))))
(define round (lambda (n) (+ (floor n) 0.5)))
(define mod (lambda (n m) (- n (* m (int (/ n m))))))
(define gcd
(lambda (n m)
(if (eq? m 0)
n
(gcd m (mod n m)))))
(define lcm (lambda (n m) (/ (* n m) (gcd n m))))
(define even? (lambda (n) (eq? (mod n 2) 0)))
(define odd? (lambda (n) (eq? (mod n 2) 1)))
(define curry (lambda (f x) (lambda args (f x . args))))
(define compose (lambda (f g) (lambda args (f (g . args)))))
(define Y (lambda (f) (lambda args ((f (Y f)) . args))))
(define reveal (lambda (f) (cons 'lambda (cons (car (car f)) (cons (cdr (car f)) ())))))
(define defun (macro (f v x) (list 'define f (list 'lambda v x))))