Chapter 3. Sequential Erlang
Erlang’s design was heavily influenced by functional and logic programming languages. When dealing with
sequential programs, those familiar with languages such as Prolog, ML, or
Haskell will recognize the influence they have had on Erlang’s constructs
and development techniques. When working in functional programming
languages, you replace iterative constructs such as while
and for
loops with recursive programming techniques.
Recursion is the most useful and powerful of all the techniques in a functional programmer’s armory. It allows a programmer to traverse a data structure via successive calls to the same function, with the patterns of function calls mirroring the structure of the data itself. The resulting programs are more compact and easier to understand and maintain. Functional programs are, importantly, side-effect-free, unless side effects are specifically needed for printing or for access to external storage.
You control recursion using various conditional constructs that enhance the expressive power of pattern matching; in the example of data structure traversal, different patterns correspond to different traversals: bottom-up, top-down, breadth-first, and so forth.
This chapter also introduces other features directly related to sequential programming. The absence of a strong type system and the flexibility and dynamic nature of some of Erlang’s constructs result in runtime errors that, although rare, have to be handled. Through exception-handling ...
Get Erlang Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.