Re: Which power function is right for Clojure?

2012-10-02 Thread Grant Rettke
On Mon, Oct 1, 2012 at 7:13 PM, Andy Fingerhut andy.finger...@gmail.com wrote: It depends. Are you trying to optimize for speed? Teaching a particular style of programming? Code size? Range of input values handled correctly? Time to write it? Something else? The most Clojury way of

Re: Which power function is right for Clojure?

2012-10-02 Thread Grant Rettke
On Mon, Oct 1, 2012 at 8:29 PM, Mark Engelberg mark.engelb...@gmail.com wrote: On Mon, Oct 1, 2012 at 4:45 PM, Grant Rettke gret...@acm.org wrote: This naming of a helper function to loop is non-idiomatic, because there is a built-in construct loop, which works somewhat like named let in

Which power function is right for Clojure?

2012-10-01 Thread Grant Rettke
))) (= 16 (non-acc-pow 2 4) (acc-pow 2 4) (lazy-pow 2 4) (iter-pow 2 4) (apply-pow 2 4)) Originally posted here: http://www.wisdomandwonder.com/article/6430/which-power-function-is-right-for-clojure -- ((λ (x) (x x)) (λ (x) (x x))) http://www.wisdomandwonder.com/ ACM, AMA, COG, IEEE

Re: Which power function is right for Clojure?

2012-10-01 Thread Andy Fingerhut
) (lazy-pow 2 4) (iter-pow 2 4) (apply-pow 2 4)) Originally posted here: http://www.wisdomandwonder.com/article/6430/which-power-function-is-right-for-clojure -- ((λ (x) (x x)) (λ (x) (x x))) http://www.wisdomandwonder.com/ ACM, AMA, COG, IEEE -- You received this message

Re: Which power function is right for Clojure?

2012-10-01 Thread Jean Niklas L'orange
Numeric tower has implemented pow (named expt in the library), so you could have a look at their implementation: https://github.com/clojure/math.numeric-tower/blob/master/src/main/clojure/clojure/math/numeric_tower.clj#L64 (It utilizes exponentiation by squaring to get the running time from O(n)

Re: Which power function is right for Clojure?

2012-10-01 Thread Mark Engelberg
On Mon, Oct 1, 2012 at 4:45 PM, Grant Rettke gret...@acm.org wrote: (ns power.examples) (defn non-acc-pow [base exp] (if (zero? exp) 1 (* base (non-acc-pow base (dec exp) This is not ideal for Clojure. The exponent will be limited by the stack size in Java. (defn

Re: Which power function is right for Clojure?

2012-10-01 Thread Mark Engelberg
On Mon, Oct 1, 2012 at 6:29 PM, Mark Engelberg mark.engelb...@gmail.comwrote: (defn efficient-pow [base pow] (loop [n pow, y 1, z base] (let [t (even? n), n (quot n 2)] (cond t (recur n y (*' z z)) (zero? n) (*' z y) :else (recur n (*' z y) (*' z z))