<s>
This	O
article	O
describes	O
the	O
features	O
in	O
Haskell	B-Language
.	O
</s>
<s>
A	O
simple	O
example	O
that	O
is	O
often	O
used	O
to	O
demonstrate	O
the	O
syntax	O
of	O
functional	B-Language
languages	I-Language
is	O
the	O
factorial	O
function	O
for	O
non-negative	O
integers	O
,	O
shown	O
in	O
Haskell	B-Language
:	O
</s>
<s>
Much	O
of	O
Haskell	B-Language
code	O
is	O
similar	O
to	O
standard	O
mathematical	O
notation	O
in	O
facility	O
and	O
syntax	O
.	O
</s>
<s>
The	O
second	O
line	O
relies	O
on	O
pattern	B-Language
matching	I-Language
,	O
an	O
important	O
feature	O
of	O
Haskell	B-Language
.	O
</s>
<s>
Using	O
the	O
product	O
function	O
from	O
the	O
Prelude	O
,	O
a	O
number	O
of	O
small	O
functions	O
analogous	O
to	O
C	B-Language
's	O
standard	B-Language
library	I-Language
,	O
and	O
using	O
the	O
Haskell	B-Language
syntax	O
for	O
arithmetic	O
sequences	O
,	O
the	O
factorial	O
function	O
can	O
be	O
expressed	O
in	O
Haskell	B-Language
as	O
follows	O
:	O
</s>
<s>
which	O
,	O
using	O
the	O
function	B-Application
composition	I-Application
operator	I-Application
(	O
expressed	O
as	O
a	O
dot	O
in	O
Haskell	B-Language
)	O
to	O
compose	O
the	O
product	O
function	O
with	O
the	O
curried	B-Application
enumeration	O
function	O
can	O
be	O
rewritten	O
in	O
point-free	B-Application
style	I-Application
:	O
</s>
<s>
In	O
the	O
Hugs	O
interpreter	B-Application
,	O
one	O
often	O
needs	O
to	O
define	O
the	O
function	O
and	O
use	O
it	O
on	O
the	O
same	O
line	O
separated	O
by	O
a	O
where	O
or	O
let	O
..	O
in	O
.	O
</s>
<s>
The	O
GHCi	B-Application
interpreter	B-Application
does	O
n't	O
have	O
this	O
restriction	O
and	O
function	O
definitions	O
can	O
be	O
entered	O
on	O
one	O
line	O
(	O
with	O
the	O
let	O
syntax	O
without	O
the	O
in	O
part	O
)	O
,	O
and	O
referenced	O
later	O
.	O
</s>
<s>
In	O
the	O
Haskell	B-Language
source	O
immediately	O
below	O
,	O
::	O
can	O
be	O
read	O
as	O
"	O
has	O
type	O
"	O
;	O
a	O
->	O
b	O
can	O
be	O
read	O
as	O
"	O
is	O
a	O
function	O
from	O
a	O
to	O
b	O
"	O
.	O
</s>
<s>
(	O
Thus	O
the	O
Haskell	B-Language
calc	O
::	O
String	O
->	O
 [ Float ] 	O
can	O
be	O
read	O
as	O
"	O
calc	O
has	O
type	O
of	O
a	O
function	O
from	O
Strings	O
to	O
lists	O
of	O
Floats	B-Algorithm
"	O
.	O
)	O
</s>
<s>
A	O
simple	O
Reverse	O
Polish	O
notation	O
calculator	O
expressed	O
with	O
the	O
higher-order	B-Language
function	I-Language
foldl	B-Application
whose	O
argument	O
f	O
is	O
defined	O
in	O
a	O
where	O
clause	O
using	O
pattern	B-Language
matching	I-Language
and	O
the	O
type	O
class	O
Read	O
:	O
</s>
<s>
The	O
empty	O
list	O
is	O
the	O
initial	O
state	B-Application
,	O
and	O
f	O
interprets	O
one	O
word	O
at	O
a	O
time	O
,	O
either	O
as	O
a	O
function	O
name	O
,	O
taking	O
two	O
numbers	O
from	O
the	O
head	O
of	O
the	O
list	O
and	O
pushing	O
the	O
result	O
back	O
in	O
,	O
or	O
parsing	B-Language
the	O
word	O
as	O
a	O
floating-point	B-Algorithm
number	I-Algorithm
and	O
prepending	O
it	O
to	O
the	O
list	O
.	O
</s>
<s>
The	O
following	O
definition	O
produces	O
the	O
list	O
of	O
Fibonacci	B-Algorithm
numbers	I-Algorithm
in	O
linear	O
time	O
:	O
</s>
<s>
The	O
infinite	O
list	O
is	O
produced	O
by	O
corecursion	B-Application
—	O
the	O
latter	O
values	O
of	O
the	O
list	O
are	O
computed	O
on	O
demand	O
starting	O
from	O
the	O
initial	O
two	O
items	O
0	O
and	O
1	O
.	O
</s>
<s>
This	O
kind	O
of	O
a	O
definition	O
relies	O
on	O
lazy	O
evaluation	O
,	O
an	O
important	O
feature	O
of	O
Haskell	B-Language
programming	I-Language
.	O
</s>
<s>
The	O
same	O
function	O
,	O
written	O
using	O
Glasgow	B-Application
Haskell	I-Application
Compiler	I-Application
's	O
parallel	O
list	B-Language
comprehension	I-Language
syntax	O
(	O
GHC	O
extensions	O
must	O
be	O
enabled	O
using	O
a	O
special	O
command-line	O
flag	O
,	O
here	O
-XParallelListComp	O
,	O
or	O
by	O
starting	O
the	O
source	O
file	O
with	O
{	O
-	O
#	O
LANGUAGE	O
ParallelListComp	O
#	O
-	O
}	O
)	O
:	O
</s>
<s>
or	O
with	O
regular	O
list	B-Language
comprehensions	I-Language
:	O
</s>
<s>
With	O
stateful	B-Application
generating	O
function	O
:	O
</s>
<s>
Using	O
data	O
recursion	O
with	O
Haskell	B-Language
's	O
predefined	O
fixpoint	B-Application
combinator	I-Application
:	O
</s>
<s>
Like	O
the	O
various	O
fibs	O
solutions	O
displayed	O
above	O
,	O
this	O
uses	O
corecursion	B-Application
to	O
produce	O
a	O
list	O
of	O
numbers	O
on	O
demand	O
,	O
starting	O
from	O
the	O
base	O
case	O
of	O
1	O
and	O
building	O
new	O
items	O
based	O
on	O
the	O
preceding	O
part	O
of	O
the	O
list	O
.	O
</s>
<s>
This	O
uses	O
the	O
more	O
efficient	O
function	O
merge	O
which	O
does	O
n't	O
concern	O
itself	O
with	O
the	O
duplicates	O
(	O
also	O
used	O
in	O
the	O
following	O
next	O
function	O
,	O
mergesort	B-Algorithm
)	O
:	O
</s>
<s>
Each	O
vertical	O
bar	O
(	O
|	O
)	O
starts	O
a	O
guard	B-Language
clause	I-Language
with	O
a	O
guard	B-Language
expression	O
before	O
the	O
=	O
sign	O
and	O
the	O
corresponding	O
definition	O
after	O
it	O
,	O
that	O
is	O
evaluated	O
if	O
the	O
guard	B-Language
is	O
true	O
.	O
</s>
<s>
Here	O
is	O
a	O
bottom-up	O
merge	B-Algorithm
sort	I-Algorithm
,	O
defined	O
using	O
the	O
higher-order	B-Language
function	I-Language
until	O
:	O
</s>
<s>
The	O
mathematical	O
definition	O
of	O
primes	O
can	O
be	O
translated	O
pretty	O
much	O
word	O
for	O
word	O
into	O
Haskell	B-Language
:	O
</s>
<s>
This	O
finds	O
primes	O
by	O
trial	B-Algorithm
division	I-Algorithm
.	O
</s>
<s>
or	O
an	O
unbounded	O
sieve	B-Algorithm
of	I-Algorithm
Eratosthenes	I-Algorithm
with	O
postponed	O
sieving	O
in	O
stages	O
,	O
</s>
<s>
Haskell	B-Language
allows	O
indentation	O
to	O
be	O
used	O
to	O
indicate	O
the	O
beginning	O
of	O
a	O
new	O
declaration	O
.	O
</s>
<s>
Haskell	B-Language
,	O
indentation	O
can	O
be	O
used	O
in	O
several	O
syntactic	O
constructs	O
,	O
</s>
<s>
off-side	B-Language
rule	I-Language
.	O
</s>
<s>
off-side	B-Language
rule	I-Language
,	O
which	O
is	O
called	O
"	O
layout	B-Language
"	O
.	O
</s>
<s>
include	O
Python	B-Language
and	O
F#	B-Operating_System
.	O
</s>
<s>
The	O
use	O
of	O
layout	B-Language
in	O
Haskell	B-Language
is	O
optional	O
.	O
</s>
<s>
Haskell	B-Language
's	O
layout	B-Language
rule	I-Language
has	O
been	O
criticised	O
for	O
its	O
complexity	O
.	O
</s>
<s>
inserting	O
a	O
close	O
brace	O
(	O
the	O
"	O
parse	B-Language
error	O
"	O
rule	O
)	O
.	O
</s>
<s>
Haskell	B-Language
distinguishes	O
function	O
calls	O
from	O
infix	O
operators	O
syntactically	O
,	O
but	O
not	O
semantically	O
.	O
</s>
<s>
See	O
List	B-Language
comprehension	I-Language
#Overview	O
for	O
the	O
Haskell	B-Language
example	O
.	O
</s>
<s>
Pattern	B-Language
matching	I-Language
is	O
used	O
to	O
match	O
on	O
the	O
different	O
constructors	O
of	O
algebraic	O
data	O
types	O
.	O
</s>
<s>
Here	O
are	O
some	O
functions	O
,	O
each	O
using	O
pattern	B-Language
matching	I-Language
on	O
each	O
of	O
the	O
types	O
below	O
:	O
</s>
<s>
Tuples	O
in	O
haskell	B-Language
can	O
be	O
used	O
to	O
hold	O
a	O
fixed	O
number	O
of	O
elements	O
.	O
</s>
<s>
In	O
the	O
GHC	B-Application
compiler	I-Application
,	O
tuples	O
are	O
defined	O
with	O
sizes	O
from	O
2	O
elements	O
up	O
to	O
62	O
elements	O
.	O
</s>
<s>
In	O
the	O
section	O
above	O
,	O
calc	O
is	O
used	O
in	O
two	O
senses	O
,	O
showing	O
that	O
there	O
is	O
a	O
Haskell	B-Language
type	O
class	O
namespace	O
and	O
also	O
a	O
namespace	O
for	O
values	O
:	O
</s>
<s>
a	O
Haskell	B-Language
type	O
class	O
for	O
calc	O
.	O
</s>
<s>
The	O
domain	B-Algorithm
and	O
range	B-Algorithm
can	O
be	O
explicitly	O
denoted	O
in	O
a	O
Haskell	B-Language
type	O
class	O
.	O
</s>
<s>
a	O
Haskell	B-Language
value	O
,	O
formula	O
,	O
or	O
expression	O
for	O
calc	O
.	O
</s>
<s>
Algebraic	O
data	O
types	O
are	O
used	O
extensively	O
in	O
Haskell	B-Language
.	O
</s>
<s>
The	O
ST	O
monad	O
allows	O
programmers	O
to	O
write	O
imperative	O
algorithms	O
in	O
Haskell	B-Language
,	O
using	O
mutable	O
variables	O
(	O
STRefs	O
)	O
and	O
mutable	O
arrays	O
(	O
STArrays	O
and	O
STUArrays	O
)	O
.	O
</s>
<s>
Here	O
is	O
an	O
example	O
program	O
(	O
taken	O
from	O
the	O
Haskell	B-Language
wiki	O
page	O
on	O
the	O
)	O
that	O
takes	O
a	O
list	O
of	O
numbers	O
,	O
and	O
sums	O
them	O
,	O
using	O
a	O
mutable	O
variable	O
:	O
</s>
<s>
The	O
STM	O
monad	O
is	O
an	O
implementation	O
of	O
Software	B-Operating_System
Transactional	I-Operating_System
Memory	I-Operating_System
in	O
Haskell	B-Language
.	O
</s>
<s>
It	O
is	O
implemented	O
in	O
the	O
GHC	B-Application
compiler	I-Application
,	O
and	O
allows	O
for	O
mutable	O
variables	O
to	O
be	O
modified	O
in	O
transactions	B-General_Concept
.	O
</s>
<s>
As	O
Haskell	B-Language
is	O
a	O
pure	O
functional	B-Language
language	I-Language
,	O
functions	O
cannot	O
have	O
side	O
effects	O
.	O
</s>
<s>
Haskell	B-Language
solves	O
this	O
with	O
monadic	O
types	O
that	O
leverage	O
the	O
type	O
system	O
to	O
ensure	O
the	O
proper	O
sequencing	O
of	O
imperative	O
constructs	O
.	O
</s>
<s>
The	O
typical	O
example	O
is	O
I/O	O
,	O
but	O
monads	O
are	O
useful	O
for	O
many	O
other	O
purposes	O
,	O
including	O
mutable	O
state	B-Application
,	O
concurrency	B-Operating_System
and	O
transactional	O
memory	O
,	O
exception	O
handling	O
,	O
and	O
error	O
propagation	O
.	O
</s>
<s>
Haskell	B-Language
provides	O
a	O
special	O
syntax	O
for	O
monadic	O
expressions	O
,	O
so	O
that	O
side-effecting	O
programs	O
can	O
be	O
written	O
in	O
a	O
style	O
similar	O
to	O
current	O
imperative	O
programming	O
languages	O
;	O
no	O
knowledge	O
of	O
the	O
mathematics	O
behind	O
monadic	O
I/O	O
is	O
required	O
for	O
this	O
.	O
</s>
<s>
See	O
also	O
wikibooks:Transwiki:List	O
of	O
hello	O
world	O
programs	O
#Haskell	O
for	O
another	O
example	O
that	O
prints	O
text	O
.	O
</s>
<s>
concurrency	B-Operating_System
or	O
parallelism	B-Operating_System
,	O
although	O
GHC	O
supports	O
both	O
.	O
</s>
<s>
parallel	O
on	O
a	O
multiprocessor	B-Operating_System
.	O
</s>
<s>
blocking	O
other	O
running	O
Haskell	B-Language
threads	O
.	O
</s>
<s>
Recently	O
,	O
Concurrent	B-Language
Haskell	I-Language
has	O
been	O
extended	O
with	O
support	O
for	O
Software	B-Operating_System
Transactional	I-Operating_System
Memory	I-Operating_System
(	O
STM	O
)	O
,	O
which	O
is	O
a	O
concurrency	B-Operating_System
abstraction	O
in	O
which	O
compound	O
operations	O
on	O
shared	O
data	O
are	O
performed	O
atomically	O
,	O
as	O
transactions	B-General_Concept
.	O
</s>
<s>
The	O
Haskell	B-Language
STM	O
library	O
also	O
provides	O
two	O
operations	O
not	O
found	O
in	O
other	O
STMs	O
:	O
retry	O
and	O
orElse	O
,	O
which	O
together	O
allow	O
blocking	O
operations	O
to	O
be	O
defined	O
in	O
a	O
modular	O
and	O
composable	O
fashion	O
.	O
</s>
