forked from felipap/sicp-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1B.scm
206 lines (144 loc) · 2.83 KB
/
1B.scm
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
; Lecture: 1B
; Lecturer: Gerald Jay Sussman
; BOARD 0:01:30
(define (sos x y)
(+ (sq x) (sq y)))
(define (sq x)
(* x x))
(sos 3 4) → 25
; END BOARD
; BOARD 0:04:10
Kinds of Expressions
Numbers
Symbols
λ-Expressions
Definitions
Conditionals
Combinations
; END BOARD
; SLIDE 0:05:50
Substitution Rule
To evaluate an application
Evaluate the operator to get procedure
Evaluate the operands to get arguments
Apply the procedure to the arguments
Copy the body of the procedure,
substituting the arguments supplied
for the formal parameters of the procedure.
Evaluate the resulting new body.
; END SLIDE
; BOARD 0:07:30
(sos 3 4)
(+ (sq 3) (sq 4))
(+ (sq 3) (* 4 4))
(+ (sq 3) 16)
(+ (* 3 3) 16)
(+ 9 16)
25
; END BOARD
; SLIDE 0:12:30
To evaluate an IF expression
Evaluate the predicate expression
if it yields TRUE
evaluate the consequent expression
otherwise
evaluate the alternative expression
; END SLIDE
; BOAD 0:12:45
(if <predicate>
<consequent>
<alternative>)
; END BOARD
; BOAD 0:13:55
(define (+ x y)
(if (= x 0)
y
(+ (-1+ x) (1+ y))))
; END BOAD
; SLIDE 0:14:30
(define (+ x y)
(if (= x 0)
y
(+ (-1+ x) (1+ y))))
(+ 3 4)
(if (= 3 0) 4 (+ (-1+ 3) (1+ 4)))
(+ (-1+ 3) (1+ 4))
(+ (-1+ 3) 5)
(+ 2 5)
(if (= 2 0) 5 (+ (-1+ 2) (1+ 5)))
(+ (-1+ 2) (1+ 5))
(+ (-1+ 2) 6)
(+ 1 6)
(if (= 1 0) 6 (+ (-1+ 1) (1+ 6)))
(+ (-1+ 1) (1+ 6))
(+ (-1+ 1) 7)
(+ 0 7)
(if (= 0 0) 7 (+ (-1+ 0) (1+ 7)))
7
; END SLIDE
; BREAK 0:16:50
; SLIDE 0:19:15
Peano Arithmetic
Two ways to add whole numbers:
(define (+ x y)
(if (= x 0)
y
(+ (-1+ x) (1+ y))))
(define (+ x y)
(if (= x 0)
y
(1+ (+ (-1+ x) y))))
; END SLIDE
; BOARD 0:21:35
(define (+ x y)
(if (= x 0)
y
(+ (-1+ x) (1+ y))))
; END BOARD
; BOARD 0:22:05
(+ 3 4) Iteration
(+ 2 5) time = O(x)
(+ 1 6) space = O(1)
(+ 0 7)
7
; END BOARD
; BOARD 0:23:15
(define (+ x y)
(if (= x 0)
y
(1+ (+ (-1+ x) y))))
; END BOARD
; BOARD 0:24:00
(+ 3 4) Linear Recursion
(1+ (+ 2 4)) time = O(x)
(1+ (1+ (+ 1 4))) space = O(x)
(1+ (1+ (1+ (+ 0 4))))
(1+ (1+ (1+ 4)))
(1+ (1+ 5))
(1+ 6)
7
; END BOARD
; BREAK 0:36:45
; BOARD 0:39:25
n: 0 1 2 3 4 .... 10
fib: 0 1 1 2 3 5 8 13 21 34 55
(define (fib n)
(if (< n 2))
n
(+ (fib (- n 1))
(fib (- n 2))))
; END BOARD
; BOARD 0:49:45
(define (move n from to spare)
(cond ((= n 0) "DONE")
(else
(move (-1+ n) from spare to)
(print-move from to)
(move (-1+ n) spare to from))
; END BOARD
; BOARD 0:53:00
(move 4 1 2 3)
(move 3 1 3 2)
(move 2 1 2 3)
(move 1 1 3 2)
; END BOARD