Split and filter a list: full examples and further reading
The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.
Use `[Head | Tail]`, then keep passing scores with a list comprehension.
Split one list into head and tail, then run [Score || Score <- Scores, Score >= 60].
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}.Head is peach.
Tail is [plum, apricot].
Passed is [60, 82, 100], while Scores keeps all five values.
Steps for the full example
[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.
Original lesson notes
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.
Symbols and terms
[ ]- 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.
- 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.
Original full exercise
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}.The result should be {[55, 60, 72, 49], [60, 72]}.
Show hint
Use Score in all three blanks before the limit, then use 60 as the limit.
Show original answer
%% Build Passed without changing Scores
Scores = [55, 60, 72, 49].
Passed = [Score || Score <- Scores, Score >= 60].
{Scores, Passed}.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].
Original lesson takeaways
[Head | Tail]splits a non-empty list.Score <- Scoresdraws each score from the source list.- A list comprehension can filter values while leaving the source list unchanged.