You are on page 1of 25

GENERAL ¨ ARTICLE

A Taste of Functional Programming – 2


Madhavan Mukund

F u n c tio n a l p ro g ra m m in g h a s its ro o ts in A lo n z o
C h u rch 's la m b d a c a lc u lu s. In th e ¯ rst p a rt 1 o f
th is a rtic le , w e e x p lo re d so m e b a sic n o tio n s o f
fu n c tio n a l p ro g ra m m in g u sin g th e la n g u a g e
H a sk e ll. W e n o w e x a m in e so m e m o r e a d v a n c e d
c o n c e p ts, in c lu d in g p o ly m o rp h ism , in ¯ n ite d a ta
Madhavan Mukund has ty p e s a n d c o m p u ta tio n s, a n d u se r-d e ¯ n e d d a ta
been on the Computer ty p e s.
Science faculty at Chennai
Mathematical Institute 1 . R ecap
since 1992, where he is
currently Professor. His H a sk ell is a ty p ed fu n ctio n a l p ro g ra m m in g la n g u a g e.
research interests are Its b u ilt-in sca la r ty p es in clu d e I n t , F l o a t , C h a r a n d
primarily in formal
B o o l fo r in teg ers, ° o a tin g p o in t n u m b ers, ch a ra cters a n d
methods for specifying and
verifying computing
b o o lea n va lu es, resp ectiv ely. A fu n ctio n fro m ty p e A to
systems. He is the ty p e B h a s ty p e A - > B . T h u s, th e fu n ctio n f a c t o r i a l
Secretary of the Indian w o u ld h av e ty p e I n t - > I n t . M u ltip le in p u ts a re rea d
Association for Research o n e a tim e, u sin g cu rryin g. F o r in sta n ce, a fu n ctio n p l u s
in Computing Science
th a t a d d s tw o in teg ers w o u ld h av e ty p e I n t - > I n t - >
(IARCS) and the National
Coordinator of the Indian
I n t ra th er th a n I n t £ I n t - > I n t .
Computing Olympiad.
T h e b a sic co llectiv e ty p e is a list { a seq u en ce o f va lu es
w ith a u n ifo rm u n d erly in g ty p e. F o r in sta n ce, [ 3 , 1 , 2 , 1 ]
is a list o f I n t . A list w ith u n d erly in g ty p e T h a s ty p e
1
A taste of functional program- [ T ] , so th e p reced in g ex a m p le h a s ty p e [ I n t ] . E v ery
ming – 1, Resonance, Vol.12, list is b u ilt u p fro m th e em p ty list [ ] b y rep ea ted a p -
No.8, pp.27–48, 2007. p lica tio n s o f th e o p era to r : th a t a p p en d s a n elem en t to
th e left o f a list. T h u s, th e list [ 3 , 1 , 2 , 1 ] is in tern a lly
rep resen ted a s 3 : ( 1 : ( 2 : ( 1 : [ ] ) ) ) , o r m o re sim p ly a s
3 : 1 : 2 : 1 : [ ] , sin ce : a sso cia tes to th e rig h t.
A H a sk ell p ro g ra m co n sists o f a n u m b er o f fu n ctio n s.
Keywords E a ch fu n ctio n is sp eci¯ ed u sin g o n e o r m o re d e¯ n itio n s
Functional programming, poly- th a t a re sca n n ed fro m to p to b o tto m . P a ttern m a tch in g
morphism, currying, reductions. ca n b e u sed to sim p lify d e¯ n itio n b y ca ses. F o r in sta n ce,

40 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

h ere is a n in d u ctiv e d e¯ n itio n o f th e f i b o n a c c i fu n c- In Haskell, it is


tio n , w h ere th e ¯ rst lin e sp eci¯ es th e ty p e o f th e fu n c- possible to say that a
tio n . function works for
fibonacci :: Int -> Int multiple types by using
fibonacci 0 = 1 type variables. For
fibonacci 1 = 1 instance, we can write
fibonacci n = fibonacci (n-1) + fibonacci (n-2) the type of length as
length::[a]->Int. Here,
F o r fu n ctio n s o n lists, p a ttern m a tch in g ca n b e u sed
the letter a in the type
to im p licitly d eco m p o se a list in to its ¯ rst elem en t, th e
[a]->Int is a type
head, a n d th e rest o f th e list, th e tail. H ere is a fu n ctio n
variable.
th a t in d u ctiv ely co m p u tes th e len g th o f a list o f [ I n t ] .
length :: [Int] -> Int
length [] = 0
length (x:xs) = 1 + length xs
2 . P o ly m o r p h ism
O b serv e th a t th e fu n ctio n l e n g t h w o rk s in th e sa m e
w ay fo r lists o f a n y ty p e. It w o u ld b e w a stefu l to h av e
to w rite a sep a ra te v ersio n o f su ch a fu n ctio n fo r ea ch
d i® eren t ty p e o f list. In H a sk ell, it is p o ssib le to say
th a t a fu n ctio n w o rk s fo r m u ltip le ty p es b y u sin g ty p e
va ria b les. F o r in sta n ce, w e ca n w rite th e ty p e o f l e n g t h
a s l e n g t h : : [ a ] - > I n t . H ere, th e letter a in th e ty p e
[ a ] - > I n t is a ty p e va ria b le. T h e ty p e [ a ] - > I n t is to
b e rea d a s \ for an y u n derlyin g type a , this fu n ction is
of type [ a ] - > I n t " .
T h e fu n ctio n r e v e r s e rev erses th e o rd er o f elem en ts in
a list a n d th e fu n ctio n c o n c a t \ d isso lv es" o n e lev el o f
b ra ck ets in a n ested list. U sin g ty p e va ria b les, w e ca n
sp ecify th e m o st g en era l ty p es o f th ese fu n ctio n s a s fo l-
low s.
reverse :: [a] -> [a]
concat :: [[a]] -> [a]
In th ese ty p e d e¯ n itio n s, it is sig n i¯ ca n t th a t th e sa m e
letter a a p p ea rs o n b o th sid es o f th e - > . T h is m ea n s, fo r

RESONANCE ¨ September 2007 41


GENERAL ¨ ARTICLE

In a nutshell, in sta n ce, th a t th e ty p e o f th e list retu rn ed b y r e v e r s e


overloading uses the is th e sa m e a s th e ty p e o f th e in p u t list. In o th er w o rd s,
same symbol to a ll o ccu rren ces o f a ty p e va ria b le in a ty p e d ecla ra tio n
denote similar m u st b e in sta n tia ted to th e sa m e a ctu a l ty p e.
operations on different F u n ctio n s th a t w o rk in th e sa m e w ay o n d i® eren t ty p es
types, but the way the a re ca lled polym orphic, w h ich in G reek m ea n s `ta k in g
operation is d i® eren t fo rm s'. W e m u st b e ca refu l to d istin g u ish p o ly -
implemented for each m o rp h ism o f th e ty p e w e h av e seen w ith lists fro m th e a d
type is different. On h o c va riety a sso cia ted w ith ov erlo a d in g o p era to rs. F o r
the other hand, in sta n ce, in m o st p ro g ra m m in g la n g u a g es, w e w rite +
polymorphism refers to d en o te a d d itio n fo r b o th in teg ers a n d ° o a tin g p o in t
to a single function n u m b ers. H ow ev er, sin ce th e u n d erly in g rep resen ta tio n s
definition with a fixed u sed fo r th e tw o k in d s o f n u m b ers a re co m p letely d i® er-
computation rule that en t, w e a re a ctu a lly u sin g th e sa m e n a m e (+ , in th is ca se)
works for multiple to d esig n a te fu n ctio n s th a t a re co m p u ted in a d i® eren t
types in the same m a n n er fo r d i® eren t b a se ty p es. T h is ty p e o f situ a tio n
way. is m o re p ro p erly referred to a s overloadin g.
In a n u tsh ell, ov erlo a d in g u ses th e sa m e sy m b o l to d e-
n o te sim ila r o p era tio n s o n d i® eren t ty p es, b u t th e w ay
th e o p era tio n is im p lem en ted fo r ea ch ty p e is d i® eren t.
O n th e o th er h a n d , p o ly m o rp h ism refers to a sin g le fu n c-
tio n d e¯ n itio n w ith a ¯ x ed co m p u ta tio n ru le th a t w o rk s
fo r m u ltip le ty p es in th e sa m e w ay.
2 .1 M a p a n d F ilte r
W e h av e seen th e b u ilt-in fu n ctio n m a p th a t a llow s u s to
a p p ly a fu n ctio n f \ p o in tw ise" to ea ch elem en t o f a list,
so th a t m a p f [ x 0 , x 1 , . . . , x k ] ; [ ( f x 0 ) , ( f x 1 ) ,
...,(f xk)].
W h a t is th e ty p e o f m a p ? T h e fu n ctio n f is in g en era l
o f ty p e a - > b . T h e list th a t m a p o p era tes o n m u st b e
co m p a tib le w ith th e fu n ctio n f , so it m u st b e o f ty p e
[ a ] . T h e list g en era ted b y m a p is o f ty p e [ b ] . T h u s,
w e h av e m a p : : ( a - > b ) - > [ a ] - > [ b ] . N o tice th a t th ere
a re tw o ty p e va ria b les, a a n d b , in th e ty p e d e¯ n itio n
fo r m a p . T h ese ca n b e in d ep en d en tly in sta n tia ted to

42 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

d i® eren t ty p es, a n d th ese in sta n tia tio n s a p p ly u n ifo rm ly


to a ll o ccu rren ces o f a a n d b .
W e h av e a lso seen th a t w e ca n ¯ lter o u t va lu es fro m a
list. L et l b e o f ty p e [ a ] a n d let p b e a fu n ctio n fro m
a to B o o l . T h en , w e ca n d e¯ n e f i l t e r a s fo llow s.

filter : (a -> Bool) -> [a] -> [a]


filter p [] = []
filter p (x:xs) | (p x) = x:(filter p xs)
| otherwise = filter p xs

H ere, th e o u tp u t o f f i l t e r is a su b list o f th e o rig in a l


list, so th e o u tp u t list h a s th e sa m e ty p e a s th e o rig in a l
list.
2 .2 C o n d itio n a l P o ly m o rp h ism
R eca ll th e fu n ctio n s u m th a t a d d s u p th e elem en ts o f a
list.

sum [] = 0
sum (x:xs) = x + (sum xs)

C lea rly, th is d e¯ n itio n o f s u m w o u ld w o rk fo r b o th lists


o f I n t a n d lists o f F l o a t , so it w o u ld b e a p p ro p ria te
to a ssig n it a p o ly m o rp h ic ty p e. H ow ev er, it w o u ld b e
w ro n g to w rite s u m : : [ a ] - > a b eca u se s u m w o rk s o n ly fo r
lists w h o se u n d erly in g ty p e su p p o rts a d d itio n . W e n eed
to a ssig n s u m a q u a li¯ ed p o ly m o rp h ic ty p e o f th e fo rm

sum :: [a] -> a, provided type a supports +

A n a lo g o u sly, co n sid er th e fu n ctio n q u i c k s o r t to so rt a


list. If w e u se co n crete ty p es, w e h av e to d e¯ n e sep a -
ra te v ersio n s o f q u i c k s o r t fo r ea ch ty p e o f list, su ch a s
iquicksort::[Int]->[Int], fquicksort::[Float]->
[ Fl oa t ] an d i l is t qu ic k so r t: : [[ In t ]] - >[ [ In t] ] .
T h is is clea rly u n d esira b le b eca u se th e a ctu a l d e¯ n itio n
o f q u i c k s o r t in a ll th ese ca ses is th e sa m e. T h e o th er
o p tio n is to d ecla re q u i c k s o r t to b e o f ty p e [ a ] - > [ a ] .

RESONANCE ¨ September 2007 43


GENERAL ¨ ARTICLE

Is th is rea so n a b le? C a n w e so rt a list o f fu n ctio n s o f


ty p e [ I n t - > I n t - > I n t ] su ch a s [ p l u s , t i m e s , m a x ] ?
T h e a n sw er is th a t w e ca n so rt a list o f va lu es p ro -
v id ed w e ca n co m p a re th ese va lu es w ith ea ch o th er. In
o th er w o rd s, w e n eed to a ssig n th e fo llow in g ty p e to
quicksort:

quicksort :: [a] -> [a],


provided we can compare values of type a

H a sk ell cla ssi¯ es ty p es in to su b sets, ca lled type classes,


b a sed o n th e a d d itio n a l p ro p erties sa tis¯ ed b y th e ty p es
in a cla ss. E a ch ty p e cla ss ca rries a n o b lig a tio n in th e
fo rm o f so m e u n d erly in g fu n ctio n s th a t a ty p e n eed s to
2
This is analogous to a class su p p o rt to b elo n g to th e cla ss2 . F o r in sta n ce, th e set o f
implementing an interface in an a ll ty p es th a t su p p o rt co m p a riso n fu n ctio n s < , < = ,...is
object oriented language.
ca lled O r d .
A su b set X o f a set Y ca n a lso b e d escrib ed in term s
o f its ch a ra cteristic fu n ctio n f X w h ere f X (x ) = T ru e if
a n d o n ly if x 2 X . T h u s, w e ca n th in k o f O r d a s a
fu n ctio n th a t m a p s ty p es to B o o l a n d w rite O r d T to
d en o te w h eth er o r n o t ty p e T b elo n g s to O r d . T h e ty p e
o f q u i c k s o r t n ow b eco m es:

quicksort :: (Ord a) => [a] -> [a]


Haskell classifies
types into subsets, T h is is rea d a s \If a is in O r d , then q u i c k s o r t is of type
called type classes, [ a ] - > [ a ] ". N o te th e d o u b le a rrow n o ta tio n to d en o te
based on the \if ...then ...".
additional properties
satisfied by the types
A n ev en m o re b a sic ty p e cla ss is E q , th e set o f a ll ty p es
th a t su p p o rt ch eck in g fo r eq u a lity. W h y is th is a n o n -
in a class. Each type
triv ia l ty p e cla ss? R eca ll th a t w e h av e fu n ctio n ty p es o f
class carries an
th e fo rm T 1 - > T 2 . O n e o f th e m o st fu n d a m en ta l re-
obligation in the form
su lts in th e th eo ry o f co m p u ta tio n is th a t it is n ot p o s-
of some underlying
sib le to e® ectiv ely ch eck w h eth er tw o co m p u ta b le fu n c-
functions that a type
tio n s a re eq u a l fo r a n y n o n triv ia l d e¯ n itio n o f eq u a lity
needs to support to
(fo r in sta n ce, f = = g if a n d o n ly if fo r ea ch in p u t x , f x
belong to the class. = = g x ). T h u s, fu n ctio n s d o n o t b elo n g to E q . A ty p ica l

44 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

ex a m p le o f a fu n ctio n th a t d ep en d s o n E q is th e b u ilt-in
fu n ctio n e l e m th a t ch eck s if a va lu e b elo n g s to a list.
H ere is a n in d u ctiv e d e¯ n itio n o f e l e m .

elem x [] = False
elem x (y:ys) | x == y = True
| otherwise = elem x ys

T h e m o st g en era l ty p e fo r e l e m is e l e m : : ( E q a ) = > a - >


[ a ] - > B o o l . O b serv e th a t a ty p e ca n b elo n g to O r d o n ly
if it b elo n g s to E q . T h is is b eca u se co m p a riso n in v o lv es
n o t o n ly th e fu n ctio n s < a n d > b u t a lso < = , > = , ...w h ich
im p ly th a t w e ca n ch eck eq u a lity. T h u s, a s su b sets o f
ty p es, O r d is a su b set o f E q . A ltern a tiv ely, w e h av e th a t
O r d a im p lies E q a fo r a n y ty p e a .
A n o th er ty p ica l ty p e cla ss in H a sk ell is N u m , th e co l-
lectio n o f a ll ty p es th a t su p p o rts \ n u m eric" o p era tio n s
su ch a s + , - a n d * . W e o b serv ed ea rlier th a t th e fu n ctio n
s u m th a t a d d s u p th e va lu es in a list w ill w o rk o n a n y list
w h o se u n d erly in g ty p e su p p o rts a d d itio n . T h is m ea n s
th a t w e ca n a ssig n th e g en eric ty p e s u m : : ( N u m a ) = > [ a ]
->a.
3 . T y p e In fe r e n c e Haskell allows us to
pass any type to a
H a sk ell a llow s u s to p a ss a n y ty p e to a fu n ctio n , in clu d -
function, including
in g a n o th er fu n ctio n . C o n sid er th e fu n ctio n a p p l y , th a t
another function.
ta k es a s in p u t a fu n ctio n f a n d a va lu e x a n d retu rn s
Consider the function
th e va lu e ( f x ) . In o th er w o rd s, th is fu n ctio n a p p lies
apply, that takes as
f to x . T h e d e¯ n itio n o f a p p l y is v ery stra ig h tfo rw a rd :
input a function f and
apply f x = f x.
a value x and returns
W h a t is th e ty p e o f a p p l y ? T h e ¯ rst a rg u m en t is a n y the value (f x). In
fu n ctio n , so w e ca n d en o te its ty p e a s a - > b fo r so m e other words, this
a rb itra ry ty p es a a n d b . T h e seco n d a rg u m en t x h a s function applies f to x.
to b e fed a s a n in p u t to f , so its ty p e m u st b e a . T h e The definition of
o u tp u t o f a p p ly is f x , w h ich h a s ty p e b . T h u s, w e h av e, apply is very
apply::(a->b)->a->b. straightforward:
W h a t if w e ch a n g e th e fu n ctio n to a p p ly f tw ice to x , apply f x = f x.

RESONANCE ¨ September 2007 45


GENERAL ¨ ARTICLE

a s in th e fu n ctio n t w i c e f x = f ( f x ) ? In th is ca se,
w e see th a t th e o u tp u t ( f x ) is fed b a ck a s a n in p u t to
f . T h is m ea n s th a t th e in p u t an d o u tp u t ty p es a a n d b
m u st b e th e sa m e, so f : : a - > a a n d th e ty p e o f t w i c e is
g iv en b y t w i c e : : ( a - > a ) - > a - > a .
T h e a n a ly sis w e d id b y h a n d w h en try in g to d ed u ce th e
ty p e o f a p p l y a n d t w i c e is b u ilt in to H a sk ell. T h u s, if
w e d o n o t p rov id e a n ex p licit ty p e fo r a fu n ctio n , H a sk ell
w ill sta rt w ith th e m o st g en era l a ssu m p tio n a b o u t th e
ty p e a n d im p o se th e co n stra in ts in ferred fro m th e fu n c-
tio n d e¯ n itio n s to a rriv e a t a ¯ n a l ty p e.
4 . O u te rm o st R e d u c tio n a n d In ¯ n ite D a ta S tru c -
tu r e s
C o m p u ta tio n in H a sk ell co n sists o f rew ritin g ex p ressio n s
u sin g fu n ctio n d e¯ n itio n s. A n ex p ressio n m ay h av e m u l-
tip le su b ex p ressio n s th a t ca n b e sim p li¯ ed . F o r in sta n ce,
if s q r x = x * x , th e ex p ressio n s q r ( 3 + 4 ) m ay b e sim -
p li¯ ed a s eith er s q r 7 o r ( 3 + 4 ) * ( 3 + 4 ) d ep en d in g o n
Computation in
w h eth er w e u se th e d e¯ n itio n o f s q r o r + ¯ rst. In g en -
Haskell consists of
era l, if th e su b ex p ressio n s th a t ca n b e red u ced ov erla p ,
rewriting expressions
a s in th is ca se, w e ca n a lw ay s u n a m b ig u o u sly d ecla re
using function
o n e to b e in side th e o th er { h ere ( 3 + 4 ) is in sid e s q r
definitions. An
(3+4).
expression may have
multiple In H a sk ell, th e o u ter ex p ressio n is a lw ay s sim p li¯ ed b e-
subexpressions that fo re th e in n er o n e. T h is ou term ost stra teg y is so m e-
can be simplified. For tim es ca lled lazy { it d o es n o t sim p lify th e a rg u m en t to
instance, if a fu n ctio n u n til th e va lu e o f th e a rg u m en t is a ctu a lly
sqr x = x*x, the n eed ed in th e eva lu a tio n o f th e fu n ctio n . O n th e o th er
expression sqr (3+4) h a n d , in n erm ost o r eager red u ctio n w o u ld a lw ay s eva lu -
may be simplified as a te a n a rg u m en t b efo re it is u sed . O u term o st red u ctio n
either sqr 7 or
so m etim es p ro d u ces a resu lt w h en in n erm o st red u ctio n
(3+4)*(3+4)
d o es n o t. F o r in sta n ce, g iv en th e d e¯ n itio n p o w e r x
0 = 1 . 0 , w e h av e p o w e r ( 8 . 0 / 0 . 0 ) 0 ; 1 . 0 , b eca u se
depending on whether
th e a rg u m en t x d o es n o t n eed to b e eva lu a ted .
we use the definition
of sqr or + first. O u term o st red u ctio n a lso p erm its th e d e¯ n itio n o f in ¯ -

46 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

n ite d a ta stru ctu res. F o r in sta n ce, th e list o f a ll in teg ers Why are infinite
sta rtin g a t n is g iv en b y th e fu n ctio n lists useful? It is
often conceptually
listfrom n = n: (listfrom (n+1))
easier to define a
A fter w e rew rite l i s t f r o m n , th e o u term o st ex p ressio n function that
is th e o n e in v o lv in g : . T h is is th u s eva lu a ted ¯ rst, re- returns an infinite
su ltin g in th e in itia l n b ein g g en era ted . H a sk ell th en list and extract a
tries to ex p a n d l i s t f r o m ( n + 1 ) w h ich , in tu rn , g en - finite prefix to get a
era tes n + 1 a n d l i s t f r o m ( n + 2 ) a n d so o n . T h u s, th e concrete value.
o u tp u t o f l i s t f r o m m is th e in ¯ n ite list [ m , m + 1 , . . . ]
w h ich is d en o ted [ m . . ] in H ask ell.
4 .1 U sin g In ¯ n ite L ists
W h y a re in ¯ n ite lists u sefu l? It is o ften co n cep tu a lly
ea sier to d e¯ n e a fu n ctio n th a t retu rn s a n in ¯ n ite list
a n d ex tra ct a ¯ n ite p re¯ x to g et a co n crete va lu e. W e
illu stra te th is b y w ritin g a fu n ctio n to ch eck co n n ectiv ity
in a d irected g ra p h .
A d irected g ra p h is a co llectio n o f vertices w ith so m e
p a irs co n n ected b y o rien ted edges. F igu re 1 is a n ex a m -
p le o f a d irected g ra p h w ith six v ertices. W e ca n d escrib e
th e g ra p h u sin g a fu n ctio n e d g e , a s sh ow n to th e rig h t
o f th e g ra p h .
O u r g o a l is to co n stru ct a fu n ctio n c o n n e c t e d : : C h a r - >
C h a r - > B o o l to co m p u te th e p a irs o f v ertices th a t a re
con n ected { th a t is, c o n n e c t e d x y is T r u e if a n d o n ly

B edge :: Char -> Char -> Bool


edge 'A' 'B' = True
A C edge 'A' 'D' = True
edge 'B' 'C' = True
edge 'C' 'A' = True
edge 'C' 'E' = True
edge 'D' 'E' = True
D E edge 'F' 'D' = True
edge 'F' 'E' = True
F edge x y = False
Figure 1. A graph in Haskell.

RESONANCE ¨ September 2007 47


GENERAL ¨ ARTICLE

if th ere is a p a th fro m x to y u sin g th e g iv en set o f ed g es.


W e b u ild u p th e set o f p a th s in d u ctiv ely. In itia lly w e
h av e p a th s o f len g th 0 { th ere is o n ly o n e. G iv en a p a th
o f len g th k , w e ex ten d it to a p a th o f len g th k + 1 b y
a d d in g a n ed g e. W e ca n rep resen t a p a th a s a list o f
n o d es, so w e h av e
extendpath :: [Char] -> [[Char]]
extendpath p = [p++c | c <- ['A'..'F'],
edge (last p) c]
w h ere l a s t p retu rn s th e la st va lu e in th e list p . W e ca n
th en m a p e x t e n d p a t h ov er th e list o f p a th s o f len g th k
to g et th e list o f p a th s o f len g th k + 1 .
extendall :: [[Char]] -> [[Char]]
extendall [] = [[c] | c <- ['A'..'F']]
extendall l = [ll | p <- l, ll <- extend p]
T h e b a se ca se o f e x t e n d a l l co n stru cts p a th s th a t co n -
sist o f a sin g le n o d e. If w e sta rt w ith th e sin g leto n list
o f em p ty p a th s [ [ ] ] a n d rep ea ted ly a p p ly e x t e n d a l l ,
w e g et lists w ith lo n g er a n d lo n g er p a th s.
T o ch eck if x a n d y a re co n n ected , w e o n ly n eed to ch eck
fo r p a th s w ith o u t lo o p s fro m x to y { th a t is, w e ca n a s-
su m e th a t th e p a th fro m x to y d o es n o t v isit a n in term e-
d ia te n o d e z tw ice. If it d id , w e ca n ex cise th e lo o p fro m
z to z a n d g et a sh o rter p a th th a t serv es o u r p u rp o se.
If w e h av e n n o d es ov era ll, a lo o p free p a th ca n h av e a t
m o st n ¡ 1 ed g es. T h is su g g ests th a t to ch eck a ll p a irs
o f co n n ected n o d es, it is su ± cien t to a p p ly e x t e n d a l l
n tim es to th e in itia l list co n ta in in g th e em p ty p a th .
H a sk ell h a s a b u ilt-in fu n ctio n i t e r a t e su ch th a t i t e r a t e
f x ; [x,f x,f(f x ) , . . . ] . W e ca n th erefo re w rite
iterate extendall [ [ ] ] to g en era te th e list w e w a n t.
W e ca n th en ex tra ct th e ¯ rst n elem en ts o f th is list (a ll
p a th s o f len g th u p to n ¡ 1) as
firstn = take n (iterate extendall [[]])

48 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

N ow , fo r ea ch p a th in th is list, w e ex tra ct th e sta rt a n d


en d p o in ts a s fo llow s.

connectedpairs = [(head p, last p) | l <- firstn,


p <- l]

F in a lly, w e ca n d e¯ n e th e fu n ctio n c o n n e c t e d .

connected x y = (elem (x,y) connectedpairs)

N o tice th a t w e h av e n o t b o th ered a b o u t th e fa ct th a t
e x t e n d a l l g en era tes p a th s th a t lo o p a n d d o o th er u n -
p ro d u ctiv e th in g s. F o r in sta n ce, th e p a th [ ' A ' , ' B ' , ' C ' ,
' A ' , ' B ' , ' C ' ] b elo n g s to th e six th itera tio n o f e x t e n d a l l
[ [ ] ] , b u t it d o es n o t m a tter. A ll th a t w e w a n t is a g u a r-
a n tee th a t ev ery p a ir ( x , y ) th a t is co n n ected is en u m er-
a ted b y th e n th step .
4 .2 S ea rc h P ro b le m s
In sea rch p ro b lem s, th ere is n o clo sed fo rm fo r th e a n -
sw er a n d w e n eed to sy stem a tica lly g en era te a ll p o ssib le
so lu tio n s, u n d o in g p a rtia l so lu tio n s w h en ev er w e rea ch
a d ea d en d . A cla ssica l p ro b lem o f th is so rt is th a t o f
p la cin g n q u een s o n a n n £ n ch essb o a rd su ch th a t n o
tw o q u een s a tta ck ea ch o th er. R eca ll th a t tw o q u een s
a tta ck ea ch o th er if th ey lie o n th e sa m e row , co lu m n o r
d ia g o n a l.
F ro m th e p ro b lem d escrip tio n , it is im m ed ia te th a t in
a n y so lu tio n to th e p ro b lem , th ere is ex a ctly o n e q u een
o n ea ch row (a n d a lso o n ea ch co lu m n ). T h u s, o n e stra t-
Figure 2. The 8 queens
eg y fo r so lv in g th e p ro b lem is th e fo llow in g : problem.
² P la ce th e ¯ rst q u een o n so m e sq u a re o f th e ¯ rst row . Q
Q
² In ea ch su cceed in g row , p la ce a q u een a t th e leftm o st Q
sq u a re th a t is n o t a tta ck ed b y a n y o f th e ea rlier q u een s. Q
Q
If w e fo llow th is stra teg y o n a n 8 £ 8 b o a rd a n d p la ce Q
th e ¯ rst q u een a t th e to p left co rn er, a fter 7 m ov es, w e Q
a rriv e a t th e co n ¯ g u ra tio n sh ow n in F igu re 2 . W e ¯ n d

RESONANCE ¨ September 2007 49


GENERAL ¨ ARTICLE

th a t th ere is n o va lid p o sitio n o n th e la st row fo r th e


8 th q u een , so w e h av e to a b a n d o n th is so lu tio n a n d try
a n o th er o n e. T h is ca n b e d o n e in a sy stem a tic w ay b y
retry in g th e n ex t p o ssib ility fo r th e 7 th q u een a n d o n ce
a g a in try in g th e 8 th q u een . If a ll p o ssib ilities fo r th e
7 th q u een fa il, w e g o b a ck a n d try th e n ex t p o ssib ility
fo r th e 6 th q u een . T h is stra teg y is ca lled backtrackin g.
W e ca n rep resen t a n a rra n g em en t o f q u een s a s a list o f
in teg ers, w h ere th e ¯ rst in teg er is th e co lu m n n u m b er o f
th e ¯ rst q u een in th e ¯ rst row , th e seco n d in teg er is th e
co lu m n n u m b er o f th e seco n d q u een in th e seco n d row ,
...T h u s, th e p o sitio n in F igu re 2 is d escrib ed b y th e list
[1,3,5,7,2,4,6].
G iv en a n a rra n g em en t o f k q u een s, w e ca n w rite a fu n c-
tio n th a t co m p u tes a ll va lid ex ten sio n s o f th is a rra n g e-
m en t to k + 1 q u een s, a n a lo g o u s to th e fu n ctio n w e w ro te
to ex ten d p a th s o f len g th k in a g ra p h to p a th s o f len g th
k + 1 . W e h av e to en su re th a t th e n ew q u een is n o t in th e
sa m e co lu m n a s a n y p rev io u sly p la ced q u een . W e a lso
Given an arrangement ca lcu la te, u sin g elem en ta ry a rith m etic, th a t th e n ew p o -
of k queens, we can sitio n is n o t o n a n y d ia g o n a l th a t is a tta ck ed b y a n y o f
write a function that th e p rev io u s p o sitio n s.
computes all valid
A s in th e p a th s ex a m p le, let u s g iv e th e n a m e e x t e n d a l l
extensions of this to th e fu n ctio n th a t co m p u tes a ll va lid ex ten sio n s o f a
arrangement to k+1 list o f a rra n g em en ts. W e ca n n ow so lv e th e n q u een s
queens, analogous to p ro b lem b y rep ea ted ly a p p ly in g th e fu n ctio n e x t e n d a l l
the function we wrote to th e em p ty a rra n g em en t a n d p ick in g u p th e va lu es g en -
to extend paths of era ted a fter th e n th a p p lica tio n . T h e fo llow in g fu n ctio n
length k in a graph to co m p u tes a ll p o ssib le a rra n g em en ts o f n q u een s o n a n
paths of length k+1. n £ n b o a rd .
We have to ensure
queens n = (iterate extendall [[]])!!(n+1)
that the new queen is
not in the same w h ere l ! ! i d en o tes th e elem en t a t p o sitio n i in list l ,
column as any sta rtin g fro m p o sitio n 0 . T h e fo llow in g retu rn s ju st o n e
previously placed su ch a rra n g em en t { th e ¯ rst o n e th a t is g en era ted .
queen.
queensone n = head ((iterate extendall [])!!(n+1))

50 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

N o tice th a t so m e o f th e p o sitio n s a fter k itera tio n s m ay


h av e n o va lid ex ten sio n s (lik e th e a rra n g m en t o f 7 q u een s
a b ov e). T h is d o es n o t m a tter. If, a t so m e sta g e, a ll
a rra n g em en ts d ie o u t a s in fea sib le, w e w ill g et th e va lu e
[ ] co n sistin g o f n o va lid a rra n g em en ts (a s o p p o sed to
[ [ ] ] , th e list co n sistin g o f th e em p ty a rra n g em en t) w h ich
w ill ju st rep ea t itself in d e¯ n itely.
It m ig h t a p p ea r th a t w e n eed to g en era te a ll p o ssib le
a rra n g em en ts w ith 1 ,2 ,...,7 q u een s b efo re w e ca n o b ta in
th e ¯ rst va lid a rra n g em en t fo r 8 q u een s in q u e e n s o n e .
H ow ev er, H a sk ell's o u term o st eva lu a tio n stra teg y w ill
a ctu a lly ex p a n d th e leftm o st so lu tio n a t ea ch lev el, so it
w ill co m p u te th e ¯ rst so lu tio n w ith 8 q u een s in a d ep th -
¯ rst, ra th er th a n a b rea d th -¯ rst, m a n n er.
5 . U s e r -D e ¯ n e d D a ta T y p e s
A d a ta ty p e is a co llectio n o f va lu es w ith a co llectiv e
n a m e. F o r in sta n ce, th e d a ta ty p e I n t co n sists o f th e
va lu es f . . . ,- 2 ,- 1 ,0 ,1 ,2 ,...g , w h ile th e d a ta ty p e B o o l
co n sists o f th e va lu es f F a l s e ,T r u e g . D a ta ty p es ca n
b e p o ly m o rp h ic a n d ev en recu rsiv ely d e¯ n ed a n d h en ce
o f u n b o u n d ed size { fo r ex a m p le, lists. In H a sk ell, w e
ca n ex ten d th e set o f b u ilt-in d a ta ty p es u sin g th e d a t a
sta tem en t.
5 .1 E n u m e ra ted D a ta T y p e s
T h is sim p lest fo rm o f d a ta ty p e is o n e co n sistin g o f a
¯ n ite set o f va lu es. W e ca n d e¯ n e su ch a ty p e u sin g th e
d a t a sta tem en t, a s fo llow s.
data Day = Sun | Mon | Tue | Wed | Thu | Fri
| Sat
H av in g in tro d u ced th is n ew ty p e, w e ca n d irectly u se it
in fu n ctio n s su ch a s:
weekend :: Day -> Bool
weekend Sun = True
weekend Sat = True

RESONANCE ¨ September 2007 51


GENERAL ¨ ARTICLE

weekend d = False

W e ca n a lso w rite a fu n ctio n n e x t d a y .

nextday :: Day -> Day


nextday Sun = Mon
...
nextday Sat = Sun

W h a t h a p p en s if w e a sk H a sk ell to eva lu a te n e x t d a y
S a t ? T h e a n sw er is co m p u ted co rrectly a s S u n b u t w e
g et a m essa g e say in g C a n n o t f i n d " s h o w " f u n c t i o n
f o r D a y . S im ila rly, if w e a sk w h eth er T u e = = W e d , th e
resp o n se is C a n n o t i n f e r i n s t a n c e E q D a y .
T h e p ro b lem is th a t w e h av e n o t a sso cia ted th e n ew d a ta
ty p e w ith a n y ty p e cla sses, in clu d in g th e m o st b a sic o n es
su ch a s E q a n d S h o w . T h e cla ss S h o w co n sists o f th o se
ty p es w h o se va lu es ca n b e d isp lay ed { th a t is, th e va lu e
ca n b e co n v erted to a S t r i n g a n d p rin ted o n screen .
T h e m o st n a tu ra l d e¯ n itio n s fo r E q a n d S h o w a re th a t
ea ch va lu e is d istin ct a n d eq u a l o n ly to itself a n d ea ch
va lu e is d isp lay ed in th e sa m e w ay it is d e¯ n ed . W e ca n
in clu d e th ese \ d efa u lt" d e¯ n itio n s fo r E q a n d S h o w u sin g
th e w o rd d e r i v i n g a s fo llow s:

data Day = Sun | Mon | Tue | Wed | Thu | Fri


| Sat
deriving (Eq, Show)

In th e sa m e w ay, w e ca n d eriv e a d e¯ n itio n fo r O r d { th e


d efa u lt d e¯ n itio n w o u ld o rd er th e va lu es in th e seq u en ce
th ey a re p resen ted , n a m ely S u n < M o n < ...< S a t .
5 .2 D a ta T y p e s w ith P a ra m e te rs
W e ca n g o b ey o n d ¯ n ite en u m era ted ty p es a n d d escrib e
d a ta ty p es w ith a p a ra m eter, a s in th e fo llow in g ex a m -
p le.

52 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

data Shape = Square Float | Circle Float | Rectangle Float Float


deriving (Eq, Ord, Show)

size :: Shape -> Float


size (Square x) = x
size (Circle r) = r
size (Rectangle l w) = l+w
E a ch va ria n t o f S h a p e h a s a con stru ctor { S q u a r e , C i r c l e
o r R e c t a n g l e . E a ch co n stru cto r is a tta ch ed to a g ro u p
o f va lu es, w h ich ca n va ry fro m co n stru cto r to co n stru c-
to r. T h e va lu es S u n , M o n , ...in th e ty p e D a y a re a lso
co n stru cto rs w ith zero va lu es a tta ch ed .
W h a t h a p p en s w h en w e d eriv e E q fo r S h a p e ? A t th e
lev el o f S h a p e , th is w ill en su re th a t ( S q u a r e x ) is eq u a l
to ( S q u a r e y ) p rov id ed x = = y (th u s, fo r th e in n er
va lu e, it derives eq u a lity fro m F l o a t ) b u t ( S q u a r e x ) is
n ev er eq u a l to ( C i r c l e y ) . S in ce w e a lso d eriv e O r d , w e
h av e S q u a r e < C i r c l e < R e c t a n g l e so ( S q u a r e x ) <
( C i r c l e y ) fo r a ll x a n d y a n d ( C i r c l e z ) < ( C i r c l e
w ) if z < w .
5 .3 P o ly m o rp h ic D a ta T y p e s
W e ca n ex ten d o u r d e¯ n itio n o f S h a p e to p erm it a n y n u -
m eric ty p e a s th e p a ra m eter. H ere is th e co rresp o n d in g
d e¯ n itio n .
data (Num a) => (Shape a) = Square a | Circle a | Rectangle a a
deriving (Eq, Ord, Show)

size :: (Shape a) -> a


size (Square x) = x
size (Circle r) = r
size (Rectangle l w) = l+w
N o te th e co n d itio n a l d ep en d en ce o n N u m a . O b serv e
a lso th a t w e n eed to in clu d e th e ty p e p a ra m eter a in th e
n a m e o f th e ty p e { th e d a ta ty p e is S h a p e a n o t ju st
Shape.

RESONANCE ¨ September 2007 53


GENERAL ¨ ARTICLE

The constructor Listof 5 .4 R ec u rsiv e D a ta T y p e s


combines a value of
W e ca n h av e recu rsiv e d a ta ty p es. H ere is a n ex a m p le.
type Int with a nested
instance of Mylist. data Mylist = Empty | Listof Int Mylist
For example, a value
of type Mylist H ere th e co n stru cto rs a re E m p t y a n d L i s t o f . E m p t y
corresponding to the h a s zero a rg u m en ts a n d is h en ce a co n sta n t, rep resen t-
list [1,3,2] is written
in g th e b a se ca se o f th e recu rsiv e ty p e. T h e co n stru cto r
Listof 1 (Listof 3
L i s t o f co m b in es a va lu e o f ty p e I n t w ith a n ested in -
sta n ce o f M y l i s t . F o r ex a m p le, a va lu e o f ty p e M y l i s t
(Listof 2 Empty).
co rresp o n d in g to th e list [ 1 , 3 , 2 ] is w ritten L i s t o f 1
( L i s t o f 3 ( L i s t o f 2 E m p t y ) . In H a sk ell's b u ilt-in d e-
¯ n itio n o f lists, E m p t y is w ritten a s [ ] a n d L i s t o f is
w ritten a s a n in ¯ x co n stru cto r \ : " , so th e va lu e a b ov e
b eco m es th e m o re fa m ilia r 1 : ( 3 : ( 2 : [ ] ) .
It is a sm a ll step to ex ten d M y l i s t to b e p o ly m o rp h ic.

data Mylist a = Empty | Listof a (Mylist a)

N ow , a term th a t u ses th e co n stru cto r L i s t o f h a s a


va lu e o f ty p e a a n d a n ested list o f th e sa m e ty p e. N o te
a g a in th a t th e fu ll n a m e o f th e ty p e is M y l i s t a , n o t
ju st M y l i s t .
W e co u ld u se m u ltip le ty p es. F o r in sta n ce, h ere is a
list in w h ich ty p es a a n d b a ltern a te, b eg in n in g w ith a
va lu e o f ty p e a . T h e a ltern a tio n o f ty p es is a ch iev ed
b y in v ertin g th e o rd er o f a a n d b in th e n ested co p y o f
Twolist.

data Twolist a b = Empty | Listof a (Twolist b a)

5 .5 S ta c k s
H a sk ell p ro v id es th e b u ilt-in co llectiv e d a ta ty p e list. It
is o ften co n v en ien t to h av e a d d itio n a l co llectiv e ty p es.
O n e su ch ty p e is a sta ck . A sta ck is a stru ctu re in w h ich
w e ca n a d d elem en ts o n e a t a tim e a n d rem ov e elem en ts
o n e a t a tim e su ch th a t th e elem en t rem ov ed ¯ rst is th e

54 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

o n e th a t w a s m o st recen tly a d d ed { a last-in -¯ rst-ou t A stack is a structure


stru ctu re. T h e in sert o p era tio n is u su a lly ca lled p u s h in which we can add
a n d th e rem ov e o p era tio n is u su a lly ca lled p o p . T h u s,
elements one at a
w e h av e:
time and remove
push :: a -> (Stack a) -> (Stack a) elements one at a
pop :: Stack a -> (a,Stack a) time such that the
element removed
N o tice th a t p o p req u ires th e sta ck to b e n o n em p ty a n d first is the one that
retu rn s a p a ir o f va lu es { th e elem en t a t th e to p o f th e was most recently
sta ck a n d th e resu ltin g sta ck w ith th is va lu e rem ov ed .
added – a last-in-
W e a lso a d d th e fo llow in g fu n ctio n th a t ch eck s if th e first-out structure.
g iv en sta ck is em p ty.
isempty :: (Stack a) -> Bool
W e h av e y et to d e¯ n e h ow to rep resen t a sta ck . H ere is
o n e p o ssib le d e¯ n itio n :
data Stack a = Empty | St a (Stack a)

W e ca n n ow in sta n tia te th e fu n ctio n s in term s o f th is


d e¯ n itio n .
push :: a -> (Stack a) -> (Stack a)
push x s = St x s

pop :: Stack a -> (a,Stack a)


pop (St x s) = (x,s)

isempty :: (Stack a) -> Bool


isempty Empty = True
isempty s = False
E x cep t fo r ren a m in g co n stru cto rs, o u r d e¯ n itio n o f th e
d a ta ty p e S t a c k is th e sa m e a s th a t o f th e h a n d cra fted
list d a ta ty p e M y l i s t th a t w e d e¯ n ed la st tim e. T h is
su g g ests th a t w e co u ld d irectly u se th e b u ilt-in list ty p e
a n d w rite
data Stack a = St [a]

RESONANCE ¨ September 2007 55


GENERAL ¨ ARTICLE

A queue is a first-in- W e n eed a co n stru cto r to a sso cia te w ith th e va lu e [ a ] ,


first-out structure.
b u t o th erw ise a ll th e fu n ctio n s w e u se a re d eriv ed fro m
Like a stack, it has
th e stru ctu re o f lists.
basic operations to push x (St xs) = St (x:xs)
add and remove pop (St (x:xs)) = (x,St xs)
elements, but the isempty (St l) = (l == [])
element that is
removed is the one 5 .6 Q u e u e s
that was added
A q u eu e is a ¯ rst-in -¯ rst-ou t stru ctu re. L ik e a sta ck , it
earliest.
h a s b a sic o p era tio n s to a d d a n d rem ov e elem en ts, b u t
th e elem en t th a t is rem ov ed is th e o n e th a t w a s a d d ed
ea rliest. H ere a re th e o p era tio n s th a t w e w o u ld lik e to
p erfo rm o n q u eu es:

addq :: a -> (Queue a) -> (Queue a)


removeq :: (Queue a) -> (a,Queue a)
isemptyq :: (Queue a) -> Bool

O b serv e th a t th e sig n a tu res a re th e sa m e a s th o se fo r


th e fu n ctio n s p u s h , p o p a n d i s e m p t y th a t w e d e¯ n ed
fo r sta ck s, m o d u lo th e n ew n a m e o f th e d a ta ty p e. W e
ca n a g a in im p lem en t a q u eu e u sin g a list, a s fo llow s.

data Queue a = Qu [a]

addq x (Qu xs) = (Qu xs ++ [x])


removeq (Qu (x:xs) = (x,Qu xs)
Adding an element to isempty (Qu l) = (l == [])
a queue takes time
proportional to the H ere, r e m o v e q a n d i s e m p t y q h av e essen tia lly th e sa m e
size of the queue. d e¯ n itio n a s p o p a n d i s e m p t y fo r sta ck s. O n ly a d d q is
Removing an element d i® eren t { th e n ew elem en t is a p p en d ed a t th e en d o f
takes constant time. th e list ra th er th a n a t th e b eg in n in g .
In contrast, in a stack,
T h is is a n im p o rta n t d i® eren ce { a d d in g a n elem en t to
both push and pop
a q u eu e ta k es tim e p ro p o rtio n a l to th e size o f th e q u eu e.
take constant time,
R em ov in g a n elem en t ta k es co n sta n t tim e. In co n tra st,
independent of the in a sta ck , b o th p u s h a n d p o p ta k e co n sta n t tim e, in d e-
size of the stack. p en d en t o f th e size o f th e sta ck .

56 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

S u p p o se w e p u sh a n d p o p n elem en ts in a sta ck . T h is
w ill ta k e O (n ) tim e, reg a rd less o f th e w ay th e p u sh es a n d
p o p s a re in terleav ed . O n th e o th er h a n d , fo r a q u eu e,
if w e ¯ rst d o n a d d q 's a n d th en n r e m o v e q 's, it ta k es
tim e O (n 2 ) to b u ild u p th e q u eu e, sin ce ea ch a d d q ta k es
tim e p ro p o rtio n a l to th e len g th o f th e q u eu e. W e co u ld ,
o f co u rse, rev erse th e rep resen ta tio n a n d a d d elem en ts
to th e fro n t o f th e list a n d rem ov e th em fro m th e rea r.
T h en , a d d q w o u ld b e a co n sta n t tim e o p era tio n w h ile
r e m o v e q w o u ld ta k e tim e p ro p o rtio n a l to th e len g th o f
th e list.
C a n w e d o b etter? C a n w e ¯ n d a n im p lem en ta tio n o f
a q u eu e in w h ich n a d d q 's a n d n r e m o v e q 's ta k e O (n )
tim e, reg a rd less o f th e o rd er in w h ich th ese o p era tio n s
a p p ea r? W e im a g in e th a t w e b rea k a q u eu e in to tw o
p a rts a n d u se a sep a ra te list to rep resen t th e fro n t a n d
th e rea r. S in ce w e rem ov e elem en ts fro m th e fro n t p o r-
tio n , th e ¯ rst elem en t in th e fro n t sh o u ld b e a t th e h ea d .
W e a d d elem en ts to th e en d o f th e rea r, so , to av o id tra -
v ersin g th e rea r p o rtio n ea ch tim e w e a d d a n ew elem en t,
w e m a in ta in th e rea r p o rtio n in rev erse, w ith th e en d o f
th e q u eu e a t th e h ea d o f th e list.
H ere is th e d a ta d ecla ra tio n a n d th e d e¯ n itio n o f a d d q .

data Queue a = Nuqu [a] [a]


addq x (Nuqu ys zs) = Nuqu ys (x:zs)

R eca ll th a t z s rep resen ts th e rea r o f th e q u eu e, in re-


v erse, so th e la st elem en t o f th e q u eu e is a t th e h ea d o f
z s , a n d x is a d d ed b efo re th is elem en t.
H ow a b o u t r e m o v e q ? If th e left list is n o n em p ty, w e ju st
ex tra ct its h ea d . If it is em p ty, w e rev erse th e en tire rea r
in to th e fro n t a n d th en ex tra ct its h ea d .

removeq (Nuqu (x:xs) ys) = (x,Nuqu xs ys)


removeq (Nuqu [] ys) =
removeq (Nuqu (reverse ys) [])

RESONANCE ¨ September 2007 57


GENERAL ¨ ARTICLE

A tree is a structure in W h y is th is a n y b etter? A fter a ll, a fter a d d in g n el-


which each node has em en ts th e q u eu e w o u ld b e N u q u [ ] [ x n , . . . , x 2 , x 1 ] ,
multiple successors, so th e ¯ rst r e m o v e q w ill ta k e O (n ) tim e, sin ce r e v e r s e
called children. There ta k es O (n ) tim e. N o te, h o w ev er, th a t th e O (n ) tim e
is a special node,
ta k en to ex tra ct x 1 a lso tra n sfers [ x 2 , . . , x n ] to th e
called the root, from
fro n t o f th e q u eu e. T h u s, a fter o n e r e m o v e q , w e h av e
N u q u [ x 2 , . . . , x n ] [ ] . T h e n ex t n ¡ 1 r e m o v e q o p er-
which the tree begins.
a tio n s ta k e o n ly O (1 ) tim e ea ch . In th is w ay, ov era ll,
a d d in g n elem en ts a n d th en rem ov in g th em ta k es o n ly
O (n ) o p era tio n s. T h e O (n ) co st o f ex tra ctin g th e ¯ rst
elem en t ca n b e th o u g h t o f a s am ortized, o r sp rea d o u t,
ov er th e n ex t n ¡ 1 r e m o v e q o p era tio n s.
5 .7 T ree s
A tree is a stru ctu re in w h ich ea ch n o d e h a s m u ltip le su c-
cesso rs, ca lled children . T h ere is a sp ecia l n o d e, ca lled
th e root, fro m w h ich th e tree b eg in s. T h e ro o t is u su a lly
d raw n a s th e to p m o st n o d e in th e tree. E v ery n o d e o th er
th a n th e ro o t h a s a u n iq u e paren t (th e n o d e o f w h ich
it is a child ), a n d h en ce a u n iq u e p a th b a ck to th e ro o t
fo llow in g p a ren t lin k s. F igu re 3 sh ow s tw o ex a m p les o f
trees.
In th e trees w e h av e d raw n , a va lu e is sto red a t ea ch
n o d e. A s in lists, th ese va lu es h av e a u n ifo rm ty p e {
I n t , in th e ex a m p les a b ov e. A b o tto m lev el n o d e w ith
n o ch ild ren is ca lled a leaf n ode. N o n -lea f n o d es a re
ca lled in tern al n odes. In tern a l n o d es in a tree n eed n o t
h av e a u n ifo rm n u m b er o f ch ild ren . F o r in sta n ce, th e
n o d e w ith va lu e 5 in th e left tree h a s o n ly o n e ch ild
w h ile th e n o d e w ith va lu e 2 h a s tw o ch ild ren . T h e o rd er
o f th e ch ild ren is im p o rta n t. In th e trees w e h av e d raw n ,
Figure 3. Trees.
ea ch n o d e h a s u p to tw o ch ild ren a n d th e tw o ch ild ren
a re o rien ted a s left a n d right. T h u s, fo r th e tree o n th e
4 3
left, 2 is th e left ch ild o f th e ro o t 4 a n d 3 is th e rig h t
2 5 2 5 ch ild o f 2 . N o tice th a t th o u g h 5 h a s o n ly o n e ch ild , 6 ,
th is is a rig h t ch ild , n o t a left ch ild .
1 3 6 1 4 6

58 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

W e w ill ty p ica lly lo o k a t bin ary trees, in w h ich ea ch n o d e Lists have a linear
h a s u p to tw o ch ild ren . H ere is o n e w ay to d escrib e a structure, so there is
b in a ry d a ta ov er a n a rb itra ry ty p e a . only one measure of
size for a list, the
data BTree a = Nil | Node (BTree a) a (BTree a)
length of the list.
L ists h a v e a lin ea r stru ctu re, so th ere is o n ly o n e m ea su re Trees are two
o f size fo r a list, th e len g th o f th e list. T rees a re tw o dimensional, so we
d im en sio n a l, so w e co n sid er tw o q u a n tities: consider two
quantities:
² S ize : th e n u m b er o f n o d es in th e tree Size: the number of
² H eight : th e len g th o f th e lo n g est p a th fro m a ro o t nodes in the tree;
to a lea f Height : the length of
the longest path from
W e ca n co m p u te b o th o f th ese q u a n tities in d u ctiv ely, a s a root to a leaf.
w ith lists.

size :: (Btree a) -> Int


size Nil = 0
size (Node t1 x t2) = 1 + (size t1) + (size t2)

height :: (Btree a) -> Int


height Nil = 0
height (Node t1 x t2) = 1 + max (height t1) (height t2)

5 .8 B in a ry S ea rc h T ree s
A n im p o rta n t u se o f b in a ry trees is to sto re va lu es th a t
w e m ay w a n t to lo o k u p la ter. F or in sta n ce, a b in a ry
sea rch tree co u ld b e u sed to sto re a d ictio n a ry o f w o rd s.
A b in a ry sea rch tree is a tree w ith n o d u p lica te va lu es
th a t sa tis¯ es th e fo llow in g p ro p erty a t ev ery n o d e v : a ll
va lu es in th e su b tree ro o ted a t v th a t a re sm a ller th a n
th e va lu e sto red a t v lie in th e left su b tree o f v a n d a ll
va lu es in th e su b tree ro o ted a t v th a t a re la rg er th a n
th e va lu e sto red a t v lie in th e righ t su b tree o f v . T o
em p h a size th a t th e va lu es in th e tree ca n b e o rd ered ,
w e ela b o ra te slig h tly o n th e H a sk ell d e¯ n itio n o f b in a ry
trees to d escrib e sea rch trees.

RESONANCE ¨ September 2007 59


GENERAL ¨ ARTICLE

A binary search tree is data (Ord a) => STree a = Nil | Node (STree a)
a tree with no a (STree a)
duplicate values that
O b serv e th a t th e stru ctu re o f a n S T r e e is id en tica l to
satisfies the following
th a t o f a n o rm a l B T r e e , b u t th ere is a ty p e cla ss d ep en -
property at every node d en ce o n O r d , sim ila r to th e o n e w e h a v e seen fo r p o ly -
v: all values in the m o rp h ic fu n ctio n s su ch a s q u i c k s o r t . W e h av e ch a n g ed
subtree rooted at v th e n a m e o f th e d a ta stru ctu re fro m B T r e e a to S T r e e
that are smaller than a to em p h a size th a t w e a re w o rk in g w ith search trees
the value stored at v ra th er th a n o rd in a ry b in a ry trees.
lie in the left subtree
of v and all values in B o th th e trees in F igu re 3 a re ex a m p les o f sea rch trees
the subtree rooted at ov er th e va lu es [ 1 , 2 , 3 , 4 , 5 , 6 ] . T h o u g h th ese tw o trees
v that are larger than
lo o k rea so n a b ly w ell b a la n ced , th is is n o t a lw ay s th e
the value stored at v
ca se. F o r in sta n ce, F igu re 4 sh ow s a h ig h ly u n b a la n ced
sea rch tree ov er th e sa m e set o f va lu es.
lie in the right subtree
of v. T o ¯ n d a va lu e in a b in a ry sea rch tree, w e sta rt a t th e
ro o t. A t ea ch n o d e, if w e h av e n o t a lrea d y fo u n d th e
va lu e w e a re lo o k in g fo r, w e ca n u se th e sea rch tree p ro p -
erty to d ecid e w h eth er to sea rch in th e rig h t su b tree o r
th e left su b tree. W e k eep w a lk in g d ow n th e tree in th is
fa sh io n till w e ¯ n d th e va lu e w e seek o r w e rea ch a lea f
n o d e fro m w h ere w e ca n n o t d escen d fu rth er. T h u s, ea ch
lo o k u p in a b in a ry sea rch tree trav erses, in th e w o rst
ca se, a sin g le p a th fro m th e ro o t to a lea f n o d e.
H ow m u ch tim e d o es it ta k e to lo o k u p a va lu e in a
b a la n ced sea rch tree w ith n n o d es? A tree is b a la n ced
if a t ea ch n o d e th e size o f th e left su b tree d i® ers fro m
th e size o f th e rig h t su b tree b y a t m o st 1 . In itia lly, w e
Figure 4. A skewed search sea rch fo r th e va lu e in th e en tire tree, w ith n n o d es. If
tree. w e d o n o t ¯ n d th e va lu e a t th e ro o t, w e sea rch eith er
th e left o r th e rig h t su b tree. S in ce th e tree is b a la n ced ,
6 th e n u m b er o f n o d es in ea ch o f th ese su b trees is a t m o st
5 n
n,
2 . In th is w ay, w e su ccessiv ely sea rch trees o f size
4 n n
; ;::: till w e rea ch a lea f n o d e, a su b tree o f size 1 .
2 4
3 T h e len g th o f th is seq u en ce is clea rly b o u n d ed b y lo g n
2 { in o th er w o rd s, th e h eig h t o f a b a la n ced sea rch tree
1
w ith n n o d es is lo g n .

60 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

H ere is a H a sk ell d e¯ n itio n o f th e sea rch p ro ced u re w e Where should we


ju st d escrib ed : insert a value into a
findtree :: (Stree a) -> a -> Bool search tree? From
findtree Nil x = False the definition of a
findtree (Node tleft y tright) x search tree, there is
| x == y = True only one possibility.
| x < y = findtree tleft x Search for the value
| otherwise = findtree tright x in the tree. If it
already exists, there
S ea rch trees a re n o t sta tic o b jects. In g en era l, w e h av e is nothing to be done.
to in sert n ew va lu es in to sea rch trees a n d rem ov e sta le Otherwise, we reach
va lu es fro m sea rch trees. a leaf node. So,
W h ere sh o u ld w e in sert a va lu e in to a sea rch tree? F ro m insert the new value
th e d e¯ n itio n o f a sea rch tree, th ere is o n ly o n e p o ssib il- as a left or right child
ity. S ea rch fo r th e va lu e in th e tree. If it a lrea d y ex ists, of the leaf node where
th ere is n o th in g to b e d o n e. O th erw ise, w e rea ch a lea f the unsuccessful
n o d e. T h is is th e sa m e p a th th a t w e w o u ld h a v e to fo l- search terminates.
low to ¯ n d th e n ew va lu e a fter it h a s b een in serted . S o ,
in sert th e n ew va lu e a s a left o r rig h t ch ild o f th e lea f
n o d e w h ere th e u n su ccessfu l sea rch term in a tes.

inserttree :: (Stree a) -> a -> (Stree a)


inserttree Nil x = Node Nil x Nil
inserttree (Node tleft y tright) x
| x == y = Node tleft y tright
| x < y = Node (inserttree tleft x) y tright
| otherwise = Node tleft y (inserttree tright x)

C lea rly, th e m a x im u m n u m b er o f step s req u ired to in -


sert a va lu e in to a sea rch tree is eq u a l to th e len g th o f
th e lo n g est p a th in th e tree. T h u s, if th e sea rch tree is
The maximum
b a la n ced a n d h a s n n o d es, i n s e r t t r e e ta k es tim e lo g n ,
number of steps
b u t w ill n o t in g en era l p reserv e th e b a la n ced stru ctu re
required to insert a
o f th e tree.
value into a search
H ow d o w e d elete a va lu e fro m a tree? W e in terp ret tree is equal to the
\delete x from t " a s \delete x from t if the valu e exists length of the longest
in t ". S u p p o se w e w a n t to d elete a va lu e x fro m a tree path in the tree.

RESONANCE ¨ September 2007 61


GENERAL ¨ ARTICLE

y == x w h o se ro o t is y . If x < y , w e in d u ctiv ely d elete x fro m


th e left su b tree o f y . S im ila rly, if x > y , w e in d u ctiv ely
d elete x fro m th e rig h t su b tree o f y . S o , th e in terestin g
w z
ca se is w h en y = = x , a s sh ow n in F igu re 5 .
t1 t2 t3 t4 If w e rem ov e y , w e h av e a h o le a t th e ro o t o f th e tree.
It is tem p tin g to m ov e eith er w (o r z ) in to th is p la ce
Figure 5. Deleting a value a n d recu rsiv ely d elete w fro m th e left su b tree (o r z fro m
from a search tree. th e rig h t su b tree). H ow ev er, th is w o u ld n o t p reserv e th e
stru ctu re o f th e tree { fo r in sta n ce, if w e m o v e w u p to
th e ro o t, va lu es in th e tree t 2 , w h ich a re b ig g er th a n w ,
w ill en d u p to th e left o f w .
T h e co rrect so lu tio n is to m ov e th e la rg est va lu e fro m
th e left su b tree o f y (o r th e sm a llest va lu e fro m th e rig h t
su b tree o f y ) in p la ce o f y. T h e la rg est va lu e in a sea rch
tree ca n b e fo u n d ea sily, b y fo llow in g th e rig h tm o st p a th
in th e tree. R em ov in g th is va lu e fro m a tree is a lso a rel-
a tiv ely ea sy o p era tio n . H ere is a fu n ctio n th a t rem ov es
th e m a x im u m va lu e fro m a n o n em p ty tree, retu rn in g
b o th th e va lu e a n d th e m o d i¯ ed tree, a fter d eletio n .
deletemax :: (STree a) -> (a,STree a)
deletemax (Node t1 y Nil) = (y,t1)
deletemax (Node t1 y t2) = (z, Node t1 y tz)
where (z,tz) = deletemax t2
W e ca n n ow rew rite d e l e t e t r e e a s fo llow s:
deletetree :: (Stree a) -> a -> (Stree a)
deletetree Nil x = Nil
deletetree (Node tleft y tright) x
| x < y = Node (deletetree tleft x) y tright
| x > y = Node tleft y (deletetree tright x)

-- In all cases below, we must have x == y

deletetree (Node Nil y tright) x = tright


deletetree (Node tleft y tright) x =
Node tz z tright
where (z,tz) = deletemax tleft

62 RESONANCE ¨ September 2007


GENERAL ¨ ARTICLE

Figure 6. A tree rotation.


x y
² ²
¡ @
¡ @
y ² ¡¡ @@
¡¡ @@
¡ @ ¢A ² x
¢t3 A ) ¢A ¡ @
¡¡ @@ ¢t1 A
¢ A
¢ A ¡¡ @@
¢A ¢A
¢t1 A ¢t2 A ¢A ¢A
¢ A ¢ A ¢t2 A ¢t3 A
¢ A ¢ A

5 .9 B a la n ced B in a ry S ea rc h T ree s
In g en era l, th e fu n ctio n s i n s e r t a n d d e l e t e th a t w e
h av e d escrib ed m ay resu lt in sk ew ed trees w h ere th e
h eig h t is n o t lo g a rith m ic in th e size. W e ca n in d u c-
tiv ely m a in ta in b a la n ce in a tree b y a p p ro p ria tely rotat-
in g p a rts o f a tree a fter ea ch u p d a te. A ty p ica l ro ta tio n
is sh ow n in F igu re 6 . W e w ill n o t g o in to th e d eta ils o f
3
h ow to u se su ch ro ta tio n s to m a in ta in a b a la n ced sea rch A number of compilers and
interpreters are available in the
tree. H ow ev er, it is in stru ctiv e to n o te th a t th e restru c-
public domain for Haskell. The
tu rin g sh ow n in F igu re 6 ca n b e d irectly ren d ered in easiest to install and use is the
H a sk ell a s fo llow s, a g a in em p h a sizin g th e cla rity th a t is interpreter Hugs, which runs on
a ch iev ed w ith a d ecla ra tiv e sty le o f p ro g ra m m in g . both Windows and Unix based
systems. There is an active
rotateright (Node x (Node y t1 t2) t3) = website, http://www.haskell.org,
that has pointers to software,
Node y t1 (Node x t2 t3)
tutorials, books and reference
material on Haskell.
6 . C o n c lu sio n
T h is co n clu d es o u r tw o -p a rt in tro d u ctio n to fu n ctio n a l
p ro g ra m m in g in H a sk ell3 . W e h av e seen th a t fu n ctio n a l Address for Correspondence
Madhavan Mukund
p ro g ra m m in g a llow s u s to d escrib e co m p lex co m p u ta -
Chennai Mathematical
tio n s in a d ecla ra tiv e m a n n er. H a sk ell's ty p e m ech a n ism Institute
su p p o rts a so p h istica ted v ersio n o f co n d itio n a l p o ly m o r- H1, SIPCOT IT Park
p h ism . O u term o st eva lu a tio n a llow s u s to d escrib e in - Padur PO
¯ n ite d a ta stru ctu res, w h ich ca n b e fru itfu lly u sed in Siruseri 603 103, India
Email:madhavan@cmi.ac.in
so lv in g sea rch p ro b lem s. F in a lly, w e h av e seen th a t w e
http://www.cmi.ac.in/
ca n d e¯ n e a n d m a n ip u la te u ser-d e¯ n ed d a ta ty p es q u ite ~madhavan
n a tu ra lly.

RESONANCE ¨ September 2007 63

You might also like