<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Jesper Louis Andersen schreef op
31-1-2015 om 16:40:<br>
</div>
<blockquote
cite="mid:CAGrdgiVJCiwnWDTnTCgNgLqV5o84a_QjncgUibV71eRGHzgxPQ@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra"><br>
<div class="gmail_quote">On Sat, Jan 31, 2015 at 3:11 PM,
Roelof Wobben <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div id=":14b" class="a3s" style="overflow:hidden">match_key(
Key, [Head | Tail], List ) -><br>
case element(1, Head) of<br>
Key -> match_key(Key, Tail, [element(2,Head) |
List] );<br>
_ -> match_key(Key, Tail, List )<br>
end.<br>
</div>
</blockquote>
</div>
<br>
This is a very imperative solution to the matcher. If you have
a tuple, T = {X, Y} you can use element(1, T) and element(2,
T) to project the X and Y elements "out of" the tuple. The
equation rule is:</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">T = {element(1, T), element(2, T)},</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">That is, if we split the tuple and
reform it, we obtain the same tuple again.</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">But there is another semantics
possible, namely matching. Most main-stream languages did not
add this form since it was not commonly known at the time, but
for some problems, it is a far more succinct:</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">T = case T of {X, Y} -> {X, Y} end,</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">which, we can use to rewrite the code:</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">match_key(K, [H|T], L) -></div>
<div class="gmail_extra"> case H of</div>
<div class="gmail_extra"> {K, V} -> match_key(K, T, [V |
L]);</div>
<div class="gmail_extra"> _ -> match_key(Key, T, L)</div>
<div class="gmail_extra"> end.</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">Note how this is more direct[0], since
we don't have to worry about projecting out of `Head` anymore.
We simply match on H and depending on the match, we do the
right thing. If the key matches, then the value, V, is already
deconstructed, so we can avoid having to write that.</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">Now, we can refactor some more. The
match can be carried out at the top level of the function:</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">match_key(_K, [], L) -> L;</div>
<div class="gmail_extra">match_key(K, [{K, V} | T], L) ->
match_key(K, T, [V | L]);</div>
<div class="gmail_extra">match_key(K, [_ | T], L) ->
match_key(K, T, L).</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">which should remove a lot of clutter
and make the function operate on the structure of the 2nd
argument directly.</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">[0] It avoids what is called boolean
blindness.</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra"><br>
<br clear="all">
<div><br>
</div>
-- <br>
<div class="gmail_signature">J.</div>
</div>
</div>
</blockquote>
<br>
<br>
Thanks, learned another thing.<br>
<br>
Roelof<br>
<br>
</body>
</html>