You can copy the template for this homework by typing in your terminal:
cp ~cs61as/autograder/templates/hw10.scm .
You can also download it by clicking here.
Read SICP 3.5.1, then answer the following:
(delay (+ 1 27))
?(force (delay (+ 1 27)))
?Evaluating this expression produces an error:
(stream-cdr (stream-cdr (cons-stream 1 '(2 3))))
Explain why.
Consider the following:
(define (enumerate-interval low high)
(if (> low high)
'()
(cons low (enumerate-interval (+ low 1) high)) ) )
(define (stream-enumerate-interval low high)
(if (> low high)
the-empty-stream
(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)
An unsolved problem in number theory concerns the following algorithm for creating a sequence of positive integers [mathjaxinline]s_1, s_2, \ldots[/mathjaxinline] where [mathjaxinline]s_1[/mathjaxinline] is some positive integer and, for all [mathjaxinline]n > 1[/mathjaxinline],
No matter what starting value [mathjaxinline]s_1[/mathjaxinline] is chosen, the sequence (called a hailstone sequence) always seems to end with the repeating values 1, 4, 2, 1, 4, 2, 1, .... However, it is not known if this is always the case.
Write a procedure num-seq
that, given a positive integer n
as argument,
returns the hailstone sequence for n
. 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, ...
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.
It's that time of the homework—SICP!
Complete the following: 3.50, 3.51, 3.52, 3.53, 3.54, 3.55, 3.56, 3.64, 3.66, 3.68.
Write and test two functions to manipulate nonnegative proper fractions.
The
first function, fract-stream
, will take as its argument a list of two
nonnegative integers, the numerator and the denominator, in which the
numerator is less than the denominator. It will return an infinite stream of
decimal digits representing the decimal expansion of the fraction.
The second
function, approximation
, will take two arguments: a fraction stream and a
nonnegative integer numdigits. It will return a list (not a stream) containing
the first numdigits digits of the decimal expansion.
Some guidelines:
(fract-stream '(1 7))
should return the stream representing the decimal(stream-car (fract-stream '(1 7)))
should return 1
.(stream-car (stream-cdr (stream-cdr (fract-stream '(1 7)))))
should return2
.(approximation (fract-stream '(1 7)) 4)
should return (1 4 2 8)
.(approximation (fract-stream '(1 2)) 4)
should return (5 0 0 0)
.For instructions, see this guide. It covers basic terminal commands and assignment submission.
If you have any trouble submitting, do not hesitate to ask a TA!