That "for" should be equivalent to just "range" by itself.  It can be
even shorter:

(defn factorial [n]
  (apply * (range 2 (inc n))))

You can replace "apply" with "reduce" and it's just as fast, at least
for n up to 10,000.  I don't have the patience to wait for 100,000.

-Stuart Sierra


On Nov 23, 8:00 pm, "Craig Andera" <[EMAIL PROTECTED]> wrote:
> Clever!
>
> Help me understand why this isn't written
>
> (defn factorial [n]
>   (apply * (range 1 (+ n 1)))
>
> instead. That is, I don't get the purpose of the for statement.
>
> On Sun, Nov 23, 2008 at 4:08 PM, prhlava <[EMAIL PROTECTED]> wrote:
>
> > Hello folks,
>
> > While learning, it occured to me that factorial function can be
> > written as:
>
> > (defn factorial [n]
> >     (apply * (for [x (range 1 (+ n 1))] x)))
>
> > I know that it has big argument list for large numbers, but it seems
> > to scale nicely (at least in clojure).
>
> > I am sure this was discussed to death in lisp groups - have searched
> > the net and did not find this version...
>
> > kind regards,
>
> > Vlad
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to