[Haskell-cafe] Re: (possibly) a list comprehensions question
yairchu at gmail.com
yairchu at gmail.com
Thu Nov 19 09:42:05 EST 2009
> This is what happens all the time, the thing I am trying to implement is
already in the library!
You can easily find those library functions using Hoogle.
In this case Hoogle gives the "sequence" function as its second result
for the query "[[a]] -> [[a]]":
Data.List transpose :: [[a]] -> [[a]]
Prelude sequence :: Monad m => [m a] -> m [a]
See: http://haskell.org/hoogle/?hoogle=%5B%5Ba%5D%5D+-%3E+%5B%5Ba%5D%5D
On Nov 19, 3:58 pm, Ozgur Akgun <ozgurak... at gmail.com> wrote:
> 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 <nc... at kent.ac.uk>
>
>
>
>
>
> > 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
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-C... at haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe
More information about the Haskell-Cafe
mailing list