← Back to lesson 05: Split and filter a list

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.

Original full example
%% 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

  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.

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.

Exercise starter
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
Reference 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
Design trade-offs and language comparisons
Original course references