SICP Study

3.3 Modeling with Mutable Data

3.3.1 Mutable List Structure

(define (cons x y)
  (let ((new (get-new-pair)))
    (set-car! new x)
    (set-cdr! new y)
    new))

Sharing and identity

Mutation is just assignment

(define (cons x y)
  (lambda (sel)
    (sel x y)))
(define (car p)
  (p (lambda (x y) x)))
(define (cdr p)
  (p (lambda (x y) y)))
(define (cons x y)
  (define (set-x! v) (set! x v))
  (define (set-y! v) (set! y v))
  (define (dispatch m)
    (cond ((eq? m 'car) x)
          ((eq? m 'cdr) y)
          ((eq? m 'set-car!) set-x!)
          ((eq? m 'set-cdr!) set-y!)
          (else (error "Undefined operation: CONS" m))))
  dispatch)

3.3.2 Representing Queues

3.3.3 Representing Tables

Two-dimensional tables

Creating local tables

(define (make-table) (lambda (k) false))
(define (lookup key table) (table key))
(define (insert! key value table)
  (lambda (k)
    (if (eq? k key)
        value
        (table k))))

3.3.4 A Simulator for Digital Circuits

Primitive function boxes

Representing wires

Highlights

The truth of the matter is that, in a language in which we can deal with procedures as objects, there is no fundamental difference between “procedures” and “data, and we can choose our syntactic sugar to allow us to program in whatever style we choose. (Footnote 3.27)

The agenda

A sample simulation

Implementing the agenda

3.3.5 Propagation of Constraints

Using the constraint system

Implementing the constraint system

Representing connectors