Hello as part of the 4Clojure problem set I am attempting to return a list of the first N fibonacci numbers. I have successfully implemented this using non-anonymous functions:
(defn nth-fibo [i]
(cond
(= i 1) 1
(= i 2) 1
:else (+ (nth-fibo (- i 1)) (nth-fibo (- i 2)))))
(defn fibos [i]
(loop [x i]
(when (> x 0)
(cons (nth-fibo x) (fibos (- x 1))))))
(reverse (fibos 5))
However, I must provide a single callable function to the 4Clojure evaluator to pass the tests. My attempt to do so:
#(
(letfn [
(nth-fibo [i]
(cond
(= i 1) 1
(= i 2) 1
:else (+ (nth-fibo (- i 1)) (nth-fibo (- i 2)))))
(fibos [i]
(loop [x i]
(when (> x 0)
(cons (nth-fibo x) (fibos (- x 1))))))
]
(reverse (fibos %))))
Leads to the error:
java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to clojure.lang.IFn