[erlang-bugs] lists:dropwhile

Sverker Eriksson <>
Fri Jun 13 16:27:42 CEST 2008


No, that's the right semantics. Drop elements _while_ Pred returns true 
and then return the remaining list. That is, return the tail if the list 
starting with the first one that returns false. Compare with takewhile/2.

Maybe the documentation isn't too obvious.

/Sverker, Erlang/OTP Ericsson


Matt Handler wrote:
> dropwhile does not work properly as defined in R12B-2:
>
> 01 dropwhile(Pred, [Hd|Tail]=Rest) ->
> 02    case Pred(Hd) of
> 03       true -> dropwhile(Pred, Tail);
> 04       false -> Rest
> 05    end;
> 06 dropwhile(Pred, []) when is_function(Pred, 1) -> [].
>
> it returns the entire list once it finds an element that doesn't match 
> true.
>
> here's what i did instead:
>
> dropwhile(Fun, List) ->
>     [Element ||
>     Element <- List,
>     Fun(Element) =/= true].   
>
> thanks,
> -matt
> ------------------------------------------------------------------------
>
> _______________________________________________
> erlang-bugs mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-bugs




More information about the erlang-bugs mailing list