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

Olav Frengstad <>
Wed Apr 17 23:08:52 CEST 2013


Hey Ivan,

Please correct me if I'm wrong, but when composing the match from an
existing variable AFAIK there is no other way:

1> X = [a, b, c].
[a,b,c]
2> [X | _] = [a, b, c, d].
** exception error: no match of right hand side value [a,b,c,d]



2013/4/17 Ivan Carmenates García <>

> Greetings Olav,****
>
> ** **
>
> I don’t understand why you try to math using that kind pattern, you can
> use the common way.****
>
> ** **
>
> Instead of using ([0,1,2] ++ _) = [0,1,2,3,4] you can use  [0,1,2| _] =
> [0,1,2,3,4] pattern matching.****
>
> ** **
>
> ** **
>
> Best,****
>
> Ivan.****
>
> ** **
>
> ** **
>
> ** **
>
> ** **
>
> ** **
>
> ** **
>
> ** **
>
> ** **
>
> *De:*  [mailto:
> ] *En nombre de *Olav Frengstad
> *Enviado el:* miércoles, 17 de abril de 2013 16:27
> *Para:*  Questions
> *Asunto:* [erlang-questions] Wildcard matching a list of strings vs
> atoms/binaries****
>
> ** **
>
> 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****
>



-- 
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/648a3464/attachment-0001.html>


More information about the erlang-questions mailing list