view 6-Clojure/fibonaci.clj @ 103:98be775c533c default tip

An odd "non-determinism" example from StackOverflow It is clever, but doesn't make much sense as to how it gets its results
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:44:13 +0100
parents eccc649d49a2
children
line wrap: on
line source

; For a pair of (assumed) consecutive (assumed) Fibonacci numbers and generates the next pair
(defn fib-pair [[a b]] ; I *think* this a vector and not just two parameters so that we can iterate (because you can't return two values)
    [b (+ a b)])

(print (fib-pair [3 5]))

(print 
    (take 5
        (map first (iterate fib-pair [1 1]))))

(print 
    (nth 
        (map first (iterate fib-pair [1 1]))
        50)) ;The book says 500th, but even 100th gives java.lang.ArithmeticException: integer overflow!