[erlang-questions] guard expression restriction

Kostis Sagonas <>
Thu Dec 2 12:51:55 CET 2010


Hynek Vychodil wrote:
> On Thu, Dec 2, 2010 at 9:37 AM, Kostis Sagonas <> wrote:
>> Richard O'Keefe wrote:
>>> ...
>>> There is of course a fairly elementary transformation by means of
>>> which anyone who *really* needs a function call in a guard finds
>>> out that they don't.
>>>
>>>        f(Arguments) when Expression -> Body;
>>>        <rest of f>
>>>
>>> =>
>>>        f(Arguments) ->
>>>            case Expression
>>>              of true -> Body
>>>               ; false -> f'(Arguments)
>>>            end;
>>>        f(Vars) -> f'(Vars).
>>>
>>> where f' is the rest of f with the name changed.
>> Yes, Erlang is Turing complete as it is (so you can express in it any
>> algorithm you want to) and of course there is that transformation, but do
>> you really claim that the bulky code with the case statement and having to
>> factor the rest of f into a separate function is more expressive and
>> programmer-friendly than the one liner with the guard? Sorry, but I do not
>> see this.
>>
> 
> Anyway, this rewritten version doesn't behaves like guards because it
> should be more like:
> 
>         f(Arguments) ->
>             case catch Expression
>               of true -> Body
>                ; _ -> f'(Arguments)
>             end;
>         f(Vars) -> f'(Vars).
> 
> or even worse
> 
>         f(Arguments) ->
>             try Expression
>               of true -> Body
>                ; _ -> f'(Arguments)
>               catch
>                  _:_ -> f'(Arguments)
>             end;
>         f(Vars) -> f'(Vars).
> 
> because soon or late one would write throw(true) inside Expression.

Very good point...  Thanks for the support Hynek!

Kostis


More information about the erlang-questions mailing list