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

Joe Armstrong erlang@REDACTED
Tue Aug 28 13:12:30 CEST 2012


On Mon, 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.

Or even

is_greater_than(X, Y) -> X > Y.

/J


> 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