
Hi, I'm just starting to learn, or trying to learn Haskell. I want to write a function to tell me if a number's prime. This is what I've 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)) The compiler is very unhappy. Apparently there's some ambiguity in this, and I'm kind of running around in circles. I'd appreciate any help that will get me started.

I'm just starting to learn, or trying to learn Haskell. I want to write a function to tell me if a number's prime. This is what I've got:
Have you read through any tutorials? Do you need to write this prime detection function yourself (if so, is this homework?) or can you use a pre-defined function? -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

Hi Ivan,
I have read through parts of the tutorials, and also purchased "Real World
Haskell" - O'Reilly. It's not homework, if I were that young I might have
more patience with my progress.
Actually I going through the problems in ProjectEuler, and primarily using
Mathematica. But it's slow. I thought that this would be a good
opportunity to learn Haskell. I read about the various functional languages
available, ocaml, ML, Erlang and flirted briefly with F#. I wanted to find
one that was fast. Haskell looked like a good choice. Also, many of the
people solving the ProjectEuler problems were using it (and of course a
million other languages).
I could definitely use a library function. I looked and found 2 libraries
that seemed to have one. However, I am beginning and haven't really learned
enough to figure out how to load the libraries. Also, I wanted a
deterministic one, and I think I recall that one was probabilistic.
Thanks for getting back to me.
Mitchell
-----Original Message-----
From: Ivan Lazar Miljenovic [mailto:ivan.miljenovic@gmail.com]
Sent: Sunday, April 25, 2010 12:43 AM
To: mitchell@kaplan2.com
Cc: haskell-cafe@haskell.org
Subject: Re: [Haskell-cafe] I need help getting started
I'm just starting to learn, or trying to learn Haskell. I want to write a function to tell me if a number's prime. This is what I've got:
Have you read through any tutorials? Do you need to write this prime detection function yourself (if so, is this homework?) or can you use a pre-defined function? -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

On Sat, Apr 24, 2010 at 10:34 PM,
Hi,
I’m just starting to learn, or trying to learn Haskell. I want to write a function to tell me if a number’s prime. This is what I’ve 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

Mitchell
You might also be interested in the beginners mailing list. I've been
enjoying for about a month now!
http://www.haskell.org/mailman/listinfo/beginners
On Sat, Apr 24, 2010 at 9:57 PM, Luke Palmer
On Sat, Apr 24, 2010 at 10:34 PM,
wrote: Hi,
I’m just starting to learn, or trying to learn Haskell. I want to write a function to tell me if a number’s prime. This is what I’ve 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 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

John,
Thanks, I didn't know it was there. I'll subscribe to it.
Mitchell
_____
From: John Bender [mailto:john.m.bender@gmail.com]
Sent: Sunday, April 25, 2010 3:01 AM
To: Luke Palmer
Cc: mitchell@kaplan2.com; haskell-cafe@haskell.org
Subject: Re: [Haskell-cafe] I need help getting started
Mitchell
You might also be interested in the beginners mailing list. I've been
enjoying for about a month now!
http://www.haskell.org/mailman/listinfo/beginners
On Sat, Apr 24, 2010 at 9:57 PM, Luke Palmer
Hi,
I'm just starting to learn, or trying to learn Haskell. I want to write a function to tell me if a number's prime. This is what I've 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 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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

Am Sonntag 25 April 2010 06:34:32 schrieb mitchell@kaplan2.com: Luke already explained the type error, so I'll focus on the implementation.
Hi,
I'm just starting to learn, or trying to learn Haskell. I want to write a function to tell me if a number's prime. This is what I've got:
f x n y = if n>=y
Since you call it with y = floor (sqrt (fromIntegral x)), the test ought to be "n > y" or you'll classify squares of primes as primes. PrimeTest> primeQ 25 True Oops.
then True else if gcd x n == 1
This is doing a lot of unnecessary work. If gcd x n > 1, then one of the prime divisors of n must also divide x. If n is composite, this test is never reached, otherwise (n is prime) n divides x, so you need only test whether n divides x, if x `rem` n /= 0 This test needs only one division, whereas calculating the gcd needs O(log n) divisions on average. For larger primes, that's a big difference: Prelude PrimeTest> primeF 1000000000039 True (0.27 secs, 85607928 bytes) Prelude PrimeTest> primeQ 1000000000039 True (1.38 secs, 457966212 bytes)
then f x (n+1) y else False
A stylistic remark, if condition then True else somethingMore is often better expressed as condition || somethingMore or using guards, so you could write f as f x n y = (n > y) || (x `rem` n /= 0 && f x (n+1) y) or f x n y | n > y = True | x `rem` n == 0 = False | otherwise = f x (n+1) y
primeQ x = f x 2 y where y = floor(sqrt(x))
The compiler is very unhappy. Apparently there's some ambiguity in this, and I'm kind of running around in circles. I'd appreciate any help that will get me started.

