-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss_booleans.rkt
229 lines (151 loc) · 4.13 KB
/
ss_booleans.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
221
222
223
224
225
226
227
228
229
#lang simply-scheme
(define (greet name)
(if (equal? (first name) 'professor)
(se '(i hope i am not bothering you) 'professor (last name))
(se '(good to see you) (first name))))
(greet '(matt wright))
(greet '(professor harold abelson))
(member? 'mick '(dave dee dozy beaky mick and tich))
(member? 'mick '(john paul george ringo))
(member? 'e 'truly)
(member? 'y 'truly)
(= 3 4)
(= 67 67)
(= (+ 52 8) 60)
(> 98 97)
(before? 'zorn 'coleman)
(before? 'pete 'ringo)
(empty? '(abbey road))
(empty? '())
(empty? 'hi)
(empty? (bf (bf 'hi)))
(empty? "")
(number? 'three)
(number? 74)
(boolean? #f)
(boolean? '(the beatles))
(boolean? 234)
(boolean? #t)
(word? 'flying)
(word? '(dig it))
(word? 87)
(sentence? 'wait)
(sentence? '(what goes on))
(define (vowel? letter)
(member? letter 'aeiou))
(define (positive? number)
(> number 0))
; using predicates
(define (abs num)
(if (< num 0)
(- num)
num))
(define (divisible? big little)
(= (remainder big little) 0))
(define (buzz num)
(if (or (divisible? num 7) (member? 7 num))
'buzz
num))
(buzz 17)
(buzz 42)
(buzz 124)
;(define (plural wd)
; (word wd 's))
(plural 'beatle)
(plural 'computer)
(plural 'fly) ; ah, but incorrect. how do we make this correct?
(define (plural wd)
(if (equal? (last wd) 'y)
(word (bl wd) 'ies)
(word wd 's)))
(plural 'beatle)
(plural 'computer)
(plural 'fly)
; special forms
; if is a special form
(if (= 3 3)
'sure
(factorial 1000))
; and & or are also special forms
(define (divisible? big small)
(= (remainder big small) 0))
(define (num-divisible-by-4? x)
(and (number? x) (divisible? x 4)))
(num-divisible-by-4? 16)
(num-divisible-by-4? 6)
(num-divisible-by-4? 'aardvark)
(divisible? 'aardvark 4)
; everything that isn't false is true
(define (divisible? big little)
(= (remainder big little) 0))
(if (+ 3 4) 'yes 'no)
(define (integer-quotient big little)
(if (divisible? big little)
(/ big little)
#f))
(integer-quotient 27 3)
(integer-quotient 12 5)
; returns a true result as soon as it evaluates true
(or #f 3 #f 4)
; returns a true result only if all arguments are true
(and 1 2 3 4 5)
(define (integer-quotient big little) ; alternate version
(and (divisible? big little)
(/ big little)))
(define (roman-value letter)
(if (equal? letter 'i)
1
(if (equal? letter 'v)
5
(if (equal? letter 'x)
10
(if (equal? letter 'l)
50
(if (equal? letter 'c)
100
(if (equal? letter 'd)
500
(if (equal? letter 'm)
1000
'huh?))))))))
(define (roman-value letter)
(cond ((equal? letter 'i) 1)
((equal? letter 'v) 5)
((equal? letter 'x) 10)
((equal? letter 'l) 50)
((equal? letter 'c) 100)
((equal? letter 'd) 500)
((equal? letter 'm) 1000)
(else 'huh?)))
(define (truefalse value)
(cond (value 'true)
(else 'false)))
(truefalse (= 2 (+ 1 1)))
(truefalse (= 5 (+ 2 2)))
; most restrictive test goes first
(cond ((number? (first sent)) &hellip)
((empty? sent) &hellip)
& hellip)
;; wrong, because the first test only makes sense once you've established that there is a first word of the sentence
(cond ((empty? sent) &hellip)
((and (not (empty? sent)) (number? (first sent))) &hellip)
&hellip)
;; this is also unnecessary because the sentence is nonempty once we get as far as the second clause
; if is composable
(define (greet name)
(if (equal? (first name) 'professor)
(se '(pleased to meet you)
'professor
(last name)
'(- how are you?))
(se '(pleased to meet you)
(first name)
'(- how are you?))))
(greet '(brian epstein))
(greet '(professor donald knuth))
(define (greet name)
(se '(pleased to meet you)
(if (equal? (first name) 'professor)
(se 'professor (last name))
(first name))
'(- how are you?)))