Developers Planet

I don't clearly see why this is the Fibonacci sequence. Am I intelligent enough to be a Haskell programmer?

This is a way to get the Fibonacci sequence in Haskell. It's from the book haskellbook.com (supposedly for beginners):

``````fibs = 1 : scanl (+) 1 fibs
``````

I can only "see" that this is Fibonacci after writing out a few of the first elements manually:

``````1 : scanl (+) 1 (1 : scanl (+) 1 (1 : scanl (+) 1 (1 : 1 ...
1 : scanl (+) 1 (1 : scanl (+) 1 (1 : 1 : 2 : 3 ...
1 : scanl (+) 1 (1 : 1 : 2 : 3 : 5 : 8 ...
1 : 1 : 2 : 3 : 5 : 8 : 13 : 21 ...
``````

Only now do I have some intuition into why this is the Fibonacci sequence. But I had to write it out like this first. And even then it's still just an intuition, not solid proof.

I have three questions:

1. Was I supposed to immediately just spot the Fibonacci sequence from the original line?
2. Would some of you professional Haskell programmer in here of 5-10 years, if you had for the sake of argument never before seen this particular example, immediately have seen that the original line is the Fibonacci sequence?
3. I sometimes find myself wondering whether I'm intelligent enough to be a Haskell programmer. Maybe it's just not for me. Haskell makes me feel inadequate sometimes :( Never had this feeling with other programming languages. So the third question: Have any tests/methods been developed to determine whether a particular programmer is intelligent enough to be a Haskell programmer? (I'm pretty sure Haskell requires a higher IQ than most other languages!)

Neuron February 2016

1. Like any other language, the semantics of code should - to some extend - document itself, so the answer is yes. But at the same time it takes time getting used to the syntax - just like with any other language. Don't worry, you'll get it. Just try not to back away from such problems and embrace them. Working them through step by step is a good approach, keep doing that!
2. I am neither a professional Haskell programmer, nor did I learn it 10 years ago, but to some extend yes. Just like you don't understand the Fibonacci in any other language within half a second when you see it, I'd have to think about it, but I'd get it a lot fast compared to the time I started writing in Haskell.
3. Like any other programming language, Haskell takes a certain mindset to be read and understood easily and quickly. Just remember the first time you saw code in any other language. Chances are, you felt the exact same thing, but don't remember it as vividly. As Haskell is a very maths inspired language, many non-mathematicians often struggle with the notation and the style of coding. My advise is not give up. You probably didn't have 3 years worth of maths at university, so it will just take a little while. But there is a huge pot of gold at the end of this rainbow. From my experience people who wrote Haskell code understand functional programming on a level no one else ever could! It will certainly help writing elegant code in any other language!

Was I supposed to immediately just spot the Fibonacci sequence from the original line?

Heck no! You were supposed to read the example, scratch your head, write it out manually... and thereby get an intuition for how `scanl` works and why this can be used for Fibonacci numbers.

Would some of you ... have seen that the original line is the Fibonacci sequence?

Well, it is called `fibs`... apart from that, yeah, `scanl` is standard enough that an experienced programmer would quickly see what's going on.

pretty sure Haskell requires a higher IQ

I think Haskell requires most of all a good deal of stubbornness, and a particular sense of æsthetics. Haskell has some quirky ways of doing stuff different. Some of these are a bit hard to grasp, but most of all they're just unfamiliar to most programmers. It's all about getting used to the stuff.

And because, if we're honest, Haskell skills are still not a terribly utilitarian thing right now (as in, any real-world task can be solved as well by some other language too), you really need some enthusiasm for the language's beauty and future potential to keep up with it.

Note that Haskell's quirks and features have a tendency to slowly seep into mainstream languages. So, even if “Haskell is not for you” you may in the long term benefit from the nice things that are prototyped here, perhaps without even noticing where they come from.