[erlang-questions] Wildcard matching a list of strings vs atoms/binaries

Olav Frengstad <>
Wed Apr 17 22:26:31 CEST 2013


Hey,

I'm getting some unexpected behavior when trying to pattern match the head
of a list.

When matching against a list of integers I can successfully do a wildcard
match:
27> ("ab ++ _)  = "abcd".

But when trying to do the same with a list of atoms i get the error: * 1:
illegal pattern:
28>  ([a,b,c] ++ _) = [a,b,c,d].
* 1: illegal pattern

So just making sure there's no magic in string matching i also try with an
actual list of integers:
29> ([0,1,2] ++ _) = [0,1,2,3,4].
[0,1,2,3,4]

Why can't I pattern match a lists with other values integers?

What i'm looking to achieve is to provide wildcard matching on a gen_event
handler so I can do matches like this:

handle_event((Match ++ _), State#state{match = Match}) ->
% handle

Any other alternatives is appreciated, current implementation uses
lists:suffix/2

-- 
Med Vennlig Hilsen
Olav Frengstad

Systemutvikler // FWT
+47 920 42 090
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130417/549e17bf/attachment.html>


More information about the erlang-questions mailing list