how is the sicp cons-stream implemented?

前端 未结 2 443
悲&欢浪女
悲&欢浪女 2021-01-03 03:32

I\'m working through the streams section of the scip and am stuck on how to define a stream.

The following is my code:

(define (memo-func function)
          


        
2条回答
  •  有刺的猬
    2021-01-03 04:23

    cons-stream needs to be a macro in order for your sample code to work correctly. Otherwise the invocation of cons-stream will evaluate all its arguments eagerly.

    Try this (not tested):

    (define-syntax cons-stream
      (syntax-rules ()
        ((cons-stream a b)
         (cons a (memo-func (lambda () b))))))
    

    P.S. Your delay needs to be a macro also, for similar reasons. Then after you fix delay, you can make your cons-stream use delay directly.

提交回复
热议问题