-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_stars.cl
178 lines (165 loc) · 3.2 KB
/
05_stars.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
;; Chapter 5
;; atom?
;; checks if element is atom
(defun atom? (x)
(not (listp x))
)
;; add1
;; ads 1 to a number
(defun add1 (x)
(+ x 1)
)
;; equan?
;; check if two atom s are equal
(defun equan? (x y)
(cond
((and (numberp x) (numberp y)) (= x y))
((or (numberp x) (numberp y)) nil)
(t
(eq x y))
)
)
;; rember*
;; removes all matching members in a l
(defun rember* (a l)
(cond
((null l) '())
((atom? (car l))
(cond
((eq a (car l)) (rember* a (cdr l)))
(t (cons (car l) (rember* a (cdr l))))
)
)
(t
(cons (rember* a (car l)) (rember* a (cdr l))))
)
)
;; insertR*
;; inserts element to the right of another in a l
(defun insertR* (new old l)
(cond
((null l) '())
((atom? (car l))
(cond
((eq old (car l))
(cons (car l) (cons new (insertR* new old (cdr l)))))
(t (cons (car l) (insertR* new old (cdr l))))
)
)
(t
(cons (insertR* new old (car l)) (insertR* new old (cdr l))))
)
)
;; occur*
;; counts occurances of element in list
(defun occur* (a l)
(cond
((null l) 0)
((atom? (car l))
(cond
((eq a (car l))
(add1 (occur* a (cdr l))))
(t
(occur* a (cdr l)))
)
)
(t
(+ (occur* a (car l)) (occur* a (cdr l))))
)
)
;; subst*
;; replace an element with another one in a list
(defun subst* (new old l)
(cond
((null l) '())
((atom? (car l))
(cond
((eq old (car l))
(cons new (subst* new old (cdr l))))
(t
(cons (car l) (subst* new old (cdr l))))
)
)
(t
(cons (subst* new old (car l)) (subst* new old (cdr l))))
)
)
;; insertL*
;; insert an element after nother on e in a l
(defun insertL* (new old l)
(cond
((null l) '())
((atom? (car l))
(cond
((eq old (car l))
(cons new (cons old (insertL* new old (cdr l)))))
(t
(cons (car l) (insertL* new old (cdr l))))
)
)
(t
(cons (insertL* new old (car l)) (insertL* new old (cdr l))))
)
)
;; member*
;; checks if element is in list
(defun member* (a l)
(cond
((null l) nil)
((atom? (car l))
(cond
((eq a (car l))
t)
(t
(member* a (cdr l)))
)
)
(t
(or (member* a (car l)) (member* a (cdr l))))
)
)
;; leftmost
;; returns the leftmost element of a list
(defun leftmost (l)
(cond
((null l) nil)
((atom? (car l))
(car l))
(t
(leftmost (car l)))
)
)
;; eqlist?
;; checks if two lists are equal
(defun eqlist? (l1 l2)
(cond
((and (null l1) (null l2)) t)
((or (null l1) (null l2)) nil)
((and (atom? (car l1)) (null l2))
nil)
((and (atom? (car l1)) (atom? (car l2)))
(and (equan? (car l1) (car l2))
(eqlist? (cdr l1) (cdr l2))
)
)
((atom? (car l1)) nil)
((null l2) nil)
((atom? (car l2)) nil)
(t
(and (eqlist? (car l1) (car l2))
(eqlist? (cdr l1) (cdr l2))
)
)
)
)
;; equal?
;; checks if two S-Expressions are equal
(defun equal? (s1 s2)
(cond
((and (atom? s1) (atom? s2))
(eq s1 s2))
((or (atom? s1) (atom? s2)) nil)
(t
(eqlist? s1 s2))
)
)