hw11 - Week 11 solutions HOMEWORK = 3.50 stream-map (define...

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

View Full Document Right Arrow Icon
Week 11 solutions HOMEWORK ======== 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; the other numbers are printed by show. The only numbers 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. 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:
Background image of page 1

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

View Full DocumentRight Arrow Icon
> (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 ;; map applies the function argument to the stream-car of the stream only. > sum 1 > (define y (stream-filter even? seq)) 2 3 Y ;; filter keeps forcing stream elements until it finds an even sum, 1+2+3. > sum 6 > (define z (stream-filter (lambda (x) (= (remainder x 5) 0)) seq)) 4 Z ;; 1+2+3+4 is divisible by 5. > (stream-ref y 7) 5 6 7 8 9 10 11 12 13 14 15 16 136 ;; even sums are 6, 10, 28, 36, 66, 78, 120, 136 (1+. ..+16). ;; (Notice how they come in pairs, by the way. Two odd sums, then two even.) > sum 136 > (print-stream z) {10 15 45 55 105 120 17 18
Background image of page 2
19 190 20 210} ;; This may look confusing. Had I not used show, print-stream would have ;; printed {10 15 45 55 105 120 190 210}. This is the entire stream z. ;; The numbers 17 to 20 were printed by show as the stream seq was forced. > 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. 3.54 factorials
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2011 for the course CS 61A taught by Professor Harvey during the Spring '08 term at University of California, Berkeley.

Page1 / 12

hw11 - Week 11 solutions HOMEWORK = 3.50 stream-map (define...

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

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