Converting to TCO

2009-11-08 Thread Robert Campbell
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?

Re: Converting to TCO

2009-11-08 Thread Robert Campbell
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 rrc...@gmail.com wrote: I've started reading SICP and I came across the Fermat primality test implemented an Scheme. I

Re: Converting to TCO

2009-11-08 Thread Mark Engelberg
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

Re: Converting to TCO

2009-11-08 Thread John Harrop
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

Re: Converting to TCO

2009-11-08 Thread Robert Campbell
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

Re: Converting to TCO

2009-11-08 Thread John Harrop
On Sun, Nov 8, 2009 at 1:39 PM, Robert Campbell rrc...@gmail.com 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,