[Haskell-cafe] list comprehension doesn't work

Daniel Díaz Casanueva dhelta.diaz at gmail.com
Tue May 14 19:21:24 CEST 2013


Hi John,


On Tue, May 14, 2013 at 5:41 PM, John <knowledge1202 at gmail.com> wrote:

> Danny Gratzer wrote
> > Well you've deleted the portion of the code referring to x and y.
> >
> > listPairs = [(a*b, y) | y <- [0..], a <- [0..], b <- [0..], (a*b) > 5,
> > (a*b) < 500, (y*y) < 1001, mod y (a*b) == 0]
> >
> > This will still never terminate however.
>
> oh I see, but as you say it doesn't terminate and I get nothing. Does it
> mean, that the function is wrong in this place?
>
> Some questions:
> 1. Does the order of conditions affect the result at all?
> 2. The "," means AND or &&, right? So how do you write OR || instead? E.g
> z<-[1..10] OR z<-[100..110].
> Ofcourse it doesn't relate to this topic, but I wanted to know it.
>
> Since I'm a very beginner I think the approach of Daniel is slightly
> complex
> for me to comprehend, right?
> Allthough I tried it, but it says isProduct and isSquer are not in Scope,
> so
> I should define them first in WHERE-Clause right?
>

Yes. The idea is to reduce a problem to simpler subproblems. It is a common
practice while programming, and often will make your code clearer. Where do
you find the difficulty? The (incomplete) solution I wrote was:

 [ (x,y) | x <- [6 .. 499] ,  y <- [0 .. 1000] , isProduct x , isSquare y ,
mod y x == 0 ]

which you can read as:

*The list of the pairs of numbers x and y, where x belongs to {6, ... ,
499}, y belongs to {0, ... , 1000}, x is a product of two numbers, y is the
square of a number and y MOD x is 0.*

These look like the conditions you asked for. So you can say that you
solved the problem of generating the list, if you know how to decide if a
number is a product or a square in the first place. You can define these
two functions separately.

isProduct :: Int -> Bool
isProduct n = ...

isSquare :: Int -> Bool
isSquare n = ...

The question of if a number is a product or not is equivalent to the
question of checking if a number is prime or not. Indeed, If it is prime,
it is not a product of two natural numbers (I am, of course, guessing that
you don't want ones in the product). If it is NOT prime, then you can
factor the number.

To solve the "isSquare" problem, you can just check if it coincides with
the square of any number below the square root of the number in question.

I think you don't want an explicit solution, but some hints, so you can
explore the problem deeper and learn from it.

I must also note, as other people have already done, that this questions
fits in better in the Haskell-Beginners mailing list.

http://www.haskell.org/mailman/listinfo/beginners

I hope this works for you,
Daniel Díaz.


>
> Thanks again to all
>
>
>
>
> --
> View this message in context:
> http://haskell.1045720.n5.nabble.com/list-comprehension-doesn-t-work-tp5730158p5730167.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



-- 
E-mail sent by Daniel Díaz Casanueva

let f x = x in x
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130514/90c1522e/attachment.htm>


More information about the Haskell-Cafe mailing list