On Oct 19, 5:52 pm, Peregrine <
stiebs...@gmail.com> wrote:
> Hey I am new to Clojure and I am doing some Project Euler problems to
> help me get started with the language. I've run into a issue I cannot
> seem to get past. I have this code:
>
> (defn findFib
> "Find the sum of all the even-valued terms in the sequence which do
> not exceed four million."
> [e n p]
> (if (< n 1000000)
> (if (= (rem n 2) 0)
> (findFib (+ e n) (+ n p) (n))
> (findFib (e) (+ n p) (n)))
> (str "Value is " e)))
>
> And I keep getting this error
>
> 1:9 problem2=> (findFib 0 1 2)
> 1:10 problem2=> java.lang.ClassCastException: java.lang.Integer cannot
> be cast to clojure.lang.IFn (repl-1:9)
You're using your integers as functions, leading to this rather
cryptic error.
These two lines are the problem:
> (findFib (+ e n) (+ n p) (n))
> (findFib (e) (+ n p) (n)))
(e) and (n) are function calls, which will fail since e and n are
plain integers. You simply need to remove the parentheses and it'll
work. :)
Furthermore, your naming is nonidiomatic... Lisp code should use
lowercase and dashes instead of camelcase. "find-fib" is better. The
function also happens to be tail-recursive so you can replace the
findFib calls with the recur special form to avoid growing the stack
needlessly.
--
Jarkko