[erlang-questions] Erlang elseif

Richard Carlsson richardc@REDACTED
Thu Nov 20 11:30:23 CET 2008

kdronnqvist@REDACTED wrote:
> I wrote a couple of examples how to solve this but none of them really
> feels right, the logic in what I'm trying to do is really REALLY not
> that complicated. The functions takes an argument and prints something
> based on what list the argument is a member of (if any). Like this:
> 42> e:elseif4($Q).
> Member of capital
> I would appreciate any input on this, even if it means you're bashing
> all my examples below :-)

If you want a reusable pattern, rather than a one-off hack,
the best thing is to start by separating the logic and the
side effects (as in your 4th example).

member_of(X, Lists) ->
  case lists:dropwhile(fun ({A,L}) -> not lists:member(X,L) end, Lists) of
    [{A,_}|_] -> A;
    [] -> none

print_class(C) ->
  case member_of(C, [{caps, ?CAPS}, {small, ?SMALL}, {nums, ?NUMS}]) of
    caps -> io:format("Member of capital\n");
    small -> ...;
    nums -> ...;
    none -> ...

For one-off things, prefer the threaded functions solution if there
are more than 2 levels.


More information about the erlang-questions mailing list