<div dir="ltr">One can implement different ways of matching string against prefix-as-binding:<div><br></div><div><div>7> Prefix = "hello ".</div><div>"hello "</div><div>8> {Prefix, Rest} = lists:split(length(Prefix), "hello world").</div><div>{"hello ","world"}</div><div>9> Rest.</div><div>"world"</div></div><div><br></div><div><div>10> Rest = lists:foldl(fun(C, [C|RestAcc]) -> RestAcc; (_, _) -> error(badmatch) end, "hello world", Prefix).</div><div>"world"</div><div>11> lists:foldl(fun(C, [C|RestAcc]) -> RestAcc; (_, _) -> error(badmatch) end, "help world", Prefix).</div><div>** exception error: badmatch</div><div> in function shell:apply_fun/3 (shell.erl, line 900)</div><div> in call from lists:foldl/3 (lists.erl, line 1263)</div></div><div><br></div><div><div>12> MatchPref = fun MatchPref([], SRest) -> SRest; MatchPref([C|Pref], [C|SRest]) -> MatchPref(Pref, SRest); MatchPref(_, _) -> error(badmatch) end.</div><div>#Fun<erl_eval.36.99386804></div><div>13> Rest = MatchPref(Prefix, "hello world").</div><div>"world"</div><div>14> Rest = MatchPref(Prefix, "help world").</div><div>** exception error: badmatch</div></div><div><br></div><div>So it should be possible to implement a syntactic sugar for Prefix ++ Rest = String.<br>I suppose one can do that even for function/case clauses (but that would be more tricky).</div><div><br></div><div>Seems like parse_transform would be enough for a proof-of-concept. In this case even no OTP patch is required.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 25, 2017 at 9:56 AM, Stefan Hellkvist <span dir="ltr"><<a href="mailto:hellkvist@gmail.com" target="_blank">hellkvist@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi, <div><br></div><div>Erlang has this syntactic sugar for matching string prefixes (<a href="http://erlang.org/doc/reference_manual/expressions.html#id80508" target="_blank">http://erlang.org/doc/<wbr>reference_manual/expressions.<wbr>html#id80508</a>) where you can do:<span style="color:rgb(56,58,66);font-family:mono,Courier,monospace;font-size:11.2px"></span></div><div><br></div><div>"prefix" ++ Rest = "prefixsomething"</div><div><br></div><div>, which would bind Rest to "something" in this case. </div><div><br></div><div><br></div><div>I'm curious why however it is ok to do:</div><div><br></div><div><div>1> "prefix" ++ Rest = "prefixsomething".</div><div>"prefixsomething"</div><div>2> Rest.</div><div>"something"</div></div><div><br></div><div><br></div><div>but it is not ok to do:</div><div><br></div><div><div>1> Prefix = "prefix".</div><div>"prefix"</div><div>2> Prefix ++ Rest = "prefixsomething".</div><div>* 1: illegal pattern</div></div><div><br></div><div><br></div><div>Is it because this syntactic sugar is transformed more or less as a preprocessing step where the value of Prefix needs to be known, or why else is "Prefix ++ Rest = Something" not allowed even when Prefix is bound?</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>/Stefan</div></font></span></div>
<br>______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><font face="'courier new', monospace">Danil Zagoskin | <a href="mailto:z@gosk.in" target="_blank">z@gosk.in</a></font></div></div></div>
</div>