Editing
Pattern Matching in Functions
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Pattern Matching in Functions == 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 === 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 === 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 === 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 === 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 === 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.
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information