New EEP draft: Pinning operator ^ in patterns
Loïc Hoguin
essen@REDACTED
Mon Jan 18 12:19:07 CET 2021
On 18/01/2021 11:53, Stefan Reichör wrote:
> Hallo Loïc,
>
> I am a newcomer to Erlang. I am subscribed to this list since a few
> days. I am really positively surprised how active the Erlang community is.
The list is usually not that active.
>> Being able to write "A = 5, A = inc(4)." is what makes the Erlang language the best I've used in my opinion.
>
> Could you please explain the semantics of this statement?
> It is not obvious for me as a beginner.
>
> Why is such a statement so useful for you?
"A = 5,"
means the variable A becomes bound and has the value 5.
Followed by
"A = inc(4),"
where it is implied that "inc(X) -> X + 1."
Because A is already bound, this becomes a match. The match will only
succeed if the value returned by the inc/1 function is the same as the
value already bound to A.
If the inc/1 function had a bug and somehow returned the wrong value,
the match will fail.
It's not so much about usefulness and more about a thought process. All
instances of A must have the same value as the first time it was
"matched" otherwise the program doesn't work. In my mind, there is no
real distinction between binding and matching, it's all just matching.
The first match just happens to dictate what the value must be
subsequently. Having to annotate explicitly the matching of bound
variables would be a pretty big change to that thought process, so the
rewards would have to be really worth it.
It's similar to the introduction of maps in the language except maps
greatly simplified many things we routinely do. The rewards were far
greater than the costs of adapting the thought process.
Have fun with Erlang, it's the best!
--
Loïc Hoguin
https://ninenines.eu
More information about the erlang-questions
mailing list