
On Mon, 05 Jul 2010 13:26:54 +0100, Simon Marlow
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] == [(1,[2,3,4]),(2,[1,3,4]),(3,[1,2,4]),(4,[1,2,3])] -- 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)
Pointless contribution: splits = zip.inits<*>tails Pointwise contribution: +1 select +1 separate +0.5 split Regards :) -- Nicolas Pouillard http://nicolaspouillard.fr