Erlang pattern matching error -
i have started learning erlang , came across next error while trying pattren match
the next look working fine:
{a,_,[b|_],{b}}={abc,23,[22,x],{22}}. resulting in = abc b = 22
the next look not working:
{a,_,[_|b],{b}}={abc,23,[22,x],{x}}. resulting in ** exception error: no match of right hand side value {abc,23,[22,x],{x}}
however if replace ',' in [22 , x | next working find , bounding x b
{a,_,[_|b],{b}}={abc,23,[22|x],{x}}. {abc,23,[22|x],{x}} b. x
any explanation highly appreciated.
many in advance
the operator |
used recursive definition of list: [a|b]
means add together element a
existing list b
. a
first element of resulting list, called head, b
rest of list called tail. b
can split head , tail, , process can go on until tail equal empty list []
.
the operator , separator between list elements, [a,b]
list of 2 elements a
, b
.
the 2 operators can combined: [a,b,c|d]
list of @ to the lowest degree 3 elements, a
, b
, c
, , tail d
can empty.
in test used syntax: [23|x]
; 23 can element of list (in fact erlang term can element of list) x atom , cannot list tail. doing broke recursive definition of list, construction not used , called improper list.
when match [_|b]
, [_,x]
, assign [x]
b
not match x
later in expression
when match [_|b]
, [_|x]
, assign x
b
indeed match x
later in expression, right way should
{a,_,[_|b],{b}}={abc,23,[22,x],{[x]}}.
erlang pattern-matching
No comments:
Post a Comment