when is like do with only the 'then' branch wrapped in a do:

(if foo
  (do
    (println "hi")
    42)))

is the same as

(when foo
  (println "hi")
  42)

And like if without an else branch, when returns nil if its predicate
yields false.

On Jan 24, 9:33 am, wubbie <sunj...@gmail.com> wrote:
> Here is code from core.clj.
> The question is when to use  when or if.
>
> (defn take
>   "Returns a lazy seq of the first n items in coll, or all items if
>   there are fewer than n."
>   [n coll]
>     (when (and (pos? n) (seq coll))
>       (lazy-cons (first coll) (when (> n 1) (take (dec n) (rest
> coll))))))
>
> (defn drop
>   "Returns a lazy seq of all but the first n items in coll."
>   [n coll]
>     (if (and (pos? n) (seq coll))
>       (recur (dec n) (rest coll))
>       (seq coll)))
>
> -sun
--~--~---------~--~----~------------~-------~--~----~
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