[erlang-questions] Erlang elseif
Richard Carlsson
richardc@REDACTED
Thu Nov 20 14:13:52 CET 2008
kdronnqvist@REDACTED 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) ->
> try
> lists:member(A,?CAPS) andalso throw(caps),
> lists:member(A,?SMALL) andalso throw(small),
> lists:member(A,?NUMS) andalso throw(nums),
> throw(none)
> catch
> 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])
> end.
>
> However, some might think this is the wrong way of handling exceptions.
> What do you think?
It's not terrible. :-) Minor points: you don't have to write the
'throw:', since that is the default, and you should probably just
skip the X:Y case altogether - let any other exceptions fall through.
/Richard
More information about the erlang-questions
mailing list