[erlang-questions] lists:all/2 unexpected result for the empty list

James Aimonetti james@REDACTED
Wed Jul 20 00:34:19 CEST 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yeah, documentation update would disambiguate the issue. Like I said,
minor quibble... :)

On 07/19/2011 03:29 PM, Alexander Krasnukhin wrote:
> It's the standard way a filter works. Otherwise you will be forced to check
> if list is empty every time you want to use this function. After writing
> this check several times you will fix function back to return true for empty
> lists.
> 
> But I agree the documentation is unclear. What about "Returns false if
> Pred(Elem) returns false for some element in List, otherwise true"
> 
> On Wed, Jul 20, 2011 at 12:16 AM, James Aimonetti <james@REDACTED> wrote:
> 
> 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
_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions
>>

- -- 
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/

iQEcBAEBAgAGBQJOJgZrAAoJENc77s1OYoGghrkIAK1BgvOz8n672yRmkjUoQSjI
0cz+/ioOXJ3m/hgUjzLKTVfPKh+QDMyoBZ1ylh5RB97cTuUOyMcQKh3NASF8hKsD
Niw+jhy7i1LjeUMWffL4tE8E/jqcEl9Hiy+7iL3Ex8yeFFBmyAekrCMagjYVmQTF
Z5lU7RWORhkT1Wm49+8mcbseN3kOJ5Fzz7GW55VoAfTna8hEHScsBxoKzvrarwsK
Y3crQ5dWPV0J6GuufExZbuhTHyx6X3elPcs8P5lyAaIcsuaZ9ijjvYPq8UdSmfQE
qkxCrAFAKduh5YDWxyvOGnDgay1ypW4b9eGSDncBbUXMd9xVK+jwccjjNYhaF74=
=bwCn
-----END PGP SIGNATURE-----



More information about the erlang-questions mailing list