madlisp/mad/fib.mad
2020-12-07 09:38:39 +07:00

12 lines
382 B
Plaintext

;; Functions to calculate Fibonacci numbers
;; Recursive, tail call optimized
(defn fibRec (i n a b) (if (= i n) (+ a b) (fibRec (inc i) n b (+ a b))))
(defn fib (n) (if (< n 2) n (fibRec 2 n 0 1)))
;; Return a vector of n Fibonacci numbers
(defn fibVec (n) (map fib (range n)))
;; Slow recursive version
(defn slowFib (n) (if (< n 2) n (+ (slowFib (- n 1)) (slowFib (- n 2)))))