Skip to content

Commit 16b0d80

Browse files
committed
Handling lists
1 parent 866624b commit 16b0d80

File tree

1 file changed

+22
-0
lines changed
  • elixir/learn-fp-with-elixir/03.using-pattern-matching-to-control-the-program-flow

1 file changed

+22
-0
lines changed

elixir/learn-fp-with-elixir/03.using-pattern-matching-to-control-the-program-flow/02.destructuring.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,25 @@ IO.inspect credentials
77
IO.inspect a
88
IO.inspect b
99
IO.inspect c
10+
11+
# lists
12+
[a, b, c] = [1, 2, 3]
13+
IO.inspect a
14+
IO.inspect b
15+
IO.inspect c
16+
17+
[_, x, _] = [1, 2, 3] # this _ operator is called wild card
18+
IO.inspect x
19+
20+
[head | rest] = [1, 2, 3, 4, 5]
21+
IO.inspect head # 1
22+
IO.inspect rest # [2, 3, 4, 5]
23+
24+
[first, second | rest] = [1, 2, 3, 4, 5]
25+
IO.inspect first # 1
26+
IO.inspect second # 2
27+
IO.inspect rest # [2, 3, 4, 5]
28+
29+
[head | rest] = [:a]
30+
IO.inspect head # :a
31+
IO.inspect rest # []

0 commit comments

Comments
 (0)