Using assigned value later in same pattern match
I have an array of character lists.
['apple', 'aardvark']
And I want to make sure that each of these two items has the same beginning letter.
I can do this with pattern matching because variables can be reused in an assignment.
iex > [[c | _], [c | _]] = ['apple', 'aardvark']
['apple', 'aardvark']
iex > c
97
Here's what happens when the first letter does NOT match.
iex > [[c | _], [c | _]] = ['apple', 'hamster']
** (MatchError) no match of right hand side value: ['apple', 'hamster']
Tweet