Erlang Programming

Book description

This book is an in-depth introduction to Erlang, a programming language ideal for any situation where concurrency, fault tolerance, and fast response is essential. Erlang is gaining widespread adoption with the advent of multi-core processors and their new scalable approach to concurrency. With this guide you'll learn how to write complex concurrent programs in Erlang, regardless of your programming background or experience.

Written by leaders of the international Erlang community -- and based on their training material -- Erlang Programming focuses on the language's syntax and semantics, and explains pattern matching, proper lists, recursion, debugging, networking, and concurrency.

This book helps you:

  • Understand the strengths of Erlang and why its designers included specific features
  • Learn the concepts behind concurrency and Erlang's way of handling it
  • Write efficient Erlang programs while keeping code neat and readable
  • Discover how Erlang fills the requirements for distributed systems
  • Add simple graphical user interfaces with little effort
  • Learn Erlang's tracing mechanisms for debugging concurrent and distributed systems
  • Use the built-in Mnesia database and other table storage features

Erlang Programming provides exercises at the end of each chapter and simple examples throughout the book.

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
  2. Preface
    1. Francesco: Why Erlang?
    2. Simon: Why Erlang?
    3. Who Should Read This Book?
    4. How to Read This Book
    5. Conventions Used in This Book
    6. Using Code Examples
    7. Safari® Books Online
    8. How to Contact Us
    9. Acknowledgments
  3. 1. Introduction
    1. Why Should I Use Erlang?
    2. The History of Erlang
    3. Erlang’s Characteristics
      1. High-Level Constructs
      2. Concurrent Processes and Message Passing
      3. Scalable, Safe, and Efficient Concurrency
      4. Soft Real-Time Properties
      5. Robustness
      6. Distributed Computation
      7. Integration and Openness
    4. Erlang and Multicore
    5. Case Studies
      1. The AXD301 ATM Switch
      2. CouchDB
      3. Comparing Erlang to C++
    6. How Should I Use Erlang?
  4. 2. Basic Erlang
    1. Integers
    2. The Erlang Shell
    3. Floats
      1. Mathematical Operators
    4. Atoms
    5. Booleans
    6. Tuples
    7. Lists
      1. Characters and Strings
      2. Atoms and Strings
      3. Building and Processing Lists
      4. List Functions and Operations
    8. Term Comparison
    9. Variables
    10. Complex Data Structures
    11. Pattern Matching
    12. Functions
    13. Modules
      1. Compilation and the Erlang Virtual Machine
      2. Module Directives
    14. Exercises
      1. Exercise 2-1: The Shell
        1. A. Erlang expressions
        2. B. Assigning through pattern matching
        3. C. Recursive list definitions
        4. D. Flow of execution through pattern matching
        5. E. Extracting values in composite data types through pattern matching
      2. Exercise 2-2: Modules and Functions
      3. Exercise 2-3: Simple Pattern Matching
  5. 3. Sequential Erlang
    1. Conditional Evaluations
      1. The case Construct
      2. Variable Scope
      3. The if Construct
    2. Guards
    3. Built-in Functions
      1. Object Access and Examination
      2. Type Conversion
      3. Process Dictionary
      4. Meta Programming
      5. Process, Port, Distribution, and System Information
      6. Input and Output
    4. Recursion
      1. Tail-Recursive Functions
      2. Tail-Call Recursion Optimization
        1. Two accumulators example
      3. Iterations Versus Recursive Functions
    5. Runtime Errors
    6. Handling Errors
      1. Using try ... catch
      2. Using catch
    7. Library Modules
      1. Documentation
      2. Useful Modules
    8. The Debugger
    9. Exercises
      1. Exercise 3-1: Evaluating Expressions
      2. Exercise 3-2: Creating Lists
      3. Exercise 3-3: Side Effects
      4. Exercise 3-4: Database Handling Using Lists
      5. Exercise 3-5: Manipulating Lists
      6. Exercise 3-6: Sorting Lists
      7. Exercise 3-7: Using Library Modules
      8. Exercise 3-8: Evaluating and Compiling Expressions
      9. Exercise 3-9: Indexing
      10. Exercise 3-10: Text Processing
  6. 4. Concurrent Programming
    1. Creating Processes
    2. Message Passing
    3. Receiving Messages
      1. Selective and Nonselective Receives
      2. An Echo Example
    4. Registered Processes
    5. Timeouts
    6. Benchmarking
    7. Process Skeletons
    8. Tail Recursion and Memory Leaks
    9. A Case Study on Concurrency-Oriented Programming
    10. Race Conditions, Deadlocks, and Process Starvation
    11. The Process Manager
    12. Exercises
      1. Exercise 4-1: An Echo Server
      2. Exercise 4-2: The Process Ring
  7. 5. Process Design Patterns
    1. Client/Server Models
      1. A Client/Server Example
    2. A Process Pattern Example
    3. Finite State Machines
      1. An FSM Example
      2. A Mutex Semaphore
    4. Event Managers and Handlers
      1. A Generic Event Manager Example
      2. Event Handlers
    5. Exercises
      1. Exercise 5-1: A Database Server
      2. Exercise 5-2: Changing the Frequency Server
      3. Exercise 5-3: Swapping Handlers
      4. Exercise 5-4: Event Statistics
      5. Exercise 5-5: Phone FSM
  8. 6. Process Error Handling
    1. Process Links and Exit Signals
      1. Trapping Exits
      2. The monitor BIFs
      3. The exit BIFs
      4. BIFs and Terminology
      5. Propagation Semantics
    2. Robust Systems
      1. Monitoring Clients
      2. A Supervisor Example
    3. Exercises
      1. Exercise 6-1: The Linked Ping Pong Server
      2. Exercise 6-2: A Reliable Mutex Semaphore
      3. Exercise 6-3: A Supervisor Process
  9. 7. Records and Macros
    1. Records
      1. Introducing Records
      2. Working with Records
      3. Functions and Pattern Matching over Records
      4. Records in the Shell
      5. Record Implementation
      6. Record BIFs
    2. Macros
      1. Simple Macros
      2. Parameterized Macros
      3. Debugging and Macros
      4. Include Files
    3. Exercises
      1. Exercise 7-1: Extending Records
      2. Exercise 7-2: Record Guards
      3. Exercise 7-3: The db.erl Exercise Revisited
      4. Exercise 7-4: Records and Shapes
      5. Exercise 7-5: Binary Tree Records
      6. Exercise 7-6: Parameterized Macros
      7. Exercise 7-7: Counting Calls
      8. Exercise 7-8: Enumerated Types
      9. Exercise 7-9: Debugging the db.erl Exercise
  10. 8. Software Upgrade
    1. Upgrading Modules
    2. Behind the Scenes
      1. Loading Code
      2. The Code Server
        1. Loading modules
        2. Manipulating the code search path
      3. Purging Modules
    3. Upgrading Processes
    4. The .erlang File
    5. Exercise
      1. Exercise 8-1: Software Upgrade During Runtime
  11. 9. More Data Types and High-Level Constructs
    1. Functional Programming for Real
    2. Funs and Higher-Order Functions
      1. Functions As Arguments
      2. Writing Down Functions: fun Expressions
      3. Functions As Results
      4. Using Already Defined Functions
      5. Functions and Variables
      6. Predefined, Higher-Order Functions
      7. Lazy Evaluation and Lists
    3. List Comprehensions
      1. A First Example
      2. General List Comprehensions
      3. Multiple Generators
      4. Standard Functions
    4. Binaries and Serialization
      1. Binaries
      2. The Bit Syntax
        1. Sizes
        2. Types
      3. Pattern-Matching Bits
      4. Bitstring Comprehensions
      5. Bit Syntax Example: Decoding TCP Segments
      6. Bitwise Operators
      7. Serialization
    5. References
    6. Exercises
      1. Exercise 9-1: Higher-Order Functions
      2. Exercise 9-2: List Comprehensions
      3. Exercise 9-3: Zip Functions
      4. Exercise 9-4: Existing Higher-Order Functions
      5. Exercise 9-5: Length Specifications in List Comprehensions
      6. Exercise 9-6: Bitstrings
  12. 10. ETS and Dets Tables
    1. ETS Tables
      1. Implementations and Trade-offs
      2. Creating Tables
      3. Handling Table Elements
      4. Example: Building an Index, Act I
      5. Traversing Tables
      6. Example: Building an Index, Act II
      7. Extracting Table Information: match
      8. Extracting Table Information: select
      9. Other Operations on Tables
      10. Records and ETS Tables
      11. Visualizing Tables
    2. Dets Tables
    3. A Mobile Subscriber Database Example
      1. The Database Backend Operations
      2. The Database Server
    4. Exercises
      1. Exercise 10-1: Pretty-Printing
      2. Exercise 10-2: Indexing Revisited
      3. Exercise 10-3: ETS Tables for System Logging
  13. 11. Distributed Programming in Erlang
    1. Distributed Systems in Erlang
    2. Distributed Computing in Erlang: The Basics
      1. Node Names and Visibility
      2. Communication and Security
        1. Distributing the Erlang code: A warning
      3. Communication and Messages
      4. Node Connections
        1. Hidden nodes
      5. Remote Procedure Calls
      6. The rpc Module
      7. Essential Distributed Programming Modules
    3. The epmd Process
      1. Distributed Erlang Behind Firewalls
    4. Exercises
      1. Exercise 11-1: Distributed Associative Store
      2. Exercise 11-2: System Monitoring
  14. 12. OTP Behaviors
    1. Introduction to OTP Behaviors
    2. Generic Servers
      1. Starting Your Server
      2. Passing Messages
      3. Stopping the Server
      4. The Example in Full
      5. Running gen_server
    3. Supervisors
      1. Supervisor Specifications
      2. Child Specifications
      3. Supervisor Example
      4. Dynamic Children
    4. Applications
      1. Directory Structure
      2. The Application Resource File
      3. Starting and Stopping Applications
      4. The Application Monitor
    5. Release Handling
    6. Other Behaviors and Further Reading
    7. Exercises
      1. Exercise 12-1: Database Server Revisited
      2. Exercise 12-2: Supervising the Database Server
      3. Exercise 12-3: The Database Server As an Application
  15. 13. Introducing Mnesia
    1. When to Use Mnesia
    2. Configuring Mnesia
      1. Setting Up the Schema
      2. Starting Mnesia
      3. Mnesia Tables
    3. Transactions
      1. Writing
      2. Reading and Deleting
      3. Indexing
      4. Dirty Operations
    4. Partitioned Networks
    5. Further Reading
    6. Exercises
      1. Exercise 13-1: Setting Up Mnesia
      2. Exercise 13-2: Transactions
      3. Exercise 13-3: Dirty Mnesia Operations
  16. 14. GUI Programming with wxErlang
    1. wxWidgets
    2. wxErlang: An Erlang Binding for wxWidgets
      1. Objects and Types
      2. Event Handling, Object Identifiers, and Event Types
      3. Putting It All Together
    3. A First Example: MicroBlog
    4. The MiniBlog Example
    5. Obtaining and Running wxErlang
    6. Exercises
      1. Exercise 14-1: Selecting the Blog File
      2. Exercise 14-2: Saving Blog Items Separately
      3. Exercise 14-3: Multiple Blogs in Separate Tabs
      4. Exercise 14-4: Extending the Entries—Rich Text
      5. Exercise 14-5: Tagging Entries
      6. Exercise 14-6: Multiple Users and Comments
      7. Exercise 14-7: Layout and wxErlang Sizers
  17. 15. Socket Programming
    1. User Datagram Protocol
    2. Transmission Control Protocol
      1. A TCP Example
    3. The inet Module
    4. Further Reading
    5. Exercises
      1. Exercise 15-1: Snooping an HTTP Request
      2. Exercise 15-2: A Simple HTTP Proxy
      3. Exercise 15-3: Peer to Peer
  18. 16. Interfacing Erlang with Other Programming Languages
    1. An Overview of Interworking
    2. Interworking with Java
      1. Nodes and Mailboxes
      2. Representing Erlang Types
      3. Communication
      4. Putting It Together: RPC Revisited
      5. Interaction
      6. The Small Print
      7. Taking It Further
    3. C Nodes
      1. Going Further
    4. Erlang from the Unix Shell: erl_call
    5. Port Programs
      1. Erlang Port Commands
      2. Communicating Data to and from a Port
    6. Library Support for Communication
      1. Working in Ruby: erlectricity
        1. An example using erlectricity
    7. Linked-in Drivers and the FFI
    8. Exercises
      1. Exercise 16-1: C Factorial via a Port
      2. Exercise 16-2: Factorial Server in Another Language
  19. 17. Trace BIFs, the dbg Tracer, and Match Specifications
    1. Introduction
    2. The Trace BIFs
      1. Process Trace Flags
      2. Inheritance Flags
      3. Garbage Collection and Timestamps
    3. Tracing Calls with the trace_pattern BIF
    4. The dbg Tracer
      1. Getting Started with dbg
      2. Tracing and Profiling Functions
      3. Tracing Local and Global Function Calls
      4. Distributed Environments
      5. Redirecting the Output
        1. Redirecting to sockets and binary files
    5. Match Specifications: The fun Syntax
      1. Generating Specifications Using fun2ms
        1. Odds and ends with fun2ms
      2. Difference Between ets and dbg Match Specifications
    6. Match Specifications: The Nuts and Bolts
      1. The Head
      2. Conditions
      3. The Specification Body
      4. Saving Match Specifications
    7. Further Reading
    8. Exercises
      1. Exercise 17-1: Measuring Garbage Collection Times
      2. Exercise 17-2: Garbage Collection Using dbg
      3. Exercise 17-3: Tracing ETS Table Entries
      4. Exercise 17-4: Who Is the Culprit?
  20. 18. Types and Documentation
    1. Types in Erlang
      1. An Example: Records with Typed Fields
      2. Erlang Type Notation
    2. TypEr: Success Types and Type Inference
      1. Dialyzer: A DIscrepancy AnaLYZer for ERlang Programs
    3. Documentation with EDoc
      1. Documenting usr_db.erl
        1. Module tags
        2. Function tags
        3. Generic tags
      2. Running EDoc
        1. Module pages
        2. Overview page
      3. Types in EDoc
      4. Going Further with EDoc
    4. Exercises
  21. 19. EUnit and Test-Driven Development
    1. Test-Driven Development
    2. EUnit
      1. How to Use EUnit
      2. Functional Testing, an Example: Tree Serialization
    3. The EUnit Infrastructure
      1. Assert Macros
      2. Test-Generating Functions
      3. EUnit Test Representation
    4. Testing State-Based Systems
      1. Fixtures: Setup and Cleanup
    5. Testing Concurrent Programs in Erlang
    6. Exercises
      1. Exercise 19-1: Testing Sequential Functions
      2. Exercise 19-2: Testing Concurrent Systems
      3. Exercise 19-3: Software Upgrade
      4. Exercise 19-4: Testing OTP Behaviors
      5. Exercise 19-5: Devising Tests for OTP Behaviors
  22. 20. Style and Efficiency
    1. Applications and Modules
      1. Libraries
      2. Dirty Code
      3. Interfaces
      4. Return Values
      5. Internal Data Structures
    2. Processes and Concurrency
    3. Stylistic Conventions
    4. Coding Strategies
    5. Efficiency
      1. Sequential Programming
      2. Lists
      3. Tail Recursion and Non-tail Recursion
      4. Concurrency
    6. And Finally...
  23. A. Using Erlang
    1. Getting Started with Erlang
      1. Installing the System
      2. Running the Erlang Shell
    2. Tools for Erlang
      1. Editors
      2. Other Tools
    3. Where to Learn More
  24. Index
  25. About the Authors
  26. Colophon
  27. Copyright

Product information

  • Title: Erlang Programming
  • Author(s): Francesco Cesarini, Simon Thompson
  • Release date: June 2009
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596518189