Pattern matching and guard expressions are fundamental to writing recursive function definitions in Elixir. Sometimes guard clauses and pattern matching can be used for the same purpose. For example:
# pattern matching
defmodule Exponent do
def power(value, 0), do: 1
def power(value, n), do: value * power(value, n - 1)
end
# guard expression
defmodule Exponent do
def power(value, n) when n == 0, do: 1
def power(value, n), do: value * power(value, n - 1)
end
In both cases above, we only want the first power
function to run when the second argument is equal to 0
.
When it is as simple as equality, I tend to use the pattern matching syntax. I typically leave the guard for more complex
logic like when rem(x, divisor) == 0
. However, to check whether one argument is equal to another I thought a guard was neccessary: when a == b
.
But, today I learned, this can also be handled with pattern matching, like so:
# guard
def equality(a, b) when a == b, do: IO.puts "equal"
def equality(a, b), do: IO.puts "not equal"
# pattern matching
def equality(a, a), do: IO.puts "equal"
def equality(a, b), do: IO.puts "not equal"
Tada! There you have it. It seems so simple I don’t know how I hadn’t tried it earlier!