-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.lisp
227 lines (196 loc) · 4.36 KB
/
stdlib.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
(def True true)
(def False false)
(def head (lambda (l) (index 0 l)))
(def tail (lambda (l) (drop 1 l)))
(def empty (lambda (l) (= 0 (len l))))
(def add (lambda (x y) (+ x y)))
(def sub (lambda (x y) (- x y)))
(def mul (lambda (x y) (* x y)))
(def div (lambda (x y) (/ x y)))
(def intdiv (lambda (x y) (// x y)))
(def mod (lambda (x y) (% x y)))
(def sqrt (lambda (x) (pow x 0.5)))
(def last_n (lambda (n l) (drop (- (len l) n) l)))
(def even (lambda (x) (= 0 (mod x 2))))
(def odd (lambda (x) (= 1 (mod x 2))))
(def last (lambda (l) (index (- (len l) 1) l)))
(def or
(lambda (x y)
(if (x)
(True)
(if (y)
(True)
(False)))))
(def and
(lambda (x y)
(if (x)
(if (y)
(True)
(False))
(False))))
(def not
(lambda (x)
(if (x)
(False)
(True))))
(def filter
(lambda (f l)
(let (rec-filter (lambda (f l acc)
(if (empty l)
acc
(let (x (head l)) (xs (tail l))
(if (f x)
(rec-filter f xs (append x acc))
(rec-filter f xs acc))))))
(rec-filter f l (list)))))
(def fold
(lambda (f acc l)
(if (empty l)
acc
(fold f (f acc (head l)) (tail l)))))
(def reduce
(lambda (f l)
(fold f (head l) (tail l))))
(def sum
(lambda (l)
(reduce add l)))
(def product
(lambda (l)
(reduce mul l)))
(def zip
(lambda (l1 l2)
(let (rec-zip (lambda (l1 l2 acc)
(if (or (empty l1) (empty l2))
acc
(rec-zip
(tail l1)
(tail l2)
(append (list (head l1) (head l2)) acc)))))
(rec-zip l1 l2 (list)))))
(def collatz
(lambda (x)
(if (= 1 x)
(list 1)
(if (= 0 (mod x 2))
(cat (list x) (collatz (// x 2)))
(cat (list x) (collatz (+ 1 (* 3 x))))))))
(def fibonacci
(lambda (n)
(let (rec-fibonacci
(lambda (acc)
(if (< n (len acc))
(tail acc)
(if (> (len acc) 2)
(rec-fibonacci (append (sum (last_n 2 acc)) acc))
(rec-fibonacci (append 1 acc))))))
(rec-fibonacci (list)))))
(def isprime
(lambda (x)
(if (<= x 1)
False
(if (= x 2)
True
(let (rec-isprime (lambda (x acc max_num)
(if (>= acc max_num)
True
(if (= 0 (mod x acc))
False
(rec-isprime x (+ 1 acc) max_num)))))
(rec-isprime x 2 (+ 1 (sqrt x))))))))
(def map
(lambda (f l)
(let (rec-map (lambda (f l a)
(if (empty l)
a
(rec-map f (tail l) (append (f (head l)) a)))))
(rec-map f l (list)))))
(def quicksort
(lambda (mylist)
(if (empty mylist)
(list)
(cat (quicksort (filter (lambda (x) (< x (head mylist))) (tail mylist)))
(list (head mylist))
(quicksort (filter (lambda (x) (>= x (head mylist)))
(tail mylist)))))))
(def gcd
(lambda (a b)
(if (= 0 b)
a
(gcd b (mod a b)))))
(def max
(lambda (l)
(fold (lambda (a b)
(if (> a b)
a
b))
(head l) (tail l))))
(def min
(lambda (l)
(fold (lambda (a b)
(if (< a b)
a
b))
(head l) (tail l))))
(def deriv
(lambda (f x)
(/
(- (f (+ x 0.001))
(f x))
0.001)))
(def print
(lambda (x) (x)))
(def avg
(lambda (l)
(/ (sum l) (len l))))
(def congruence-classes
(lambda (n)
(filter
(lambda (x) (= 1 (gcd x n)))
(range 1 n))))
(def euler-phi
(lambda (n)
(len (congruence-classes n))))
(def factors
(lambda (n)
(filter
(lambda (x)
(let (quotient (/ n x)) (= (int quotient) quotient)))
(range 1 (+ 1 n)))))
(def factorial
(lambda (x)
(if (> x 1)
(* x (factorial (- x 1)))
1)))
(def sigma
(lambda (start end func)
(sum (map func (range start end)))))
(def exp
(lambda (x)
(sigma 0 100 (lambda (k) (/ (pow x k) (factorial k))))))
(def is-perfect-number
(lambda (x)
(=
x
(- (sum (factors x)) x))))
(def prime-factorization
(lambda (n)
(filter isprime (factors n))))
(def all
(lambda (l)
(if (empty l)
True
(let (x (head l)) (xs (tail l))
(if (not x)
False
(all xs))))))
(def any
(lambda (l)
(if (empty l)
False
(let (x (head l)) (xs (tail l))
(if x
True
(any xs))))))
(def length
(lambda (l)
(len l)))