Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading