Erlang basics · Lesson 05 (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 0525 minutes

Split and filter a list

Use `[Head | Tail]`, then keep passing scores with a list comprehension.

01 · Start with the whole example

Split one list and filter another

The match reads an existing list. The comprehension builds a new list without changing Scores.

Erlang
%% Split the first item from the rest
Fruits = [peach, plum, apricot].
[Head | Tail] = Fruits.

%% Draw every score, then keep scores at or above 60
Scores = [45, 60, 82, 59, 100].
Passed = [Score || Score <- Scores, Score >= 60].

{Head, Tail, Scores, Passed}.
Check the result first
  • Head is peach.
  • Tail is [plum, apricot].
  • Passed is [60, 82, 100], while Scores keeps all five values.
02 · Take the code apart

Read from the first line down

  1. [Head | Tail] = Fruits works only when the list has at least one item.
  2. Tail does not mean “the last item.” It means “the whole list after the first item.”
  3. Score <- Scores draws one score at a time. Score >= 60 decides whether that score stays.
  4. The expression before || is collected into Passed. The original Scores list is unchanged.
03 · Meet the new symbols

Symbols are not secret signs

[ ]

Wrap a list.

|

Separate the head and tail in a list pattern.

||

Separate the value to collect from the generators and filters in a list comprehension.

<-

Draw each value from the list on the right.

04 · Ideas inside the code

Match each name to its meaning

01

Head

The first item in a non-empty list.

02

Tail

The list left after removing the first item. It is still a list.

03

empty list

[] contains no items. It is also the stopping point when a recursive function processes a list.

04

list comprehension

An expression that draws values from a list, optionally filters them, and collects a new list.

05 · Make the idea clear

Why these forms are useful

A list is an ordered collection. In [peach, plum, apricot], the first item is the head. The remaining [plum, apricot] is the tail.

Erlang writes this as [Head | Tail]. The bar does not mean “or.” It separates the head from the tail.

The empty list [] has no head. Matching [Head | Tail] against an empty list fails.

A list comprehension builds a new list. In [Score || Score <- Scores, Score >= 60], <- draws each score and the final condition keeps only passing scores. Scores itself does not change.

06 · Change it yourself

Close the answer and try

Use a list comprehension to keep scores of 60 or higher from [55, 60, 72, 49]. Return the old and new lists.

Practice starting point
Scores = [55, 60, 72, 49].
Passed = [____ || ____ <- Scores, ____ >= ____].

{Scores, Passed}.

Target result: The result should be {[55, 60, 72, 49], [60, 72]}.

Stuck? Read one hint

Use Score in all three blanks before the limit, then use 60 as the limit.

After you run it, see one answer
One answer
%% Build Passed without changing Scores
Scores = [55, 60, 72, 49].
Passed = [Score || Score <- Scores, Score >= 60].

{Scores, Passed}.
Think about it: After building `Passed`, did the list bound to `Scores` lose the failing scores?

No. A list comprehension returns a new list. Scores remains [55, 60, 72, 49].

Take with you

Remember these three lines

  1. 1

    [Head | Tail] splits a non-empty list.

  2. 2

    Score <- Scores draws each score from the source list.

  3. 3

    A list comprehension can filter values while leaving the source list unchanged.

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