CS61A Summer 2011 Lab 11
1. What is the type of the value of (delay (+ 1 27))?
What is the type of the value of(force (delay (+ 1 27)))?
2. Evaluation of the expression
(stream-cdr (stream-cdr (cons-stream 1 '(2 3))))
produces an error. Why?
3. Consider the following two procedures.
(define (enumerate-interval low high)
(if (> low high)
(cons low (enumerate-interval (+ low 1) high)) ) )
(define (stream-enumerate-interval low high)
(if (> low high)
(cons-stream low (stream-enumerate-interval (+ low 1) high)) ) )
What's the difference between the following two expressions?
(delay (enumerate-interval 1 3))
(stream-enumerate-interval 1 3)
4. An unsolved problem in number theory concerns the following algorithm
for creating a sequence of positive integers:
Choose a number n to be some positive integer.
For n > 1,
if n is odd, then the next number is 3n + 1;
if n is even, then the next number is n/2.
No matter what starting value is chosen, the sequence always
seems to end with the values 1, 4, 2, 1, 4, 2, 1, ...
However, it is not known if this is always the case.
4a. Write a procedure num-seq that, given a positive integer n
as argument, returns the stream of values produced for n
by the algorithm just given. For example, (num-seq 7) should return
the stream representing the sequence
7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, ...
4b. Write a procedure seq-length that, given a stream produced
by num-seq, returns the number of values that occur in the sequence
up to and including the first 1.
For example, (seq-length (num-seq 7)) should return 17.
You should assume that there is a 1 somewhere in the sequence.