Modules Are Where We Store Code

Modules are the basic units of code in Erlang. Modules are contained in files with erl extensions and must be compiled before the code in the modules can be run. Compiled modules have the extension beam.

Before we write our first module, we’ll remind ourselves about pattern matching. All we’re going to do is create a couple of data structures representing a rectangle and a square. Then we’re going to unpack these data structures and extract the sides from the rectangle and the square. Here’s how:

 
1>​ Rectangle = {rectangle, 10, 5}.
 
{rectangle, 10, 5}.
 
2>​ Square = {square, 3}.
 
{square, 3}
 
3>​ {rectangle, Width, Height} = Rectangle.
 
{rectangle,10,5}
 
4>​ Width.
 
10
 
5>​ Height.
 
5
 
6>​ {square, Side} ...

Get Programming Erlang, 2nd Edition 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.