hw13 - Week 13 solutions HOMEWORK = 3.50 stream-map (define...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 Week 13 solutions HOMEWORK Err:510 3.50 stream-map (define (stream-map proc . argstreams) (if (STREAM-NULL? (car argstreams)) the-empty-stream (CONS-STREAM (apply proc (map STREAM-CAR argstreams)) (apply stream-map (cons proc (map STREAM-CDR argstreams)))))) 3.51 stream-ref > (stream-ref x 5) 1 2 3 4 5 5 > (stream-ref x 7) 6 7 7 > The repeated last number in each case is the actual value returned by stream-ref shown are the ones we haven't already computed. If we ask for a value nearer the front of the stream, nothing is SHOWn at all: > (stream-ref x 4) 4 > Notice that the first element of the stream is zero, not one -- why isn't the zero printed? Answer: It was printed when you defined X in the first place. NOTE: The important thing to take away from this example is that if DELAY didn't involve memoization, the printed results would be quite different. All the numbers would be printed every time.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sheet1 Page 2 3.52 accum/filter To make the situation more visible, I've chosen to define accum to use show (as above), so we can see intermediate results: > (define sum 0) SUM > (define (accum x) (set! sum (+ (show x) sum)) sum) ACCUM > (define seq (stream-map accum (stream-enumerate-interval 1 20))) 1 SEQ > sum 1 > (define y (stream-filter even? seq)) 2 3 Y > sum 6 > (define z (stream-filter (lambda (x) (= (remainder x 5) 0)) seq)) 4 Z > (stream-ref y 7) 5 6 7 8 9 10 11 12 13 14
Background image of page 2
Sheet1 Page 3 15 16 136 > sum 136 > (print-stream z) {10 15 45 55 105 120 17 18 19 190 20 210} > sum 210 > Had we not used the memoizing version of delay, the computation of z would have forced the stream seq over again, from the beginning. Not only would accum have shown the results 1, 3, 6, 10, etc. over again, it would have gotten the wrong answers! Since it uses the global variable sum, it only works if each term is added to the sum only once. 3.53 implicit definition Try it yourself! We know the first element of S is 1 and the STREAM-CDR is the result of adding S and S, so we get: 1 . .. + 1 . .. -------- 2 . .. So the second element must be 2. That means we have: 1 2 . .. + 1 2 . .. ---------- 2 4 . .. So the third element is 4, and so on.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sheet1 Page 4 3.54 factorials (define (mul-streams s1 s2) (stream-map * s1 s2)) (define factorials (cons-stream 1 (mul-streams FACTORIALS INTEGERS))) will give you the stream {1 1 2 6 24 . ..} starting with zero factorial, or (define factorials (cons-stream 1 (mul-streams FACTORIALS (STREAM-CDR INTEGERS)))) will give the stream {1 2 6 24 . ..} starting with one factorial. 3.55 partial sums (define (partial-sums s) (define result (cons-stream (stream-car s) (add-streams (stream-cdr s) result))) result) Alternatively, it can be done explicitly: (define (partial-sums s) (define (helper s sofar) (cons-stream sofar (helper (stream-cdr s) (+ sofar (stream-car s))))) (helper (stream-cdr s) (stream-car s))) but the first version is way cooler. 3.56
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/14/2009 for the course PEIS 100 taught by Professor Mckenzie during the Spring '08 term at University of California, Berkeley.

Page1 / 39

hw13 - Week 13 solutions HOMEWORK = 3.50 stream-map (define...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online