[erlang-questions] Weird lists:dropwhile/2 behavior?

zxq9 <>
Sun Feb 21 15:11:12 CET 2016


On 2016年2月21日 日曜日 14:36:05 Jesper Louis Andersen wrote:
> On Sun, Feb 21, 2016 at 1:39 PM, fxmy wang <> wrote:
> 
> > Is this behavior expected?
> 
> 
> yes. To make it explicit:
> 
> In lists:dropwhile(fun(E) -> E > 3 end, [1,2,3,4,5]), we first check if 1 >
> 3. This is false, so we stop dropping and return the remainder of the list,
> which is [1,2,3,4,5].

Hi Fxmy.

To follow up with a counter example:

  1> J = fun(Z) -> Z < 3 end.
  #Fun<erl_eval.6.54118792>
  2> lists:dropwhile(J, [1,2,3,4,5]).
  [3,4,5]

Compare that with yours:

  2> K = fun(Elem) -> Elem > 3 end.
  #Fun<erl_eval.6.54118792>
  4> lists:dropwhile(K, [1,2,3,4,5]).
  [1,2,3,4,5]

And now an example over a valley:

  3> Q = fun(Z) -> Z > 3 end.
  #Fun<erl_eval.6.54118792>
  4> lists:dropwhile(Q, [5,4,3,2,1,2,3,4,5]).
  [3,2,1,2,3,4,5]

Just like it says on the box.

-Craig


More information about the erlang-questions mailing list