Haskell Cheat Sheet Page 12

ADVERTISEMENT

Using
, we can write the
function
That is,
can take two arguments. The
changes. “Updating” is really a copy operation,
which converts certain letters to numbers:
first is the conversion function which converts indi-
with new values in the fields that “changed.” For
vidual characters and the second is the string to be
example, using the
type defined earlier, we
converted.
can write a function that sets the
field to zero
A curried form of any function which takes
easily:
multiple arguments can be created. One way to
think of this is that each “arrow” in the function’s
The above is a bit verbose and can be rewriten us-
signature represents a new function which can be
ing record syntax. This kind of “update” only sets
created by supplying one more argument.
values for the field(s) specified and copies the rest:
Sections
Operators are functions, and they can
be curried like any other. For example, a curried
Here we capture the
value in
and return a
version of “ ” can be written as:
new
value. That value happens to have the
Notice that
has no arguments specified. Also,
same value for
and
as
and it’s
the final argument to
is not given.
component is 0. We can combine this with pattern
However, this can be unwieldy and hard to read.
However, the type signature of
tells the whole
matching to set the
and
fields to equal
“Sections” are curried operators, using parenthe-
story:
the
field:
ses. Here is
using sections:
That is,
takes a string and produces a string.
The supplied argument can be on the right or left,
Notice we must use argument capture (“ ”) to get
It is a “constant”, in the sense that
always re-
which indicates what position it should take. This
the
value and pattern matching with record
turns a value that is a function which takes a string
is important for operations such as concatenation:
syntax (“
”) to get the inner
field.
and produces a string.
returns a “curried”
form of
, where only two of its three
Anonymous Functions
arguments have been supplied.
This can be taken further. Say we want to write
An anonymous function (i.e., a lambda expression
Which produces quite different results:
a function which only changes upper case letters.
or lambda for short), is a function without a name.
We know the test to apply,
, but we don’t
They can be defined at any time like so:
want to specify the conversion. That function can
be written as:
which defines a function which takes an argument
and returns a tuple containing that argument in
both positions. They are useful for simple func-
which has the type signature:
“Updating” values and record syntax
tions which don’t need a name. The following de-
Haskell is a pure language and, as such, has no
termines if a string has mixed case (or is all whites-
mutable state. That is, once a value is set it never
pace):
c 2009 Justin Bailey.
12

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education