[erlang-questions] Erlang elseif

Dave Smith <>
Wed Nov 26 22:29:20 CET 2008


The other thing is that I don't understand what the proposal is.

One post above states the the following would be identical.

cond
 Test1 ->
     Result1;
 Test2 ->
     Result2;
 Test... ->
     Result...;
  true ->
     yay
end


case Test1 of
  true -> Result1;
  false ->
     case Test2 of
        true -> Result2;
        false ->
           case Test... of
              true -> Result...
              false -> yay
           end
     end
end

In the latter, each of the TestN conditions is a boolean expression; that
is, it returns either the atom 'true' or the atom 'false', otherwise a bad
match exception is thrown.  In the proposed "cond" construct above, are the
TestN expressions the same; the 'true' atom in the last conditions would
suggest that this is the case.

However, I saw an example in another post as follows:

cond
  {ok, X} = f(Y) -> g(X);   %% clause 1
  true -> h(X)              %% clause 2
end

The expression {ok, X} = f(Y) does not evaluate to 'true' or 'false'; it
evaluate to {ok, X}.  And given the generalization above would be equivalent
to be:

case {ok, X} = f(Y) of
  true-> g(x);
  false-> h(x)
end

... which would always result in a bad match exception.  I'm assuming this
example is erroneous.  Perhaps what was meant was

cond
  {ok, X} =:= f(Y) -> g(X);   %% clause 1
  true -> h(X)              %% clause 2
end

... in which case X whould have to be bound prior to this expression being
evaluated, otherwise an exception would be thrown.

So perhaps someone could better explain the usage of this proposed 'cond'
construct and give some examples.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081126/b06dfef9/attachment.html>


More information about the erlang-questions mailing list