[Haskell-cafe] Help with generalizing function

Ryan Ingram ryani.spam at gmail.com
Mon Jun 23 02:42:43 EDT 2008


Hint: use recursion.

Hint 2: You can use <- in a list comprehension or list "do"-block to
select a single list from a list of lists.

  -- ryan

On Sun, Jun 22, 2008 at 11:30 PM, leledumbo <leledumbo_cool at yahoo.co.id> 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. 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.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list