<div dir="ltr">Thanks for the explanation Richard, that answers my question.<div><br></div><div style>lists:prefix/2 seems the obvious way then.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/4/17 Richard Carlsson <span dir="ltr"><<a href="mailto:carlsson.richard@gmail.com" target="_blank">carlsson.richard@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On 2013-04-17 22:26, Olav Frengstad wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hey,<br>
<br>
I'm getting some unexpected behavior when trying to pattern match the<br>
head of a list.<br>
<br>
When matching against a list of integers I can successfully do a<br>
wildcard match:<br>
27> ("ab ++ _)  = "abcd".<br>
<br>
But when trying to do the same with a list of atoms i get the error: *<br>
1: illegal pattern:<br>
28>  ([a,b,c] ++ _) = [a,b,c,d].<br>
* 1: illegal pattern<br>
<br>
So just making sure there's no magic in string matching i also try with<br>
an actual list of integers:<br>
29> ([0,1,2] ++ _) = [0,1,2,3,4].<br>
[0,1,2,3,4]<br>
<br>
Why can't I pattern match a lists with other values integers?<br>
</blockquote>
<br></div>
The Erlang Reference Manual (<a href="http://www.erlang.org/doc/reference_manual/expressions.html#id76648" target="_blank">http://www.erlang.org/doc/<u></u>reference_manual/expressions.<u></u>html#id76648</a>) says the following:<br>

<br>
  When matching strings, the following is a valid pattern:<br>
<br>
    f("prefix" ++ Str) -> ...<br>
<br>
  This is syntactic sugar for the equivalent, but harder to read<br>
<br>
    f([$p,$r,$e,$f,$i,$x | Str]) -> ...<br>
<br>
Since strings are really just lists of integers, and the double-quoted string syntax has been expanded to actual lists by the time the compiler gets the code, it actually works on lists of any integers, as in your example:<div class="im">
<br>
<br>
  ([0,1,2] ++ _) = [0,1,2,3,4].<br>
<br></div>
There's no particular reason that this syntax couldn't be allowed for lists of arbitrary elements, but apparently, that hasn't been done. In fact, it seems that the compiler (in the module sys_pre_expand) doesn't care about what the elements are, but the erl_lint module that checks the code after parsing explicitly disallows this syntax for other things than strings/lists of integers.<br>

<br>
For now, you'll just have to use the normal list pattern syntax for matching prefixes:<br>
<br>
   [a,b,c | _] = [a,b,c,d].<br>
<br>
In any case, the (Prefix++Tail) pattern can only be used when Prefix is a fixed-length list literal (so it can be rewritten to the last form at compile time). Prefix can never be a variable.<span class="HOEnZb"><font color="#888888"><br>

<br>
    /Richard</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
______________________________<u></u>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Med Vennlig Hilsen<div>Olav Frengstad</div><div><br></div><div>Systemutvikler // FWT</div><div>+47 920 42 090</div>
</div>