{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# hw13 - Week 13 solutions HOMEWORK = 3.50...

This preview shows pages 1–6. Sign up to view the full content.

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.

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

View Full Document
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
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.

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

View Full Document
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 merge and Hamming's problem This is easy: > (define S (cons-stream 1 (merge (scale-stream S 2) (merge (scale-stream S 3) (scale-stream S 5))))) S > (show-stream S 41) (1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 40 45 48 50 54 60 64 72 75 80 81 90 96 100 108 120 125 128 135 144 150 ...)
Sheet1 Page 5 3.64 stream-limit (define (stream-limit s tolerance) (if (< (abs (- (stream-car s) (stream-car (stream-cdr s)))) tolerance) (stream-car (stream-cdr s)) (stream-limit (stream-cdr s) tolerance))) 3.66 examine the pairs of integers

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern