Proposal: Add these two handy functions to Data.List
marlowsd at gmail.com
Mon Jul 5 08:26:54 EDT 2010
On 05/07/2010 13:21, Simon Marlow wrote:
> On 02/07/2010 00:48, Cale Gibbard wrote:
>> When working with the list monad, I often find myself in need of one
>> of the two following functions:
>> -- | Produce a list of all ways of selecting an element from a list,
>> each along with the remaining elements in the list.
>> -- e.g. select [1,2,3,4] ==
>> -- This is useful for selection without replacement in the list monad
>> or list comprehensions.
>> select :: [a] -> [(a,[a])]
>> select  = 
>> select (x:xs) = (x,xs) : [(y,x:ys) | (y,ys)<- select xs]
> I'd start with something a bit more basic, that we don't have yet:
> splits :: [a] -> [([a],[a])]
> splits xs = zipWith splitAt [0..length xs] (repeat xs)
oh, I just realised
splits xs = zip (inits xs) (tails xs)
so that makes it slightly less attractive as an addition, I suppose.
More information about the Libraries