Professional Documents
Culture Documents
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,
sum [] = 0
sum (x:xs) = x + (sum xs)
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
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 ¯ -
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
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 .
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
weekend d = False
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:
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
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 .
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.
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.
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 .
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.