my attempt to do the exercises in sicp.

Monday, December 27, 2010

sicp exercise 3.68

;; Exercise 3.68.  Louis Reasoner thinks that building a stream of pairs from three parts is unnecessarily complicated. Instead of separating the pair (S0,T0) from the rest of the pairs in the first row, he proposes to work with the whole first row, as follows:

;(define (pairs s t)
;  (interleave
;   (stream-map (lambda (x) (list (stream-car s) x))
;               t)
;   (pairs (stream-cdr s) (stream-cdr t))))
;Does this work? Consider what happens if we evaluate (pairs integers integers) using Louis's definition of pairs.

;; Answer: This way, the problem cannot be cleanly decomposed recursively into smaller parts like shown in the example,
;;         and since the second pairs being invoked from body f pairs, is not a part of a cons-stream, so it will be evaluated immedialtely instead of being delayed. which leads to an infinite loop.

;; replacing this definition of pairs in the previous exercise leads to:

;; Answer:
;Loading "sicp_prob_03.68.scm"... aborted
;Aborting!: maximum recursion depth exceeded

No comments: