leifericf

leifericf

First encounter with variable rebinding in pattern matching

As I’m currently in the early stages of learning Elixir, having learned Erlang first, I’m encountering some curiosities and confusions. I thought I’d share them. Here is the first such topic.

This is explained in more detail on pages 15-19 in Programming Elixir.

In Elixir, we can do this:

Interactive Elixir (1.6.5) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> a = 1
1
iex(2)> a = 2
2

If we try the same thing in Erlang, this happens:

Eshell V8.3  (abort with ^G)
1> A = 1.
1
2> A = 2.
** exception error: no match of right hand side value 2

When I first encountered this behaviour in Elixir, my immediate thought was something like: “Huh! That’s odd. I thought variables were inmmutable in Elixir, as in Erlang.”

This was a bit confusing for me, for two reasons:

  • The behavior in the REPL differed from Erlang.
  • This feature of Elixir gives the immediate impression of mutable variables.

Then I noticed that variables in Elixir bind once per match. Is this also the case for Erlang outside of the REPL? Now I’m thinking of matches sort of like their own local scopes.

Back in Elixir, we can also use the pin operator:

Interactive Elixir (1.6.5) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> a = 1
1
iex(2)> [^a, 2, 3] = [1, 2, 3]
[1, 2, 3]
iex(3)> a = 2
2
iex(4)> [^a, 2] = [1, 2]
** (MatchError) no match of right hand side value: [1, 2]

The pin operator, I think of as “extending the scope” out of the current match; sort of like lexical scoping, where the innermost scope (the current match) is searched first and ^ moves us to the outer scope.

I’m not sure if this is the correct way of thinking about it, though.

Most Liked

peerreynders

peerreynders

To me, Elixir allows rebinding (i.e. the values are immutable) while Erlang does not. Pinning is necessary to suppress the default rebind behaviour.

See also:

Where Next?

Popular in Discussions Top

pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19204 150
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
jsonify
So, is Heroku the only free option for hosting Phoenix/Elixir at this point? I’m not ready to commit to paying monthly and was wondering ...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement