concept pattern in category elixir

appears as: pattern, pattern, patterns, The pattern, patterns
Elixir in Action

This is an excerpt from Manning's book Elixir in Action.

First you sort the list, and then, with the pattern [min | _], you take only the head of the (sorted) list. Note that this could also be done with the hd function mentioned in chapter 2. In fact, for this case, hd would be more elegant. The pattern [head | _] is more useful when pattern-matching function arguments, as you’ll see in section 3.2.

Elixir in Action

This is an excerpt from Manning's book Elixir in Action.

First you sort the list, and then, with the pattern [min | _], you take only the head of the (sorted) list. Note that this could also be done with the hd function mentioned in chapter 2. In fact, for this case, hd would be more elegant. The pattern [head | _] is more useful when pattern-matching function arguments, as you’ll see in section 3.2.

Using ^expected_name in patterns says that you expect the value of the variable expected_name to be in the appropriate position in the right-side term. In this example, it would be the same as if you used the hard-coded pattern {"Bob", _}. Therefore, the first match succeeds, but the second one fails.

Notice that the pin operator doesn’t bind the variable. You expect that the variable is already bound—in other words, that it has a value—and you try to match against that value. This technique is used less often and is mostly relevant when you need to construct the pattern at runtime.