-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.scm
110 lines (87 loc) · 2.64 KB
/
3.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
;;; -*- buffer-read-only:t -*-
(load "2.scm")
(define (rember a lat)
"remove the first occurence of a member"
(cond ((null? lat) '())
((eq? a (car lat)) (cdr lat))
(else (cons (car lat)
(rember a (cdr lat))))))
;;; (rember 'min '(lamb chops and mint jelly))
(define (firsts l)
(cond ((null? l) l)
(else (cons (car (car l))
(firsts (cdr l))))))
(define (seconds l)
(cond ((null? l) l)
(else (cons (car (cdr (car l)))
(seconds (cdr l))))))
;; (firsts '((a 1)
;; (b 2)
;; (c 3)
;; (d 4) ) )
;; (seconds '((a 1)
;; (b 2)
;; (c 3)
;; (d 4) ) )
(define (insertR new old lat)
"insert `new` at the right of the first occurrece of `old` in `lat`"
(cond ((null? lat) '())
((eq? old (car lat))
(cons old (cons new (cdr lat))))
(else (cons (car lat)
(insertR new old (cdr lat))))))
;;; (insertR 'e 'x '(a b c d f g h))
(define (insertL new old lat)
"insert `new` at the left of the first occurrece of `old` in `lat`"
(cond ((null? lat) '())
((eq? old (car lat))
(cons new lat))
(else (cons (car lat)
(insertL new old (cdr lat))))))
;;; (insertL 'e 'f '(a b c d f g h))
(define (subst new old lat)
"substitute first occurrece of `old` with `new` in `lat`"
(cond ((null? lat) '())
((eq? old (car lat))
(cons new (cdr lat)))
(else (cons (car lat)
(subst new old (cdr lat))))))
;;; (subst 'x 'a '(a b c d f g h))
(define (subst2 new o1 o2 lat)
"substitute first occurrece of `o1` or `o2` with `new` in `lat`"
(cond ((null? lat) '())
((or (eq? o1 (car lat))
(eq? o2 (car lat)))
(cons new (cdr lat)))
(else (cons (car lat)
(subst2 new o1 o2 (cdr lat))))))
;;; (subst2 'x 'h 'c '(a b c d f g h))
(define (multirember a lat)
"removes all occurrences of `a` from `lat`"
(cond ((null? lat) '())
((eq? (car lat) a)
(multirember a (cdr lat)))
(else (cons (car lat)
(multirember a (cdr lat))))))
;;; (multirember 'a '(a b c a x a y z a))
(define (multiinsertR new old lat)
(cond ((null? lat) '())
((eq? (car lat) old)
(cons old (cons new (multiinsertR new old (cdr lat)))))
(else (cons (car lat)
(multiinsertR new old (cdr lat))))))
;;; (multiinsertR 'w 'a '(a b c a d a f g a))
(define (multiinsertL new old lat)
(cond ((null? lat) '())
((eq? (car lat) old)
(cons new (cons old (multiinsertL new old (cdr lat)))))
(else (cons (car lat)
(multiinsertL new old (cdr lat))))))
;;; (multiinsertL 'w 'a '(a b c a d a f g a))
(define (multisubst new old lat)
(cond ((null? lat) '())
((eq? (car lat) old)
(cons new (multisubst new old (cdr lat))))
(else (cons (car lat)
(multisubst new old (cdr lat))))))
;;; (multisubst 'w 'a '(a b c a d a f g a))