wow!
So if it is called with 1 argument, then the body of the function is:
(factorial n 1)
But if it is called with 2 arguments then the body of the function
is:
(if (= n 0) acc
(recur (dec n) (* acc n)))
Is this a standard feature of lisp? Sorry I am very noobish.
thx!
On Jan 25, 9:29 am, Mike DeLaurentis <[email protected]> wrote:
> That's defining a function factorial that can be called with either one or
> two arguments. When called with one argument, it immediately calls itself
> with two arguments. So the (factorial n 1) call provides acc with an
> initial value of 1. The ([n] and ([n acc] lines are the declarations of the
> parameter lists for the function. Does that make sense?
>
> On Sun, Jan 24, 2010 at 7:23 PM, Glen Rubin <[email protected]> wrote:
> > I am trying to learn clojure by learning the programing by example
> > open wiki book. For an excercise demonstrating iteration they give
> > the following example:
>
> > (defn factorial
> > ([n]
> > (factorial n 1))
> > ([n acc]
> > (if (= n 0) acc
> > (recur (dec n) (* acc n)))))
>
> > first I don't understand how acc ever gets an initial value?
>
> > also the arguments to the function are a list and not a vector?
>
> > why is the vector [n acc] on a line by itself?
>
> > This function is very confusing to me!! thanks for any help!!
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.> To post to this group, send email
> > [email protected]
> > Note that posts from new members are moderated - please be patient with
> > your first post.
> > To unsubscribe from this group, send email
> > to>[email protected]<clojure%[email protected]>
> > 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 post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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