Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errlang: no match of right hand side value

Tags:

erlang

I'm getting started with Erlang, but I'm already in troubles. I copied this example from a book:

-module(echo).
-export([start/0, loop/0]).

start() ->
    spawn(echo, loop, []).

loop() ->
    receive
        {From, Message} ->
            From ! Message,
            loop()
    end.

But when I try it I get an error I don't understand:

31> c(echo).          
{ok,echo}
32> f.      
f
33> Pid = echo:start().
** exception error: no match of right hand side value <0.119.0>

Why does this happen?

like image 239
Federico Razzoli Avatar asked Mar 09 '14 10:03

Federico Razzoli


People also ask

Why is the error report more friendly than the simple bad match?

the error report is more friendly that the simple bad match, it gives also the value that fails to match. If this value is "small" enough, it is fully displayed and it is often enough to understand what is going wrong:

How to pattern-match against an already bound variable?

Replace the left-hand side with a fresh variable. Here you may figure out that you are trying to pattern-match against an already bound variable... Run the program again to print the value of this variable and understand why it doesn't match the given pattern

What is a badmatch error in Python 2?

2. Badmatch errors happen whenever pattern matching fails. This most likely means you're trying to do impossible pattern matches (such as above), trying to bind a variable for the second time, or just anything that isn't equal on both sides of the = operator (which is pretty much what makes rebinding a variable fail!).

Should I use catch-all patterns in Erlang?

In practice, you should be careful when using the catch-all patterns: try to protect your code from what you can handle, but not any more than that. Erlang has other facilities in place to take care of the rest.


1 Answers

Probably, 'Pid' has some value assigned already and you're trying to re-assign it.

Here is how it behaves on my machine:

Eshell V5.9.1  (abort with ^G)
1> c(echo).
{ok,echo}
2> f.
f
3> Pid = echo:start().
<0.39.0>
4> Pid = echo:start().
** exception error: no match of right hand side value <0.41.0>
5>

As you can see, the first 'Pid = ' construction woks fine, but the second one throws error message you described.

I think, you used Pid in the shell before already and it has some value assigned.

Try to 'reset' Pid variable and use it like following:

8> f(Pid).
ok
9> Pid.
* 1: variable 'Pid' is unbound
10> Pid = echo:start().
<0.49.0>

Or you can forget all variables by using such a construction:

13> f().
ok
14> Pid = echo:start().
<0.54.0>

Pay attention on used f(). - not just f.

like image 56
fycth Avatar answered Oct 19 '22 04:10

fycth