[erlang-questions] Erlang elseif

Håkan Huss huss01@REDACTED
Thu Nov 20 10:55:29 CET 2008

2008/11/20  <kdronnqvist@REDACTED>:
> Hi everyone I just joined the list and tried to search for information about
> this but I could not find anything that answered my question. The thing is
> that I want a general way to branch the execution of my code based on the
> result on an expression. Very similar to how if works but I want to be able
> to test expressions and not only guards. If you're thinking "then you have
> case" now look below at function elseif2() and you'll see my point.
> 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:

I might go for some variation of the function clause based version
suggested by NAR
in this case where the number of "branches" is small. Another way to
handle this is
to use a list to do the branching, e.g.,

elseif(A) ->
    Notin = fun ({Chars, _Desc}) -> not lists:member(A, Chars) end,
    Desclist = [{?CAPS, capital}, {?SMALL, small}, {?NUMS, nums}],
    case lists:dropwhile(Notin, Desclist) of
	[{_Chars, Desc} | _] ->
	    io:format("Member of ~p\n", [Desc]);
	[] ->
	    io:format("Not a member\n")

/Håkan Huss

More information about the erlang-questions mailing list