[erlang-questions] comma vs andalso

Martin Engström whongo@REDACTED
Mon Jul 6 15:07:56 CEST 2009


On Mon, Jul 6, 2009 at 2:21 PM, Roberto Ostinelli <roberto@REDACTED>wrote:

>
> On 06/lug/09, at 14:19, Roberto Ostinelli wrote:
>
>  foo(..) when (islist(X) == true ansalso (lists:nth(1,X) == 'test') -> ..
>>
>
> that would be
>
> foo(..) when (islist(X) =:= true) ansalso (lists:nth(1,X) =:= 'test') -> ..
>

'==' and '=:=' are equivalent except when applied to numbers, are they not?
(1 == 1.0) is true, while (1 =:= 1.0) is false. When applied to atoms, as in
your example, there is no difference.
And the difference between comma and 'andalso' doesn't actually matter much
for the purpose of avoiding evaluation errors in guard context, since such
an error in a guard just means that the guard fails. For instance, try this:
1> length(a).
** exception error: bad argument
  in function length/1
  called as length(a)
2> Foo = fun(X) when length(X) > 3 -> "Long list"; (X) -> "Short or no list"
end.
3> Foo(a).
"Short or no list"
Also, lists:nth(X) is an illegal guard expression. ;)


More information about the erlang-questions mailing list