This is interesting as I hit a similar stumbling block when I was
first playing with recur. I'm new to Lisp though not completely new to
functional programming, and have a style question about your solution:
Is there a particular reason you chose to arity overload the factorial
function rather than use the loop construct to include an
accumulator?
eg:
(defn factorial [n]
(loop ([acc 1, i n]
(if (= i 0) acc
(recur (dec i), (* acc i)))))
The reason I ask is I've always been a little bothered by having to
have a second 'accumulator version' of functions in functional
languages, especially since they're often meaningless out of context
(how can factorial take a second argument?) I really like loop..recur
because it allows the accumulator version to be hidden. Does that make
sense? Is there a general preference for the non-loop version?
I'm absolutely loving clojure by the way!
On Oct 13, 2:06 pm, "Stewart Griffin" <
ste...@gmail.com> wrote:
> 2008/10/13 Stuart Halloway <
stuart.hallo...@gmail.com>: