[erlang-questions] is there "return" in Erlang.

Raimo Niskanen <>
Tue Mar 1 16:00:16 CET 2011


On Mon, Feb 28, 2011 at 11:14:31AM +0800, Wang Wei wrote:
> Hello, I has a question about how to convert the bellow C program into Erlang.
> 
> void judge()
> {
>     int a;
>     int b;
> 
>     a = getA();
>     if (a == CONF1)
>    {
>        doSomeThing(a);
>        return;
>    }
> 
>    b = getB();
>    if (b == CONF2)
>    {
>        doOtherThing(b);
>         return;
>    }
> 
>    doThings();
>    return;
> }

judge() ->
    A = getA(),
    if A =:= 'CONF1' ->
	    doSomeThing(A);
	true ->
	    B = getB(),
	    if  B =:= 'CONF2' ->
		    doOtherThing(B);
		true ->
		    doThings()
	    end
    end.

But I guess that was a too obious transformation that did not answer the question.

Provided called functions do not throw/1:

judge() ->
    try
	A = getA(),
	if  A =:= 'CONF1' ->
		doSomeThing(A),
		throw(ok);
	    true -> ok
	end,
	B = getB(),
	if  B =:= 'CONF2' ->
		doOtherThing(B),
		throw(ok);
	    true -> ok
	end,
	doThings(),
	ok
    catch
    	Result -> Result
    end.


And if they can throw(Anything) elaborate with:

judge() ->
    OK = make_ref(),
    :
    :
		throw(OK);
    :
    :
		throw(OK);
    :
    :
    catch
	OK -> ok;
	Other ->
	    erlang:raise(throw, Other, erlang:get_stacktrace())
    end.

But only library code should have to be prepared for such nonsense.


	    

> 
> I think about "case" and "if" construct, but none of it seems work fine, thanks for help.
-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list