lists:partition/2

Robert Virding <>
Thu Jan 18 11:05:27 CET 2001


Raimo Niskanen <> writes:
>Hello,
>
>I am missing function in the module lists, one that using a predicate
>partitions a list into two; one with all elements for which the
>predicate was true, and one for the rest, while preserving the ordering
>of the elements.
>
>It could be defined as follows:
>...
>This could be accomplished with lists:foldr/3 like this:
>...
>	        
>The latter one is unfortunately not tail recursive, and it is less
>efficient because it assembles / disassembles a tuple in each loop
>recursion.

A function like partition would be useful.

The latter may not be tail recursive (because foldr is not) but it may 
not be so much more inefficient as you save doing to reverses.  In 
terms of memory they are almost equal.

	Robert





More information about the erlang-questions mailing list