Pattern Matching in Functions

From Elixir Wiki
Jump to navigation Jump to search

Pattern Matching in Functions[edit]

Pattern matching is a fundamental feature in the Elixir programming language. It allows developers to match specific patterns in function arguments, making code more readable, concise, and powerful. This article will explore the concept of pattern matching in functions and how it can be used effectively in Elixir.

Basic Pattern Matching[edit]

In Elixir, pattern matching is used to define function clauses with different argument patterns. When a function is called, Elixir tries to match the given arguments with the defined patterns. If a match is found, the corresponding function clause is executed. Let's look at a simple example:

```elixir defmodule Math do

 def add(a, b) do
   a + b
 end
 
 def add([a, b, c]) do
   a + b + c
 end

end ```

In the code above, two function clauses are defined for the `add` function. The first clause matches two arguments and performs their addition, while the second clause matches a list of three elements and adds them together. When calling the `add` function, Elixir will automatically select the appropriate clause based on the given arguments.

Pattern Matching with Guards[edit]

In addition to simple pattern matching, Elixir also supports pattern matching with guards. Guards are expressions that must evaluate to true for a function clause to be selected. They allow for more complex pattern matching conditions. Let's see an example:

```elixir defmodule Math do

 def divide(a, b) when b != 0 do
   a / b
 end
 
 def divide(a, 0) do
   {:error, "Division by zero"}
 end

end ```

In the code above, the first clause of the `divide` function matches any two arguments, but it is only selected if the guard condition `b != 0` is true. This prevents division by zero, ensuring the function is safe to use.

Pattern Matching in Function Head[edit]

Pattern matching is not limited to function arguments alone. Elixir also allows pattern matching in the head of a function definition. This can be useful for extracting values from complex data structures. Here's an example:

```elixir defmodule Sample do

 def get_first_name(%{name: %{first: first_name}}) do
   first_name
 end
 
 def get_first_name(_) do
   nil
 end

end ```

In the code above, the `get_first_name` function accepts a map as an argument. It extracts the value of the `first` key nested inside the `name` key and returns it. If the argument does not match the expected pattern, the second clause with the wildcard `_` is selected, returning `nil`.

Aspects of Pattern Matching[edit]

Pattern matching in Elixir supports various aspects that give developers more flexibility. Some of these aspects include:

  • Binding variables in patterns - Variables can be bound within a pattern and used within the corresponding clause.
  • Ignoring variables - Variables that are not used in a pattern can be ignored using the `_` wildcard.
  • Pin operator (`^`) - The pin operator allows developers to enforce that a variable should match its current value, instead of being rebound within the pattern.

For further information on these aspects, refer to the [Variables and Pattern Matching](link_to_variables_and_pattern_matching) article.

Conclusion[edit]

Pattern matching in functions is a powerful feature of the Elixir programming language. It enables developers to write concise and readable code by matching patterns in function arguments. By using guards, pattern matching can become more expressive and allow for complex conditions. With the ability to match patterns in function heads, developers can easily extract values from data structures. Elixir's pattern matching supports various aspects that provide flexibility and control. Understanding and utilizing pattern matching effectively will enhance your Elixir programming skills.

See Also[edit]

  • [Elixir Programming Overview](link_to_elixir_programming_overview)
  • [Variables and Pattern Matching](link_to_variables_and_pattern_matching)