# Proposal (Trac ticket #3671): Add takeRec, genericTakeRec and spanRec to Data.List

Christian Maeder Christian.Maeder at dfki.de
Tue Nov 24 04:23:48 EST 2009

Ok, lets try to get back to the proposal.

1. We want something like takeRec being named splitAts or groupsOf
(groupsOf is too similar to the existing groupsBy)

2. genericTakeRec is just a variant of takeRec based on genericSplitAt
rather than splitAt (or genericTake, genericDrop instead of take, drop)

3. spanRec changed to a mutual recursive implementation for spans and
breaks (in the ticket) that is not supported much. Rather breaks was
more discussed in this thread as a splitting function (that was
discussed years before under splitBy or splitOn without any agreement.)

Agreement seems to be about "breaks p = spans (not . p)" (or vice versa)

Furthermore, proposal for runs (4.) and replace (5.) came up in this
thread, that are not part of the ticket.

4. I find "runs" useful in the sense proposed by Philip with type

runs :: (a -> a -> Bool) -> [a] -> [[a]]

> runs (<) [1,2,3,4,3,4,5]
[[1,2,3,4],[3,4,5]]

(there's no proposal to rename groupsBy)

5. The type of my replace function was:

replace :: Eq a => [a] -> a -> [a] -> [a]

and a (reasonable) alternative would be to replace a sublist by another
sublist and not by a single element.

Which of the above functions (1. - 4.) should be part of the proposal?

How should these functions be implemented?

I only made a proposal how they could be implemented using replaceBy
(below), but direct recursive definitions may be more appropriate.

I.e. unlines and unwords are also defined with explicit recursion
although alternative definitions are there (based on concatMap and foldr1)

Cheers Christian

Jules Bean schrieb:
> Christian Maeder wrote:
>> My proposal would be to add a function like
>>
>> \begin{code}
>> replaceBy :: ([a] -> (b, [a])) -> [a] -> [b]
>> replaceBy splt l = case l of
>>   [] -> []
>>   _ -> let (ft, rt) = splt l in
>>     ft : replaceBy splt rt
>> \end{code}
>>
>> that takes a function "splt" that splits a non-empty list and returns a
>> shorter list as second component (to ensure termination).
>>
>
> .... if we're in the business of proposing generalised search and
> replace I'd like to propose this one:
>
>
> which is along the general lines above but slightly more general.

It's more special in the sense that the result list cannot be "[[a]]" as
required for all the above functions except replace.

> When I made that posting in 2007 I was hoping for name suggestions. In
> the absence of anythign better I suggest 'Data.List.transform' or the
> more whimsical 'Data.List.transmogrify'.
>
> I would also suggest a convenience function 'Data.List.replace' defined
> from it in the obvious way.
>
> I am also in agreement with 'groupsOf' (which I sometimes called
> 'chunksOf' but I like both names) proposed by Yitzchak.
>
> Will all that in mind I oppose this proposal, because whilst I fully
> support filling in some gaps in Data.List I don't think these are the
> best primitives.
>
> Jules