Haskell platform proposal: split package
Twan van Laarhoven
twanvl at gmail.com
Tue Jul 24 15:13:08 CEST 2012
On 20/07/12 22:35, Brent Yorgey wrote:
> Hello everyone,
>
> This is a proposal for the split package [1] to be included in the
> next major release of the Haskell platform.
>
> Missing strategies
> ------------------
>
> The specific way that the generic splitting algorithm is implemented
> does preclude some imaginable splitting strategies. For example, a
> few years ago I tried adding a strategy that used a predicate on pairs
> of elements, splitting down the middle of any pairs that satisfy the
> predicate, but gave up because it simply did not fit.
I just needed a function for splitting a list into two equal parts. This is
something you often need for MergeSort-like functions. Is that something that
belongs in Data.List.Split?
I came up with this code, I am sure that there are other possible implementations.
-- | Split a list into two equally sized parts.
-- If the number of elements in the list is odd,
-- then the first part will be one element longer.
splitMiddle :: [a] -> ([a],[a])
splitMiddle xs = let (_,ys,zs) = go 0 xs in (ys,zs)
where
go _ [] = (0,[],[]) -- note: should be made strict in 1st argument
go i xxs@(x:xs)
| i <= j = (j+1,x:ys,zs)
| otherwise = (j+1,ys,xxs)
where (j,ys,zs) = go (i+1) xs
Twan
More information about the Libraries
mailing list