Foreword
Learning to program is fun, or at least it should be fun. If its not fun, you wont enjoy doing it. During my career as a programmer, I have taught myself several different programming languages, and it hasnt always been fun. Whether or not learning a language is fun depends to a large extent on how the language is introduced.
When you start working with a new programming language, on the surface it seems that all you are doing is learning a new language. But at a deeper level, you are doing something much more profoundyou are learning a new way of thinking. Its this new way of thinking that is exciting, not the minor details of the punctuation or how the language looks compared to your favorite programming language.
Functional programming is one of those areas of programming that has acquired a reputation for being hard (concurrent programming even more so), and so writing a book about Erlang that covers the ideas of functional programming plus concurrent programming is a daunting prospect. Make no mistake about it: Introducing functional programming is not so easy, and introducing concurrent programming has its difficulties. Doing both with humor and ease requires a very particular kind of talent.
Fred Hebert has shown that he has this talent. He explains complex ideas in a way that makes them seem simple.
One of the biggest barriers to learning Erlang is not so much that the ideas involved are intrinsically difficult but that they are very different from the ideas in most of the other languages that you will have encountered. To learn Erlang, you have to temporarily unlearn what you have learned in other programming languages. Variables in Erlang do not vary. Youre not supposed to program defensively. Processes are really, really cheap, and you can have thousands of them or even millions, if you feel like it. Oh, and then there is the strange syntax. Erlang doesnt look like Java; there are no methods or classes and no objects. And wait a moment... even the equals sign doesnt mean equalsit means match this pattern.
Fred is completely undaunted by these issues; he treats the subject matter with a delicate dry humor and teaches complex subjects in such a way that we forget the complexity.
This is now the fourth major text on Erlang and is a great addition to the Erlang library. But its not only about Erlang. Many of the ideas in Freds book are equally applicable to Haskell or OCaml or F#.
I hope that many of you will enjoy reading Freds book as much as I did and that you find learning Erlang to be an agreeable and thought-provoking process. If you type in the programs in this book and run them as you go along, youll learn even more. Writing programs is much more difficult than reading them, and the first step is just letting your fingers get used to typing in the programs and getting rid of the small syntax errors that inevitably occur. As you get deeper into the book, youll be writing programs that are pretty tricky to write in most other languagesbut hopefully you wont realize this. Soon youll be writing distributed programs. This is when the fun starts...
Thanks, Fred, for a great book.
Joe Armstrong
Stockholm, Sweden
November 6, 2012
Preface
This book initially started as a website, which is still available at http://learnyousomeerlang.com/ (thanks to No Starch Presss open-mindedness regarding all things related to publishing and technical material).
Since the first chapters were made public in 2009, Learn You Some Erlang has grown from a three-chapter micro-tutorial with a request for proofreading on the erlang-questions mailing list into one of the official documentations suggestions for learning Erlang, a book, and a major accomplishment in my life. Im baffled and thankful for all it has brought me, from friends to jobs to the title of Erlang User of the Year 2012.
To the Foreigner
When youre looking at Erlang programmers from afar, as an outsider, they may seem like a weird little community of people who believe in principles that nearly nobody else needs or wants to follow. Their principles look impractical, limited in how they can be applied. To make matters worse, Erlang citizens may appear similar to members of a religious sect, entirely sure that they know the one true way to the heart of software. This is the same kind of one true way previously preached by fanatics of languages like those of the Lisp family, Haskellers, proud members of the formal proof school of thought, Smalltalk programmers, stack aficionados from the world of Forth, and so on. Same old, same old; they all offer great promises of success, and deliver in various ways, but the programs we programmers write are still buggy, too expensive, or unmaintainable.
With Erlang, its likely the promise of concurrency or parallelism that brings you here. Maybe its the distributed computing aspect of the language, or possibly its unusual approach to fault tolerance. Of course, approaching Erlang with skepticism is a good thing. It wont solve all your problemsthats your job, after all. Erlang is merely a nifty toolbox to help you do so.
To the Erlang Regular
You already know Erlang, possibly very well. In that case, I hope this book becomes an interesting read or a possible reference, or that a few of its chapters help you learn more about bits of the language and its environment that you werent too familiar with before.
Its also possible that you know Erlang better than I do in every respect. In that case, I hope this book makes an adequate paperweight or space-filler in your library.
To the Person Who Has Read This Online
Thanks for your support, and I hope you enjoy what professional editing has brought to the original text, along with a boost into R15B+ versions of Erlang.
Acknowledgments
Thanks to Miran Lipovaa for coming up with the Learn You a Language idea first, and for letting me borrow the concept for this book and its related website.
Thanks to Jenn (my girlfriend) for the original website design, the long yeomans work required to redraw most of the images of this book so they would be suitable for print, her support, and her patience in letting me spend so many hours working on this project.
Thanks to all the people who gave their time away to help review the online copy of this book, find errors, and offer support (in no particular order): Michael Richter, OJ Reeves, Dave Pawson, Robert Virding, Richard OKeefe, Ulf Wiger, Lukas Larsson, Dale Harvey, Richard Carlsson, Nick Fitzgerald, Brendon Hogger, Geoff Cant, Andrew Thompson, Bartosz Fabianowski, Richard Jones, Tuncer Ayaz, William King, Mahesh Paolini-Subramanya, and Malcolm Matalka. There were also many other people who provided minor reviews and spotted typos and other errors.
A second thanks to Geoff Cant, who was the official tech reviewer for this version of the book.
Thanks to the team at No Starch Press (Keith, Alison, Leigh, Riley, Jessica, Tyler, and Bill) for their professional work.
Finally, thanks to the countless readers of the online copy of this book: those who bought this version, and those who read it without buying it.
Introduction
This is the beginning of Learn You Some Erlang for Great Good! Reading this book should be one of your first steps in learning Erlang, so lets talk about it a bit.
I got the idea to write this book after reading Miran Lipovaas Learn You a Haskell for Great Good! (LYAH) tutorial. I thought he did a great job making the language attractive and the learning experience friendly. As I already knew him, I asked him how he felt about me writing an Erlang version of his book. He liked the idea, being somewhat interested in Erlang.