Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the value rest contain the value "llo"

Tags:

elixir

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"
like image 455
Brettski Avatar asked Oct 16 '15 02:10

Brettski


2 Answers

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".

like image 118
Dair Avatar answered Oct 13 '22 05:10

Dair


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.

like image 22
Onorio Catenacci Avatar answered Oct 13 '22 04:10

Onorio Catenacci