DEV Community

Discussion on: [Discuss] What's your favourite Fibonacci implementation and in which language? Mine is in Haskell

Collapse
 
deciduously profile image
Ben Lovy • Edited

That is a nice fibonacci! I love Haskell because no matter how nicely I think I've seen a solution expressed there's always something neat I hadn't quite seen. In a similar vein, I like the example from the Clojure docs for lazy-seq:

;; A lazy-seq of Fibonacci numbers (fn = fn-1 + fn-2)
;; The producer function takes exactly two parameters
;; (because we need the last 2 elements to produce a new one)
user=> (defn fib 
         ([]
           (fib 1 1))
         ([a b]
           (lazy-seq (cons a (fib b (+ a b))))))

user=> (take 5 (fib))
(1 1 2 3 5)

You don't really need to know Clojure much at all to read this and understand what it does.

Collapse
 
patrickbuhagiar profile image
PatrickBuhagiar

I've yet to dabble in Clojure, but thanks for sharing!😁 You're right, it certainly is easier to understand without knowing the language