[Haskell-cafe] Help with generalizing function

Richard A. O'Keefe ok at cs.otago.ac.nz
Mon Jun 23 19:46:25 EDT 2008


On 23 Jun 2008, at 6:30 pm, leledumbo wrote:
> I've successfully create a function to return lists of N-ple that  
> satisfy the
> following function:
> x1 + x2 + x3 + ... + xN = C
> But unfortunately, it's not generic.

Why do you want it to be a tuple?  All the elements are the same type,  
so it
might as well be a list.

	part 0 c | c == 0 = [[]]
         part (n+1) c | c >= 0 = [(x:xs) | x <- [0..c],
                                           xs <- part n (c-x)]

(WARNING: UNTESTED CODE!)
Knuth TAOCP Volume 4 has some stuff on enumerating partitions.



More information about the Haskell-Cafe mailing list