my attempt to do the exercises in sicp.

Wednesday, December 15, 2010

sicp exercise 3.54

;; Exercise 3.54.  Define a procedure mul-streams, analogous to add-streams, that produces the elementwise product of its two input streams. Use this together with the stream of integers to complete the following definition of the stream whose nth element (counting from 0) is n + 1 factorial:

;; (define factorials (cons-stream 1 (mul-streams <??> <??>)))

(define (mul-streams s1 s2)
  (stream-map * s1 s2))

(define (integers-from-n n)
  (cons-stream n (integers-from-n (+ 1 n))))

(define integers (integers-from-n 1))

(define factorials (cons-stream 1 (mul-streams integers factorials)))

(display (stream-ref factorials 5))(newline)

No comments: