Introducing Erlang
by Simon St.Laurent
Copyright 2017 Simon St.Laurent. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com/safari). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Dawn Schanafelt
- Production Editor: Colleen Lobner
- Copyeditor: Christina Edwards
- Indexer: WordCo Indexing Services
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Rebecca Demarest
- March 2017: Second Edition
Revision History for the Second Edition
- 2017-02-28: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491973370 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Introducing Erlang, the cover image of a giant red flying squirrel, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-97337-0
[LSI]
Preface
Erlang has long been a mysterious dark corner of the programming universe, visited mostly by developers who need extreme reliability or scalability and people who want to stretch their brains.
Developed at Ericsson to serve on telephone-switching equipment, it seemed like a strangely special-purpose language until recently, when our computer and network architectures came to look a lot more like massively parallel telephone-switching equipment. Thanks to the spread of NoSQL data stores CouchDB and Riak, you may already be using Erlang without realizing it, and Erlang is moving out into many more fields. WhatsApp is a classic Erlang success story.
Erlang provides a short path from discipline to resilience. The decisions made by the creators of the language let the Erlang environment seamlessly scale and handle failure in ways that other environments have to manage by adding ever more infrastructure. In my utterly biased opinion, any application that needs to run for a long time and scale to many interactions should be built in Erlang (or its more recent cousin Elixir).
If you come from pretty much any background other than functional programming, exploring Erlang will require you to clear your mind of many of the techniques used in other programming languages. Forget classes, forget variables that change valueseven forget the conventions of variable assignment.
Instead, youre going to have to think about pattern matching, message passing, and establishing pathways for data rather than telling it where to go. Erlang programming can feel like making a key whose teeth set the tumblers on a lock just right for the key to pass, or playing pachinko and watching the balls fall through a maze.
Sound strange? It isbut also enjoyable, powerful, and fun.
My first explorations of Erlang confused me and excited me at the same time. Id had some experience with what Id called invariant variables, variables that can be bound to a value only once, in XSLT. That created a lot of headaches for me until I realized I was coming at the problems all wrong, and then it suddenly made sense.
Who This Book Is For
This book is mostly for people whove been programming in other languages but want to look around. Maybe youre being very practical, and Erlangs distributed model and the resulting scale and resilience advantages appeal to you. Maybe you want to see what this functional programming stuff is all about. Or maybe youre just going for a hike, taking your mind to a new place.
I suspect that functional programming is more approachable as a first language, before youve learned to program in other paradigms. However, getting started in Erlangsometimes even just installing itrequires a fair amount of computing skill. If youre a complete newcomer to programming, welcome, but there will be a few challenges along the way.
Who This Book Is Not For
This book is not for people in a hurry to get things done.
If you already know Erlang, you dont likely need this book unless youre looking for a slow brush-up.
If youre already familiar with functional languages, you may find the pacing of this Gentle Introduction hopelessly slow. Definitely feel free to jump to another book that moves faster if you get bored. Come back if you find the others go too fast, and feel welcome to use this as a companion guide or reference with other books.
What This Book Will Do For You
In his book Seven Languages in Seven Weeks, Bruce Tate suggests that Erlang makes hard things easy and easy things hard. This book will get you through the easy things hard part, and show you a bit of the promised land of hard things easy.
In practical terms, youll learn to write simple Erlang programs. Youll understand why Erlang makes it easier to build resilient programs that can scale up and down with ease. Perhaps most importantly, youll be able to read other Erlang resources that assume a fair amount of experience and make sense of them.
In more theoretical terms, youll get to know functional programming. Youll learn how to design programs around message passing and recursion, creating process-oriented programs focused more on data flow.
Youll also be better prepared to read other books and conversations about Erlang.
How This Book Works
This book tries to tell a story with Erlang. Youll probably get the most out of it if you read it in order at least the first time, though youre always welcome to come back to find whatever bits and pieces you need.
Youll start by getting Erlang installed and running, and looking around its shell. Youll spend a lot of time in the shell, so get cozy. Next, youll start loading code into the shell to make it easier to write programs, and youll learn how to call that code and mix it up.
Next, youll take a close look at numbers because theyre an easy place to get familiar with Erlangs basic structures. Then youll learn about atoms, pattern-matching, and guardsthe likely foundations of your program structure. After that youll learn about strings, lists, and the recursion at the heart of most Erlang processing. Once youve gone a few million recursions down and back, itll be time to look at processes, a key part of Erlang that relies on the message-passing model to support concurrency and resilience.
Once you have the foundation set, you can take a closer look at debugging and data storage, and then get a quick look at a toolset that is likely at the heart of your long-term development with Erlang: the Open Telecom Platform (OTP), which is about much much more than telephones.
Some people want to learn programming languages through a dictionaryheres a list of operators, heres a list of control structures, these are the datatypesand then smash them together. Those lists are here, but theyre in , not in the main flow of the book.