; Hal summarizes the new problems. ; * Why did we end here? ; -> we wanted MODULARITY. ; 6A:(7m) Today we look from another angle. ; Leaving the realms of OBJECT-ORIENTED PROGRAMMING ; and entering the perspective of "stream processing" ; or OPERATIONS ON AGGREGATES 1A:(24m5s) ; ; => Thereby creating CONVENTIONAL INTERFACES. ; 6A:(14m25s) Naming the spells. ; (cons-stream x y) --> stream ; (head s) --> extract first ; (tail s) --> extract last ; FOR ANY X, Y: ; (HEAD (CONS-STREAM X Y)) -> X ; (TAIL (CONS-STREAM X Y)) -> Y ; (define (map-stream proc s) ; (if (empty-stream? s) ; the-empty-stream ; (cons-stream ; (proc (head s)) ; (map-stream proc (tail s))))) ; (define (filter pred s) ; (cond ; ((empty-stream? s) the-empty-stream) ; ((pred (head s)) ; (cons-stream (head s) ; (filter pred ; (tail s)))) ; (else (filter pred (tail s))))) ; (define (accumulate combiner init-val s) ; (if (empty-stream? s) ; init-val ; (combiner (head s) ; (accumulate combiner ; init-val ; (tail s))))) ; (define (enumerate-tree tree) ; (if (leaf-node? tree) ; (cons-stream tree ; the-empty-stream) ; (append-streams ; (enumerate-tree ; (left-branch tree)) ; (enumerate-tree ; (right-branch tree))))) ; (define (append-streams s1 s2) ; (if (empty-stream? s1) ; s2 ; (cons-stream ; (head s1) ; (append-streams (tail s1) ; s2)))) ; (define (enum-interval low high) ; (if (> low high) ; the-empty-stream ; (cons-stream ; low ; (enum-interval (1+ low) high)))) ; These operations can be used to express general means ; of combination. ; Instead of: ; Instead of: ;;; SUM-ODD-SQUARES ; (define (sum-odd-squares tree) ; (if (leaf-node? tree) ; (if (odd? tree) ; (square tree) ; 0) ; (+ (sum-odd-squares ; (left-branch tree)) ; (sum-odd-squares ; (right-branch tree))))) ;;; ODD-FIBONACCI ; (define (odd-fibs n) ; (define (next k) ; (if (> k n) ; '() ; (let ((f (fib k))) ; (if (odd? f) ; (cons f (next (1+ k))) ; (next (1+ k)))))) ; (next 1)) ; you write: ;;; SUM-ODD-SQUARES ; (define (sum-odd-squares tree) ; (accumulate ; + ; 0 ; (map ; square ; (filter odd ; (enumerate-tree tree))))) ;;; ODD-FIBONACCI ; (define (odd-fibs n) ; (accumulate ; cons ; '() ; (filter ; odd ; (map fib (enum-interval 1 n))))) ; 6A:(21m40s) ; QUESTIONS 6A:(23m30s) BREAK "This is like APL." ; Case Analysis ; Flattening Streams of Streams: ; (define (flatten st-of-st) ; (accumulate append-streams ; the-empty-stream ; st-of-st)) ; GIVEN N: FIND ALL PAIRS O