[Haskell-beginners] List Comprehensions

Benjamin Edwards edwards.benj at gmail.com
Wed May 11 13:42:54 CEST 2011


It needs to read x <- xs

You are wrapping a list in a list constructor :-)

On 11 May 2011 12:37, "Hamster" <machamster at gmail.com> wrote:

Hello,

I am trying to understand list comprehensions (I am very new to
Haskell) and there's one thing I simply don't understand.

If I have code that looks like:

combinations = [ (x,y) | x <-[1,2,3], y<-[1,2,3]]

Then it happily gives me all the possible combinations of x and y.
[(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)]

However...

If I change the function to look like:

combinations xs ys = [ (x,y) | x <-[xs], y<-[ys]]

The output changes to:
[([1,2,3],[1,2,3])]

I don't understand what's going on there. Is there any way I can get
it to produce all the combinations of a user supplied list?

Thanks

H.

_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110511/544c6b62/attachment.htm>


More information about the Beginners mailing list