-
Notifications
You must be signed in to change notification settings - Fork 2
/
chapter03.scm
99 lines (87 loc) · 2.47 KB
/
chapter03.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
; The Little Schemer Chapter 3 Exercises
(load "common.scm")
; Remove a member
(define rember
(lambda (a lat)
(cond
((null? lat) '())
((eq? (car lat) a) (cdr lat))
(else (cons (car lat)
(rember a (cdr lat)))))))
; Grab first item from each element in a list
(define firsts
(lambda (l)
(cond
((null? l) (quote ()))
(else (cons (car (car l)) (firsts (cdr l)))))))
; Insert to the right
(define insertR
(lambda (new old lat)
(cond
((null? lat) (quote ()))
((eq? (car lat) old)
(cons old (cons new (cdr lat))))
(else
(cons (car lat)
(insertR new old (cdr lat)))))))
; Insert to the left
(define insertL
(lambda (new old lat)
(cond
((null? lat) (quote()))
((eq? (car lat) old)
(cons new lat))
(else
(cons (car lat)
(insertL new old (cdr lat)))))))
; Substitute
(define subst
(lambda (new old lat)
(cond
((null? lat) (quote ()))
((eq? (car lat) old) (cons new (cdr lat)))
(else (cons (car lat)
(subst new old (cdr lat)))))))
; Substitute two
(define subst2
(lambda (new o1 o2 lat)
(cond
((null? lat) (quote()))
((or (eq? (car lat) o1) (eq? (car lat) o2)) (cons new (cdr lat)))
(else (cons (car lat)
(subst2 new o1 o2 (cdr lat)))))))
; Remove all members
(define multirember
(lambda (a lat)
(cond
((null? lat) (quote ()))
((eq? (car lat) a) (multirember a (cdr lat)))
(else (cons (car lat)
(multirember a (cdr lat)))))))
; Multi insert right
(define mutliinsertR
(lambda (new old lat)
(cond
((null? lat) (quote ()))
((eq? (car lat) old) (cons old
(cons new (multiinsertR new old (cdr lat)))))
(else (cons (car lat)
(multiinsertR new old (cdr lat)))))))
; Multi insert left
(define multiinsertL
(lambda (new old lat)
(cond
((null? lat) (quote ()))
((eq? (car lat) old) (cons new
(cons old
(multiinsertL new old (cdr lat)))))
(else (cons (car lat)
(multiinsertL new old (cdr lat)))))))
; Multi substitute
(define multisubst
(lambda (new old lat)
(cond
((null? lat) (quote ()))
((eq? (car lat) old) (cons new (multisubst new old (cdr lat))))
(else (cons (car lat)
(multisubst new old (cdr lat)))))))