-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_cons.cl
117 lines (107 loc) · 2.46 KB
/
03_cons.cl
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
;; Chapter 3
;; rember
;; removes first occurance of matching atom from a list of atoms
(defun rember (a lat)
(cond
((null lat) '())
((eq a (car lat)) (cdr lat))
(t (cons (car lat) (rember a (cdr lat) )))
)
)
;; firsts
;; returns a list consisting of each first element of a list of lists
(defun firsts (l)
(cond
((null l) '())
(t (cons (car (car l)) (firsts (cdr l))))
)
)
;; insertR
;; inserts an atom after the occurance of another in a lat
(defun insertR (new old lat)
(cond
((null lat) '())
((eq old (car lat))
(cons old (cons new (cdr lat))))
(t
(cons (car lat) (insertR new old (cdr lat))))
)
)
;; insertL
;; inserts an atom before the occurance of another in a lat
(defun insertL (new old lat)
(cond
((null lat) '())
((eq old (car lat))
(cons new lat))
(t
(cons (car lat) (insertL new old (cdr lat))))
)
)
;; _subst (subst alredy a sbcl builtin)
;; substitute one atom for another in a lat
(defun _subst (new old lat)
(cond
((null lat) '())
((eq old (car lat))
(cons new (cdr lat)))
(t
(cons (car lat) (_subst new old (cdr lat))))
)
)
;; subst2
;; replace the first of two occurances in a lat
(defun subst2 (new o1 o2 lat)
(cond
((null lat) '())
((or (eq o1 (car lat))
(eq o2 (car lat)))
(cons new (cdr lat)))
(t
(cons (car lat) (subst2 new o1 o2 (cdr lat))))
)
)
;; multirember
;; removes all occurances of an atom in a lat
(defun multirember (a lat)
(cond
((null lat) '())
((eq a (car lat))
(multirember a (cdr lat)))
(t
(cons (car lat) (multirember a (cdr lat))))
)
)
;; multiinsertR
;; inserts an atom after the occurances of another in a lat
(defun multiinsertR (new old lat)
(cond
((null lat) '())
((eq old (car lat))
(cons old (cons new (multiinsertR new old (cdr lat)))))
(t
(cons (car lat) (multiinsertR new old (cdr lat))))
)
)
;; multiinsertL
;; inserts an atom before the occurances of another in a lat
(defun multiinsertL (new old lat)
(cond
((null lat) '())
((eq old (car lat))
(cons new (cons old (multiinsertL new old (cdr lat)))))
(t
(cons (car lat) (multiinsertL new old (cdr lat))))
)
)
;; multisubst
;; substitute all atoms for another in a lat
(defun multisubst (new old lat)
(cond
((null lat) '())
((eq old (car lat))
(cons new (multisubst new old (cdr lat))))
(t
(cons old (multisubst new old (cdr lat))))
)
)