I have started learning Erlang recently and came across the following error while trying to pattren match
The following expression is working fine:
{A,_,[B|_],{B}}={abc,23,[22,x],{22}}.
Resulting in
A = abc
B = 22
The following expression is not working:
{A,_,[_|B],{B}}={abc,23,[22,x],{x}}.
Is resulting in
** exception error: no match of right hand side value {abc,23,[22,x],{x}}
However if I replace the ',' in [22 , x with a | like the following its working find and bounding x to B
{A,_,[_|B],{B}}={abc,23,[22|x],{x}}.
{abc,23,[22|x],{x}}
B.
x
Any explanation about this would highly appreciated.
Many thanks in advance
_can consume/match anything, including the entire element of the match. That means "Anything at all, followed byWhatever-- which we don't define here".[22|Whatever]or any[FiniteThing|_]means "something concrete and limited, followed byWhatever", which can be matched because it can match and consume the first part of the list exactly. IOW, "anything" matches/consumes everything, whereas a specific and limited thing can only represent itself. - zxq9