O'Reilly logo

Introducing Erlang, 2nd Edition by Simon St. Laurent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 values—even forget the conventions of variable assignment.

Instead, you’re 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 is—but also enjoyable, powerful, and fun.

My first explorations of Erlang confused me and excited me at the same time. I’d had some experience with what I’d 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 who’ve been programming in other languages but want to look around. Maybe you’re being very practical, and Erlang’s 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 you’re 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 you’ve learned to program in other paradigms. However, getting started in Erlang—sometimes even just installing it—requires a fair amount of computing skill. If you’re 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 don’t likely need this book unless you’re looking for a slow brush-up.

If you’re 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, you’ll learn to write simple Erlang programs. You’ll understand why Erlang makes it easier to build resilient programs that can scale up and down with ease. Perhaps most importantly, you’ll be able to read other Erlang resources that assume a fair amount of experience and make sense of them.

In more theoretical terms, you’ll get to know functional programming. You’ll learn how to design programs around message passing and recursion, creating process-oriented programs focused more on data flow.

You’ll 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. You’ll probably get the most out of it if you read it in order at least the first time, though you’re always welcome to come back to find whatever bits and pieces you need.

You’ll start by getting Erlang installed and running, and looking around its shell. You’ll spend a lot of time in the shell, so get cozy. Next, you’ll start loading code into the shell to make it easier to write programs, and you’ll learn how to call that code and mix it up.

Next, you’ll take a close look at numbers because they’re an easy place to get familiar with Erlang’s basic structures. Then you’ll learn about atoms, pattern-matching, and guards—the likely foundations of your program structure. After that you’ll learn about strings, lists, and the recursion at the heart of most Erlang processing. Once you’ve gone a few million recursions down and back, it’ll 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 dictionary—here’s a list of operators, here’s a list of control structures, these are the datatypes—and then smash them together. Those lists are here, but they’re in Appendix A, not in the main flow of the book.

Many of the examples I use are built on the same foundation. While you will probably be tired of falling objects by the end of the book, staying with a small set of examples makes it easier to introduce new features rather than explaining endless projects.

The main point you should get from this book is that you can program in Erlang. If you don’t get that, let me know!

Etudes for Erlang

While I was writing this book, J. David Eisenberg was developing a broad set of exercises to accompany it. They proved comprehensive enough to become a separate project, which you can find (for free on the web) on GitHub. The front page is about contributing to the project, but if you click on a chapter name you’ll get to the content.

The etudes are structured to match this book, but have grown over time to cover a slightly larger scope than this book does. You’ll probably get the most out of them if you explore them each time you finish a chapter, but they’re also great for general review and to test your understanding.

Why I Wrote This Book

I’m not an Erlang expert hoping to create more Erlang experts to get a lot of work done.

I’m a writer and developer who encountered Erlang, thought it was the programming language I’d been seeking for a long time, and felt compelled to share some of that. I’m hoping that the path I followed will work for other people, probably with variations, and that a book written from a beginner’s perspective (and vetted by experts) will help more people find and enjoy Erlang.

Other Resources

This book may not be the best way for you to learn Erlang. It all depends on what you want to learn and why.

If your primary interest in learning Erlang is to break out of a programming rut, you should explore Bruce Tate’s wild tour of Seven Languages in Seven Weeks (Pragmatic Publishers), which explores Ruby, Io, Prolog, Scala, Erlang, Clojure, and Haskell. Erlang gets only (an excellent) 37 pages, but that might be what you want.

For an online experience (now also in print from No Starch Books) with more snark and funnier illustrations, you should explore Fred Hebert’s Learn You Some Erlang for Great Good!. While much longer than Tate’s telling, it certainly moves faster and may feel more like an experienced programmer’s guide to Erlang.

The two classic general books on Erlang are the similarly titled Programming Erlang (Pragmatic Publishers) by Erlang creator Joe Armstrong, and Erlang Programming (O’Reilly) by Francesco Cesarini and Simon Thompson. They cover a lot of similar and overlapping terrain, and both may be good places to start if this book moves too slowly or you need more reference material. Erlang Programming goes further into what you can do with Erlang, whereas Programming Erlang provides a lot of detail on setting up an Erlang programming environment.

On the more advanced side, Erlang and OTP in Action (Manning) by Martin Logan, Eric Merritt, and Richard Carlsson, opens with a high-speed 72-page introduction to Erlang and then spends most of its time applying the OTP, Erlang’s framework for building upgradeable and maintainable concurrent applications. More recently, Designing for Scalability with Erlang/OTP (O’Reilly), by Francesco Cesarini and Steve Vinoski, focuses squarely on building large and resilient applications with Erlang’s OTP libraries.

At the end of each chapter of this book, you’ll find a note pointing to relevant information on the chapter’s content in other Erlang-focused books. Hopefully these notes will help you move quickly among them if you use this book as a companion to the rest of the growing Erlang library.

If you want to focus on connecting Erlang to the web, you should definitely also explore Building Erlang Web Applications (O’Reilly) by Zachary Kessin.

You’ll also want to visit the main Erlang website for updates, downloads, documentation, and more.

