Thanks for your solutions, they give me good insight in how the Erlang world would solve this. I have one more way of solving this that I think is kind of clean and it would be efficient because the lists:member() would need to be evaluated once a match is found.<br>
<br>elseif5(A) -><br> try<br> lists:member(A,?CAPS) andalso throw(caps),<br> lists:member(A,?SMALL) andalso throw(small),<br> lists:member(A,?NUMS) andalso throw(nums),<br> throw(none)<br>
catch<br> throw:caps -> io:format("Member of capital\n");<br> throw:small -> io:format("Member of small\n");<br> throw:nums -> io:format("Member of nums\n");<br>
throw:none -> io:format("Not a member\n");<br> X:Y -> io:format("Bad exception ~p:~p\n",[X,Y])<br> end.<br><br>However, some might think this is the wrong way of handling exceptions. What do you think?<br>
<br>BR,<br>Daniel Rönnqvist<br><br><br><div class="gmail_quote">2008/11/20 Richard Carlsson <span dir="ltr"><<a href="mailto:richardc@it.uu.se">richardc@it.uu.se</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d"><a href="mailto:kdronnqvist@gmail.com">kdronnqvist@gmail.com</a> wrote:<br>
> I wrote a couple of examples how to solve this but none of them really<br>
> feels right, the logic in what I'm trying to do is really REALLY not<br>
> that complicated. The functions takes an argument and prints something<br>
> based on what list the argument is a member of (if any). Like this:<br>
><br>
> 42> e:elseif4($Q).<br>
> Member of capital<br>
><br>
> I would appreciate any input on this, even if it means you're bashing<br>
> all my examples below :-)<br>
<br>
</div>If you want a reusable pattern, rather than a one-off hack,<br>
the best thing is to start by separating the logic and the<br>
side effects (as in your 4th example).<br>
<br>
member_of(X, Lists) -><br>
case lists:dropwhile(fun ({A,L}) -> not lists:member(X,L) end, Lists) of<br>
[{A,_}|_] -> A;<br>
[] -> none<br>
end.<br>
<br>
print_class(C) -><br>
case member_of(C, [{caps, ?CAPS}, {small, ?SMALL}, {nums, ?NUMS}]) of<br>
<div class="Ih2E3d"> caps -> io:format("Member of capital\n");<br>
</div> small -> ...;<br>
nums -> ...;<br>
none -> ...<br>
end.<br>
<br>
<br>
For one-off things, prefer the threaded functions solution if there<br>
are more than 2 levels.<br>
<font color="#888888"><br>
/Richard<br>
</font></blockquote></div><br>