Learn You Some erLang for great good! Erlang to the People! L e a Learn You Some r Erlang is the language of choice for • Passing messages, raising errors, and n programmers who want to write robust, starting/stopping processes over many concurrent applications, but its strange nodes Y o Erlang to sytnthax aend fuPncteionoal dpesiglne ca!n intimi- • Storing and reLtrieving data using mne- Erlang for date the uninitiated. luckily, there’s a new e u sia and ETS weapon in the battle against Erlang-phobia: a Lea rn You Some S Erlang is the language of choice for Learn Y•o uP aSsosmineg Emrleasnsga fgoers G, rreaaisti nGgo oedrr!ors, and • network progrramming with TCP, uDP, o and the inet mnodule pcorongcruarmremnte arsp pwlihcoa twioannst, tbou wt rititse s rtorabnugset, Erlang m snatoeadsrettrisnogE F/sretrodp Hlpéianbge nprtr osgctaerst sse tssl ooowve r mtahny• e Th eP simepleo joy Yps anlde po!tential pitfalls Lm Great Good! sdyanteta txh aen udn finuintciatitoenda. ll duecskiilgyn, tchaenre i’nst aim nie-wale nadr ne aa•sb eoSsut oty roEinurgl ai nantngod’ st h rueent rboieartvshiincogsd: o dYxao tsuay’ nlul- sing mne- oapf pwlricitaintigo ndsistouributed, concurrent Eeae rlanLge afornr You Some Etarxla, nitgs id s as tithaae as lntardnu gcEutTuaSrgees ,o ift sc htoypicee sfyosrt em • Passing mess ages, raising errors, and rE weapon in the battle against Erlang-phobia: S n p(orro glarca•km tmnheeetrrwes oowfr!hk) o,p arwnoadgnr bat amtosm iwci nrfiugten w crtiotihob nuTasCtl ,P , uDP, P a sctkaerdt inwgit/hs tloigphptihnega prtreodc eilslussetsr aotvioenr sm aanndy r Learn You Some Erlang for Great Good! cporongcruar mr e mannti ndag pt hpteelicc ihannteiiotq numseo,s db. uuolten i ctse sytoraun’vgee ju s nt othdee sright mixo of offbeat and practical Yla A Beginner’s Guide Erlang maestro Fred Hébert starts slow swyrnatpapxe adn ydo fuurn hcetiaodn aalr oduensidg nth cea sni minptilme i- example programms, Learn You Some Erlang Gonreat GoEordla! ng for • The simple joys and potential pitfalls • Storing and retrieving data using mne- and eases you into the basics: You’ll dstautfef ,t yhoeu u’lnl itnaictikaltee tdh. el urecakill ym, ethaetr-ea’nsd a- new for Great Good! is the perfect entry point ug of writing distributed, concurrent sia and ETS e learn about Erlang’s unorthodox syn- wpoetaaptoo ne s ina o ptfhp telhic eba altatiotnlnegs uaaggaein:s cto Enrclaunrrge-npchyo,b ia: into the sometim es-crazy, always-thrilling S tax, its data structures, its type system Ldeisatrrnib Yuoteud S coommep Eurtlianngg, hfoort Gcoredaet lGoaodoidn!g, •w o nrledt wofo rEkr lparnogg.rE amming with TCP, uDP, f oo and the inet module (or lack thereof!), and basic functional and allP tahcek eodth weirt hd alirgkh mthaegaircte tdh ailtl umstarkaetiso ns and r programming techniques. once you’ve EErrllaannggj umssuatc ethhs eatr rohig oFhtr tet omdp iHixc éo abf meorfoftbn segta attro tadsna sdyl ’opswr a ctical •A bThoeu sitm tphlee jo ylaAs uantdh poorte n tial pitfalls mr A Beginner’s GGuidree at Good! and eases you into the basics: You’ll G wrapped your head around the simple savvy deexavemlopplee prsro.grams, Learn You Some Erlan g of writing distnributed, concurrent e stuff, you’ll tackle the real meat-and- learn afobro uGtr eEarlta Gnogo’sd u! niso trhtheo pdeorxf escytn e-ntry pointF r eadp pHléicbaetriot niss ag self-taught programmer r taasx ,y oitus ddiavtea i nsttoru Ecrtluarnegs’,s i tfsu ntycptieo nsayls tfeanm- who used to teach Erlang. He is currently Ee potatoes of the language: concurrency, into the sometimes-crazy, always-thrilling distributed computing, hot code loading, (toars yla wcwok rotlhdrl,ed yr eooofu f’El!l)r ,ll aeanangrdn. baabsoiuc tf:unctional wPaocrkkeindg wointh a l irgehaftlh-teiamrete bdi dildluinstgr aptliaotnfos ramn d ra aEsanrlvdav nayg ldl sethuveceh loo atph eherors t.d taorpki cm aamgiocn tgh atot dmaayk’se s pws• tr r u oTaCfgepfors,pm atyeAimonmdugbm oy’ lyonioln ou tTguuare rcthts ekae tlcpteahph dtnlh ieiacqera u ortAeieuosanun.ld s otm thwnhecioeate htsr - yiE amo nuupdn’lv-eiet and a2Yjefuoo0xnsrau1dt 2mG tSw.h rpoHaeelmesa irs tiepn g oGr ahEonmotgrl iolmrneaadednimx! g or G tEiosu sfrf, tol toaLohrrnef eiGfgaab pr lre,uene aLsa rtYeeft oe ara ucnrotnd fS eptonhrmaterc eyyt eipEcaoarrlila n nt g langt Go A Beginner’s Guide p• o BtautioldFeirsne god f aH tnhédeb relearltne igasus aain gsgee :ylf oc-tuoarnu caguphrptr lepicnraoctygi,or anms mer Ginotoo dth!,e i ss owmideetliym reesg-acrrdaezdy ,a asl ways-thrilling r o as you dive into Erlang’s functional fan- d i s wtriitbhwu tthheoed uocsoTemPd p fturoat itmnegea,wc hhoo rEtk rclaondge. lHoaed isin cgu, rrentlytw hoer lbde osft Ewralay ntog .le earn Erlang. f od tasy world, you’ll learn about: and allw tohrek iontgh eorn daa rrek aml-atigmice tbhiadtd minga kpelas tform a r! Erlanga snudc hw aas h noat mtoepdi cE arlmanogn gu steord aoyf ’tsh e year About the tAuthor • Testing your applications with Eunit ansda vvy d2e0v1e2.lo Hpiesr os.nline tutorial, Learn G G Common Test YoTuHE SFIoNEmST eIN EGErElKa EnNTgER TfAoINrM GENrT™eat Fred Hébert is a self-taught programmer r as you dwivwew .ninosttaorc hE.crolmang’s functional fan- who used to teaoch Erlang. He is currently e • Building and releasing your applications Good!, is widely regarded as with the oTP framework tasy wtohreld b, eyostu ’wlla lye atorn l eaabronu Et:rlang. working on a reaol-time bidding plaSHtEfolvrEm In : a This book u“seIs RLeIpEK oFvLeAr—T”a durable binding that won’t snap shut. PrICE: $49.95a (n$5d2 .w95a CsD nn)a mePdr doEgrrlaamnmgI nugs learn goufa tghEeS/ Eyrelaanr g t • Testing your applications with Eunit and 2012. His online !tutorial, Learn H ébert G Common Test You Some Erlang for Great o Fred Hébert • Building and releasing your applications Good!, is widely regarded as THE FINEST IN GEEK ENTERTAINMENT™ o www.nostarch.com with the oTP framework the best way to learn Erlang. d Foreword by Joe Armstrong SHElvE In: ! “I LIE FLAT” PrICE: $49.95 ($52.95 CDn) ProgrammIng languagES/Erlang This book uses RepKover—a durable binding that won’t snap shut. Hébert THE FINEST IN GEEK ENTERTAINMENT™ www.nostarch.com Fred Hébert SHElvE In: “I LIE FLAT” PrICE: $49.95 ($52.95 CDn) ProgrammIng languagES/Erlang Foreword by Joe Armstrong This book uses RepKover—a durable binding that won’t snap shut. Hébert San Francisco Fred Hébert Foreword by Joe Armstrong Learn You Some erLang for great good! Copyright © 2013 by Fred Hébert. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed in USA First printing 17 16 15 14 13 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-435-1 ISBN-13: 978-1-59327-435-1 Publisher: William Pollock Production Editor: Alison Law Cover Design: Sonia Brown Developmental Editor: Keith Fancher Technical Reviewer: Geoff Cant Copyeditor: Marilyn Smith Compositor: Susan Glinert Stevens Proofreader: Greg Teague For information on book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 38 Ringold Street, San Francisco, CA 94103 phone: 415.863.9900; fax: 415.863.9950; [email protected]; www.nostarch.com Library of Congress Cataloging-in-Publication Data A catalog record of this book is available from the Library of Congress. No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the infor- mation contained in it. Brief Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Foreword by Joe Armstrong . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1: Starting Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Chapter 2: Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 Chapter 3: Syntax in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Chapter 4: Types (or Lack Thereof) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 Chapter 5: Hello Recursion! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Chapter 6: Higher-Order Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 Chapter 7: Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 Chapter 8: Functionally Solving Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Chapter 9: A Short Visit to Common Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Chapter 10: The Hitchhiker’s Guide to Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . .135 Chapter 11: More on Multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Chapter 12: Errors and Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Chapter 13: Designing a Concurrent Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175 Chapter 14: An Introduction to OTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199 Chapter 15: Rage Against the Finite-State Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Chapter 16: Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Chapter 17: Who Supervises the Supervisors? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263 Chapter 18: Building an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Chapter 19: Building Applications the OTP Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Chapter 20: The Count of Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315 Chapter 21: Release Is the Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335 Chapter 22: Leveling Up in the Process Quest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353 Chapter 23: Buckets of Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Chapter 24: EUnited Nations Council . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397 Chapter 25: Bears, ETS, Beets: In-Memory NoSQL for Free! . . . . . . . . . . . . . . . . . . . . . . 419 Chapter 26: Distribunomicon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .441 Chapter 27: Distributed OTP Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .473 Chapter 28: Common Test for Uncommon Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .485 Chapter 29: Mnesia and the Art of Remembering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 Chapter 30: Type Specifications and Dialyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .543 Afterword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 Appendix: On Erlang’s Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .577 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 vi Brief Contents Contents in De tail about the author xvii foreword by Joe armstrong xix Preface xxi To the Foreigner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi To the Erlang Regular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii To the Person Who Has Read This Online . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii acknowLedgmentS xxiii IntroductIon 1 So What’s Erlang? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Don’t Drink Too Much Kool-Aid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 What You Need to Dive In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Where to Get Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1 StartIng out 7 Using the Erlang Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Entering Shell Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Exiting the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Some Erlang Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Invariable Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Atoms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Boolean Algebra and Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . 14 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 List Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Working with Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Bit Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Bitwise Binary Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Binary Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Binary Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2 moduLeS 31 What Are Modules? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Creating Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Compiling Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Compiler Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Defining Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 More About Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Circular Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3 SYntax In functIonS 43 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Fancier Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Variables in a Bind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Guards, Guards! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 What the If ?! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 In case . . . of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Which Should We Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4 tYPeS (or Lack thereof) 55 Dynamite-Strong Typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 To Guard a Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 For Type Junkies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 5 heLLo recurSIon! 61 How Recursion Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Length of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Length of a Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 More Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 A Duplicate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 A Reverse Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 A Sublist Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 A Zip Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Quick, Sort! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 More Than Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Thinking Recursively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 6 hIgher-order functIonS 77 Let’s Get Functional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 More Anonymous Function Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Function Scope and Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Maps, Filters, Folds, and More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Fold Everything . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 More Abstractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 viii Contents in Detail