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

Yogish Baliga <>
Wed Apr 17 23:29:34 CEST 2013


This will do instead

1> X = [a, b, c].
[a,b,c]
2> Y = [a, b, c, d].
[a,b,c,d]
3> X == lists:sublist(Y, length(X)).
true


On Wed, Apr 17, 2013 at 2:08 PM, Olav Frengstad <> wrote:

> 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
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130417/c4732800/attachment.html>


More information about the erlang-questions mailing list