[erlang-questions] difference between ", " and "andalso" in guards
Bob Ippolito
bob@REDACTED
Sun May 18 07:12:18 CEST 2014
I think you'll have a hard time showing the difference between comma and
andalso, but you can demonstrate it with semicolon and orelse.
These two functions have different semantics:
right_age(X) when hd(X) >= 16; X =< 104 ->
true;
right_age(_) ->
false.
right_age_2(X) when hd(X) >= 16 orelse X =< 104 ->
true;
right_age_2(_) ->
false.
1) The assembly code can of course tell the difference, in this case you
can see the jumps are different.
2) The {f, N} refers to a {label, N}. I believe the {x, N} refers to
something like a register. I've never spent much time trying to understand
this assembly though.
3) See above :) The assembly code is different for these two versions, you
can see how the first one does a second test if the bif fails but the
latter immediately returns false when the bif fails.
-bob
On Sat, May 17, 2014 at 9:20 PM, Adel Zhang <adelzhang@REDACTED> 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. What this means is that if there is
> an error thrown in the first part of the guard X >= N; N >= 0, the second
> part can still be evaluated and the guard might succeed; if an error was
> thrown in the first part of X >= N orelse N >= 0, the second part will also
> be skipped and the whole guard will fail.
> </quote>
>
> So I write a codesnippet:
>
> -module(g).
> -export ([right_age/1, right_age_2/1, right_age_3/1]).
>
> right_age(X) when X >= 16, X =< 104 ->
> true;
> right_age(_) ->
> false.
>
>
> right_age_2(X) when (X >= 16) andalso (X =< 104) ->
> true;
> right_age_2(_) ->
> false.
>
> right_age_3(X) when (X >= 16) and (X =< 104) ->
> true;
> right_age_3(_) ->
> false.
>
>
> And compile with 'S' option to get the assembly code as follow:
>
> {function, right_age, 1, 2}.
> {label,1}.
> {line,[{location,"g.erl",4}]}.
> {func_info,{atom,g},{atom,right_age},1}.
> {label,2}.
> {test,is_ge,{f,3},[{x,0},{integer,16}]}.
> {test,is_ge,{f,3},[{integer,104},{x,0}]}.
> {move,{atom,true},{x,0}}.
> return.
> {label,3}.
> {move,{atom,false},{x,0}}.
> return.
>
>
> {function, right_age_2, 1, 5}.
> {label,4}.
> {line,[{location,"g.erl",10}]}.
> {func_info,{atom,g},{atom,right_age_2},1}.
> {label,5}.
> {test,is_ge,{f,6},[{x,0},{integer,16}]}.
> {test,is_ge,{f,6},[{integer,104},{x,0}]}.
> {move,{atom,true},{x,0}}.
> return.
> {label,6}.
> {move,{atom,false},{x,0}}.
> return.
>
>
> {function, right_age_3, 1, 8}.
> {label,7}.
> {line,[{location,"g.erl",15}]}.
> {func_info,{atom,g},{atom,right_age_3},1}.
> {label,8}.
> {test,is_ge,{f,9},[{x,0},{integer,16}]}.
> {test,is_ge,{f,9},[{integer,104},{x,0}]}.
> {move,{atom,true},{x,0}}.
> return.
> {label,9}.
> {move,{atom,false},{x,0}}.
> return.
>
>
>
> Apparently the three version right_age function assembly code are almost
> same. It confuse me totally.
>
> 1) can assembly code tell the function behaviour difference?
> 2) what does {f,6} {x,0} in assembly code mean?
> 3) how can I verify Fred's statement?
>
>
> _______________________________________________
> 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/20140517/bbb3d1c6/attachment.htm>
More information about the erlang-questions
mailing list