Structure and
Interpretation
of Computer
Programs

Fall 2021
course
site
-->

Thu Oct 14

Long weekend ahead! No class on Monday; no assignment due next Thursday.

Today: finish up with chapter 2 material; see below.

I've posted my answers to the homework (including the picture language stuff).

Next: "local state variables" - chapter 3.


Before talking about the homework, here are a few scheme built-ins and ideas we haven't discussed yet :


OK, the homework :


And a bit more on the stuff we're heading towards next : here's a new scheme primitive : set! .

(define a 3)
(set! a 5)
(display a)  ;; a is now 5

But is that any different that this :

(define a 3)
(define a 5)
(display a)  ;; a is now 5

The answer is yes, these are actually quite different internally : it's the difference between creating a new thing (with a new address in memory) and editing (i.e. replacing the value) of a previous thing.

The ! in set! is another naming convention; it indicates an "in place edit".

Can you find a similar difference in python?


from 3.1.1 in the text , let's take a look at this :

(define (make-withdraw balance)
     (λ (amount) 
       (if (>= balance amount)
           (begin (set! balance (- balance amount))
                  balance)
           "Insufficient funds")))
(define w1 (make-withdraw 100)
(define w2 (make-withdraw 1000)
(w1 10)
(w1 10)
(w2 10

Discuss what's going on. What happens if we try to do this without set! ?

aside

https://cs.bennington.college /courses /fall2021 /sicp /notes /10-14
last modified Thu October 14 2021 12:33 am