
Thanks for the both quick answers.
This is what happens all the time, the thing I am trying to implement is
already in the library!
Neil,
I clearly understood the way you implemented. Actually I had a similar
implementation with some problems, one of which is the base-case you
mentioned.
Eugene,
I'll definitely have a look at the implementation of sequence.
Cheers!
2009/11/19 Neil Brown
Ozgur Akgun wrote:
Anyway, just forget the fact that these funstions do not do a check on the length of the input list for a moment. My question is, how can I generalize this function to accept a list of lists of arbitrary length, and produce the required result.
Hi,
The concise solution is the list monad, as already posted. If that confuses you, here is a version using list comprehensions (well, mostly):
allPossibilities :: [[a]] -> [[a]] allPossibilities [] = [[]] allPossibilities (l:ls) = [ x : xs | x <- l, xs <- allPossibilities ls]
The second line prefixes all possibilities from the later lists with every element from the the first list. Note that the base-case is crucial; if it is the empty list [], that "xs <- allPossibilities ls" will not find any elements, and thus the list comprehension becomes the empty list, and the whole thing falls apart. Thus the base case must be the list containing the empty list, so that you have one possibility arising at the end upon which to prefix the items. Hope that makes sense...
Thanks,
Neil.
-- Ozgur Akgun