Thanks to all for the nice explanations.  I understand the reasoning. 

On Tuesday, April 8, 2014 1:53:44 AM UTC-7, Colin Yates wrote:
>
> Depends who is doing the expecting as to whether that behaviour is 
> correct.  Formal logicians, mathematicians, computer scientists etc. would 
> respond "sure, it is vacously true".  For almost everybody else it "feels" 
> wrong but is then true when you think about it a bit.
>
> I would suggest the question you are trying to ask is (and (not (empty? 
> nil)) (every? #(= 77 %) nil)).
>
> For more info check out http://en.wikipedia.org/wiki/Vacuous_truth.
>
> On Tuesday, 8 April 2014 07:08:56 UTC+1, Jeff Mad 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.

Reply via email to