mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-26 15:14:12 +00:00
17 lines
679 B
Plaintext
17 lines
679 B
Plaintext
;; Functions to calculate Fibonacci numbers
|
|
|
|
;; Slow recursive version
|
|
(def slowFib (fn (n) (if (< n 2) n (+ (slowFib (- n 1)) (slowFib (- n 2))))))
|
|
|
|
;; Return the sum of the last 2 numbers in a sequence
|
|
(def sumOfLast (fn (l) (+ (last l) (penult l))))
|
|
|
|
;; Faster version, return vector of n numbers, tail call optimized
|
|
(def fibListRec (fn (n l) (if (< (len l) n) (fibListRec n (push l (sumOfLast l))) l)))
|
|
(def fibList (fn (n) (fibListRec n [0 1])))
|
|
(def fastFib (fn (n) (sumOfLast (fibList n))))
|
|
|
|
;; Add documentation
|
|
(doc slowFib "Return the nth number from the Fibonacci sequence recursively.")
|
|
(doc fastFib "Return the nth number from the Fibonacci sequence iteratively.")
|