<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: Times New Roman; font-size: 12pt; color: #000000'>I don't know if it is actually explicitly stated anywhere but multiple occurrences of a variable in a pattern match means that the values the variable would get in each occurrence are tested for equality. They are not being used as you would like. Also the order in which function arguments are matched is not defined, and if we just test multiple variable occurrences for equality does not matter. This means that when you match the binary N may not yet have a value. In fact we do match left-to-right (but don't tell anyone) so N will in fact not have a value. Flipping the order of the arguments will not help here.<br><br>It is different when match *INSIDE* a binary. There, by necessity, the pattern match goes left-to-right and if match a value from a binary you can use the value later in the binary match. So you can do:<br><br><<N,B1:N/binary,Rest/binary>>  = Bin<br><br>Robert<br><br><hr id="zwchr"><blockquote style="border-left:2px solid rgb(16, 16, 255);margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Erik Pearson" <erik@defunweb.com><br><b>To: </b>"erlang-questions Questions" <erlang-questions@erlang.org><br><b>Sent: </b>Sunday, 28 October, 2012 11:08:44 AM<br><b>Subject: </b>Re: [erlang-questions] Binary match in function head doesn't compile<br><br><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">Is there a reference for the resolution of patterns in function/clause head similar to </div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><a href="http://www.erlang.org/doc/apps/erts/match_spec.html" target="_blank" style="color:rgb(17,85,204)">http://www.erlang.org/doc/apps/erts/match_spec.html</a></div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
but for regular Erlang? The docs on the abstract format is useful</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<a href="http://www.erlang.org/doc/apps/erts/absform.html" target="_blank" style="color:rgb(17,85,204)">http://www.erlang.org/doc/apps/erts/absform.html</a></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">There are a few places in the docs that refer to the process of matching the function clause head against arguments</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
e.g. the function overview</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<a href="http://www.erlang.org/doc/reference_manual/functions.html#id74558" target="_blank" style="color:rgb(17,85,204)">http://www.erlang.org/doc/reference_manual/functions.html#id74558</a></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">but it would be really useful to have those references link to documentation which describes this process.</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
Thanks,</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">Erik.</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">(ps - I'm happy with your answer, Björn-Egil, but hoping this thread can include some solid leads for others researching similar issues.)</div>
<div><br></div><div style="font-size:13px;color:rgb(34,34,34);font-family:arial,sans-serif;background-color:rgb(255,255,255)">(pps - there is a definitive response to my original post from Björn-Egil <span style="background-color:transparent">Dahlberg below -- he inadvertently sent it directly to me)</span></div>
<div><span style="background-color:transparent"><br></span></div><div class="gmail_quote">On Fri, Oct 26, 2012 at 11:59 AM, Björn-Egil Dahlberg <span dir="ltr"><<a href="mailto:wallentin.dahlberg@gmail.com" target="_blank">wallentin.dahlberg@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br><br><div class="gmail_quote"><div class="im">2012/10/26 Erik Pearson <span dir="ltr"><<a href="mailto:erik@defunweb.com" target="_blank">erik@defunweb.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">Hi,</div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"><br></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">


I'm wondering why this </div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"><br></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<div>test(<<Field:Len/binary, Rest/binary>>, Len) -></div><div>    {Len, Field, Rest}.</div></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<br></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">does not compile, complaining that "variable 'Len' is unbound", while this</div><div><br><div><font color="#222222" face="arial, sans-serif">test(<<Field:2/binary, Rest/binary>>, Field) -></font></div>


<div><font color="#222222" face="arial, sans-serif">    {Field, Rest}.</font></div></div><div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">does. For some reason the compiler doesn't see the Len from the match spec in the arguments, but it does see Field. Is that by design?</font></div>

</blockquote><div><br></div></div><div>Yes and a limitation that is being adressed. </div><div class="im"><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">BTW  supplying a variable for Len does work in this case:</font></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">


<br></div><div><div><font color="#222222" face="arial, sans-serif">test(Bin, Len) -></font></div><div><font color="#222222" face="arial, sans-serif">  <<Field:Len/binary, Rest/binary>> = Bin, </font></div>

<div>
<font color="#222222" face="arial, sans-serif">  {Len, Field, Rest}.</font></div></div></blockquote><div><br></div></div><div>The difference here is that Len is bound when entering the function body as opposed when it is in the function head. </div>

<div><br></div><div>We have had fierce debates on, among other things, matching behaviors for Maps (extended frames/hashes) which also have led to redesigning parts how binary matching is done in function heads. This is currently in the prototyping stages and it is to early to say to which release this will be ready.</div>
<span class="HOEnZb"><font color="#888888">
<div><br></div><div>// Björn-Egil</div></font></span></div>
</blockquote></div><br>
<br>_______________________________________________<br>erlang-questions mailing list<br>erlang-questions@erlang.org<br>http://erlang.org/mailman/listinfo/erlang-questions<br></blockquote><br></div></body></html>