Trying to do exercise 1.16 (iterative version of fast-exp) in "Structure and Interpretation of Computer Programs" with Clojure I came up with this:
(defn fast-it-exp [base exp res]
(cond (= exp 0) res
(odd? exp) fast-it-exp base (- exp 1) (* base res)
:else fast-it-exp base (/ exp 2) (* base base res)))
Trying it out:
user=> (fast-it-exp 0 0 10)
10 ;yep
user=> (fast-it-exp 2 2 2)
1 ;no...
user=> (fast-it-exp 1 1 1)
#<user$fast_it_exp__59 user$fast_it_exp__59@138c63> ;huh?!
Seems the "odd" part of the cond expression returns a function instead of evaluating. Why? I've tried putting parenthesis around the expressions after the predicates but that seems to be incorrect syntax, this is the best I've been able to come up with. I'm using rev 1146 of Clojure.