Haskell Cheat Sheet

ADVERTISEMENT

Haskell Cheat Sheet
Strings
– Syntax error; need [-100.. -110] for
negatives.
– Unicode string.
This cheat sheet attempts to lay out the fundamen-
– List from 1 to 100 by
– Single character.
tal elements of the Haskell language and libraries.
2, -1 to 100 by 4.
It should serve as a reference to both those learn-
Multi-line Strings
Normally, it is syntax error if
ing Haskell and those who are familiar with it, but
a string has any actual new line characters. That is,
Lists & Tuples
maybe can’t remember all the varieties of syntax
this is a syntax error:
– Empty list.
and functionality.
– List of three numbers.
It is presented as both an executable Haskell file
– Alternate way to write lists us-
and a printable document. Load the source into
ing “cons” ( ) and “nil” ( ).
your favorite interpreter to play with code samples
However, backslashes (‘ ’) can be used to “escape”
– List of three characters (strings are lists).
shown.
around the new line:
– List of characters (same
as
).
Syntax
– 2-element tuple of a number and a string.
– 4-element tuple of two
Below the most basic syntax for Haskell is given.
The area between the backslashes is ignored. An
functions, a number and a character.
important note is that new lines in the string must
Comments
still be represented explicitly:
“Layout” rule, braces and semi-colons.
A single line comment starts with ‘ ’ and extends
Haskell can be written using braces and semi-
to the end of the line. Multi-line comments start
colons, just like C. However, no one does. Instead,
with ’ ’ and extend to ’ ’. Comments can be
the “layout” rule is used, where spaces represent
That is,
evaluates to:
nested.
scope. The general rule is – always indent. When
Comments above function definitions should
the compiler complains, indent more.
start with ‘
’ and those next to parameter types
Braces and semi-colons
Semi-colons terminate
While
evaluates to:
with ‘
’ for compatibility with Haddock, a sys-
an expression, and braces represent scope:
tem for documenting Haskell code.
Reserved Words
Function Definition
Indent the body at least
Numbers
one space from the function name:
The following lists the reserved words defined by
Haskell. It is a syntax error to give a variable or
- Integer
function one of these names.
- Floating point
– List of numbers – 1, 2, ...10
Unless a
clause is present. In that case, in-
– Infinite list of numbers – 100, 101, 102, ...
dent the where clause at least one space from the
– Empty list; ranges only go forwards.
function name and any function bodies at least one
– Negative integers.
space from the
keyword:
c 2008 Justin Bailey.
1

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education