Table Of ContentAlgorithms: a
functional
•
programm1ng
approach
Secoenddi tion
FethRia bhi
Guy Lapalme
•
TT
Addison-Wesley
HarloEwn,g lan•d ReadinMga,s sachus•et Mtesn loP arkC,a lifor•n Nieaw Y ork
DonM illOsn,t ari•o A msterda•m Bonn• Sydne•y Singapore
Tokyoe Madri•d SanJ uane Mila•n MexicCoi tye Seou•l Taipei
© PearsEodnu catLiiomni t1e9d9 9
PearsEodnu catLiiomni ted
EdinbuGragthe
Harlow
EsseCxM 202 JE
England
andA ssociated Comtpharonuigehsot uhtew orld.
Ther ighotfFs e tRhaib bain dG uyL apaltmobee identaisafi uetdh oofrt sh iWsor kh avbee eans serted by
theimn a ccordwaintctheh C eo pyriDgehsti,ga nnsdP a teAnctt1s 988.
Alrli ghts rNeosp earrovtfte hdpi.us b licmaatybi eor ne produced,i nasr teotrreisdeyv satloe rmt ,r ansmitted
ina nyf oromr b ya nym eanesl,e ctrmoencihca,n ipchaolt,oc opying,o ror tehceorrwdiisneg,e iwtihtehro ut
thper iworri tpteernmi ssoifto hnpe u bliosrah l eirc epenrcmiet tirnegs trciocptyeiidnn tg hU en itKeidn gdom
issubeytd h Ceo pyriLgihcte nAsgienngcL yt d9,0T otntheaCmo urRto adL,o ndoWnlP 9 HE.
Thep rogriantm hsbi oosk h avbee einn cludtehde iifnrosu rtrc tiovnaalluT eh.eh ya vbee etne stweidtc ha rbeu t
nogtu aranftoearen dpy a rtipcuurlpaorTs heep. u blidsoehsen roo tff earn y warraonrrt eiperse sentations
are
nodro eisat cceapntly i abiwliittrhie esspet cott h per ogmrsa.
Manyo ft hdee signautsiebodny ms a nufactaunrdse erlsl teodr iss tintghueipisrrho ductcsl aimaesd
are
trademaPrekasr.sE odnu caiLtiomni theadsm a dee verayt temtpost u pptlrya demianrfko rmaatbioount
manufactaunrdte hrespi rrod ucmtesn tioinnte hdib so okA. l iosftt hter ademark deasnidtg hneaitri ons
ownearpspe arosnt hipsa ge.
Covedre sniegdb yD esniegr&s P artneOrxsf,o rd
Transfertroed di gitalo npd reimnantd ,2 006
Firpsutb lis1h9e9d9
ISB0N 201-59604-0
BritLiisbhr aCrayt aloguing-inD-aPtuab lication
A catalroegcuoer dt hfibooosrk i sa vailabltehB efr riotLmii sbhr ary
LibraorfCy o ngrCeastsa loguing-inD-aPtuab lication
RabbFie,t hi
Algori:t hfmaus n ctiaopnparloI aF ceht Rhaib bGiu,yL apalme.
p. em.
Inclubdiebslr iaopghirceafle reanncdie nsd ex.
ISB0N- 201-59604-0
1.C omputaelrg ori2t.hF musn.c tipornoaglrm aimnlga nguaIgL.ea sp.a lGmuey,I. I T.i tle.
QA76.9.A431R93949
005.1-dc21 99-10106
CIP
Trademnaortki ce
Thef olloawritenr ga demoarrr kesg istterraedde moafrt khser iers pectciovmep anies.
Miranidsaat rademoafRr eks eaSrocfthw aLriem ited.
UNIXi sl icensed X/tOhperno Cuogmhp aLntyd .
Windoiwsast rademoafMr ikc rosCoofrtp oration.
Printanedd b ounbdy A ntonRyo weL tdE,a stboume
Contents
Preface ix
1 Introduction 1
1.1 Algorithms 1
1.2 Functiolnaanlg uages 5
1.3 Bibliogranpohtiecsa l 10
2 Functiopnraolg rammiinngH askell 11
2.1 Aboutth el anguage 11
2.2 Equatiaonndsf unctions 12
2.3 Basitcy peasn dc onstructypteesd 15
2.4 Lists 18
2.5 Higher-ofrudnecrt ipornoaglr ammitnegc hniques 27
2.6 Algebrtyapiecsa ndp olymorphism 33
2.7 Arrays 40
2.8 Typec lasasnedsc lamsest hods 43
Exercises 48
2.9 Bibliogranpohtiecsa l 51
3 Thee fficienocffy u nctionparlo grams 52
3.1 Reductoirodne r 52
3.2 Analyztihneeg ff icieonfcp yr ograms 56
3.3 Progratmr ansformation 65
3.4 Conclusion 69
Exercises 69
3.5 Bibliogranpohtiecsa l 70
vi Contents
4 Concredtaet tyap es 71
4.1 Lists 71
4.2 Trees 77
4.3 Arrays 82
Exercises 84
4.4 Bibliogranpohtiecsa l 85
5 Abstradcatt tay pes 86
5.1 Introduction 86
5.2 Stacks 87
5.3 Queues 90
5.4 Priorqiuteyu es 92
5.5 Sets 93
5.6 Tables 97
5.7 Binasreya rch trees 99
5.8 Heaps 103
5.9 AVLt reest 107
Exercises 112
5.10B ibliogranpohtiecsa l 113
6 Sorting 114
6.1 Introduction 114
6.2 Comparison-sboarsteidn g 115
6.3 Bassiocr tianlgg orithms 118
6.4 Tree-bassoerdt ing 122
6.5 Efficioefcn ocmyp arison-ablagsoerdi thmst 123
6.6 Representatiosno-rbtaisnegd 129
Exercises 132
6.7 Bibliogranpohtiecsa l 133
7 Grapha lgorithms 135
7.1 Definitainodtn esr minology 135
7.2 The grapAhD T 136
7.3 Depth-fainrdbs rte adth-sfeiarrscth 140
7.4 Topologsiocratl 144
7.5 Minimusmp annitnrge e 146
7.6 Depth-fsierasrttc rhe aesn df orestst 149
7.7 Conclusion 152
Exercises 153
7.8 Bibliogranpohtiecsa l 154
8 Top-dowdne sigtne chniques 155
8.1 Divide-and-conquer 155
8.2 Backtracskeianrgc h 159
8.3 Prioritsye-africrhs t 167
8.4 Greedayl gorithms 170
Contentvsi i
Exercises 174
8.5 Bibliogranpohtiecsa l 176
9 Dynamipcr ogramming 178
9.1 Introduction 178
9.2 Thed ynamipcr ogrammihnigg her-ofrudnecrt ion 180
9.3 Chainmeadt rmiuxl tiplications 181
9.4 Optimbailn asreya rtcrhe es 184
9.5 All-psahiorrst peastth 188
9.6 Thet ravelsianlge sperson 191
9.7 Conclusion 194
Exercises 194
9.8 Bibliogranpohtiecsa l 196
10A dvancetdo pics 197
101. Procensestw orks 197
10.2M onads 202
10 .3 Paralalleglo rithms 207
10 .4 Bibliogranpohtiecsa l 216
Bibliography 218
A Haskeilmlp lementations 227
B Mathematibcaaclk ground 228
B.1 Notation 228
B.2 Logarithms 228
B.3 Summatifoonr mulas 229
B.4 Solvirnegc urreenqcuea tions 229
Index 230
Preface
........................................................................ ..................................................... ....
Puproseg/o als
Thibso oiksp r imaarnii nltyr odtuocth tedio ens iogfan l goriftophrrm osb lseoml ving.
Itpsr ominfeeanttiu str oeu saefu nctiloannaglua asag nie m plemenltaantgiuoang e.
Becaoufsth eeh iglhe voefal b strapcrtoivoifndun ecdt,i pornoaglr taemntsdob es horter,
clearefra sattnoedd re vetlhoaptn h eiimrp eractoiuvnet erpTahrictsos n.t ritboau tes
bettuenrd erstoafnt dhiaenl gg orbietihinmmg p lemeanntdme adk eistp ossitbol e
explaolrtee rnastoilvuetm ioornresa pidAllyt.h owuegb he litehvatheti is str uef oar
widrea nogfea lgoritthhbemo soa,kl sdoi scutshsleei sm itations sotfy lfuen citni onal
dealwiintgh certain problems.
Thibso oiksn oatb oaup tart icuflunarc tiolnaanlg uaTgheem .a teriisaa rrla nged
int hsea mwea ya si na c lasasligco ribthomoski :t ais su ccesosfi ocnh aopnt ers
'rtadititoonpaislcu'sca hss ortainndsg e archailnggo riItnha mdsd.i ttihocenh ,o ice
ofa f unctiolnaanlg uage thpeie nrtmriotdsu ction deosfi sagtlnrg aotsreuigcthihme s
asd ivide-anda-ncddoy nnqamupiercro grambmymi enagn osfh igher-fuonrcdteiro ns.
Newc oncesputcashsp rocneestsw oarnkds paraallgloerlic tahanml ssb oei ntroduced
inan on-convewnatyDi.uo etn ota hlei xst mraat ertioaplsi,uc csah sl owbeoru npdro ofs
and cN-oPm plettheenoerarsyesn octo vered.
Thee mphaosfthi esb ooikso ni ntuiatnipdrv aeg mpartoigcr am development tech
niqu.Te hsiisso nlays masltlef por warda,sw ebe liethvaetf u nctipornoaglr amming
proviald ienbsek t wetehsnet uodfay l goriathnmthdse s tuodfcy o rrecptrnoeoasfnssd
systemdaertiivcfa rtoifmoor nm aslp ecificWaetar ieoh nosp.it nhgma otr peu blications
covertihnaigrs e wai elmle rignte h nee arf uture.
Anothaeirmo ft his ibsto oop kr ovai deu sreeffuelr oefnfu cnec tipornoaglr ams
relattoae v da rioefpt ry obl.e Pmrsogramwmielbrlesa blteoc hoo(soear d apat )
functiopnraolg rtahmai tsr elevtaotn hte pirro blaestm h eayl redaodw yi tho ther
langusaugceahssF ortPraasnc,oa rCl . arWeea lso hthoapttih nbigos o wki ll contribute
towarmdakisn gf unctiloannaglu maogrevesi abalsae p rogramtmoionlg.
x Preface
Apporach
Eacchh apatdedrr eass speesc pirfiocb lareemab yd escrtihbaeis nsgo cailagtoerdi thms.
Eacahl goriisst phemc iufiseidan t gr iv(ibauslto metiimneesffi cifeunnctt)i opnraol
gramI.ns omcea s,e msao reeffi ci(eanntod f tmeonr oeb scuvreer)sc iaobnne d erived.
Secticoonnst ainnoinn-ge ssmeantteiraila l awriet ah( m tars)ky emdb aonldc abne
safeslkiyp ped. Earxeei rnccilsuaedtste hdee n do fe acchh aptFeorra. ne xcellent
companbiooownkh ipcrho vifduerstd heetraa iblosmu ots otft hael goriptrhemsse nted
int hbioso ,wk er ecommBernads saarndBd r at'lFseu yndamseo nfAt laglohrmiic[ts1 ]9.
Anothefre atoufr et bhoeoi kst hues oef H askealn le,w lpyr opofsunecdt ional
langusatgaen dnaradm,ea df ttehrle o giHcaisaknBe .lC lu rryH.a skiesplu lr eflunyc
tionuaslel,sa zy evaalnuidan tcioorpnmo orasttte hsofe fea turoefms o dern functional
languaIgtef srie.tse o u seha,ns u meriomupsl emenotnaa tlimooanslpstll atmfsao,rn d
iasv ailfraobmsl iet weosr ldwAipdpee.n Ad pirxo visdoemisen formaabtoituohtne se
implemieon.nt sat
TheH askseolulr ccoed et hopefr ogrgaimvsie nnt his abnodo kat nhsew etros
seleecxteerdc airsaeev sa ilfarbotlmhe fe ol lowWWiWn g si:t e
http://www.rio.umontrea.cla/-plaal/meAlogrimts-hfunctoina.hltml
Targeatu idence
Thibso oikps r imlaytr arigeatteudn dergrsatduudaietnnect osm pustceire sntcued ying
algomrs.iW tihthait nr aditcioounrtashleau ts eism peraltainvgeu aognelasyf , r action
oft hken own algcoarni tcbhoemm sp leitmepllye meBnyta eddo.p tifnungc tai onal
langumaogrepe,r ototcyapbnee ds e velotpheicdso; n tritboau b teetsut nedr erstand
ing thotefe chniiqnuveosl vedi,m pslienmceaenp tairntgi aclugloarr rietqhumia r es
compluentdee rstoafna dltilhn dege tariellsat tote hdai lsg oriTthhiamsp. p roaalcsho
alloewxsp erimewnittachte irottnay ipnoe fsa lgori(tshumacsshp rocneestsw orks)
whicwho ulbed t odoi ffictuolp tr ogrianam ni mperative language.
Thep rerequaries:i tes
Studemnutshsta vhea ds omper ogramemxipnegr iienan ncye lanpgrueafgeer,a bly
�
af unctiolnaanlg uaSgomene.o tioofhn o wf unctiolnaanlg uaraegi emsp lemented
isa lsdoe sirable.
A minimum mathbeamcaktgirc(oamulno dss tulmym atiiosrn esq)u ired for the
analoyfsa ilsg oriatlhtmhsot uhgehss eec tciaosnnaf se lbyeo mitbtyet dh owsheo
aren oitn teriencs otmepdl eoxrei ffitcyi eannclayy s.i s
Theb oocka anl sboeu setdos uppaonra td vanfcunecdt ipornoaglr amcmoiunrigsn e
av arioefwt ayys (:1 b)y p reseenxtaimnpgol fie msp lemeanntudis nidgna gts atr uctures
sucahsq ueuterse,ae nsgd r ap(h)2sb ,yi ntrodpurcoignrdgae ms itgenc hni(q3bu)ye s,
increatshaeiw nagr enceosnsc erntiinmagen ds paecffiec ienacnyd(, b4 y)p resenting
somaed vantcoepdi csa sps ruocchne estsw oarnkpdsar alalleglo rithms.
Asm entiopnreedv iotuhsbelo yoi,ks a lsao usseofuurlo cffue n ctipornoaglr ams
whicchab ne r egaradsee xde cutsapbelcei ficoaftt hiaeol ngso rictohnmcse rned.
Prefacxei
Ovevriew
Theb ooikrs o ughly idnittvohi rdpeearteds. Tfihresp atr cto ntaailtnlhs be a ckground
informnaeteidobnee df oermeb arkionngt hmea ibno doyf t hbeo okC.h apt1e r
presetnhbteas s ciocn coefpa tna lgoraintdhh img hlkiegyfh etast uorffe unsc tional
languwaigteahsb riheifs tooftr hye eivro luatnidro onli enr eceynetar sC.h apt2e r
contaainin nst rodtuoc tfitunhocent iolnaanlg uHaagsek eClhla.p t3ie sra review of
evalumaetcihoann siuscmahsss traincldta zeyv aluawtiitaohnon v,e rvoifte hwge r aph
reducmtoidoenIl td. e finwehsa itsm eanbty' efficieanncdpy r'e sesnotmsse i mple
transfoonrtsmh aaittni cretahesefife c ieonfcp yr ograCmhsa.p 4te exra misniemsp le
concrdeattseat ruc(tluirtsertses ae,ns da, rr ayasn)dt heeffi cieonfct yho ep erations
usetdom aniputlhaetdseae ts at ruecst.u r
Thes ecopnadro tft hbeo ocko nsisdoemrbesa sailcg oriftoarhv masr ioefpt uyr poses.
Chap5te exra mianbesst draatctaty p(eAsD sT)u,s itnhge tmor eprefsoeernx ta mple
prioqruietuyae nsbd i nasreyar cthr eCehsa.p tperre s6es notrstt iencgh nisquucaehss
quickasnohdre ta psaonrCdth ap7td eirs cugsrsaepashn sad ssocailagtoerdi stuhcmhs
asc omputtihmneig n imsupma nntirneagen dd epth-first search.
Thel apsatr atd derssseosm hei gh-mleetvheold oluosgeiidnte hsde e sign of algo
rithms. 8Cd heaspctreirb es top-downs udceahssd i igvni dtee-cahnndi-qcuoensq uer
anbda cktrascekaricnhg. C9eh xappltsaeoirmn iesn efficieinntc oipe-sda olwgno rithms
andh owt hecya nb es olvbeyda doptai bnogt toma-puppr oancahm,ed lyyn amic
programmFiinnga.Cl hlayp,t 1e0er x amisnoemsae d vantcoepdis cusca hsp rocess
netwomroknsa,,d a sndp aralalleglo ritthhrmosus gohm e extteont shiHeoa nssk ell
language.
Ackonwledgeenmts
The iodfte hae bcoaomwkeh itlhee afiurtshwtoa rasv isilteicntgau Atrl elre gChoelny
legseoo usrp ectihaalna krdseu eM tiokHe o lcomBbobeC ,u ppaenrBd e Hna ytofcork
makiinptgo ssiWbelw oeu.l lditk oet haAnskt Tehri eMna,r iMnaah mooCdh,rM iese n,
LenB ottaancdRi o bT urner for theitros ocmooenf t rptihrbeou gtripaormness ented
int hbioso kWe.a lstoh aGnuky S t-Denhiisms a fnocyro nstruscutgigvees otnia o ns
preliminary ovfe rtbshoiioosokn u, er d iMtiocrh aSretaln fgo hri cso ntinued support,
antdh aen onymroeuvsi ewwheofr osu mnadn iyn accuarnagdca iveceso nstructive ideas
onp resentFaitniaowlnelar. ye ,g ratteofu l(t ehnet husHiaassktceiolcml!m )u nfiotry
theeiffro ritnds e sigans itnagn dfarundc tional alnafdno pgrru oavgietd hisenc gi entific
communwiittfyhr eiem plemieonntsa.t
FetiRh ahbi( F.A R.a hbi<Od.ch uslla.c .uk )
GuyLap alm(elp aal<Omeir.umo ontrea.lc a)
Description:The design of algorithms for problem-solving lies at the heart of computer science. Concise yet authoritative, Algorithms - A Functional Programming Approach teaches the skills needed to master this essential subject. The authors challenge more traditional methods of teaching algorithms by using a f