It's very common for a sequence argument to also work with nil, with the same semantics as an empty sequence. They are completely different things but Clojure is sloppy in this regard.
I believe this is intended. Thanks, Ambrose On Tue, Apr 8, 2014 at 2:08 PM, Jeff Mad <jeff...@gmail.com> wrote: > Hi, > I am new to Clojure, so please forgive me if this does not make sense. > > I was surprised to find out in the REPL that every? returns true if you > pass in an empty or nil collection. > > user=> (every? #(= 77 %) nil) > > true > > user=> (every? #(= 77 %) '()) > > true > > > I looked at the source for every? and it made sense to me why this > happens given that every? is recursive and the termination condition is > when coll runs out of items to process. > > Would it make more sense to define every? with a loop, or is the caller > expected to know better than to call it with nil? > > Thanks, > > --jeff > > > (defn every2? > > "Returns true if (pred x) is logical true for every x in coll, else > > false." > > {:tag Boolean > > :added "1.0" > > :static true} > > [pred coll] > > (if (empty? coll) > > false > > (loop [c coll] > > (cond > > (nil? (seq c)) true > > (pred (first c)) (recur (next c)) > > :else false)))) > > > -- > 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 > Note that posts from new members are moderated - please be patient with > your first post. > 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 > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.