sicp:问怎么用惰性写高阶函数,他这里给了我一个错误

这是我写的高阶函数,用到惰性

(define (stream-delay expr)
  (lambda ()
    expr))
(define (stream-force delayed)
  (delayed))

(define (stream-car stream) (car stream))
(define (stream-cdr stream) (stream-force (cdr stream)))
(define (cons-stream a b) (cons a (stream-delay b)))

(define (stream-map proc stream)
  (if (null? stream)
      '()
      (cons-stream (proc (stream-car stream))
		   (stream-map proc (stream-cdr stream)))))

调用(stream-map square '(1 2 3 4))时,他给我返回一个错误
Exception: attempt to apply non-procedure (2 3 4) Type (debug) to enter the debugger.
这是怎么一回事??

'(1 2 3 4 5) 本身并不是个 Stream 吧。仍然是提前运算了 cdr 的。你需要自己构造一个从1到5的流作为输入

1赞
(define (list->stream lst) 
  (if (null? lst) 
   lst
   (cons-stream (car lst) (list->stream (cdr lst)))))
;; or
(define (list->stream lst)
  (fold-right cons-stream '() lst))
> (list->stream '(1 2 3 4 5))
(1 . #<procedure>)
1赞

照你这么写,stream-delaycons-stream 都不能是函数,否则参数早被求值了。