Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Fibonacci sounds perfect!

  fibonacci :: Integer -> Integer
  fibonacci 0 = 0
  fibonacci 1 = 1
  fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)
Arguments for this:

* "Find the Nth Fibonacci Number" is among the most universally known programming tasks, so visitors are far more likely to immediately pick up the example than they are with sieve.

* It shows off a bit of Haskell syntax that (A) can be learned just by looking at an example like this, (B) has a clear benefit to readability that any programmer can appreciate, and (C) is a syntax not found in most mainstream languages.

* The visitor needs no functional programming experience to follow it; it doesn't even use any higher-order functions! This is important, as many visitors will be completely new to FP, and an example that they can't follow is not going to be effective at encouraging them to continue reading.



That's a horrible algorithm though, it takes exponential time. Any good implementation of Fibonacci numbers would be logarithmic in the number of arithmetic operations and polynomial in overall running time (because the numbers get bigger). Here's a good implementation in Haskell: http://nayuki.eigenstate.org/res/fast-fibonacci-algorithms/f... , and the same in Python: http://nayuki.eigenstate.org/res/fast-fibonacci-algorithms/f... . BTW, I think even functional programmers would find the Python code slightly easier to follow :-)


Remember, the target audience is newcomers to Haskell. The purpose of a code sample is to give them a glimpse into the syntax of the language, which should be understandable enough that it piques their interest to learn more.

It would be to the detriment of haskell.org to showcase a superior algorithm that is harder for newcomers to follow.


Maybe it's because I've been using Haskell exclusively for a few months, but I find the Haskell example more clear. This surprises me because I have much more experience with Python.


I think the factorial function is even nicer: everyone with basic high school math has seen it. It's even shorter.

Also, I think the example should not be a partial function ;).


Your example doesnt highlight lazyness like the sieve one does. I would expect to see something like this instead:

    fibonacci :: [Integer]
    fibonacci = 1 : 1 : zipWith (+) fibonacci (tail fibonacci)


True, but remember that, say, a Rubyist arriving at haskell.org has no knowledge of:

* what zipWith does

* that (+) is a function being passed, not an operator being invoked

* cons syntax for 1 : 1 : ...

* how this could terminate (having no preexisting knowledge of laziness, remember)

A better goal than "show off all the features of Haskell" is "show a Haskell example that's understandable, demonstrates a clear benefit, and makes you want to learn more."

If a newcomer encounters so much unfamiliar territory that Haskell comes across as too alien to be useful, that only reinforces existing negative stereotypes about it.

Better to give a first impression of "you absolutely can pick up Haskell, and it'll be nice!"


> True, but remember that, say, a Rubyist arriving at haskell.org has no knowledge of:

> * what zipWith does

haskell:

  zipWith f xs ys
ruby:

  xs.lazy.zip(ys).map(&f)
While Ruby doesn't have a method of the same name, its not exactly foreign.

> * that (+) is a function being passed, not an operator being invoked

Again, sure, Haskell has different syntax than ruby, but the parens signal something special is going on here.

> * cons syntax for 1 : 1 : ...

Sure, but if you know what the Fibonacci sequence is -- and the reason it and primes are almost without exception the two things chosen for these infinite stream examples in every language is because its presumed that programmers do, its pretty easy to get the idea of what is being done from knowing what the function is trying to do and looking at the values presented.

> * how this could terminate (having no preexisting knowledge of laziness, remember)

A Rubyist that has no knowledge of laziness isn't a very knowledgable Rubyist, since laziness is an important and central concept in Ruby's Enumerable module (one of the core modules most frequently used by Rubyists.)




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: