my attempt to do the exercises in sicp.

Monday, December 13, 2010

sicp exercise 3.51

;; Exercise 3.51.  In order to take a closer look at delayed evaluation, we will use the following procedure, which simply returns its argument after printing it:

;; (define (show x)
;;   (display-line x)
;;   x)

;; What does the interpreter print in response to evaluating each expression in the following sequence?59

;; (define x (stream-map show (stream-enumerate-interval 0 10)))
;; (stream-ref x 5)
;; (stream-ref x 7)

(define (stream-enumerate-interval low high)
  (if (> low high)
      (cons-stream low
                   (stream-enumerate-interval (+ 1 low) high))))
(define (stream-ref s n)
  (if (= n 0) (stream-car s)
      (stream-ref (stream-cdr s) (- n 1))))

(define (stream-map proc . argstreams )
  (if (stream-null? (car argstreams))
        (apply proc (map stream-car argstreams))
        (apply stream-map
               (cons proc (map stream-cdr argstreams))))))

(define (show x)
  (display x)(newline)
(define x (stream-map show (stream-enumerate-interval 0 10)))
(display (stream-ref x 5))(newline)
(display (stream-ref x 7))(newline)

No comments: