SICP Study

5A Assignment, State, and Side-Effects

Part 1

Introducing assignment

Functional programming

Assignment and Time

Identity

Factorial example

(define (fact n)
  (define (iter p i)
    (if (> i n) m (iter (* i p) (+ i 1))))
  (iter 1 1))
(define (fact n)
  (let ((p 1) (i 1))
    (define (loop)
      (cond ((> i n) p)
            (else (set! p (* i p))
                  (set! i (+ i 1))
                  (loop))))
    (loop)))

Part 2

Environment model

Free and bound variables

Environments, frames, and bindings

Procedure creation and application

Part 3

Counter example

(define make-counter
  (lambda (n)
    (lambda ()
      (set! n (inc n))
      n)))

(define c1 (make-counter 0))
(define c2 (make-counter 10))

(c1)
→ 1

(c1)
→ 2

(c2)
→ 11

(c2)
→ 12

Objects

Highlights

For example, here I am, I am a particular person, a particular object. Now, I can take out my knife, and cut my fingernail. A piece of my fingernail has fallen off onto the table. I believe I am the same person I was a second ago, but I’m not physically the same in the slightest. I have changed. Why am I the same? What is the identity of me? I don’t know. Except for the fact that I have some sort of identity. And so, I think by introducing assignment and objects, we have opened ourselves up to all the horrible questions of philosophy that have been plaguing philosophers for some thousands of years about this sort of thing. It’s why mathematics is a lot cleaner. Sussman

Actions and identity

Uses of objection orientation