at the moment my code looks a bit like this:
gen (x:[]) = [[a] | a <- (someOp x)]
gen (x:y:[]) = [[a,b] | a <- (someOp x), b <- (someOp y)]
gen (x:y:z:[]) = [[a,b,c] | a <- (someOp x), b <- (someOp y), c <- (someOp z)]
... and so on
is it possible to conclude the rest with gen(x:xs) ??
You can do this recursively:
gen [] = [[]]
gen (x:xs) = [ a:g | a <- someOp x, g <- gen xs ]
At each step you take all the lists generated by the previous step and combine each of them with every result of someOp.
You can verify that this degrades into your special cases by substitution.
For the function you here construct, a more generic function already exists: traverse :: (Applicative f, Traversable t) => (a -> f b) -> t a -> f (t b). Your gen is equivalent to:
gen = traverse someOp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With