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 :
eq?
, =
, equal?
, and all that(memq 3 '(1 2 3 4)) is (3 4)
... this is a "find-member-in-list" function, with a badly-chosen name. (Lisp has quite a bit of that, IMHO.)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!
?