[erlang-questions] Why does Erlang have control structures?

Andrzej Sliwa andrzej.sliwa@REDACTED
Mon Aug 27 23:00:14 CEST 2012


there is no difference in speed at all, 

> switch_signal({signal, _What, _From, _To}) ->
>     true;
> switch_signal({signal, _What, _To}) ->
>     true;
> switch_signal(_Else) ->
>     false.

is compiled internally to one function with case
so code speed is equal

there is only difference in readability.

On Aug 27, 2012, at 10:56 PM, Jayson Barley <jayson.barley@REDACTED> wrote:

> I am not sure I understand why we have them. For instance I can take the following code
> is_greater_than(X, Y) ->
>     if
>         X>Y ->
>             true;
>         true -> % works as an 'else' branch
>             false
>     end.
> And make it
> is_true(true) ->
>     true;
> is_true(false) ->
>     false.
> 
> is_greater_than(X, Y) ->
>     is_true(X>Y).
> I can do the same thing with case statements
> is_valid_signal(Signal) ->
>     case Signal of
>         {signal, _What, _From, _To} ->
>             true;
>         {signal, _What, _To} ->
>             true;
>         _Else ->
>             false
>     end.
> Becomes
> switch_signal({signal, _What, _From, _To}) ->
>     true;
> switch_signal({signal, _What, _To}) ->
>     true;
> switch_signal(_Else) ->
>     false.
> 
> is_valid_signal(Signal) ->
>     switch_signal(Signal).
> 
> I know that the control structures are a little bit faster, not much, but I find that the function form is more readable. 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list