Erlang basics · Lesson 16 (open contents)
01 · Open erl02 · Meet common terms03 · Tell two kinds of text apart04 · Bind a variable once05 · Split and filter a list06 · Parse a number safely07 · Choose one path with case08 · Let a function keep going09 · Put code in a module10 · Compare terms and Boolean results11 · Read and update nested maps12 · Build and inspect UTF-8 binaries13 · Let clauses choose the function path14 · Solve a list twice15 · Choose with patterns, guards, and errors16 · Describe a module and one record17 · Put one tested module in Rebar318 · Return success or failure as data19 · Read and write one file safely20 · Build a data pipeline with functions21 · Keep records behind a module boundary22 · Define a module contract23 · Describe the data contract24 · Test small rules and whole flows25 · Shape builds and make a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ERLANG · FOUNDATION · LESSON 1625 minutes

Describe a module and one record

Use module attributes, exports, records, and record patterns.

01 · Start with the whole example

Run this first

Run it once, change one input, and compare the new result.

Erlang
%% Save in book.erl and compile it
-module(book).
-export([long/1]).
-record(book, {title, pages = 0}).
long(#book{pages = Pages}) -> Pages >= 300.
Check the result first
  • Inside the module, long(#book{title = <<"River">>, pages = 320}) returns true.
02 · Take the code apart

Read from the first line down

  1. Read -record(book, {...}).: Declare record fields and defaults.
  2. Read #book{pages = Pages}: Match a book record and extract one field.
  3. Read -export([long/1]).: Make long/1 callable from other modules.
03 · Meet the new symbols

Symbols are not secret signs

-record(book, {...}).

Declare record fields and defaults.

#book{pages = Pages}

Match a book record and extract one field.

-export([long/1]).

Make long/1 callable from other modules.

04 · Ideas inside the code

Match each name to its meaning

01

module attribute

Forms such as -module, -export, and -doc describe compiled code.

02

record

A record gives tuple positions field names at compile time; its runtime value is still a tuple.

03

include file

Shared record definitions commonly live in an .hrl file included at compile time.

05 · Make the idea clear

Why these forms are useful

Run the example first. Predict one result, then change one input and run it again.

Forms such as -module, -export, and -doc describe compiled code.

A record gives tuple positions field names at compile time; its runtime value is still a tuple.

06 · Change it yourself

Close the answer and try

Add an author field with the default unknown.

Practice starting point
-record(book, {title, pages = 0, ____ = ____}).

Target result: Declare author = unknown.

Stuck? Read one hint

Record defaults are ordinary Erlang terms.

After you run it, see one answer
One answer
-record(book, {title, pages = 0, author = unknown}).
Think about it: Is a record a tagged map at runtime?

No. Records compile to tuples, so keep them behind module boundaries.

Take with you

Remember these three lines

  1. 1

    Attributes describe compiled modules.

  2. 2

    Records name tuple positions at compile time.

  3. 3

    Do not expose record layout as a public cross-language contract.

Lesson completeRun the code once, then mark this lesson as done.