
Hi Luke,
Your fix worked. Thanks very much. I guess I thought that x was local to
primeQ and when I called f, with y, it was just passing a value. As I was
this response to you, I realized that I'm also passing x itself, oops. So I
tried setting z=floor x, and passing z instead of x, and that also worked!
A little light is beginning to glimmer.
This will take a little getting used to. Thanks very much.
Mitchell
-----Original Message-----
From: Luke Palmer [mailto:lrpalmer@gmail.com]
Sent: Sunday, April 25, 2010 12:58 AM
To: mitchell@kaplan2.com
Cc: haskell-cafe@haskell.org
Subject: Re: [Haskell-cafe] I need help getting started
On Sat, Apr 24, 2010 at 10:34 PM,
Hi,
Im just starting to learn, or trying to learn Haskell. I want to write a function to tell me if a numbers prime. This is what Ive got:
f x n y = if n>=y
then True
else
if gcd x n == 1
then f x (n+1) y
else False
primeQ x = f x 2 y
where y = floor(sqrt(x))
Pretty good so far. The only trouble is that the type of x is inconsistent. In f it is an integer, but in primeQ it is a floating point (because you are taking its square root). Getting past this just involves understanding Haskell's type system peculiarities. Change that last line to: where y = floor (sqrt (fromIntegral x)) And you should be fine. (Untested) In the future, post the error that you are getting addition to the code that is causing it. That helps us find it faster. Luke