Reputation: 313
I picked up clojure a few hours ago and am trying to get a Fibonacci function working
Here is the function I'm defining
(defn fib [x]
(if (or (= x 1) (= x 2))
((inc 0))
((+ (fib (- x 1)) (fib (- x 2))))
))
and I get this error: java.lang.Long cannot be cast to clojure.lang.IFn user/fib (NO_SOURCE_FILE:3)
Though I'm unfamiliar with almost all the language constructs this seems to be correct as far as I know. Any idea what's wrong?
Upvotes: 0
Views: 38
Reputation: 53881
You've made the classic mistake of too many parens, in Lisps, parens mean "apply this function" so it's important to only put them around functions:
(defn fib [x]
(if (or (= x 1) (= x 2))
1
(+ (fib (- x 1))
(fib (- x 2)))))
Your error essentially means "Hey, you just tried to apply a number!" so we need to remove the redundant parens.
Upvotes: 5