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

Michael Truog mjtruog@REDACTED
Wed Jul 20 03:09:49 CEST 2011


I asked about this almost exactly 2 years ago (5 more days).  However, no response came up on the mailing list.  I think you might be expected to modify the documentation in github.

The choice of true for lists:all/2 with an empty list still seems arbitrary to me.  However, I don't think there is a good argument for the choice being wrong.  Also, even if there was such an argument, it doesn't seem like the lists module would be changed because of the legacy code that depends on it.

On 07/19/2011 03:34 PM, James Aimonetti wrote:
> 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
> >>
>
_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions





More information about the erlang-questions mailing list