Common Misunderstandings
From HaskellWiki
(Difference between revisions)
m |
m |
||
| Line 3: | Line 3: | ||
== Indentation == | == 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 | + | 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 <hask>then</hask> and <hask>else</hask> must be indented deeper than the <hask>if</hask> statement (or on the same line as the if). |
== If / Then / Else == | == If / Then / Else == | ||
Revision as of 04:33, 22 February 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 thatthen
else
if
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:
x = b ? y;
x when b is false? One should also recognize that the types returned by the then
else
if
else
main = do startNetwork <- askUser "Network? " if startNetwork then do iface <- initNetworkInterface handlePackets iface else return ()
1.3 do Notation
If the do notation page ever exists I'll advice you to check it out. Until then, understand that a missingdo
if
case
do
This certainly isn't the full picture - for an inverse point of view see do notation considered harmful.
