summaryrefslogtreecommitdiff
path: root/h27.hs
diff options
context:
space:
mode:
Diffstat (limited to 'h27.hs')
-rw-r--r--h27.hs16
1 files 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