sicp icon indicating copy to clipboard operation
sicp copied to clipboard

4.9

Open FengLi666 opened this issue 8 years ago • 1 comments

while 的实现你没有写完,一次就停了:

(define (expand-predicate predicate i body)
  (if (predicate i)
    (cons 'begin body)))

(define (while->combination exp)
  (expand-predicate
    (while-predicate exp)
    (while-variable exp)
    (while-body exp)))

此外,for的实现我有疑问:

(define (expand-range start end proc)
  (cond
    ((< start end)
      (proc start)
      (expand-range (+ start 1) end proc))))

在这里面 (proc start)的求值需要环境吧? 是否需要写成这样?

(define (expand-range start end proc env)
  (cond
    ((< start end)
      (eval (lambda () (proc start)) env)
      (expand-range (+ start 1) end proc env)))

FengLi666 avatar Oct 23 '16 10:10 FengLi666

看了下我的代码,这道题中的whilefor实现方式确实都错了, 正确的方法应该是构造一个lambda,然后调用这个lambda来实现迭代。

其次,你提到的的(proc start)求值需要环境,我觉得是不需要的,因为procstart在传递给expand-range的时候已经求好值了,这里直接用就好了。

等几天抽空,我把这个题重新做一遍,谢谢指出错误。

jiacai2050 avatar Oct 23 '16 15:10 jiacai2050