Creating a function that declares a parameter twice in order to check if two different passed-in parameters are identical is not something I would have thought of.
Drives home the point that pattern matching within a function is the same pattern matching used to select the function to run.
Notes
All values on the left-hand side of a pattern match are unbound before the match is made.
Avoid this by using the pin (
^
) operator.
Exercises
Write a function that takes a two-element tuple parameter and uses pattern matching to return a two-element tuple with the elements swapped.
defmodule Patterns do def swap({a, b}) do {b, a} end end Patterns.swap({1, 2}) {2, 1}
Write a function that takes two parameters and returns
true
if they are the same. Use pattern matching and not conditional logic. All notes and comments are my own opinion. Follow me at @rgacote@genserver.social