[erlang-questions] Weird lists:dropwhile/2 behavior?
Richard A. O'Keefe
ok@REDACTED
Mon Feb 22 01:51:28 CET 2016
On 22/02/16 1:39 am, fxmy wang wrote:
K = fun (E) -> E > 3 end.
lists:dropwhile(K, [1,2,3,4,5]).
What should we expect that to do?
dropwhile(Test, List) says "throw away the longest prefix
of List such that every element of the prefix satisfies the
Test." What is the longest prefix of [1,2,3,4,5] such that
every element is greater than 3? The empty list. So we
expect the answer [1,2,3,4,5], and indeed, that's what we get.
lists:filter(K, [3,1,4,2,5])
will of course answer [4,5], because filter is defined to look
at all the elements, not just a prefix.
Without looking at the source code, it will be something like
dropwhile(F, L = [X|Xs]) ->
case F(X)
of true -> dropwhile(F, Xs)
; false -> L
end;
dropwhile(_, L) ->
L.
More information about the erlang-questions
mailing list