On Sun, Nov 8, 2009 at 1:39 PM, Robert Campbell wrote:
> John: good catch.
Thanks.
> Can you confirm the difference between my original
> defn and your letfn? Both work, but as I understand it from the
> documentation, defn would actually define that local function
> globally, while letfn act
Mark: that looks a lot like the "collector" I learned about in The
Little Schemer. I actually do have How to Design Programs, but I
wanted to finish Seasoned Schemer first. I was poking around in SICP
because of a Project Euler problem. Thanks for the tip!
John: good catch. Can you confirm the di
You have a bug:
(defn exp-mod [base exp m]
(cond
(zero? exp) 1
(even? exp) (mod (Math/sqrt (exp-mod base (/ exp 2) m)) m)
:else (mod (* base (exp-mod base (inc exp) m)) m)))
should be
(defn exp-mod [base exp m]
(cond
(zero? exp) 1
(even? exp) (mod (Math/sqrt (exp-mod base
Hint: Use an accumulator.
http://htdp.org/2003-09-26/Book/curriculum-Z-H-39.html#node_chap_31
In fact, you may want to consider reading How to Design Programs before SICP.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Gro
One correction: after playing with the functions a bit I noticed I
screwed up, putting sqrt where I needed square.
On Sun, Nov 8, 2009 at 4:43 PM, Robert Campbell wrote:
> I've started reading SICP and I came across the Fermat primality test
> implemented an Scheme. I reimplemented it in Clojur
I've started reading SICP and I came across the Fermat primality test
implemented an Scheme. I reimplemented it in Clojure and was able to
switch the recursive call in fast-prime to TCO/recur, but I was unable
to do the same for the exp-mod function.
(defn exp-mod [base exp m]
(cond
(zero?