LABELS中定义的Lambda不会创建函数吗?不执行?

时间:2012-03-24 16:02:33

标签: lambda common-lisp sbcl sicp

通过钢铁银行Common Lisp与SICP合作,我取得了一定的成功,但我在“图片语言”示例中遇到了障碍。以下似乎没有写入PPM文件,而我写的其他旋转和翻转。我怀疑LAMBDA在调用LABELS时会以某种方式返回一个函数吗?

我已经有M-x检查 - 并且没有任何孤儿或看似不合适的事情。

(defun below (painter1 painter2)
  "Divides frame along 2nd axis and invokes a painter on each region"
  (let ((split-point (make-vect 0.0 0.5)))
    (labels ((paint-up (fr)
               (transform-painter painter2
                                  (make-vect 0.0 0.0)
                                  (make-vect 1.0 0.0)
                                  split-point))
             (paint-down (fr)
               (transform-painter painter1
                                  split-point
                                  (make-vect 1.0 0.5)
                                  (make-vect 0.0 1.0))))
      #'(lambda (frame)
          (paint-up frame)
          (paint-down frame)))))

(defun krest-below ()
  "Tests BELOW on the KREST-SEGMENTS"
  (init-buffer 400 400)
  (funcall (below (segments->painter krest-segments)
                  (segments->painter krest-segments))
           (make-frame (cons 0 0)
                       (cons 399 0)
                       (cons 0 399)))
  (rgb-buffer->ppm "krest-below.ppm" *buffer*))

1 个答案:

答案 0 :(得分:1)

从Svante的提示我发现答案是我需要确保TRANSFORM-PAINTER返回的函数实际上是使用FUNCALL调用并在标签PAINT-UP和PAINT-DOWN时传递FR被调用。修正后的BELOW函数如下所示:

(defun below (painter1 painter2)
  "Divides frame along 2nd axis and invokes a painter on each region"
  (let ((split-point (make-vect 0.0 0.5)))
    (labels ((paint-up (fr)
         (funcall (transform-painter painter2
                         (make-vect 0.0 0.0)
                         (make-vect 1.0 0.0)
                         split-point) fr))
       (paint-down (fr)
         (funcall (transform-painter painter1
                         split-point
                         (make-vect 1.0 0.5)
                         (make-vect 0.0 1.0)) fr)))
    #'(lambda (frame)
    (paint-up frame)
    (paint-down frame)))))