From 681c2a9ea65a6c6cd5935763765f7ef91c024b7b Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Thu, 19 Mar 2015 00:47:55 +0800 Subject: +27 --- h27.hs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/h27.hs b/h27.hs index 68efa73..6666203 100644 --- a/h27.hs +++ b/h27.hs @@ -1,8 +1,12 @@ -combination :: Int -> [a] -> [[a]] -combination 0 _ = [[]] +import Control.Arrow +combination :: Int -> [a] -> [([a],[a])] +combination 0 xs = [([],xs)] combination _ [] = [] -combination c (x:xs) = (map (x:) (combination (c-1) xs)) ++ (combination c xs) +combination c (x:xs) = (map ((x:).fst &&& snd) (combination (c-1) xs)) ++ (map (fst &&& (x:).snd) (combination c xs)) -group :: [Int] -> [a] -> [a] -group [] xs = [[xs]] -group (n:ns) xs = map ((combination n) . head) group (n:ns) xs +group :: [Int] -> [a] -> [[[a]]] +group [n] xs = map ((:[]).fst) (combination n xs) +group (n:ns) xs = concatMap (\(comb,rest) -> map (comb:) (group ns rest)) (combination n xs) + +group' [] = const [[]] +group' (n:ns) = concatMap (uncurry $ (. group' ns) . map . (:)) . combination n -- cgit v1.2.3-70-g09d2