DEV Community

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

Collapse
 
patrickbuhagiar profile image
PatrickBuhagiar • Edited

While discussing what I've written here with a colleague, I've come across another neat implementation using unfoldr from this article which I've missed.

fibs = unfoldr (\(a,b) -> Just (a,(b,a+b))) (0,1)
take 10 fibs

unfoldr is a method that builds an array list (towards the right) when given an initial seed (in this case, 0 and 1).

Just is a term used in Haskell's Maybe type, which draws parallel to how Optionals work in Java. Think of it as Optional.of()