Ordered partitions: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: Tidied (applied Ormolu and Hlint to last example)) |
|||
Line 1,193: | Line 1,193: | ||
Faster by keeping track of the length of lists: |
Faster by keeping track of the length of lists: |
||
<lang haskell> |
<lang haskell>import Data.Bifunctor (first, second) |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- choose m out of n items, return tuple of chosen and the rest |
|||
⚫ | |||
⚫ | |||
choose :: [Int] -> Int -> Int -> [([Int], [Int])] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
choose aa _ 0 = [([], aa)] |
|||
⚫ | |||
⚫ | |||
| otherwise = |
|||
⚫ | |||
⚫ | |||
partitions :: [Int] -> [[[Int]]] |
|||
⚫ | |||
where |
|||
⚫ | |||
⚫ | |||
combos s n (x : xs) = |
|||
[ l : r |
|||
⚫ | |||
r <- combos rest (n - x) xs |
|||
] |
|||
main :: IO () |
|||
main = mapM_ print $ partitions [5,5,5]</lang> |
main = mapM_ print $ partitions [5, 5, 5]</lang> |
||
=={{header|J}}== |
=={{header|J}}== |