I am trying to solve the project euler 2nd question. Why is the below code resulting into stack overflow ? I am using recur so it should not be storing all the recursive calls on the stack.
(defn sum
[[a b]]
[b (+ a b)])
(defn fib-r
([n] (fib-r n 0 [0 1]))
([n s [a b]]
(if (= n 0)
s
(let [[c d] (sum [a b])
e (if (even? c) c 0)
f (+ s e)]
(recur (dec n) f [c d])))))
(fib-r 4000000)
your getting an integer overflow (rather than a stack overflow)
If you use BigInts (BigInt literals end with N) then Clojure will
happily compute the correct result: