[Haskell-cafe] Help with generalizing function

leledumbo leledumbo_cool at yahoo.co.id
Mon Jun 23 02:30:51 EDT 2008


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. The N is supposed to be an input, too.
I don't know how to make a dynamic N-ple (is it possible anyway?).
Currently, here's the implementation:
[code]
findAllAns c = [ (x1,x2,x3,x4,x5) |
    x1 <- [0..c],
    x2 <- [0..c],
    x3 <- [0..c],
    x4 <- [0..c],
    x5 <- [0..c],
    x1 + x2 + x3 + x4 + x5 == c
  ]
[/code]
I tried using lists of lists, like this:
[code]
findAllAns c n = [ [x] | x <- [0..c], length [x] == n ]
[/code]
but [x] doesn't mean "a list of x", instead it's "a list that contains an x,
where x in [0..c]". Can anyone help me?
-- 
View this message in context: http://www.nabble.com/Help-with-generalizing-function-tp18063291p18063291.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list