Re: Clojure noob: refactoring factorial code

2010-07-16 Thread Mike Meyer
"James Reeves" wrote: >On 15 July 2010 16:24, Brisance wrote: >> Thanks for the response. >> >> To get an idea of what I mean,  visit http://www.wolframalpha.com/''. >> Then enter something ridiculous (to me, at least) like 10! >> >> The answer is almost instantaneous. > >Wolfram Alpha is me

Re: Clojure noob: refactoring factorial code

2010-07-16 Thread Brendan Ribera
> > In conventional imperative/procedural languages, as you pointed out, > the algorithm used to calculate the factorial would be dependent on > available compute resources. In order to select the appropriate > algorithm one might select an arbitrary value (let's say 1000) and > decide to use one a

Re: Clojure noob: refactoring factorial code

2010-07-16 Thread James Reeves
On 15 July 2010 16:24, Brisance wrote: > Thanks for the response. > > To get an idea of what I mean,  visit http://www.wolframalpha.com/''. > Then enter something ridiculous (to me, at least) like 10! > > The answer is almost instantaneous. Wolfram Alpha is merely approximating the result to

Re: Clojure noob: refactoring factorial code

2010-07-16 Thread Brisance
Hello Mike, Thanks for taking time to respond. I replied to another post but somehow it didn't show up. Perhaps it is awaiting moderation. Anyway, perhaps I should explain the situation more clearly. In conventional imperative/procedural languages, as you pointed out, the algorithm used to calcu

Re: Clojure noob: refactoring factorial code

2010-07-16 Thread Brisance
Thanks for the response. To get an idea of what I mean, visit http://www.wolframalpha.com/''. Then enter something ridiculous (to me, at least) like 10! The answer is almost instantaneous. The question is: how would someone write idiomatic Clojure in such a way that it gives exact results f

Re: Clojure noob: refactoring factorial code

2010-07-15 Thread Mike Meyer
On Thu, 15 Jul 2010 04:38:22 -0700 (PDT) Brisance wrote: > Here's a factorial function as found in the WikiBook "Learning > Clojure" : > > (defn factorial [n] > (defn fac [n acc] > (if (zero? n) >acc > (recur (- n 1) (* acc n

Re: Clojure noob: refactoring factorial code

2010-07-15 Thread Frederick Polgardy
Well, no, I was merely demonstrating how to use loop/recur. -- Science answers questions; philosophy questions answers. On Jul 15, 2010, at 8:52 AM, Daniel Gagnon wrote: > There's no reason to recur at all: > > (defn factorial [n] (reduce * (range 1 (inc n > > On Thu, Jul 15, 2010 at 9:49

Re: Clojure noob: refactoring factorial code

2010-07-15 Thread Daniel Gagnon
There's no reason to recur at all: (defn factorial [n] (reduce * (range 1 (inc n On Thu, Jul 15, 2010 at 9:49 AM, Frederick Polgardy wrote: > You don't need to recur to another function, just recur to a loop: > > (defn factorial [n] > (loop [x n acc 1] >(if (zero? x) acc (recur (dec x)

Re: Clojure noob: refactoring factorial code

2010-07-15 Thread Frederick Polgardy
You don't need to recur to another function, just recur to a loop: (defn factorial [n] (loop [x n acc 1] (if (zero? x) acc (recur (dec x) (* acc x) (println (factorial 5)) -- Science answers questions; philosophy questions answers. On Jul 15, 2010, at 6:38 AM, Brisance wrote: > Here

Clojure noob: refactoring factorial code

2010-07-15 Thread Brisance
Here's a factorial function as found in the WikiBook "Learning Clojure" : (defn factorial [n] (defn fac [n acc] (if (zero? n) acc (recur (- n 1) (* acc n ; recursive call to fac, but reuses the stack; n will be (- n 1), and acc