I know when is a macro and if is a special form. "when" might be more efficient (short-circuit test). Why "when" is used in take and if is used in drop.
thanks -sun On Jan 24, 10:35 am, e <evier...@gmail.com> wrote: > thanks for putting these side by side so I could get an idea what when > does. Up until now, when I see "when", my eyes have glazed over. Thus the > only reason I need when is to read other people's code. Good question. Is > there a reason to have when? > > On Sat, Jan 24, 2009 at 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 -~----------~----~----~----~------~----~------~--~---