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
|
;;;; lib.lith
;;;; the standard library of lith
(define (list . args) args)
(define (caar x) (car (car x)))
(define (cadr x) (car (cdr x)))
(define (cdar x) (cdr (car x)))
(define (cddr x) (cdr (cdr x)))
(define (not x) (if x #f #t))
(define (pair? p)
(eq? (typeof p) 'pair))
(define (integer? i)
(eq? (typeof i) 'integer))
(define (number? n)
(eq? (typeof n) 'number))
(define (boolean? n)
(eq? (typeof n) 'boolean))
(define (string? s)
(eq? (typeof s) 'string))
(define (foldl f init lst)
(if (nil? lst)
init
(foldl f (f init (car lst)) (cdr lst))))
(define (map f lst)
(if (nil? lst)
()
(cons (f (car lst)) (map f (cdr lst)))))
(define (foldr f init lst)
(if (nil? lst)
init
(f (car lst) (foldr f init (cdr lst)))))
(define (last lst)
(if (nil? lst)
()
(if (nil? (cdr lst))
(car lst)
(last (cdr lst)))))
(define (reverse lst)
(foldl (lambda (a x) (cons x a)) () lst))
(define (append a b)
(foldr cons b a))
(define-macro (quasiquote x)
(if (pair? x)
(if (eq? (car x) 'unquote)
(cadr x)
(if (if (pair? (car x)) (eq? (caar x) 'unquote-splicing) #f)
(list 'append
(cadr (car x))
(list 'quasiquote (cdr x)))
(list 'cons
(list 'quasiquote (car x))
(list 'quasiquote (cdr x)))))
(list 'quote x)))
(define (flip f)
(lambda (a b)
(f b a)))
(define-macro (and . x)
(if (nil? x)
#t
`(if ,(car x) (and . ,(cdr x)) #f)))
(define-macro (or . x)
(if (nil? x)
#f
`(if ,(car x) #t (or . ,(cdr x)))))
(define-macro (let env . body)
`((lambda ,(map car env) . ,body) . ,(map cadr env)))
(define (numeric? x)
(or (integer? x) (number? x)))
(define (+ . n)
(foldl :+ 0 n))
(define (* . n)
(foldl :* 1 n))
(define (- . n)
(if (nil? n)
0
(foldl :- (car n) (cdr n))))
(define (/ . n)
(if (nil? n)
1
(foldl :/ (car n) (cdr n))))
(define infty (:/ 1.0 0.0))
(define -infty (:/ -1.0 0.0))
(define (:<= a b) (not (:> a b)))
(define (:>= a b) (not (:< a b)))
(define (< a b . c)
(if (nil? c)
(:< a b)
(and (:< a b) (apply < (cons b c)))))
(define (> a b . c)
(if (nil? c)
(:> a b)
(and (:> a b) (apply > (cons b c)))))
(define (= a b . c)
(if (nil? c)
(:== a b)
(and (:== a b) (apply = (cons b c)))))
(define (<= a b . c)
(if (nil? c)
(:<= a b)
(and (:<= a b) (apply <= (cons b c)))))
(define (>= a b . c)
(if (nil? c)
(:>= a b)
(and (:>= a b) (apply >= (cons b c)))))
(define (!= a b)
(not (:== a b)))
(define (mod a b) (:% a b))
(define-macro (begin a . body)
`((lambda () ,a . ,body)))
(define-macro (cond . body)
(if (nil? body)
(error "cond: no else clause")
(if (not (pair? (car body)))
(error "cond: expecting a list as clause")
(if (eq? (caar body) 'else)
`(begin . ,(cdar body))
`(if ,(caar body)
(begin . ,(cdar body))
(cond . ,(cdr body)))))))
(define (sign x)
(cond
((not (numeric? x)) (error "sign: input must be numeric"))
((< x 0) -1)
((> x 0) 1)
(else 0)))
(define (filter f lst)
(if (nil? lst)
()
(let ((rest (filter f (cdr lst)))
(cur (car lst)))
(if (f cur)
(cons cur rest)
rest))))
(define (abs x)
(if (< x 0) (- x) x))
(define (divides a b)
(= (mod a b) 0))
(define (1+ x) (+ x 1))
(define (1- x) (- x 1))
(define (range a b)
(if (> a b)
()
(cons a (range (1+ a) b))))
(define (o f g) (lambda (x) (f (g x))))
(define (for-each f lst)
(if (nil? lst)
()
(begin (f (car lst)) (for-each f (cdr lst)))))
|