Programming Erlang (2nd edition)
by Joe Armstrong
A multi-user game, web site, cloud application, or networked database
can have thousands of users all interacting at the same time. You need a
powerful, industrial-strength tool to handle the really hard problems
inherent in parallel, concurrent environments. You need Erlang. In this
second edition of the bestselling Programming Erlang, you’ll learn how
to write parallel programs that scale effortlessly on multicore systems.
“A gem; a sensible, practical introduction to functional programming."
—Gilad Bracha - Co-author of the Java language and Java Virtual
Machine specifications, creator of the Newspeak language, member of the
Dart language team
Using Erlang, you’ll be surprised at how easy it becomes to deal with
parallel problems, and how much faster and more efficiently your
programs run. That’s because Erlang uses sets of parallel
processes—not a single sequential process, as found in most
programming languages.
Joe Armstrong, creator of Erlang, introduces this powerful language in
small steps, giving you a complete overview of Erlang and how to use it
in common scenarios. You’ll start with sequential programming, move to
parallel programming and handling errors in parallel programs, and learn
to work confidently with distributed programming and the standard
Erlang/Open Telecom Platform (OTP) frameworks.
You need no previous knowledge of functional or parallel programming.
The chapters are packed with hands-on, real-world tutorial examples and
insider tips and advice, and finish with exercises for both beginning
and advanced users.
The second edition has been extensively rewritten and covers Erlang R17
features. New to this edition are seven chapters covering the latest
Erlang features: maps, the type system and the Dialyzer, WebSockets,
programming idioms, and a new stand-alone execution environment. You’ll
write programs that dynamically detect and correct errors, and that can
be upgraded without stopping the system. There’s also coverage of rebar
(the de facto Erlang build system), and information on how to share and
use Erlang projects on github, illustrated with examples from cowboy and
bitcask.
Erlang will change your view of the world, and of how you program.
What You Need
The Erlang/OTP system. Download it from
erlang.org
Resources
Releases:
- P2.0 2014/08/12
- P1.0 2013/10/08
- B6.0 2013/07/27
- B5.0 2013/07/09
- Introduction
- Why
Erlang?
- Introducing Concurrency
- Modelling Concurrency
- Benefits of Concurrency
- Concurrent Programs and Parallel Computers
- Sequential vs. Concurrent Programming Languages
- A Whirlwind Tour of Erlang
- The shell
- Processes, Modules and Compilation
- Hello Concurrency
- Sequential Programming
- Basic Concepts
- Starting and Stopping the Erlang Shell
- Simple Integer Arithmetic
- Variables
- Floating-Point Numbers
- Atoms
- Tuples
- Lists
- Strings
- Pattern Matching Again
- Modules and Functions
- Modules are where we store code
- Back to Shopping
- Funs: the basic unit of abstraction
- Simple List Processing
- List Comprehensions
- BIFs
- Guards
- case and if Expressions
- Building Lists in Natural Order
- Accumulators
- Records and Maps
- Records are used to name the items in a tuple
- Maps are improved records
- When to Use Maps or Records
- Error Handling in Sequential Programs
- Handling errors in sequential code
- Trapping an Exception with Try…Catch
- Trapping an Exception with Catch
- Programming Style with Exceptions
- Stack Traces
- Fail Fast and Noisily, Fail Politely
- Binaries and the Bit Syntax
- Binaries
- The Bit Syntax
- Bitstrings – Processing Bit-Level Data
- The Rest of Sequential Erlang
- apply
- Arithmetic Expressions
- Arity
- Attributes
- Block Expressions
- Booleans
- Boolean Expressions
- Character Set
- Comments
- Dynamic Code Loading
- Erlang Preprocessor
- Escape Sequences
- Expressions and Expression Sequences
- Function References
- Include Files
- List Operations []{.underline} and - -
- Macros
- Match Operator in Patterns
- Numbers
- Operator Precedence
- The Process Dictionary
- References
- Short-Circuit Boolean Expressions
- Term Comparisons
- Tuple Modules
- Underscore Variables
- Types
- Specifying Data and Function Types
- Erlang Type Notation
- A Session With The Dialyzer
- Type Inference and Success Typing
- Limitations of the Type System
- Compiling and Running Your Program
- Modifying the Development Environment
- Different Ways to Run Your Program
- Automating Compilation with Makefiles
- When Things Go Wrong
- Getting Help
- Tweaking the Environment
- Concurrent and Distributed Programs
- Real-World Concurrency
- Concurrent Programming
- The Concurrency Primitives
- Introducing Client-Server
- Process Creation Time
- Receive with a Timeout
- Selective Receive
- Registered Processes
- A Word About Tail Recursion
- Spawning with MFAs or Funs
- Errors in Concurrent
Programs
- Error handing Philosophy
- Error Handling Semantics
- Creating Links
- Groups of processes which all die togther
- Setting up a firewall
- Monitors
- Error Handling Primitives
- Programming techniques for achieving fault tolerence
- Distributed Programming
- Two Models For Distribution
- Building The Name Server
- Libraries and BIFS for Distributed Programming
- The Cookie Protection System
- Socket-Based Distribution
- Programming Libraries and Frameworks
- Interfacing Techniques
- How Erlang Communicates With External Programs
- Interfacing an External C Program with a Port
- Calling a Shell Script from Erlang
- Advanced Interfacing Techniques
- Programming with Files
- Modules for Manipulating Files
- Ways to Read a File
- Ways to Write a File
- Directory and File Operations
- Bits and Pieces
- A Find Utility
- Programming with Sockets
- Using TCP
- Active and Passive Sockets
- Error Handling with Sockets
- UDP
- Broadcasting to Multiple Machines
- A SHOUTcast Server
- Interfacing Erlang to the Browser with
Websockets
- Six Examples of Controlling the Browser From Erlang
- The Browser-Server Protocol
- Storing data with ETS and DETS
- Types of Table
- ETS Table Efficiency Considerations
- Creating an ETS Table
- Example Programs with ETS
- Storing Tuples on Disk
- What Haven’t We Talked About?
- Mnesia: The Erlang Database
- Creating the Initial Database
- Database Queries
- Adding and Removing Data in the Database
- Mnesia Transactions
- Storing Complex Data in Tables
- Table Types and Location
- The Table Viewer
- Digging Deeper
- Profiling, Debugging, and Tracing
- Tools for Profiling Erlang Code
- Testing Code Coverage
- Generating Cross References
- Compiler Diagnostics
- Runtime Diagnostics
- Debugging Techniques
- The Erlang Debugger
- Tracing Messages and Process Execution
- Frameworks for Testing Erlang Code
- Introducing OTP
- The Road to the Generic Server
- Getting Started with gen_server
- The gen_server Callback Structure
- Code and Templates
- Digging Deeper
- Making a System with OTP
- Generic Event Handling
- The Error Logger
- Alarm Management
- The Application Servers
- The Supervision Tree
- Starting the System
- The Application
- File System Organization
- The Application Monitor
- How Did We Make That Prime?
- Digging Deeper
- Building Applications
- Programming Idioms
- Maintaining the Erlang view of the world
- A Multi-Purpose Server
- Stateful Modules
- Adapter Patterns
- Intentional programming
- Third Party Programs
- Making a Shareable Archive and Managing Your Code With Rebar
- Integrating External Programs With Our Code
- Making a local copy of the dependencies.
- Building Embedded Web Servers with Cowboy
- Programing Multicore CPUs
- Good News for Erlang Programmers
- How to Make Programs Run Efficiently on a Multicore CPU
- Parallelizing Sequential Code
- Small Messages, Big Computations
- Paralleizeing computations with mapreduce
- Sherlock’s Last Case
Author
Joe Armstrong is one of the creators of Erlang. He has a Ph.D. in
computer science from the Royal Institute of Technology in Stockholm,
Sweden and is an expert on the construction of fault-tolerant
systems. He has worked in industry, as an entrepreneur, and as a
researcher for more than 35 years.