[erlang-questions] lists:all/2 unexpected result for the empty list
James Aimonetti
james@REDACTED
Wed Jul 20 00:16:38 CEST 2011
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Was wondering the reasoning for lists:all/2 returning true when passed
an empty list as the second parameter?
lists:all(some_fun/1, []) => 'true'
The docs state "Returns true if Pred(Elem) returns true for all elements
Elem in List, otherwise false."[1]
The definition in lists.erl for all/2 shows why it returns true:
all(Pred, [Hd|Tail]) ->
case Pred(Hd) of
true -> all(Pred, Tail);
false -> false
end;
all(Pred, []) when is_function(Pred, 1) -> true.
To me, at least, since Pred(Elem) never returned true, the outcome
should be false. I might write it as:
all(_, []) -> false;
all(Pred, L) ->
all_(Pred, L).
all_(Pred, [Hd|Tail]) ->
case Pred(Hd) of
true -> all(Pred, Tail);
false -> false
end;
all_(Pred, []) when is_function(Pred, 1) -> true.
lists:any/2 returns false, as expected, for the empty list, but for
similar reasons as lists:all/2.
Anyway, minor quibble, but I was curious of the reasoning...
James
[1] http://erldocs.com/R14B/stdlib/lists.html?i=0&search=lists%20all#all/2
- --
James Aimonetti
Distributed Systems Engineer / DJ MC_
2600hz | http://2600hz.com
sip:james@REDACTED
tel: 415.886.7905
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJOJgJGAAoJENc77s1OYoGgvskIAI+B9jZfbTWrNarFraF68Enw
Vpjt11jedrDe1nLPLXuVWKsV+lckqfET/EWl8f0GrrDOJlNXYucpw5V/iSQKyOfK
KIDvQRFvgjzqky3gZb259HF9SAblwdPIQ7M2pXtN9jejOPmT1az/b6NyYt/adAM/
r4WTllf/OTFPyDhqQ27cKRgfRAv4nt4EmESnYTfJkduxSmgps7IW7XzGrvlCDTQs
GboIwX7ICFcoFdOnxtlCN7iPQC2w5KYOvxcuF8RK788umY5DHZLrq3PqhkUjrtM+
A1ZUiefkmo3Z+IOTlYeYeLN9zpAmMVQ93gCd1vgdEfzDUHmcyN8XXcuyj8OF6KY=
=QHzR
-----END PGP SIGNATURE-----
More information about the erlang-questions
mailing list