letfn runtime error

57 views
Skip to first unread message

Sid Kurias

unread,
Oct 9, 2012, 2:04:58 AM10/9/12
to 4clo...@googlegroups.com
I am trying to solve the prime sandwich problem (116)
The following solution works in the repl (except for the last case), but gives me a runtime exception on the 4clojure site.

java.lang.RuntimeException: Unable to resolve symbol: primes in this context, compiling:(NO_SOURCE_PATH:0)



(fn p-s [n]
        (letfn [(primes []  (filter #(.isProbablePrime (BigInteger/valueOf %) 5) 
         (cons 2 (take-nth 2  (range 1 Integer/MAX_VALUE)))))])
        (if (not (.isProbablePrime (BigInteger/valueOf n) 5)) false
          (let [x (primes) y (flatten
                                        (take 1 (second (split-with nil? (map #(if (= n %2) [%
                                               %2 %3]) x (rest x) (rest (rest x))))))) ]
          (if (= (second y)  (/  (+ (first y) (last y) ) 2)) true false) )))

Alan Malloy

unread,
Oct 9, 2012, 5:08:29 AM10/9/12
to 4clo...@googlegroups.com
That code is incorrectly nested, and only works at the repl because you have also defined a global function named primes.


-----Original message-----

Sid Kurias

unread,
Oct 9, 2012, 10:41:19 AM10/9/12
to 4clo...@googlegroups.com
Thank you. Corrected the mistake.

Although I don't follow it when you say a global function is also defined called primes.

I certainly haven't defined any  function called primes, outside of the code shown here. Just to be safe I also renamed the function to something else and saw the same behaviour. Since my letfn had no body - only fnspecs, I guess I  was referring to the primes function from outside the letfn scope. That it still works, I don't understand.

Could you please clarify, or point me to someplace, where I can understand this better.  Why does the repl behave differently?

Thank you
Sid

Alan Malloy

unread,
Oct 9, 2012, 1:33:37 PM10/9/12
to 4clo...@googlegroups.com
I can't give you an answer because you describe something impossible and don't provide a paste of your repl interaction. If you paste the original definition into a brand-new repl (one for which primes is not defined), I guarantee you it will not work.

Sid Kurias

unread,
Oct 10, 2012, 12:08:36 AM10/10/12
to 4clo...@googlegroups.com
right again. it does not work from a clean repl.

Now to figure out what I did!!!

Thank you for your time.

Sid
Reply all
Reply to author
Forward
0 new messages