[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

lexical closure bug?



                       Subject:                               Time:1:08 PM
  OFFICE MEMO          lexical closure bug?                   Date:10/20/93
>  (let ((*fun* nil))
>    (do ((a (list 'a 'b 'c 'd 'e) (cdr a)))
>      ((endp a))
>      (push #'(lambda () (format t "~a~%" a)) *fun*))
>    (mapcar #'funcall *fun*))

>  I think that this should print out something like:

>  (a b c d e)
>  (b c d e)
>  (c d e)
>  (d e)
>  (e)

>  Instead, it prints:
>  nil
>  nil
>  nil
>  nil
>  nil

>  Am I missing something here?


(let ((*fun* nil)
      )
  (do ((a (list 'a 'b 'c 'd 'e) (cdr a)))
      ((endp a))
    (let ((b a)
          )
      (push #'(lambda () (format t "~a~%" b)) *fun*)
      )
    )
  (mapcar #'funcall *fun*)
  )

yields:

(E)
(D E)
(C D E)
(B C D E)
(A B C D E)
(NIL NIL NIL NIL NIL)