Fibonacci series one-liner in Haskell
Here’s a beautiful example of why Haskell is the most advanced programming language on the planet – a one-line definition of the entire fibonacci series:
fiblist = 0 : 1 : (zipWith (+) fiblist (tail fiblist))
Stunning. Note that that is not a function to calculate the nth fibonacci number: it really is a definition of the entire series. If you want the nth fibonacci number, look up the nth element of that list. Let’s see you do that in Java! (Or C#)
(Via the introductory series on Haskell at the rather good Good Math, Bad Math (more on the author here).)
6 Responses to “Fibonacci series one-liner in Haskell”
Leave a reply
You can use HTML, but you don't have to. Formatting tips (for code, quotes, etc.) here.





I was going to post a really long comment, but I decided to put it on my blog instead.
Cultural note: coding up the fibonacci series is a standard greeting for newcomers in
#haskell For example:
:-)
I presume for the second to last one you mean:
Which is more or less equivalent (in terms of how it works) to the zipWith implementation. I got caught out with the < too – you have to html-escape it. (More “must use HTML” than “may use HTML” really :)
Edit 2007.01.21 — not any more! -gimbo
Same thing but in scheme
Here it is in Perl 6:
my @fiblist = 0, 1 ... { $^a + $^b };