Re: New EEP draft: Pinning operator ^ in patterns

Stefan Reichör <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <[email protected]>
Loïc Hoguin <[email protected]> writes:

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

I would love to see it 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.

Thanks for the explaination. Now I understand the meaning of this code.

You use the bound variable and the following match like an assertion.

> 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!

I hope that I will get into the language soon. I am impressed by the
BEAM, especially its capabilities to run processes in parallel.

However I am used to do a lot of programming in Python. I have a very
efficient development process with Python.

I am not sure how I will get to a similar efficiency in Erlang.
Perhaps I will get back to this list with some questions one day.



Stefan.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.