[cl-faq] [lispfaq commit] r8 - trunk
codesite-noreply@google.com
codesite-noreply at google.com
Wed Mar 7 23:09:45 CST 2007
Author: peter.seibel
Date: Wed Mar 7 21:09:18 2007
New Revision: 8
Modified:
trunk/faq.txt
trunk/lispfaq.txt
Log:
Moving MAKE-ARRAY question from lispfaq to faq.
Modified: trunk/faq.txt
==============================================================================
--- trunk/faq.txt (original)
+++ trunk/faq.txt Wed Mar 7 21:09:18 2007
@@ -799,6 +799,30 @@
value as \cl{AND} does.
+*** I tried to create an array of objects with \cl{MAKE-ARRAY} and am seeing strange bugs. Why?
+
+Because \cl{MAKE-ARRAY} is a regular function, it's arguments are all
+evaluated before it is called. This can trip people up when they say
+something like this:
+
+ (make-array 10 :initial-element (make-foo))
+
+when they mean to make an array of ten distinct \code{foo} objects.
+When this expression is evaluated, \code{(make-foo)} is evaluated once
+and the resulting object is passed to \cl{MAKE-ARRAY} which makes it
+the value of all ten elements of the array. If the object is later
+modified, the change will be visible via any and all elements of the
+array. Of course if the object is immutable that probably wouldn't
+matter which is why it's okay to say things like:
+
+ (make-array 10 :initial-element 0)
+
+To initialize an array with distinct objects, you must iterate over
+the array somehow. One concise idiom is the following:
+
+ (map-into (make-array 10) #'make-foo)
+
+
** History
*** Why doesn't Lisp have a Benevolent Dictator like Perl or Python?
Modified: trunk/lispfaq.txt
==============================================================================
--- trunk/lispfaq.txt (original)
+++ trunk/lispfaq.txt Wed Mar 7 21:09:18 2007
@@ -27,13 +27,6 @@
Are you in a breakpoint in FOO or in BAR?
-*** I want an array of foos, but \programlisting{ (make-array 10 :initial-element (make-foo)) } causes strange bugs. Why?
-
-Well, the array created above contains 10 pointers to the same foo,
-which will indeed cause strange bugs. The correct way to initialize
-your array is probably \programlisting{ (map-into (make-array 10)
-#'make-foo) }
-
*** What books should I read to learn more about lisp?
*** Why doesn't Common Lisp have continuations?
More information about the cl-faq
mailing list