-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter3.scm
97 lines (86 loc) · 2.46 KB
/
chapter3.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
;;; REMBER
;; old version
;(define rember
; (lambda (a lat)
; (cond
; ((null? lat) (quote ()))
; (else (cond
; ((eq? (car lat) a) (cdr lat))
; (else (cons (car lat)
; (rember a (cdr lat)))))))))
;; refactored version (flattened down the cond)
(define rember
(lambda (a lat)
(cond
((null? lat) (quote ()))
((eq? (car lat) a) (cdr lat))
(else (cons (car lat)
(rember a (cdr lat)))))))
;;; FIRSTS
(define firsts
(lambda (l)
(cond
((null? l) (quote ()))
(else (cons (car (car l)) (firsts (cdr l)))))))
;;; insertR
(define insertR
(lambda (new old lat)
(cond
((null? lat) (quote ()))
(else (cond
((eq? (car lat) old)
(cons old
(cons new (cdr lat))))
(else (cons (car lat)
(insertR new old (cdr lat)))))))))
;;; insertL
(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)))))))
;;; subst
(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)))))))
;;; subst2
(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)))))))
;;; multirember
(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)))))))
;;; multiinsertR
(define multiinsertR
(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)))))))
;;; multiinsertL
(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)))))))
;;; multisubst
(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)))))))