-
Notifications
You must be signed in to change notification settings - Fork 16
/
lists
69 lines (58 loc) · 1.51 KB
/
lists
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
; List-processing procedures
; Uses: startup
(define reverse
(lambda (L) (revappend L '())))
(define revappend
(lambda (L1 L2)
(while L1
(set! L2 (cons (car L1) L2))
(set! L1 (cdr L1)))
L2))
(define append!
(lambda (L1 L2)
(set-cdr! (last-pair L1) L2)
L1))
(define last-pair
(lambda (lst)
(while (cdr lst)
(set! lst (cdr lst)))
lst))
(define memq (lambda (x lst) (member:test eq? x lst)))
(define member (lambda (x lst) (member:test equal? x lst)))
(define member:test
(lambda (=? x lst)
(while (and lst (not (=? x (car lst))))
(set! lst (cdr lst)))
lst))
(define assq (lambda (key pairs) (assoc:test eq? key pairs)))
(define assoc (lambda (key pairs) (assoc:test equal? key pairs)))
(define assoc:test
(lambda (=? key pairs)
(while (and pairs (not (=? key (caar pairs))))
(set! pairs (cdr pairs)))
(and pairs (car pairs))))
(define sublis
(lambda (a-list exp)
(cond
((null? exp) '())
((atom? exp)
(let ((binding (assq exp a-list)))
(if binding (cdr binding) exp)))
(else
(cons (sublis a-list (car exp))
(sublis a-list (cdr exp)))))))
(define remove
(lambda (key lst)
(if (null? lst)
'()
(if (eq? key (car lst))
(cdr lst)
(cons (car lst)
(remove key (cdr lst)))))))
(define list-ref
(lambda (lst n)
(if (null? lst)
'()
(if (= n 0)
(car lst)
(list-ref (cdr lst) (- n 1))))))