I am working through the Elixir getting started documentation and have come across something that I don't understand the output. In the example below, I don't understand why rest
ends up being "llo" or for that fact how the first result, results to "hello" and not "hehello".
iex> "he" <> rest = "hello"
"hello"
iex> rest
"llo"
This is a result of the way Elixir's =
works. This doesn't evaluate to:
"he";
rest = "hello";
No, instead it does pattern matching. From the docs:
In this chapter, we will show how the = operator in Elixir is actually a match operator and how to use it to pattern match inside data structures.
(Emphasis mine.) The chapter I link to explains how =
sign is treated differently in Elixir compared to other languages and explains the somewhat strange behavior you are witnessing.
The =
sign must have both sides be equal. Already looking at the right side we have a string with the value "hello". Therefore, the left side must be a string and also have the value "hello". The variable on the left side is rest. "he" + rest = "hello"
. There is one unique solution to this equation -> "he" + "llo" = "hello"
. Finally, we can conclude that rest = "llo"
.
Although @Dair's answer is good, I'll add this for whatever it's worth.
It's helpful to think of Elixir's "=" as a bit more than assignment. It's more like assignment and assertion combined. That is if the statement can be made true via the assignment then the assignment happens. Otherwise an error is generated.
For example, consider this:
a = 1
Looks like I'm assigning 1 to a right? But then consider this:
1 = a
How can I assign to a constant value? I can't. But I can assert that they're equal and that's part of what's going on. The pattern match operator is trying to assert equality and if the two sides of the operator can be made equal via assignment then assignment occurs. Hence when you compare:
"he" <> rest = "hello"
The assertion can be made true by assigning "llo" to rest and so it is.
I hope that adds a little clarity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With