html5-img
1 / 41

Lecture 17

Lecture 17. OO, the notion of inheritance. Stacks in OO style. ( define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define (delete!) (if (null? top-ptr) (error . . .) (set! top-ptr (cdr top-ptr))) top-ptr )

cicero
Télécharger la présentation

Lecture 17

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 17

  2. OO, the notion of inheritance

  3. Stacks in OO style (define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define (delete!) (if (null? top-ptr) (error . . .) (set! top-ptr (cdr top-ptr))) top-ptr ) (define (insert! elmt) (set! top-ptr (cons elmt top-ptr)) top-ptr) (define (top) (if (null? top-ptr) (error . . .) (car top-ptr))) (define (dispatch op) (cond ((eq? op 'empty?) empty?) ((eq? op 'top) top) ((eq? op 'insert!) insert!) ((eq? op 'delete!) delete!))) dispatch))

  4. Stacks in OO style undef (define s (make-stack)) ==> ((s 'insert!) 'a) ==> ((s 'insert!) 'b) ==> ((s 'top)) ==> ((s 'delete!)) ==> ((s 'top)) ==> ((s 'delete!)) ==> (a) (b a) b (a) a ()

  5. Queues in OO style A lazy approach: We know how to do stacks so lets do queues with stacks :) We need two stacks: stack1 stack2 insert delete

  6. a a b a b b c b c Queues in OO style ((q ‘insert) ‘a) ((q ‘insert) ‘b) ((q ‘delete)) ((q ‘insert) ‘c) ((q ‘delete))

  7. Queues in OO style (define (make-queue) (let ((stack1 (make-stack)) (stack2 (make-stack))) (define (reverse-stack s1 s2) _______________) (define (empty?) (and ((stack1 'empty?)) ((stack2 'empty?)))) (define (delete!) (if ((stack2 'empty?)) (reverse-stack stack1 stack2)) (if ((stack2 'empty?)) (error . . .) ((stack2 'delete!)))) (define (first) (if ((stack2 'empty?)) (reverse-stack stack1 stack2)) (if ((stack2 'empty?)) (error . . .) ((stack2 'top)))) (define (dispatch op) (cond ((eq? op 'empty?) empty?) ((eq? op 'first) first) ((eq? op 'delete!) delete!) (else (stack1 op)))) dispatch))

  8. Queues in OO style Inheritance: One class is a refinement of another The queue class is a subclass of the stack class Stack Queue

  9. Streams

  10. Motivation • Modeling objects changing with time without assignment. • Describe the time-varying behaviour of an object as an • infinite sequence x1,x2,… • Think of the sequence as representing a function x(t). • Make the use of lists as conventional interface more efficient.

  11. Motivation (Cont.) (define (sum-primes a b) (define (iter count accum) (cond ((> count b) accum) ((prime? count) (iter (+ count 1) (+ count accum))) (else (iter (+ count 1) accum)))) (iter a 0)) (define (sum-primes a b) (accumulate + 0 (filter prime? (enumerate-interval a b)))) Second implementation consumes a lot of storage

  12. Motivation (Cont.) (car (cdr (filter prime? (enumerate-interval 10000 1000000)))) Requires a lot of time and space

  13. Streams Can formulate programs elegantly as sequence manipulators while attaining the efficiency of incremental computation. Delayed lists.

  14. Remember normal order evaluation? • Normal (Lazy) Order Evaluation: • go ahead and apply operator with unevaluated argument subexpressions • evaluate a subexpression only when value is needed • to print • by primitive procedure (that is, primitive procedures are "strict" in their arguments) • Compromise approach: give programmer control between normal and applicative order.

  15. Constructor, selectors, and contract (stream-car (cons-stream x y)) = x (stream-cdr (cons-stream x y)) = y There is a distinguished object: the-empty-stream that can be identified with the predicate stream-null? On the surface streams are just lists but the cdr of a stream is not evaluated until it is accessed by stream-cdr

  16. delay and force (delay <exp>) ==> a promise to evaluate exp delay must be a special form (delay (+ 1 1)) ;Value 6: #[promise 6] (force <delayed object>) ==> evaluate the delayed object an return the result (define x (delay (+ 1 1))) ;Value: x (force x) ;Value: 2

  17. Streams via delay and force (cons-stream <a> <b>) is a special form equivalent to (cons <a> (delay <b>)) (define (stream-car stream) (car stream)) (define (stream-cdr stream) (force (cdr stream)))

  18. What are these mysterious delay and force ? delay is a special form such that (delay <exp>) is equivalent to (lambda () <exp>) Force is a procedure that call a procedure produced by delay: (define (force delayed-object) (delayed-object))

  19. How does delay and force work ? (define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (cons-stream low (stream-enumerate-interval (+ low 1) high)))) (define s (stream-enumerate interval 1 3)) (stream-enumerate-interval 1 3) (cons-stream 1 (stream-enumerate-interval 2 3)) (cons 1 (delay (stream-enumerate-interval 2 3))) (cons 1 (lambda () (stream-enumerate-interval 2 3)))

  20. s: low 1 high 3 1 p: b: (stream-enu. . .) (define s (stream-enumerate-interval 1 3)) | GE stream-enumerate-interval: GE p: b: (if (> low high) the-empty-stream (cons-stream . . . ) (cons-stream 1 (stream-enumerate-interval 2 3)) | E1 (cons 1 (lambda () (stream-enumerate-interval 2 3)) | E1

  21. (define s (stream-enumerate-interval 1 3)) (stream-cdr s) (stream-cdr (cons 1 (lambda () (stream-enu-int 2 3)))) (force (cdr (cons 1 (lambda () (stream-enu-int 2 3))))) (force (lambda () (stream-enu-int 2 3))) ((lambda () (stream-enu-int 2 3))) (stream-enu-int 2 3) (cons-stream 2 (stream-enu-int 3 3)) (cons 2 (delay (stream-enu-int 3 3))) (cons 2 (lambda () (stream-enu-int 3 3)))

  22. s1: stream : delayed-obj: low 2 high 3 2 (define s1 (stream-cdr s)) | GE(force (cdr s)) | GE stream-enumerate-interval: GE s: E1 low1 high 3 p: b: (if (> low high) the-empty-stream (cons-stream . . . ) 1 p: b:(stream-enu. . .)

  23. Manipulating streams (define (stream-ref s n) (if (= n 0) (stream-car s) (stream-ref (stream-cdr s) (- n 1)))) (define (stream-map proc s) (if (stream-null? s) the-empty-stream (cons-stream (proc (stream-car s)) (stream-map proc (stream-cdr s)))) (define (stream-for-each proc s) (if (stream-null? s) 'done (begin (proc (stream-car s)) (stream-for-each proc (stream-cdr s)))))

  24. Manipulating streams (define (stream-filter pred stream) (cond ((stream-null? stream) the-empty-stream) ((pred (stream-car stream)) (cons-stream (stream-car stream) (stream-filter pred (stream-cdr stream)))) (else (stream-filter pred (stream-cdr stream))))) (stream-car (stream-cdr (stream-filter prime? (stream-enumerate-interval 10000 1000000))))

  25. Manipulating streams (stream-car (stream-cdr (stream-filter prime? (stream-enumerate-interval 10000 1000000)))) (stream-filter prime? (cons 10000 (lambda () (stream-enu-int 10001 1000000)))) (stream-filter prime? (cons 10001 (lambda () (stream-enu-int 10002 1000000)))) (cons 10007 (lambda () (stream-filter prime? (cons 10008 (lambda () (stream-enu-int 10009 1000000)))))) (stream-filter prime? (cons 10008 (lambda () (stream-enu-int 10009 1000000)))) (cons 10009 (lambda () (stream-filter prime? (cons 10010 (lambda () (stream-enu-int 10009 1000000))))))

  26. s1: stream : delayed-obj: low 2 high 3 2 (define s1 (stream-cdr s)) | GE(force (cdr s)) | GE stream-enumerate-interval: GE s: E1 low1 high 3 p: b: (if (> low high) the-empty-stream (cons-stream . . . ) 1 p: b:(stream-enu. . .)

  27. Forcing a delayed object many times Repeats exactly the same computation again ! Can’t we just remember the result ? (define (memo-proc proc) (let ((already-run? false) (result false)) (lambda () (if (not already-run?) (begin (set! result (proc)) (set! already-run? true) result) result))))

  28. What does memo-proc do ? (define fact-5 (lambda () (fact 5))) (fact-5) (fact-5) (define memo-fact-5 (memo-proc fact-5)) (memo-fact-5) (memo-fact-5)

  29. n : 5 n : 5 n : 4 n : 4 (fact-5) | GE fact-5: GE p: b:(fact 5)

  30. (define memo-fact-5 (memo-proc fact-5))| GE memo-fact-5: fact-5: GE proc: p: b:(fact 5) already-run: #f result: #f p: b:(if (not already-run?) (begin (set! result (proc)) (set! already-run? true) result) result)

  31. n : 5 n : 4 #t 120 (memo-fact-5) | GE memo-fact-5: fact-5: GE proc: p: b:(fact 5) already-run: #f result: #f p: b:(if (not already-run?) (begin (set! result (proc)) (set! already-run? true) result) result)

  32. How do we use it ? Change the definition of delay so that (delay <exp>) is equivalent to (memo-proc (lambda () <exp>))

  33. Infinite streams (define (integers-starting-from n) (cons-stream n (integers-starting-from (+ n 1)))) (define integers (integers-starting-from 1))

  34. integers: n : 1 1 proc: already-run: #f result: #f (define integers (integers-starting-from 1)) | GE (cons-stream 1 (integers-starting-from 2)) | E1 (cons 1 (memo-proc (lambda () (integers-starting-from 2))) | E1 integers-starting-from: GE p:n b:(cons-stream 1 . . .)

  35. t: 2 n : 2 #t proc: already-run: #f result: #f (define t (stream-cdr integers)) | GE ((cdr integers)) | GE integers: integers-starting-from: GE n : 1 1 p:n b:(cons-stream 1 . . .) proc: already-run: #f result: #f

  36. Infinite streams -- more examples (define (divisible? x y) (= (remainder x y) 0)) (define no-sevens (stream-filter (lambda (x) (not (divisible? x 7))) integers)) (stream-ref no-sevens 100)

  37. Infinite streams -- more examples (define (fibgen a b) (cons-stream a (fibgen b (+ a b)))) (define fibs (fibgen 0 1))

  38. 2 3 4 5 6 7 8 9 10 2 X X 7 X XX 11 12 13 14 15 16 17 18 19 20 XX XX XX XX XX XX 21 22 23 24 25 26 27 28 29 30 XX XX XX XX XX XX XX 3 5 X X XX 31 32 33 34 35 36 37 38 39 40 XX XX XX XX XX XX XX XX XX XX XX 41 42 43 44 45 46 47 48 49 50 XX XX XX XX XX XX XX XX XX XX XX XX XX 51 52 53 54 55 56 57 58 59 60 XX XX XX XX XX XX XX XX XX XX XX 61 62 63 64 65 66 67 68 69 60 XX XX XX XX XX XX XX XX XX XX XX XX 71 72 73 74 75 76 77 78 79 80 XX XX XX XX XX XX XX XX XX XX XX XX 81 82 83 84 85 86 87 88 89 90 XX XX XX XX XX XX XX XX XX XX XX XX XX 91 92 93 94 95 96 97 98 99 100 XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX Finding all the primes

  39. Remember our sieve? (define (sieve str) (cons-stream (stream-car str) (sieve (stream-filter (lambda (x) (not (divisible? X (stream-car str)))) (stream-cdr str))))) (define primes (sieve (stream-cdr integers))) (sieve (2 3 4 . . . )) (2 (sieve (stream-filter /2 (2 3 4 . . .)))) (2 (sieve (3 (stream-filter /2 (4 5 . . .))))) (2 3 (sieve (stream-filter /3 (stream-filter /2 (4 5 . . . )))))

  40. Pitfalls (stream-filter odd? (stream-filter even? integers)) (stream-null? (stream-filter odd? (stream-filter even? integers)))

  41. Defining stream implicitely (define ones (cons-stream 1 ones))

More Related