The for macro is only valid for a vector of even size. Throwing an
exception when this isn't the case would have helped the original
poster:

user=> (take 100 (for [x  (range 1000) y (range 1000) (< x y)][x y]))
java.lang.IllegalArgumentException: for got a malformed vector
argument, more details in (doc for). (NO_SOURCE_FILE:409)

One could trivially add more checks (such as the vector not being
empty) but then the question is when to stop. I do think that checking
for an even-sized vector could be reasonable to do since the for macro
currently silently drops trailing odds (by the use of partition).

~/projects/clojure % cat for_argument_check.patch
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 976423e..0bcb50f 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -2528,8 +2528,10 @@
                                        (recur (rest ~gxs))))
                                   `(lazy-cons ~expr (~giter (rest
~gxs))))
                               (recur (rest ~gxs))))))))]
-    `(let [iter# ~(emit (to-groups seq-exprs))]
-       (iter# ~(second seq-exprs))))))
+    (if (odd? (count seq-exprs))
+      (throw (IllegalArgumentException. "for got a malformed vector
argument, more details in (doc for)."))
+      `(let [iter# ~(emit (to-groups seq-exprs))]
+         (iter# ~(second seq-exprs)))))))

 (defmacro comment
   "Ignores body, yields nil"


/Olov

On Jan 3, 11:06 am, Kyle Schaffrick <k...@raidi.us> wrote:
> On Sun, 28 Dec 2008 21:48:25 -0600
>
>
>
> "David Nolen" <dnolen.li...@gmail.com> wrote:
>
> > > Yes, but I think maybe there is a bug in Clojure that causes the
> > > first case to "work" when it should give a syntax error.  If it is
> > > not a bug I do not understand why it ignores the expression.
>
> > It's not a "bug" in Clojure because Clojure doesn't really have
> > syntax in the way that you seem to be referring to, and I assume that
> > you mean this in the sense that Python has list comprehensions as a
> > syntactical feature of the language.  A list comprehension in Clojure
> > is just a macro not any part of the "core" language so to speak.
> > Much of Clojure is like this.
>
> > The macro could do some sort of syntactical checking.  But in this
> > case it doesn't really seem like it's worth it.  You could of course
> > re-implement your own version of list comprehension that does
> > incorporate this.  Unlike Python where this is probably not realistic
> > (you can't easily implement new Python syntax for yourself), Clojure
> > being a Lisp makes this quite easy.
>
> I don't think the "Lisps have no syntax" card should be played to excuse
> silent failures of the sort that create mysterious and difficult to
> find/understand bugs. The "you can roll your own version" thing doesn't
> really hold up for me, not even in a Lisp: people who reimplement custom
> versions of basic language/library features in their app code are a
> regular staple on The Daily WTF, for good reason :)
>
> If this had happened to me I know *I* would be disappointed that it
> wasn't caught automatically, since A) I would guess without immediate
> proof that it isn't terribly difficult to do so, and B) this kind of
> behavior tends to cause me hours of frustrating debugging. If it is
> otherwise customary and expected that Clojure code throws exceptions
> when called incorrectly, my intuition would be that silently generating
> [incorrect] output for a malformed argument list is broken behavior.
>
> "It is not blowing up," I would reason, "and thus it must be a logical
> error in the structure of my algorithm, not something piddly like
> accidentally omitting a keyword."
>
> If you don't want to throw a syntax error (since you're right in the
> sense that it *isn't* a syntax error) it should at least throw
> something, for example Python tends to use a TypeError for malformed
> argument lists.
>
> Of course if someone can explain how this behavior gives list comps
> useful properties and (perhaps more importantly) could have been
> expected, then I'll happily retract this!
>
> > David
>
> -Kyle
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to