Haskell Cheat Sheet Page 2

ADVERTISEMENT

“Layout” rule, braces and semi-colons.
Let
Indent the body of the let at least one space
Nesting & Capture
Nested matching and bind-
from the first definition in the
. If
appears
ing are also allowed.
Haskell can be written using braces and semi-
on its own line, the body of any definition must
colons, just like C. However, no one does. Instead,
appear in the column after the let:
the “layout” rule is used, where spaces represent
scope. The general rule is: always indent. When
Figure 1: The definition of
the compiler complains, indent more.
Using
we can determine if any choice was
Braces and semi-colons
Semi-colons termi-
given using a nested match:
As can be seen above, the
keyword must also be
nate an expression, and braces represent scope.
in the same column as
. Finally, when multiple
They can be used after several keywords:
,
definitions are given, all identifiers must appear in
,
and
. They cannot be used when defin-
the same column.
ing a function body. For example, the below will
not compile.
Keywords
Haskell keywords are listed below, in alphabetical
Binding can be used to manipulate the value
However, this will work fine:
order.
matched:
Case
is similar to a
statement in C# or Java,
but can match a pattern: the shape of the value be-
Function Definition
Indent the body at least
ing inspected. Consider a simple data type:
one space from the function name:
can be used to determine which choice was
Unless a
clause is present. In that case, in-
given:
dent the where clause at least one space from the
Matching Order
Matching proceeds from top to
function name and any function bodies at least one
bottom. If
is reordered as follows, the
space from the
keyword:
first pattern will always succeed:
As with pattern-matching in function definitions,
the ‘ ’ token is a “wildcard” matching any value.
c 2009 Justin Bailey.
2

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education