diff options
| author | Joe Zhao <ztuowen@gmail.com> | 2015-03-19 00:47:55 +0800 | 
|---|---|---|
| committer | Joe Zhao <ztuowen@gmail.com> | 2015-03-19 00:47:55 +0800 | 
| commit | 681c2a9ea65a6c6cd5935763765f7ef91c024b7b (patch) | |
| tree | 2d7213b498526050311919f74a4ce17c4ea7d19d | |
| parent | f07a21427d4d15ff5c9c98b528e83eb60867bc98 (diff) | |
| download | h99-681c2a9ea65a6c6cd5935763765f7ef91c024b7b.tar.gz h99-681c2a9ea65a6c6cd5935763765f7ef91c024b7b.tar.bz2 h99-681c2a9ea65a6c6cd5935763765f7ef91c024b7b.zip  | |
+27
| -rw-r--r-- | h27.hs | 16 | 
1 files changed, 10 insertions, 6 deletions
@@ -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  | 
