-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-toys.ss
172 lines (141 loc) · 5.01 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
; Chapter 1
; Examples of atom
;
'atom
(quote atom)
'turkey
1492
'u
'*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)
0
'()
'(()()())
;Examples of not-lists
;
'(atom turkey)'or ; it's two seprate s-expressions
;Examples of not-atoms
;
'() ; 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 'hostdog 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 '(hambuger)) ; '()
(cdr '((x) t r)) ; '(t r)
;Examples of not-applicable cdr
;
;(cdr 'hotdogs) ; not-applicable because 'hostdogs 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 non-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 non-list
;
'()
; Examples of null?
;
(null? '()) ; true
(null? '(a b c)) ; false
; Example of non-applicable null?
;
(null? 'scheme) ; not-applicable because 'scheme 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))))) ; pair or 'pair?
; 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
; Examples 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 '(sound milk)) 'milk) ; not-applicable because (cdr '(...)) is a list