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

Jayson Barley <>
Mon Aug 27 22:56:37 CEST 2012


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120827/52dea012/attachment.html>


More information about the erlang-questions mailing list