-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.scm
221 lines (172 loc) · 5.73 KB
/
lists.scm
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
;More built-in functions to manipulate lists.
;refer to R5RS for specification
;using only special forms and primitives (e.g., car, cdr, cons, null?, pair?, and apply)
;and also other functions we have written in math.scm
(load "math.scm")
;caar ;; (caar '((1 2) (3 4) (5 6) (7 8))) ==> 1
;cadr ;; (cadr '((1 2) (3 4) (5 6) (7 8))) ==> (3 4)
;...
;cddddr ;; (cddddr '((1 2) (3 4) (5 6) (7 8))) ==> ()
;
;list ;; (list 1 2 3) ==> (1 2 3)
;list? ;; (cons 1 2) ==> #f
;length ;; (length '(1 2 3)) ==> 3
;list-ref ;; (list-ref '(0 1 2 3 4) 3) ==> 3
;list-tail ;; (list-tail '(0 1 2 3 4) 3) ==> (3 4)
;member ;; (member 'a '(1 2 3 a b c)) ==> (a b c)
;assq ;; (assq 2 '((0 a) (1 b) (2 c) (3 d))) ==> (2 c)
;append ;; (append '(a b) '(c d)) ==> (a b c d)
;reverse ;; (reverse '(1 2 3)) ==> (3 2 1)
;
;map ;; (map (lambda (x) (* x x)) '(1 2 3)) ==> (1 4 9)
;filter ;; (filter odd? '(1 2 3)) ==> (1 3)
;foldl ;; (foldl cons '() '(1 2 3)) ==> (3 2 1)
;foldr ;; (foldr cons '() '(1 2 3)) ==> (1 2 3)
(define caar
(lambda (lst)
(if (pair? lst)
(if (pair? (car lst))
(car (car lst))
(evaluationError "caar expects pair as input for (car arg)"))
(evaluationError "caar expects pair as input"))))
(define cadr
(lambda (lst)
(if (pair? lst)
(if (pair? (cdr lst))
(car (cdr lst))
(evaluationError "cadr expects pair as input for (cdr arg)"))
(evaluationError "cadr expects pair as input"))))
(define list
(lambda x x))
(define list?
(lambda (lst)
(if (pair? lst)
(list? (cdr lst))
(null? lst))))
(define length
(lambda (lst)
(if (list? lst)
(if (null? lst)
0
(+ 1 (length (cdr lst))))
(evaluationError "length expects list as input"))))
(define list-ref
(lambda (lst num)
(if (and (list? lst) (>= num 0))
(if (= num 0)
(car lst)
(list-ref (cdr lst) (- num 1)))
(evaluationError "list-ref expects list and non-negative integer as input"))))
(define list-tail
(lambda (lst num)
(if (and (list? lst) (>= num 0))
(if (= num 0)
lst
(list-tail (cdr lst) (- num 1)))
(evaluationError "list-tail expects list and non-negative integer as input"))))
(define member
(lambda (v lst)
(if (list? lst)
(cond
((null? lst) #f)
((equal? v (car lst)) lst)
(else
(member v (cdr lst))))
(evaluationError "member expects list as one of the input"))))
(define assq
(lambda (v lst)
(if (list? lst)
(cond
((null? lst) #f)
((eq? v (caar lst)) (car lst))
(else (assq v (cdr lst))))
(evaluationError "assp expects list as one of the input"))))
(define append
(lambda x
(letrec ((helper (lambda (x y)
(if (null? y)
x
(letrec ((helper2 (lambda (x)
(if (null? x)
(helper (car y) (cdr y))
(if (pair? x)
(cons (car x) (helper2 (cdr x)))
(evaluationError "append expects pairs as input")
)))))
(helper2 x))))))
(helper (quote ()) x))))
(define reverse
(lambda (lst)
(if (list? lst)
(letrec ((helper
(lambda (lst result)
(if (null? lst)
result
(helper (cdr lst)
(cons (car lst) result))))))
(helper lst (quote ())))
(evaluationError "reverse expects a list as input"))))
(define map
(lambda (f lst)
(if (list? lst)
(if (null? lst)
(quote ())
(cons (f (car lst)) (map f (cdr lst))))
(evaluationError "map expects list as one of its input"))))
(define cadr
(lambda (lst)
(car (cdr lst))))
(define cdar
(lambda (lst)
(cdr (car lst))))
(define cddr
(lambda (lst)
(cdr (cdr lst))))
(define caaar
(lambda (lst)
(car (car (car lst)))))
(define caadr
(lambda (lst)
(car (car (cdr lst)))))
(define cadar
(lambda (lst)
(car (cdr (car lst)))))
(define caddr
(lambda (lst)
(car (cdr (cdr lst)))))
(define cdaar
(lambda (lst)
(cdr (car (car lst)))))
(define cdadr
(lambda (lst)
(cdr (car (cdr lst)))))
(define cddar
(lambda (lst)
(cdr (cdr (car lst)))))
(define cdddr
(lambda (lst)
(cdr (cdr (cdr lst)))))
;taken from class lecture
(define filter
(lambda (f lst)
(cond ((null? lst)
(quote ()))
((f (car lst))
(cons (car lst)
(filter f (cdr lst))))
(else (filter f (cdr lst))))))
;taken from class lecture
(define foldl
(lambda (f init lst)
(if (null? lst)
init
(foldl f
(f (car lst) init)
(cdr lst)))))
;;taken from class lecture
(define foldr
(lambda (f init lst)
(if (null? lst)
init
(f (car lst)
(foldr f init (cdr lst))))))