Haskell Cheat Sheet Page 7

ADVERTISEMENT

have a prefix attached through qualified imports.
This is particularly useful for modules which have
a large number of functions having the same name
as
functions.
is a good example:
As opposed to
, the
and
“values” on
The same syntax as used for importing can be used
are not just
values. The typechecker
here to specify which functions, types, construc-
tors, and classes are exported, with a few differ-
treats them as entirely new types. That means our
This form requires any function, type, constructor
function from above would not compile.
ences. If a module imports another module, it can
or other name exported by
to now be pre-
The following produces a type error:
also export that module:
fixed with the alias (i.e.,
) given. Here is one way
to remove all duplicates from a list:
Instead, we must use pattern-matching to get to the
“values” to which we apply
:
A second form does not create an alias. Instead,
the prefix becomes the module name. We can write
a simple function to check if a string is all upper
A module can even re-export itself, which can be
case:
useful when all local definitions and a given im-
The key observation is that this keyword does not
ported module are to be exported. Below we export
introduce a new value; instead it introduces a new
ourselves and
, but not
:
type. This gives us two very useful properties:
No runtime cost is associated with the new
type, since it does not actually produce new
Except for the prefix specified, qualified imports
values. In other words, newtypes are abso-
support the same syntax as normal imports. The
lutely free!
name imported can be limited in the same ways as
described above.
The type-checker is able to enforce that com-
mon types such as
or
are used in
Exports
If an export list is not provided, then all
restricted ways, specified by the programmer.
functions, types, constructors, etc. will be available
Newtype
to anyone importing the module. Note that any im-
While
introduces new values and
just
Finally, it should be noted that any
ported modules are not exported in this case. Limit-
creates synonyms,
falls somewhere be-
clause which can be attached to a
declaration
ing the names exported is accomplished by adding
tween. The syntax for
is quite restricted –
can also be used when declaring a
.
a parenthesized list of names before the
key-
only one constructor can be defined, and that con-
word:
structor can only take one argument. Continuing
Return
the example above, we can define a
type like
the following:
See
above.
c 2008 Justin Bailey.
7

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education