Erlang basics · Lesson 03 (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 · SCRATCH · LESSON 0325 minutes

Tell two kinds of text apart

Understand charlists, binaries, and UTF-8 text.

01 · Start with the whole example

Text can look alike while having a different shape

Here =:= performs a strict equality comparison.

Erlang
%% Double quotes make a charlist by default
"cat" =:= [99, 97, 116].

%% Double angle brackets make a binary
is_binary(<<"cat">>).

%% A non-ASCII binary states its UTF-8 encoding
byte_size(<<"café"/utf8>>).
Check the result first
  • The charlist comparison returns true.
  • is_binary(<<"cat">>) returns true.
  • byte_size(<<"café"/utf8>>) returns 5: five bytes, not four written characters.
02 · Take the code apart

Read from the first line down

  1. "cat" looks like text in the shell, but it equals the integer list [99, 97, 116].
  2. <<"cat">> is a binary. It is not equal to the charlist "cat".
  3. byte_size(<<"café"/utf8>>) counts bytes. The letter é takes two UTF-8 bytes.
03 · Meet the new symbols

Symbols are not secret signs

"cat"

A charlist by default, which means a list of integers.

<<"cat">>

The double angle brackets wrap a binary.

/utf8

Encode the text before it as UTF-8 inside the binary.

=:=

Strictly compare whether the values on both sides are the same.

04 · Ideas inside the code

Match each name to its meaning

01

charlist

A list of character numbers. "cat" and [99, 97, 116] are the same value.

02

binary

A sequence of bytes written as <<...>>. Binaries are common for text, files, and network data.

03

UTF-8

An encoding rule that turns Unicode text into bytes.

05 · Make the idea clear

Why these forms are useful

Erlang has no separate string type. "cat" in double quotes is a list of character numbers by default. This is called a charlist.

<<"cat">> is a binary. Many network protocols, files, and modern Erlang interfaces use binaries for text.

For a non-ASCII character inside a binary, add /utf8, as in <<"café"/utf8>>. This tells Erlang exactly how to encode the text.

06 · Change it yourself

Close the answer and try

Write café as a binary, then check it with is_binary(...).

Practice starting point
%% Give the UTF-8 binary directly to the check
is_binary(<<____/utf8>>).

Target result: The result should be true.

Stuck? Read one hint

Write the text in double quotes inside the blank.

After you run it, see one answer
One answer
%% /utf8 states how to encode the non-ASCII letter
is_binary(<<"café"/utf8>>).
Think about it: Are `"cat"` and `<<"cat">>` the same value?

No. The first is a charlist by default. The second is a binary. They can show similar text but have different types.

Take with you

Remember these three lines

  1. 1

    Erlang has no separate string type.

  2. 2

    Double quotes make a charlist by default. <<>> makes a binary.

  3. 3

    Use /utf8 when a binary literal contains non-ASCII text.

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