[erlang-questions] lists:takewhile and lists:splitwith

Masklinn masklinn@REDACTED
Thu Feb 3 11:02:25 CET 2011


On 2011-02-03, at 10:56 , maruthavanan s wrote:
> Hi,
> 
> The document says lists:takewhile(Pred,List) would return tuple of lists which would split the List when Pred returns true.
> 
> I tried the below.
> 
> lists:takewhile(fun(A)-> A==3 end,[1,2,3,4,5]).
> 
> it gives me an empty result.. What should I do to  get {[1,2],[3,4,5]}
I don't know which documentation you read, mine says the following:

  takewhile(Pred, List1) -> List2
    Takes elements Elem from List1 while Pred(Elem) returns true, that is, the function returns the longest prefix of the list for which all elements satisfy the predicate.

Here, the first element of `List1` is `1`, for which `Pred` is `false`, so `takewhile` returns an empty list. Sounds sensible and works as I would expect.

What you seem to describe is lists:splitwith[0], and note that in any case your predicate is incorrect.

[0] http://www.erlang.org/doc/man/lists.html#splitwith-2


More information about the erlang-questions mailing list