
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.

It needs to read x <- xs
You are wrapping a list in a list constructor :-)
On 11 May 2011 12:37, "Hamster"

On Wed, 11 May 2011 13:36:30 +0200, Hamster
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?
Change the function to: combinations xs ys = [ (x,y) | x <- xs, y <- ys] xs and ys are already lists; if you put square brackets around them, you get a list with another list as the only element. Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --
participants (3)
-
Benjamin Edwards
-
Hamster
-
Henk-Jan van Tuyl