Are You Sure You Want Erlang?

Though they’ve been obscure for a long time, there’s a crowd of functional languages rising into greater popularity.

Six of them in particular—Elixir, Clojure, Scala, F#, Haskell, and OCaml—may be more appealing than Erlang if you have specific needs.

  • Elixir is built on the same foundations as Erlang, but has a Ruby-like syntax with strong support for metaprogramming.

  • Clojure and Scala run on the Java Virtual Machine (JVM), making them insanely portable, and they have access to Java libraries as a result. ClojureScript does the same with JavaScript, too. (Erjang makes it possible to run Erlang on the JVM, but it’s not a core part of the language.)

  • F# runs on the .NET Common Language Runtime (CLR), making it very portable in the Microsoft ecosystem, and again, has access to .NET libraries.

  • Haskell, with deliberately mathematical roots, offers a stronger type system and a different kind of discipline (and laziness).

  • OCaml also offers a strong type system, and focuses on performance.

Personally, I got my start with these concepts in XSLT. It’s a very different kind of language meant for a specific domain of document transformation, but many of the same ideas flow through it.

You don’t, of course, have to decide if Erlang is your life’s dream now. You can learn concepts in Erlang and apply them elsewhere if another language turns out to be a better idea for your work.

Erlang Will Change You

Before you go deeper, you should know that working in Erlang may irrevocably change the way you look at programs. Its combination of functional code, process-orientation, and distributed development may seem alien at first. However, once it sinks in, Erlang can transform the way you solve problems, and potentially make it difficult to return to other languages, environments, and programming cultures.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Note

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

A Note on Erlang Syntax

Erlang’s syntax seems to be a sticking point for a lot of people. It doesn’t look like the C family of languages. Punctuation is different and capitalization matters. Periods even get used as conclusions rather than connectors!

To me, Erlang syntax mostly feels natural, and I’m especially happy that it’s different from the other languages I typically use. I make a lot fewer mixups that way.

Rather than dwell on syntax, I’ve chosen just to present it as it is. Comparing it to other languages doesn’t seem likely to be helpful, especially when different readers may come from different programming backgrounds. Hopefully you will find Erlang syntax as pleasant as I do. If you just can’t get past it, you may want to try Elixir instead.

Using Code Examples

The examples in this book are meant to teach basic concepts in small bites. While you may certainly borrow code and reuse it as you see fit, you won’t be able to take the code of this book and build a stupendous application instantly (unless perhaps you have an unusual fondness for calculating the speeds of falling objects).

The examples in this book are deliberately simple and perhaps even stupid. They aren’t designed to dazzle or to show off, but to let you figure out how pieces fit together in the simplest possible way. You should, however, be able to figure out the steps you need to take to build a great application.

You can download the code from the Examples link on GitHub.

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you are reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Introducing Erlang, 2nd Edition, by Simon St.Laurent (O’Reilly). Copyright 2017 Simon St.Laurent, 978-1-4919-7337-0.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Help This Book Grow

While I hope that you will enjoy reading this book and learn from it, I also hope that you can contribute to helping other readers learn Erlang here. You can help your fellow readers in a number of ways:

  • If you find specific technical problems, bad explanations, or things that can be improved, please report them through the errata system.

  • If you like (or don’t like) the book, please leave reviews. The most visible places to do so are on Amazon.com (or its international sites) and at the O’Reilly catalog page. Detailed explanations of what worked and what didn’t work for you (and the broader target audience of programmers new to Erlang) are helpful to other readers and to me.

  • If you find you have much more you want to say about Erlang, please consider sharing it, whether on the web, in a book of your own, in training classes, or in whatever form you find easiest.

I’ll update the book for errata, and try to address issues raised in reviews. Even once the book is “complete,” I may still add some extra pieces to it. If you purchased it as an ebook, you’ll receive these updates for free at least up to the point where it’s time for a whole new edition. I don’t expect that new edition declaration to come quickly, however, unless the Erlang world changes substantially.

Hopefully this book will engage you enough to make you consider sharing.

Please Use It For Good

I’ll let you determine what “good” means, but think about it. Please try to use Erlang’s power for projects that make the world a better place, or at least not a worse place.

O’Reilly Safari

Note

Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals.

Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.

For more information, please visit http://oreilly.com/safari.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/introducing-erlang-2e.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Many thanks to Zachary Kessin for introducing me to Erlang in the first place, and to him and Francesco Cesarini for encouraging me to write this. Detailed feedback from Steve Vinoski and Fred Hebert made it possible, I hope, for the first edition of this book, to get readers started on the right track. J. David Eisenberg and Chuck Ha helped make it especially possible for beginners to get started right, pointing out gaps and issues in my prose. Steve Vinoski’s continued technical assistance on the second edition has improved the book further, and Dawn Schanafelt’s editorial assistance on the second edition has made the prose much smoother.

In particular, thanks to my wife, Angelika, for encouraging me to finish this, to my son Konrad for not throwing the printouts around too much, and to my daughter Sungiva for understanding that after I told her the story about Ned and Ernie, adventuring snakes, I needed to go back downstairs and work on this.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required