[erlang-questions] illegal guard expression for IF illegal guard expression for "if"

Barco You barcojie@REDACTED
Fri Dec 2 10:56:20 CET 2011


Hi CGS,

I can't understand your statement --- " if in the first branch you use
again random:uniform(), the second branch condition can report an
inaccurate result."  Could you please make it clearer? Thanks!

Hi Others,

Why functions with side effect can not be in the guard expressions?


Thank you!
Barco

On Fri, Dec 2, 2011 at 5:50 PM, CGS <cgsmcmlxxv@REDACTED> wrote:

> **
> Hi,
>
> To put in simple words for better understanding, you can have only
> constant variables withing the guard expression. That means, in your case,
> if in the first branch you use again random:uniform(), the second branch
> condition can report an inaccurate result.
>
> Alternatively, you can use case statement:
>
> case (random:uniform()<0.5) of
>      true -> good;
>      false -> bad
> end
>
> I hope this answer will help you.
>
> CGS
>
>
>
>
> On 12/02/2011 10:30 AM, Barco You wrote:
>
> Why does the following expression got "illegal guard expression" when
> compiling:
> X = 0.5,
> if
>     random:uniform() < X ->      %error reported for this line
>            good;
>     true ->
>            bad
> end.
>
>  But if I change it to following expression, it's ok:
>  X = 0.5,
> Ran = random:uniform(),
> if
>      Ran < X ->
>            good;
>     true ->
>            bad
> end.
>
>  BRs,
> Barco
>
>
> _______________________________________________
> erlang-questions mailing listerlang-questions@REDACTED://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111202/d57c0b04/attachment.htm>


More information about the erlang-questions mailing list