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

Richard O'Keefe ok@REDACTED
Mon Feb 28 22:55:13 CET 2011


On 28/02/2011, at 4:14 PM, Wang Wei wrote:

> Hello, I has a question about how to convert the bellow C program into Erlang.

It isn't a C program (there is no 'main') and it doesn't bellow.

> 
> void judge()
> {
>    int a;
>    int b;
> 
>    a = getA();
>    if (a == CONF1)
>   {
>       doSomeThing(a);
>       return;
>   }
> 
>   b = getB();
>   if (b == CONF2)
>   {
>       doOtherThing(b);
>        return;
>   }
> 
>   doThings();
>   return;
> }
> 
> I think about "case" and "if" construct, but none of it seems work fine, thanks for help.


I think your main problem was the 'return' statements, but you never needed
them in C.  You can write your C code neatly as

void judge(void) {
    int a = get_a();

    if (a == CONF1) {
        do_some_thing(a);
    } else {
        int b = get_b();

        if (b == CONF2) {
            do_other_thing(b);
        } else {
            do_things();
        }
    }
}

Having done that, the translation into Erlang is direct.

judge() ->
    A = get_a(),
    if A =:= conf1 ->
       do_some_thing(A)
     ; true ->
       B = get_b(),
       if B =:= conf2 ->
          do_other_thing(B)
        ; true ->
          do_things()
       end
    end.

The baStudlyCaps wayOfWriting namesIsNotGood inAnyLanguage
and it is NOT recommended for Erlang
(nor is it the traditional style for C).



More information about the erlang-questions mailing list