forked from pkrumins/the-little-schemer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-toys.ss
executable file
·198 lines (162 loc) · 6.69 KB
/
01-toys.ss
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
;
; Chapter 1 of The Little Schemer:
; Toys
;
; Code examples assemled by Peteris Krumins ([email protected]).
; His blog is at http://www.catonmat.net -- good coders code, great reuse.
;
; Get yourself this wonderful book at Amazon: http://bit.ly/4GjWdP
;
; Examples of atoms:
;
'atom
(quote atom)
'turkey
1492
'*abc$
(quote *abc$)
; Examples of lists and s-expressions
;
'(atom)
(quote (atom))
'(atom turkey or)
'((atom turkey) or)
'xyz
'(x y z)
'((x y z))
'(how are you doing so far)
'(((how) are) ((you) (doing so)) far)
'()
'(() () () ())
; Example of not-lists
;
'(atom turkey) 'or ; because it's two separate s-expressions
; Example of not-atoms
;
'() ; because it's a list
; Examples of car
;
(car '(a b c)) ; 'a
(car '((a b c) x y z)) ; '(a b c)
; Examples of not-applicable car
;
; (car 'hotdog) ; not-applicable because 'hotdog is not a list
; (car '()) ; not-applicable because '() is an empty list
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The law of car: ;
; ;
; The primitive /car/ is defined only for non-empty lists. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; More examples of car
;
(car '(((hotdogs)) (and) (pickle) relish)) ; '((hotdogs))
(car (car '(((hotdogs)) (and)))) ; '(hotdogs)
; Examples of cdr
;
(cdr '(a b c)) ; '(b c)
(cdr '((a b c) x y z)) ; '(x y z)
(cdr '(hamburger)) ; '()
(cdr '((x) t r)) ; '(t r)
; Examples of not-applicable cdr
;
; (cdr 'hotdogs) ; not-applicable because 'hotdogs is not a list
; (cdr '()) ; not-applicable because '() is an empty list
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The law of cdr: ;
; ;
; The primitive /cdr/ is defined only for non-empty lists. ;
; The /cdr/ of any non-empty list is always another list. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Examples of car and cdr
;
(car (cdr '((b) (x y) ((c))))) ; '(x y)
(cdr (cdr '((b) (x y) ((c))))) ; '(((c)))
; Examples of cons
;
(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 '(a b (c)) '()) ; '((a b (c)))
(cons 'a '()) ; '(a)
; Examples of not-applicable cons
;
; (cons '((a b c)) 'b) ; not-applicable because 'b is not a list
; (cons 'a 'b) ; not-applicable because '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. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Examples of cons, car and cdr
;
(cons 'a (car '((b) c d))) ; (a b)
(cons 'a (cdr '((b) c d))) ; (a c d)
; Example of the null-list
;
'()
; Examples of null?
;
(null? '()) ; true
(null? '(a b c)) ; false
; Example of not-applicable null?
;
; (null? 'spaghetti) ; not-applicable because 'spaghetti is not a list
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The law of null? ;
; ;
; The primitive /null?/ is defined only for lists ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; We first need to define atom? for Scheme as it's not a primitive
;
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
; Examples of atom?
;
(atom? 'Harry) ; true
(atom? '(Harry had a heap of apples)) ; false
; Examples of atom?, car and cdr
;
(atom? (car '(Harry had a heap of apples))) ; true
(atom? (cdr '(Harry had a heap of apples))) ; false
(atom? (cdr '(Harry))) ; false
(atom? (car (cdr '(swing low sweet cherry oat)))) ; true
(atom? (car (cdr '(swing (low sweet) cherry oat)))) ; false
; Examples of eq?
;
(eq? 'Harry 'Harry) ; true
(eq? 'margarine 'butter) ; false
; Example of not-applicable eq?
;
; (eq? '() '(strawberry)) ; not-applicable because eq? works only on atoms
; (eq? 5 6) ; not-applicable because eq? works only on non-numeric atoms
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The law of eq? ;
; ;
; The primitive /eq?/ takes two arguments. ;
; Each must be a non-numeric atom. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Examples of eq?, car and cdr
;
(eq? (car '(Mary had a little lamb chop)) 'Mary) ; true
(eq? (car '(beans beans)) (car (cdr '(beans beans)))) ; true
; Examples of not-applicable eq?, car and cdr
;
; (eq? (cdr '(soured milk)) 'milk) ; not-applicable because (cdr '(...)) is a list
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; This space reserved for ;
; JELLY STAINS! ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Go get yourself this wonderful book and have fun with these examples!
;
; Shortened URL to the book at Amazon.com: http://bit.ly/4GjWdP
;
; Sincerely,
; Peteris Krumins
; http://www.catonmat.net
;