[Haskell-beginners] problem exercise 3 page 60 Programming in Haskell

Daniel Seidel ds at iai.uni-bonn.de
Fri Jul 22 19:22:28 CEST 2011


Hi Roelof,

I don't have a book, so I don't know the exercise exactly, but if I'm
correct, you want the list 

[(1,4), (1,5), (1,6), (2,4), (2,5), (2,6), (3,4), (3,5), (3,6)]

So the basic idea is to nest one list comprehension into another:

You create a list [(x,4), (x,5), (x,6)] by the inner list comprehension
with  y <- [4..6] as generator.
It should be [(x,y) | y <- [4..6]], where x is just free.

The whole comprehension you stick as result into an outer comprehension
whos generator creates the values for x (ie. 1,2 and 3).

This will return the list of lists
[[(1,4), (1,5), (1,6)],[(2,4), (2,5), (2,6)], [(3,4), (3,5), (3,6)]]
which you can flatten by concat.

I hope you manage the solution with the description above. I think it
helps you more if I don't write the plain solution as working code.


Cheers,

Daniel.

 

On Fri, 2011-07-22 at 16:48 +0000, Roelof Wobben wrote:
> Hello, 
> 
> I don't see the answer here.
> If I brake down the problem.
> 
> We have this : 
> 
> [(x,y) | x <- [1..3], y <- [4,5,6]]
> 
> I have to use one generator with two nested list compreshession and
> make use of concat.
> 
> So the x generator  is [x | x <- [1..3]] 
> and the Y genarator is [y| y <- [4..6]]
> 
> So if I put it together it uses the numbers [1..6] so I could do
> something like this  in pseudo code as guard.
> If generator smaller or equal 3 then its x else it's a y.
> 
> But if I do concat [x,y] then I get [1..6] and that not good.
> If I use zip [x,y] then I get [ (1,4) (2,5) (3,6)] which is also not
> good but better.
> 
> So im stuck now and im puzzeling the whole afternoon about this ?
> 
> Anyone who can give me a hint ?
> 
> Roelof
> 
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners




More information about the Beginners mailing list