Beginner: java.lang.Integer cannot be cast to clojure.lang.IFn (repl)

1,184 views
Skip to first unread message

Peregrine

unread,
Oct 19, 2009, 10:52:26 AM10/19/09
to Clojure
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)

I know my code may be incorrect to find the issue at hand but I cannot
get passed this issue.

Can anyone help?

Thanks!!
peregrine.

Jarkko Oranen

unread,
Oct 19, 2009, 11:54:18 AM10/19/09
to Clojure

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

Siddhartha Reddy

unread,
Oct 19, 2009, 11:55:18 AM10/19/09
to clo...@googlegroups.com
The arguments 'n' and 'e' to the calls to findFib on lines 7 and 8 are enclosed in parens, causing them to be treated as lists. Clojure expects the first element in a list to be a function and is therefore trying to cast it to an IFn which of course fails.

I think this is what you are looking for:


(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)))

Please note the absence of parens around the third argument to findFib on line 7 and around the first and third arguments to findFib on line 8.

Best,
Siddhartha

Michael Wood

unread,
Oct 19, 2009, 4:12:24 PM10/19/09
to clo...@googlegroups.com
2009/10/19 Peregrine <stie...@gmail.com>:

>
> 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)

Your question's already been answered, but to put it another way:

user=> 42
42
user=> (42)


java.lang.ClassCastException: java.lang.Integer cannot be cast to

clojure.lang.IFn (NO_SOURCE_FILE:0)
user=> (def x 42)
#'user/x
user=> x
42
user=> (x)


java.lang.ClassCastException: java.lang.Integer cannot be cast to

clojure.lang.IFn (NO_SOURCE_FILE:0)
user=>

--
Michael Wood <esio...@gmail.com>

Peregrine

unread,
Oct 19, 2009, 4:02:47 PM10/19/09
to Clojure
Thanks everyone! This seems so obvious now that I look at it.
Reply all
Reply to author
Forward
0 new messages