0) On board announcement: Quiz One: Wednesday, March 19 5pm xor 7pm, Walker 3rd floor Gym 1) Introduction, 5 minutes, 10:05 - 10:10 Data types and abstraction -- "what can you do with it?" numbers: arithmetic strings: length, characters procedures: apply to arguments Pairs as "Universal Glue" -- n-dimensional points, lines, figures pairs: left, right (car, cdr) Conventions for Usage, or Standards plugs and telephone connectors 2) Distance in n-space, 5 minutes, 10:10 - 10:15 i=0 --- \ 2 sqrt ( / (Xi - Yi) ) --- i=n (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))))) We just need: zero-dimension?, first-dimension, other-dimensions, and a way of making points. 3) Lessons from Lego, 10 minutes, 10:15 - 10:25 a) Make units that can plug together b) General purpose procedures for making pairs and taking them apart. (define (make-pair a b) (lambda (which?) (if (= which? 1) a b)) (define (left pair) (pair 1)) (define (right pair) (pair 2)) c) Substitution on (left (make-pair 10 20)) (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> d) Traditional names (predefined) are CONS, CAR, CDR e) That gives us first-dimension and other-dimensions (define (first-dimension point) (car point)) (define (other-dimensions point) (cdr point)) 4) Box and pointer, 3 minutes, 10:25 - 10:28 a) Just draw the box AND THE POINTER TO IT b) Infinitely squishable boxes, so we use a pointer 5) Lists as a standard structure, 10 minutes, 10:35 - 10:45 a) First and rest, just like our points b) How do you find the end? Introduce '() and NULL? (define (zero-dimension? point) (null? point)) c) Simple creation using LIST. (define origin-3d (list 0 0 0)) (define p1 (list 2 3)) (define p2 (list 3 4)) (define p3 (list 2 7)) d) Box and pointer for 3-D origin, (LIST 0 0 0) ----------- ----------- ----------- ---->| 0 | --|-->| 0 | --|-->| 0 | / | ----------- ----------- ----------- 6) Lines as pairs of points, 3 minutes, 10:45 - 10:48 (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))) 7) Figures as lists of points, 3 minutes, 10:48 - 10:51 (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)) (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 (points->open-figure (list (list 0 0) (list 0 1) (list 1 0) (list 0 0)))) (figure-length unit-square) (figure-length unit-right-triangle) 8) Summary, 4 minutes, 10:51 - 10:55 i) Wishful thinking (useful for recursion and building big programs) ii) first-class data (name it, pass it in, pass it out, put it in a data structure) iii) closure (you can combine things made by combining other things) ================================================================ | Wishful Thinking | | Announcements | | First-class data | PC PROJECTOR | | | Closure | | | | | | | | | | | ================================================================ | | | | | Distance | | Box-and-Pointer | | Dist-square | | of Origin-3d | | | | | | | | | ================================================================ | | | | | (left (make-pair | | | | 10 20)) | | | | | | | | | | | ================================================================