This preview shows pages 1–5. Sign up to view the full content.
Sheet1
Page 1
Week 13 solutions
HOMEWORK
Err:510
3.50 streammap
(define (streammap proc . argstreams)
(if (STREAMNULL? (car argstreams))
theemptystream
(CONSSTREAM
(apply proc (map STREAMCAR argstreams))
(apply streammap
(cons proc (map STREAMCDR argstreams))))))
3.51
streamref
> (streamref x 5)
1
2
3
4
5
5
> (streamref x 7)
6
7
7
>
The repeated last number in each case is the actual value returned by
streamref
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:
> (streamref 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 (streammap accum (streamenumerateinterval 1 20)))
1
SEQ
> sum
1
> (define y (streamfilter even? seq))
2
3
Y
> sum
6
> (define z (streamfilter (lambda (x) (= (remainder x 5) 0)) seq))
4
Z
> (streamref y 7)
5
6
7
8
9
10
11
12
13
14
Sheet1
Page 3
15
16
136
> sum
136
> (printstream 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 STREAMCDR 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 (mulstreams s1 s2)
(streammap * s1 s2))
(define factorials (consstream 1 (mulstreams FACTORIALS INTEGERS)))
will give you the stream {1 1 2 6 24 .
..} starting with zero factorial, or
(define factorials (consstream 1 (mulstreams FACTORIALS
(STREAMCDR INTEGERS))))
will give the stream {1 2 6 24 .
..} starting with one factorial.
3.55 partial sums
(define (partialsums s)
(define result (consstream (streamcar s)
(addstreams (streamcdr s) result)))
result)
Alternatively, it can be done explicitly:
(define (partialsums s)
(define (helper s sofar)
(consstream sofar (helper (streamcdr s) (+ sofar (streamcar s)))))
(helper (streamcdr s) (streamcar s)))
but the first version is way cooler.
3.56
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.
 Spring '08
 mckenzie

Click to edit the document details