The Big Ideas

  1. Wishful thinking
  2. First-class Data
  3. Closure Property

Distance in N-dimensions

(define (distance point-1 point-2)
  (sqrt (dist-square point-1 point-2)))

(define (dist-square p1 p2)
  (if (zero-dimension? p1)
      0
      (+ (square (- (first-dimension p1)
		    (first-dimension p2)))
	 (dist-square (other-dimensions p1)
		      (other-dimensions p2)))))

Implementing Pairs

(define (make-pair a b)
  ; Traditionally called CONS (for "construct")
  (lambda (which?)
    (if (= which? 1)
        a
        b)))

(define (left pair)
  ; Traditionally called CAR
  (pair 1))

(define (right pair)
  ; Traditionally called CDR
  (pair 2))

Proof by Substitution

(left (make-pair 10 20))
(left (lambda (which?)
        (if (= which? 1) <10> <20>)))
((lambda (which?) (if (= which? 1) <10> <20>)) 1)
(if (= <1> <1>) <10> <20>)
(if <#T> <10> <20>)
<10>


Implementation of Points

(define (first-dimension point) (car point))
(define (other-dimensions point) (cdr point))
(define (zero-dimension? point) (null? point))

(define origin-3d (list 0 0 0))
(define p1 (list 2 3))
(define p2 (list 3 4))
(define p3 (list 2 7))

(distance p1 p2)
(distance p1 p3)
(distance p3 p1)

Box and Pointer Diagrams

(define origin-3d (list 0 0 0))

Lines as Pairs of Points

(define (make-line p1 p2)
  (cons p1 p2))
(define (line-start l) (car l))
(define (line-end l) (cdr l))

(define (line-length line)
  (distance (line-start line) (line-end line)))

Figures as Lists of Lines

(define (figure-length figure)
  (if (no-more-lines? figure)
      0
      (+ (line-length (first-line figure))
	 (figure-length (rest-of-lines figure)))))

(define (no-more-lines? f) (null? f))
(define (first-line f) (car f))
(define (rest-of-lines f) (cdr f))

Testing Figures

(define (points->open-figure points)
  (if (or (null? points) (null? (cdr points)))
      '()
      (cons (make-line (car points) (car (cdr points)))
	    (points->open-figure (cdr points)))))

(define unit-square
  (points->open-figure
   (list ((list 0 0) (list 0 1)
	  (list 1 1) (list 1 0) (list 0 0)))))

(define unit-right-triangle
  (list (list 0 0) (list 0 1) (list 1 0) (list 0 0)))

(figure-length unit-square)
(figure-length unit-right-triangle)