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

Michael Truog mjtruog@REDACTED
Tue May 20 01:58:27 CEST 2014


The semantics have been mentioned as different.  However, it seems like people haven't mentioned a practical aspect, which is and/or/andalso/orelse can be nested with parenthesis within a guard, but ;/, are always top-level and unable to be nested.  That may be a helpful way of thinking about it.

On 05/17/2014 09: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. 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
>




More information about the erlang-questions mailing list