SICP Exercise 3.58 expand

Exercise 3.58.  Give an interpretation of the stream computed by the following procedure:

(define (expand num den radix)
  (cons-stream
   (quotient (* num radix) den)
   (expand (remainder (* num radix) den) den radix)))


(Quotient is a primitive that returns the integer quotient of two integers.) What are the successive elements produced by (expand 1 7 10) ? What is produced by (expand 3 8 10) ?

SOLUTION

The code and tests are here.

EXPLANATION

(expand 1 7 10) produces:

1, 4, 2, 8, 5, 7, 1, 4, 2, 8, 5, 7, 1, 4 ...

(expand 3 8 10) produces


3, 7, 5, 0, 0, 0, 0 ...

Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions