select and selectSplit
Henning Thielemann
lemming at henning-thielemann.de
Fri Feb 15 07:53:50 EST 2008
On Thu, 14 Feb 2008, Cale Gibbard wrote:
> I know Bulat will be terribly disappointed by my suggestion to make an
> addition to Data.List ;) but I mentioned the following couple of
> functions on #haskell and got some positive response that they were
> things other people ended up writing all the time as well, so I
> decided I'd propose them here as additions to Data.List and see what
> kind of reaction I got:
>
> -- | The 'select' function takes a list and produces a list of pairs
> -- consisting of an element of the list together with a list of the
> -- remaining elements.
> select :: [a] -> [(a,[a])]
> select [] = []
> select (x:xs) = (x,xs) : [(y,x:ys) | (y,ys) <- select xs]
>
> -- | The 'selectSplit' function takes a list and produces a list of
> -- triples consisting of a prefix of the list, the element after it,
> -- and the remainder of the list.
> selectSplit :: [a] -> [([a],a,[a])]
> selectSplit [] = []
> selectSplit (x:xs) = ([],x,xs) : [(x:lys,y,rys) | (lys,y,rys) <- selectSplit xs]
I use these functions regularly, too.
select c =
init $
zipWith (\xs (y:ys) -> (y, xs++ys)) (List.inits c) (List.tails c)
More information about the Libraries
mailing list