-
Notifications
You must be signed in to change notification settings - Fork 2
/
01-toys.rkt
220 lines (144 loc) · 4.27 KB
/
01-toys.rkt
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#lang racket
(or (symbol? 'atom) (symbol? (quote atom)))
;; => #t
(symbol? 'turkey)
;; => #t
(symbol? '1492)
;; => #t
(symbol? 'u)
;; => #t
(symbol? '*abc$) ; Special characters are allowed except for "(" and ")"
;; => #t
(or (list? '(atom)) (list (quote (atom))))
;; => #t
'(atom turkey or)
;; => #t
'(atom turkey) 'or ; is not a list but a collection of a list and a symbol.
(list? '((atom turkey) or))
;; All atoms and lists are S-expressions.
(list? '(how are you doing so far))
;; => #t
;; How many S-expressions are in the list?
(length '(how are you doing so far))
;; => 6
;; But not sure if I should include the whole expression
;; itself as another S-exp.
;; How many S-expressions are in the list?
(count '(((how) are) ((you) (doing so)) (far)))
;; => 3
(list '())
;; => #t
;; Special S-expression called null (or empty) list.
(symbol? '())
;; => #f
;; Lists are not symbols.
(list? '(() () () ()))
;; => #t
(first '(a b c))
;; => 'a
(first '((a b c) x y z))
;; => '(a b c)
;; (first 'hotdog)
;; You cannot use first on an atom.
;; (first '())
;; You cannot ask for the car of the empty list.
;; The Law of Car
;; The primitive car is defined only for non-empty lists.
(car '(((hotdogs)) (and) (pickle) (relish)))
(car (car '(((hotdogs)) (and))))
;; => '(hotdogs)
(cdr '(a b c))
;; => '(b c)
(cdr '((a b c) x y z))
;; => '(x y z)
(cdr '(hamburger))
;; => '()
(cdr '((x) t r))
;; => '(t r)
;; (cdr 'hotdogs) and (cdr '()) is invalid.
;; The Law of Cdr
;; The primitive car is defined only for non-empty lists. The cdr of
;; any non-empty list is always another list.
(first (cdr '((b) (x y) ((c)))))
;; => '(x y)
(cdr (cdr '((b) (x y) ((c)))))
;; => '(((c)))
;; (cdr (first '(a (b (c)) d))) is invalid.
;; Since (first '(a (b (c)) d)) is 'a and (cdr 'a) is invalid.
;; first and cdr always takes in non-empty lists as arguments.
(cons 'peanut '(butter and jelly))
;; => '(peanut butter and jelly)
(cons '(banana and) '(peanut butter and jelly))
;; => '((banana and) (peanut butter and jelly))
(cons '((help) this) '(is very ((hard) to learn)))
;; => '(((help) this is very (hard) to learn))
;; cons takes two arguments: first one is any S-exp and second is any list.
(cons '(a b (c)) '()) ; This one was a bit tricky since I coasted
;; => '((a b (c))) ; along almost mindlessly so far.
(cons 'a '())
;; => '(a)
;; (cons '((a b c)) 'b) is invalid since 'b is not a list.
;; But (cons 'a 'b) works for any value.
(first (cons 'a 'b)) ; => 'a
(cdr (cons 'a '(b))) ; => '(b)
;; (cons 'a 'b) is invalid since 'b is not a list.
;; The Law of Cons
;; The primitive cons takes two arguments.
;; The second argument to cons must be a list.
;; The result is a list.
(cons 'a (first '((b) c d)))
;; => (cons 'a '(b))
;; => '(a b)
(cons 'a (cdr '((b) c d)))
;; => (cons 'a '(c d))
;; => '(a c d)
(and (null? '()) (null? (quote ())))
;; => #t
(null? '(a b c))
;; => #f
(null? 'spaghetti)
;; null? is only valid for lists but returns false for any presence of value.
;; I guess this is called nil punning.
;; => #f
;; The Law of Null?
;; The primitive null? is defined only for lists.
(define atom? (lambda (x) (and (not (pair? x)) (not (null? x)))))
(atom? 'harry)
;; => #t
(atom? '(Harry had a heap of apples))
;; => #f
;; atom? takes one argument and it can be any S-exp.
(atom? (first '(Harry had a heap of apples)))
;; => #t
(atom? (cdr '(Harry had a heap of apples)))
;; => #f
(atom? (cdr '(Harry)))
;; => #f since '() is not an atom
(atom? (first (cdr '(swing low sweet cherry oat))))
;; => (atom? (first '(low sweet cherry oat)))
;; => (atom? 'low)
;; => #t
(atom? (first (cdr '(swing (low sweet) cherry oat))))
;; => (atom? (first '((low sweet) cherry oat)))
;; => (atom? '(low sweet))
;; => #f
(eq? 'Harry 'Harry)
;; => #t
(eq? 'margarine 'butter)
;; => #f
;; eq? takes two arguments which are non-numeric atoms.
(eq? '() '(strawberry)) ; is invalid
;; But in practice lists may be arguments as well.
;; => #f
(eq? 6 7) ; is invalid as it needs non-numeric atoms.
(eq? (first '(Mary had a little lamb chop)) 'Mary)
;; => #t
(eq? (cdr '(soured milk)) 'milk)
;; (eq? '(milk) 'milk)
;; => #f
(define l '(beans beans we need jelly beans))
(eq? (first l) (first (cdr l)))
;; => #t
;; Because it compares the first and second atoms in the list.
;; Summary
;; Pretty basic a start.