Haskell Cheat Sheet Page 6

ADVERTISEMENT

silly example which gives the sum of a list of num-
A module can only be defined in one file, though
bers, their average, and their median:
its exports may come from multiple sources. To
make a Haskell file a module, just add a module
declaration at the top:
Import
See the section on
on page 6.
Module names must start with a capital letter but
In
otherwise can include periods, numbers and un-
derscores. Periods are used to give sense of struc-
See
on page 6.
ture, and Haskell compilers will use them as indi-
cations of the directory a particular source file is,
Deconstruction
The left-hand side of a
def-
Infix, infixl and infixr
but otherwise they have no meaning.
inition can also destructure its argument, in case
The Haskell community has standardized a set
See the section on
operators
on page 11.
sub-components are to be accessed. This definition
of top-level module names such as
,
,
would extract the first three characters from a string
, etc. Be sure to consult them for an appro-
Instance
priate place for your own module if you plan on
See the section on
on page 3.
releasing it to the public.
Let
Imports
The Haskell standard libraries are di-
vided into a number of modules. The functionality
Local functions can be defined within a function us-
provided by those libraries is accessed by import-
ing
. The
keyword must always be followed
ing into your source file. To import all everything
Note that this is different than the following, which
by
. The
must appear in the same column as
exported by a library, just use the module name:
only works if the string has three characters:
the
keyword. Functions defined have access to
all other functions and variables within the same
scope (including those defined by
). In this ex-
Everything means everything: functions, data types
ample,
multiplies its argument
by , which
and constructors, class declarations, and even other
was passed to the original
.
is used
modules imported and then exported by the that
by map to give the multiples of x up to 10:
module. Importing selectively is accomplished by
giving a list of names to import. For example, here
Of
we import some functions from
:
See the section on
on page 2.
“functions” with no arguments are actually
Module
Data types can imported in a number of ways. We
constants and, once evaluated, will not evaluate
can just import the type and no constructors:
again. This is useful for capturing common por-
A module is a compilation unit which exports func-
tions of your function and re-using them. Here is a
tions, types, classes, instances, and other modules.
c 2009 Justin Bailey.
6

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education