Search code examples
clojure

Why is this function not evaluating


I have been stuck on this one for a while and cant figure it out. Why is the function not evaluating for values greater or equal than 2? It works fine for values under it but anything above and it just prints the output seen in the pic

((fn fib
   ([x] (cond
          (zero? x) []
          (= x 1) [1]
          (>= x 2) fib (- x 2) [1 1]))
   ([x seq] (if (zero? x)
              seq
              (recur (dec x) (conj seq (+ (last seq) (nth seq (- (count seq) 2)))))))) 3)

enter image description here


Solution

  • You forgot one parenthesis on this line, before fib:

    (>= x 2) fib (- x 2) [1 1]))

    After you will add it, this code will work. Just note that your variables shouldn't have the same name as already existing functions- you were shadowing core function seq.

    ((fn fib
       ([x] (cond
              (zero? x) []
              (= x 1) [1]
              (>= x 2) (fib (- x 2) [1 1])))
       ([x values]
        (if (zero? x)
          values
          (recur (dec x)
                 (conj values (+ (last values)
                                 (nth values (- (count values) 2)))))))) 3)