summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Zhao <ztuowen@gmail.com>2015-03-19 00:47:55 +0800
committerJoe Zhao <ztuowen@gmail.com>2015-03-19 00:47:55 +0800
commit681c2a9ea65a6c6cd5935763765f7ef91c024b7b (patch)
tree2d7213b498526050311919f74a4ce17c4ea7d19d
parentf07a21427d4d15ff5c9c98b528e83eb60867bc98 (diff)
downloadh99-681c2a9ea65a6c6cd5935763765f7ef91c024b7b.tar.gz
h99-681c2a9ea65a6c6cd5935763765f7ef91c024b7b.tar.bz2
h99-681c2a9ea65a6c6cd5935763765f7ef91c024b7b.zip
+27
-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