Elixir Pattern match a list - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Thursday, September 28, 2017

Elixir Pattern match a list

 A list can be pattern matched

Program:

[head|tail] = [1, 2, 3]
head = 1
tail = [2, 3]

[head|tail] = [1]
head = 1
tail = []

[] = []

# This does not match, no value for head
[head|tail] = []

# match head value
[1 | tail ]= [1, 2, 3]
tail = [2, 3]

# use underscore to ignore a variable
[head | _ ]= [1, 2, 3]

No comments:

Post a Comment