Hi Daniel, I haven't yet absorbed everything you've written. One thing though, I didn't know how long division takes, but yeah, obviously gcd would take longer. Also when I put in the correction based on Luke's note, I found out about the perfect square problem. With regard to style, thanks for the suggestions, I'll have to spend a little more time going over that. This was my first posting to this list, and you and your colleagues have given me a real boost. Thanks so much, Mitchell -----Original Message----- From: daniel.is.fischer@web.de [mailto:daniel.is.fischer@web.de] Sent: Sunday, April 25, 2010 4:47 AM To: haskell-cafe@haskell.org Cc: mitchell@kaplan2.com Subject: Re: [Haskell-cafe] I need help getting started Am Sonntag 25 April 2010 06:34:32 schrieb mitchell@kaplan2.com: Luke already explained the type error, so I'll focus on the implementation.
Hi,
I'm just starting to learn, or trying to learn Haskell. I want to write a function to tell me if a number's prime. This is what I've got:
f x n y = if n>=y
Since you call it with y = floor (sqrt (fromIntegral x)), the test ought to be "n > y" or you'll classify squares of primes as primes. PrimeTest> primeQ 25 True Oops.
then True else if gcd x n == 1
This is doing a lot of unnecessary work. If gcd x n > 1, then one of the prime divisors of n must also divide x. If n is composite, this test is never reached, otherwise (n is prime) n divides x, so you need only test whether n divides x, if x `rem` n /= 0 This test needs only one division, whereas calculating the gcd needs O(log n) divisions on average. For larger primes, that's a big difference: Prelude PrimeTest> primeF 1000000000039 True (0.27 secs, 85607928 bytes) Prelude PrimeTest> primeQ 1000000000039 True (1.38 secs, 457966212 bytes)
then f x (n+1) y else False
A stylistic remark, if condition then True else somethingMore is often better expressed as condition || somethingMore or using guards, so you could write f as f x n y = (n > y) || (x `rem` n /= 0 && f x (n+1) y) or f x n y | n > y = True | x `rem` n == 0 = False | otherwise = f x (n+1) y
primeQ x = f x 2 y where y = floor(sqrt(x))
The compiler is very unhappy. Apparently there's some ambiguity in this, and I'm kind of running around in circles. I'd appreciate any help that will get me started.

On Sun, Apr 25, 2010 at 6:34 AM,
Hi,
I’m just starting to learn, or trying to learn Haskell. I want to write a function to tell me if a number’s prime. This is what I’ve 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))
The compiler is very unhappy. Apparently there’s some ambiguity in this, and I’m kind of running around in circles. I’d appreciate any help that will get me started.
I won't go over your code, since more skilled people than me are already helping you out with it. However, once you've finished your own implementation, you may be interested in http://blog.natulte.net/posts/2010-04-23-delicious-primes.html . It may give you some interesting insights, imho as the post's author :-). - Dave
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi David,
Thanks for the suggestion. I took a quick look at your article, and I'll
have to spend a little more time on it. "Delicious Primes?" Great name.
Also I appreciate your correct use of the phrase "begging the question".
The common and incorrect use that I hear constantly is one of my pet peeves.
Mitchell
-----Original Message-----
From: David Anderson [mailto:dave@natulte.net]
Sent: Sunday, April 25, 2010 11:18 AM
To: mitchell@kaplan2.com
Cc: haskell-cafe@haskell.org
Subject: Re: [Haskell-cafe] I need help getting started
On Sun, Apr 25, 2010 at 6:34 AM,
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))
The compiler is very unhappy. Apparently theres some ambiguity in this, and Im kind of running around in circles. Id appreciate any help that will get me started.
I won't go over your code, since more skilled people than me are already helping you out with it. However, once you've finished your own implementation, you may be interested in http://blog.natulte.net/posts/2010-04-23-delicious-primes.html . It may give you some interesting insights, imho as the post's author :-). - Dave
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Am Sonntag 25 April 2010 17:49:05 schrieb mitchell@kaplan2.com:
Hi David,
Thanks for the suggestion. I took a quick look at your article, and I'll have to spend a little more time on it. "Delicious Primes?" Great name.
And it's a good read. "I find this definition of prime numbers absolutely delightful. It has a wonderful zen-like quality to it, and yet successfully gets away with defining the list of prime numbers in terms of itself." Very well put.
Also I appreciate your correct use of the phrase "begging the question". The common and incorrect use that I hear constantly is one of my pet peeves.
I fully agree.
Mitchell
participants (6)
-
Daniel Fischer
-
David Anderson
-
Ivan Lazar Miljenovic
-
John Bender
-
Luke Palmer
-
mitchell@kaplan2.com