Erlang basics · Lesson 05 (open contents)
Split and filter a list
Use `[Head | Tail]`, then keep passing scores with a list comprehension.
Split one list and filter another
The match reads an existing list. The comprehension builds a new list without changing Scores.
%% 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}.Headispeach.Tailis[plum, apricot].Passedis[60, 82, 100], whileScoreskeeps all five values.
Read from the first line down
[Head | Tail] = Fruitsworks only when the list has at least one item.Taildoes not mean “the last item.” It means “the whole list after the first item.”Score <- Scoresdraws one score at a time.Score >= 60decides whether that score stays.- The expression before
||is collected intoPassed. The originalScoreslist is unchanged.
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.
Match each name to its meaning
Head
The first item in a non-empty list.
Tail
The list left after removing the first item. It is still a list.
empty list
[] contains no items. It is also the stopping point when a recursive function processes a list.
list comprehension
An expression that draws values from a list, optionally filters them, and collects a new list.
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.
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.
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
%% 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].
Remember these three lines
- 1
[Head | Tail]splits a non-empty list. - 2
Score <- Scoresdraws each score from the source list. - 3
A list comprehension can filter values while leaving the source list unchanged.