Revision as of 03:34, 17 April 2008

Contents

1 Common Mistakes and Incorrect Beliefs By Haskell Beginners

People going from zero to Haskell are likely gain a misunderstanding or miss a point that isn't stressed enough. Here are some mistakes that have been observed from multiple sources.

1.1 Indentation

Perhaps the first trip-up - you might understand that indentation defines where a code block starts and the lack of an equal amount of indentation indicates the previous code block ended. What some miss is that

then

and

else

must be indented deeper than the

if

statement:

if boolean
then expr1
else expr2

Or they can be on the same line as the if:

if boolean then expr1 else expr2

1.2 If / Then / Else

if-then statements must always include an 'else' portion. It might be best not to think of if-then-else as flow control, as in most imperative languages, but think of it as construction of a value using a well formed expression.

x = b ? y : z;

The above is valid, though not common, C code. It states that if b is true then x = y otherwise x = z. Notice how this makes no sense without z. Similarly, in Haskell an

if

/

then

makes no sense without an

else

.

let x =if b then y -- compare to x = b ? y

What is x when b is false? One should also recognize that the types returned by the

then

and

else

branches must match due to Haskells strong and static type system.
When

1.4 Iterating Over a List

Some beginners make the mistake of mistaking a single-element list pattern (such as

[x]

) for a pattern that iterates over every element in the list.
One example that recently (in April, 2008) appeared on the Haskell-Cafe mailing list was the following. Here, one coder attempted to write a function

hanoi

to solve the Towers of Hanoi problem, but to code it so that each tower could be named polymorphically. The problematic code segment was the following: