[erlang-questions] Erlang elseif
Fri Nov 21 03:37:39 CET 2008
On 21 Nov 2008, at 1:10 am, <> wrote:
> 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.
> elseif5(A) ->
> lists:member(A,?CAPS) andalso throw(caps),
> lists:member(A,?SMALL) andalso throw(small),
> lists:member(A,?NUMS) andalso throw(nums),
> throw:caps -> io:format("Member of capital\n");
> throw:small -> io:format("Member of small\n");
> throw:nums -> io:format("Member of nums\n");
> throw:none -> io:format("Not a member\n");
> X:Y -> io:format("Bad exception ~p:~p\n",[X,Y])
Until you *measure* it, you have no reason to believe
that throwing and catching an exception is any cheaper
than doing all the list:member/2 calls. Even C++ with
its "zero-cost try blocks" doesn't promise zero-cost or
even cheap throwing; C++ 'try' blocks are only cheap
when it turns out you didn't need them.
Speaking as always only for myself I consider this an
abuse of exceptions, and an abuse of your readers.
Exceptions are for EXCEPTIONAL circumstances, and
finding what you were looking for surely doesn't count
Note too that your code suddenly accreted an extra
X:Y -> line that has nothing to do with your task.
As always, we need to see a REAL example, because there
is an excellent chance that the right answer for your
real problem is "none of the above".
More information about the erlang-questions