|
Top Document: FAQ: Lisp Frequently Asked Questions 3/7 [Monthly posting] Previous Document: [3-8] Name conflict errors are driving me crazy! (EXPORT, packages) Next Document: [3-10] What is the difference between FUNCALL and APPLY? See reader questions & answers on this topic! - Help others by sharing your knowledge
DOTIMES, DOLIST, DO and LOOP all use assignment instead of binding to
update the value of the iteration variables. So something like
(let ((l nil))
(dotimes (n 10)
(push #'(lambda () n)
l)))
will produce 10 closures over the same value of the variable N. To
avoid this problem, you'll need to create a new binding after each
assignment:
(let ((l nil))
(dotimes (n 10)
(let ((n n))
(push #'(lambda () n)
l))))
Then each closure will be over a new binding of n.
This is one reason why programmers who use closures prefer MAPC and
MAPCAR to DOLIST.
Top Document: FAQ: Lisp Frequently Asked Questions 3/7 [Monthly posting] Previous Document: [3-8] Name conflict errors are driving me crazy! (EXPORT, packages) Next Document: [3-10] What is the difference between FUNCALL and APPLY? Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page [ Usenet FAQs | Web FAQs | Documents | RFC Index ] Send corrections/additions to the FAQ Maintainer: ai+lisp-faq@cs.cmu.edu
Last Update November 21 2011 @ 12:59 AM
|
