Skip to content

Instantly share code, notes, and snippets.

@Koitaro
Created August 20, 2010 23:21
Show Gist options
  • Select an option

  • Save Koitaro/541398 to your computer and use it in GitHub Desktop.

Select an option

Save Koitaro/541398 to your computer and use it in GitHub Desktop.
IntegerPartitions
definition module IntegerPartitions
integerPartitions :: Int Int -> [[Int]]
implementation module IntegerPartitions
import StdEnv
table :: [[[[Int]]]]
table =: [f m \\ m <- [1..]] where
f m = [g m n \\ n <- [1..m]] where
g m n
| m < n = [[]]
| m == n = [repeatn m 1]
| n == 1 = [[m]]
| otherwise = map (add1 n) (flatten [integerPartitions (m-n) x \\ x <- [1..min (m-n) n]])
where
add1 len xs = take len [x+1 \\ x <- xs ++ (repeat 0)]
integerPartitions :: Int Int -> [[Int]]
integerPartitions m n = table !! (m-1) !! (n-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment