[Haskell-beginners] problem exercise 3 page 60 Programming in Haskell
David Place
d at vidplace.com
Fri Jul 22 19:16:45 CEST 2011
On Jul 22, 2011, at 12:48 PM, 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 liOt 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.
You won't need a guard to do this.
>
> 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 ?
I think that this exercise is very artificial. It is very easy to solve if you know how list comprehensions are implemented using map and other library functions. Personally, Roelof, I think you will be better off to forget about list comprehensions for the present and focus on understanding basic functions on lists like map, filter and concat.
I'll give you the answer to the question because, I can't think of any hints. Don't look if you want to figure it out yourself.
> concat [[(x,y) | y <- [4,5,6]] | x <- [1..3]]
>
> Roelof
>
> _______________________________________________
> 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/20110722/3dc44d95/attachment.htm>
More information about the Beginners
mailing list