Proposal: Add chop function to Data.List
Henning Thielemann
lemming at henning-thielemann.de
Tue Dec 14 11:35:43 CET 2010
On Mon, 13 Dec 2010, Lennart Augustsson wrote:
> I would like to propose the following function for inclusion in Data.List
>
> chop :: (a -> (b, [a]) -> [a] -> [b]
> chop _ [] = []
> chop f as = b : chop f as'
> where (b, as') = f as
>
> It's commonly occuring recursion pattern. Typically chop is called
> with some function that will consume an initial prefix of the list
> and produce a value and the rest of the list.
>
> The function is clearly related to unfoldr, but I find it more
> convenient to use in a lot of cases.
Is the difference between 'unfoldr' and 'chop' just the Maybe result type
of f?
> I first encountered this function around 1981 when I was talking to
> S?ren Holmstr?m about this recursion pattern and he said that he
> had also observed it and he called the function chopList.
> Ever since then I've used chopList a lot, but unfortunately I always
> have to make my own definition of this common function.
To work around this problem I have written utility-ht for the basic
functions that I need all the time.
More information about the Libraries
mailing list