[erlang-questions] difference between ", " and "andalso" in guards

Pierre Fenoll pierrefenoll@REDACTED
Mon May 19 01:18:29 CEST 2014


May I quote IRC on this one, if that can help…

21:53 < captain_fur>| in guards what is the difference between or/;
and/,
21:54 < captain_fur>| what do people usually use, do they mean the same
thing syntactically
21:54 <         nox>| captain_furious: no
21:55 <         nox>| ;, or and orelse are all different
21:55 <     DeadZen>| and and and also andalso
21:55 -!- supertramp [~Alexander@REDACTED] has quit [Ping timeout: 245
seconds]
21:55 -!- hiyosi [~skip_it@REDACTED] has quit [Ping
timeout: 264 seconds]
21:55 <         nox>| captain_furious: "BadGuard; true", "true; BadGuard"
and "true orelse BadGuard" will all succeed
21:56 <         nox>| err
21:56 <   darkthink>| :) @ DeadZen
21:56 <         nox>| nah, that's right
21:56 < captain_fur>| i have only ever used ,/;/orelse andalso
21:56 < captain_fur>| never plain and/or
21:56 <         nox>| then, "BadGuard orelse true", "BadGuard or true",
"true or BadGuard" will all fail
21:56 < captain_fur>| i am doing a talk and so wondered what the difference
was
21:56 <         nox>| A; B is compiled to the following Core:
21:57 <         nox>| (Core-like)
21:57 <         nox>| "try A of true -> true; _ -> false catch _ -> false
end orelse B"
21:57 <         nox>| now you are confused because guards don't fail and no
try are allowed in guards
21:58 <         nox>| but they are allowed in Core guards, and guards *can*
fail, the failures must just be silenced to false
21:58 -!- troutwine_away is now known as troutwine
21:58 -!- asabil [uid11150@REDACTED/web/irccloud.com/x-nkbqvyqukslcpbvg] has
joined #erlang
22:00 < captain_fur>| hmmm ok
22:00 <         nox>| so "length(X) =:= 3; true" succeeds for all X
22:00 <         nox>| and "length(X) =:= 3 orelse true" doesn't
22:01 < captain_fur>| short circuit
22:01 <         nox>| orelse shortcircuits too
22:01 -!- PavelSh [~pavelsh@REDACTED] has joined
#erlang
22:01 <         nox>| but it doesn't shortcircuit the nonbooleanness or the
crash of its first argument
22:01 <         nox>| while ; does
22:02 <         nox>| if X is not a list, the ; guard will succeeds anyway
and the orelse one won't
22:03 < captain_fur>| ok



Cheers,
-- 
Pierre Fenoll



On 19 May 2014 01:02, Richard A. O'Keefe <ok@REDACTED> wrote:

>
> On 18/05/2014, at 4:20 PM, Adel Zhang wrote:
>
> > hi, I am currently reading "learnyousomeerlang". In "Syntax in
> Functions" chapter "guards" section, Fred said
> >
> > <quote>
> > Note: I've compared , and ; in guards to the operators andalso and
> orelse. They're not exactly the same, though. The former pair will catch
> exceptions as they happen while the latter won't.
>
>
> This is seriously misleading.
>
> The difference between  X > Y , X < Z
> and X > Y andalso X < Z
> with respect to exceptions has NOTHING TO DO with whether
> it's comma or andalso.  The right semantic model is that
> it is the tests X > Y and X < Z themselves which change.
> If a guard test would have raised an exception, that
> turns into a simple failure of the guard test.
>
> Example:
>
> 1> F1 = fun (X) when X+1 > 2 -> ok ; (_) -> oops end.
> 2> F2 = fun (X) -> B = (X + 1 > 2), if B -> ok ; true -> oops end end.
> 3> F1(snark).
> oops
> 4> F2(snark).
> ** exited: {badarith,...} **
>
> This being so, "," never gets to *see* an exception,
> so how could it catch one?
>
> You can't verify Fred's statement, because it isn't true.
> "," and ";" no more catch exceptions than "->" does.
>
> _______________________________________________
> 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/20140519/74ef22b7/attachment.htm>


More information about the erlang-questions mailing list