Re: [Haskell-cafe] A tale of Project Euler

2007-11-30 Thread Felipe Lessa
On Nov 30, 2007 5:39 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: > Hmm. Secret... library... How do you guys find out about all this stuff? There's http://www.haskell.org/haskellwiki/Arrays#Unsafe_indexing.2C_freezing.2Fthawing.2C_running_over_array_elements . Cheers, -- Felipe. _

Re: [Haskell-cafe] A tale of Project Euler

2007-11-30 Thread Brent Yorgey
On Nov 30, 2007 2:39 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: > Sebastian Sylvan wrote: > > On Nov 29, 2007 9:10 PM, Andrew Coppin <[EMAIL PROTECTED]> > wrote: > > > >> How do you avoid accidentally recomputing the list multiple times? > >> > > > > What do you mean? It's exactly the same as yo

Re: [Haskell-cafe] A tale of Project Euler [ST]

2007-11-30 Thread Andrew Coppin
Bulat Ziganshin wrote: Hello Andrew, Friday, November 30, 2007, 12:10:16 AM, you wrote: I don't understand the ST monad. From what I can tell, it's not definable without using strange language extensions. (I don't really like using things where it's unclear why it works.)

Re: [Haskell-cafe] A tale of Project Euler

2007-11-30 Thread Andrew Coppin
Sebastian Sylvan wrote: On Nov 29, 2007 9:10 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: How do you avoid accidentally recomputing the list multiple times? What do you mean? It's exactly the same as your original program but with ST instead of IO? Why would it get accidentally recompu

Re: [Haskell-cafe] A tale of Project Euler

2007-11-30 Thread Henning Thielemann
On Fri, 30 Nov 2007, Daniel Fischer wrote: > Am Freitag, 30. November 2007 14:39 schrieb Henning Thielemann: > > > Is this thread still about the prime sieve? As I mentioned, I think one > > can avoid the mutable array, because if there is only a small number of > > array updates with much change

Re: [Haskell-cafe] A tale of Project Euler

2007-11-30 Thread Daniel Fischer
Am Freitag, 30. November 2007 14:39 schrieb Henning Thielemann: > Is this thread still about the prime sieve? As I mentioned, I think one > can avoid the mutable array, because if there is only a small number of > array updates with much changes per update, it should be efficient enough > to copy

Re[2]: [Haskell-cafe] A tale of Project Euler

2007-11-30 Thread Henning Thielemann
On Fri, 30 Nov 2007, Bulat Ziganshin wrote: > Hello Andrew, > > Thursday, November 29, 2007, 9:43:48 PM, you wrote: > > >> Fifth thing: better use an STUArray, don't drag IO in if it's not > >> necessary. > >> > > > I don't understand the ST monad. > > it's just a subset of IO monad, with rename

Re: [Haskell-cafe] A tale of Project Euler

2007-11-30 Thread Henning Thielemann
On Thu, 29 Nov 2007, Andrew Coppin wrote: > Sebastian Sylvan wrote: > > On Nov 29, 2007 6:43 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: > > > >> I don't understand the ST monad. > > > > There's not a whole lot to understand if you just want to use it > > (though it's all very cool from a theore

Re[2]: [Haskell-cafe] A tale of Project Euler

2007-11-30 Thread Bulat Ziganshin
Hello Sebastian, Friday, November 30, 2007, 11:31:22 AM, you wrote: >> I don't see Data.Array.Base documented anywhere. (How did you know it >> exists?) i use library sources as reference. it allows to study implementation and learn good programming style -- Best regards, Bulat

Re: [Haskell-cafe] A tale of Project Euler

2007-11-30 Thread Sebastian Sylvan
On Nov 29, 2007 9:10 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: > Sebastian Sylvan wrote: > > On Nov 29, 2007 6:43 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: > > > >> I don't understand the ST monad. > >> > > > > > > There's not a whole lot to understand if you just want to use it > > (though i

Re[2]: [Haskell-cafe] A tale of Project Euler

2007-11-29 Thread Bulat Ziganshin
Hello Andrew, Thursday, November 29, 2007, 9:43:48 PM, you wrote: >> Fifth thing: better use an STUArray, don't drag IO in if it's not necessary. >> > I don't understand the ST monad. it's just a subset of IO monad, with renamed operations the subset is selected in the way that guarantees r

Re[2]: [Haskell-cafe] A tale of Project Euler

2007-11-29 Thread Bulat Ziganshin
Hello Andrew, Friday, November 30, 2007, 12:10:16 AM, you wrote: >>> I don't understand the ST monad. > From what I can tell, it's not definable without using strange language > extensions. (I don't really like using things where it's unclear why it > works.) this extension used only to guaran

Re: [Haskell-cafe] A tale of Project Euler

2007-11-29 Thread Jan-Willem Maessen
On Nov 29, 2007, at 6:19 PM, Stefan O'Rear wrote: On Thu, Nov 29, 2007 at 09:10:16PM +, Andrew Coppin wrote: Sebastian Sylvan wrote: On Nov 29, 2007 6:43 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: I don't understand the ST monad. ...[and ST uses language extensions Andrew doesn't un

Re: [Haskell-cafe] A tale of Project Euler

2007-11-29 Thread Stefan O'Rear
On Thu, Nov 29, 2007 at 09:10:16PM +, Andrew Coppin wrote: > Sebastian Sylvan wrote: >> On Nov 29, 2007 6:43 PM, Andrew Coppin <[EMAIL PROTECTED]> >> wrote: >> >>> I don't understand the ST monad. >>> >> >> >> There's not a whole lot to understand if you just want to use it >> (though

Re: [Haskell-cafe] A tale of Project Euler

2007-11-29 Thread Daniel Fischer
Am Donnerstag, 29. November 2007 19:43 schrieb Andrew Coppin: > Daniel Fischer wrote: > > One thing: since You check the array bounds, the system needn't check > > them again, use unsafeWrite and unsafeRead. And use Int for the index, > > that would be MUCH faster. > > I can't find the functions yo

Re: [Haskell-cafe] A tale of Project Euler

2007-11-29 Thread Andrew Coppin
Sebastian Sylvan wrote: On Nov 29, 2007 6:43 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: I don't understand the ST monad. There's not a whole lot to understand if you just want to use it (though it's all very cool from a theoretical standpoint too). From what I can tell, it's not d

Re: [Haskell-cafe] A tale of Project Euler

2007-11-29 Thread Sebastian Sylvan
On Nov 29, 2007 6:43 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: > Daniel Fischer wrote: > > One thing: since You check the array bounds, the system needn't check them > > again, use unsafeWrite and unsafeRead. And use Int for the index, that would > > be MUCH faster. > > > > I can't find the func

Re: [Haskell-cafe] A tale of Project Euler

2007-11-29 Thread Andrew Coppin
Daniel Fischer wrote: One thing: since You check the array bounds, the system needn't check them again, use unsafeWrite and unsafeRead. And use Int for the index, that would be MUCH faster. I can't find the functions you're talking about. I have however changed the index type. (Make little

Re: [Haskell-cafe] A tale of Project Euler

2007-11-28 Thread Brandon S. Allbery KF8NH
On Nov 28, 2007, at 16:28 , Andrew Coppin wrote: Michaeljohn Clement wrote: Andrew Coppin wrote: First, somebody else wrote this in C: int n = 2 , m , primesFound = 0; for( n=0;n < MAX_NUMBERS;n++ ) if( prime[n] ) { primesFound++; if( primesFound == 10001 ) cout << n << " is the 10001st

Re: [Haskell-cafe] A tale of Project Euler

2007-11-28 Thread Sebastian Sylvan
On Nov 28, 2007 9:28 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: > Michaeljohn Clement wrote: > > Andrew Coppin wrote: > > > >> First, somebody else wrote this in C: > >> > >> int n = 2 , m , primesFound = 0; > >> > >> for( n=0;n < MAX_NUMBERS;n++ ) > >> if( prime[n] ) > >> { > >> primesFound++; >

Re: [Haskell-cafe] A tale of Project Euler

2007-11-28 Thread Daniel Fischer
Am Mittwoch, 28. November 2007 22:31 schrieb Andrew Coppin: > There are problems for which it's important to know how many times a > given prime factor occurs. And there are other problems where it is > merely necessary to know which primes are factors. I would say it's > useful to have *both* func

Re: [Haskell-cafe] A tale of Project Euler

2007-11-28 Thread Andrew Coppin
Michaeljohn Clement wrote: Andrew Coppin wrote: First, somebody else wrote this in C: int n = 2 , m , primesFound = 0; for( n=0;n < MAX_NUMBERS;n++ ) if( prime[n] ) { primesFound++; if( primesFound == 10001 ) cout << n << " is the 10001st prime." << endl; Um, I can't *believe* nob

Re: [Haskell-cafe] A tale of Project Euler

2007-11-28 Thread Andrew Coppin
Sebastian Sylvan wrote: On Nov 28, 2007 12:12 PM, Kalman Noel <[EMAIL PROTECTED]> wrote: Sebastian Sylvan: primes :: [Integer] primes = 2 : filter (null . primeFactors) [3,5..] primeFactors :: Integer-> [Integer] primeFactors n = factor n primes where factor m (p:ps) | p*p

Re: [Haskell-cafe] A tale of Project Euler

2007-11-28 Thread Sebastian Sylvan
On Nov 28, 2007 12:12 PM, Kalman Noel <[EMAIL PROTECTED]> wrote: > Sebastian Sylvan: > > primes :: [Integer] > > primes = 2 : filter (null . primeFactors) [3,5..] > > > > primeFactors :: Integer-> [Integer] > > primeFactors n = factor n primes > > where > > factor m (p:ps) | p*p > m

Re: [Haskell-cafe] A tale of Project Euler

2007-11-28 Thread Kalman Noel
Sebastian Sylvan: > primes :: [Integer] > primes = 2 : filter (null . primeFactors) [3,5..] > > primeFactors :: Integer-> [Integer] > primeFactors n = factor n primes > where > factor m (p:ps) | p*p > m= [] > | m `mod` p == 0 = p : factor (m `div` p) (p:

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Andrew Coppin
Don Stewart wrote: This is an FAQ. Unless you use the same algorithm and data types in benchmarks, you're not really benchmarking anything. And expecting one of the worst possible algorithms to be good is hoping for a little too much :) Well, if I was comparing my Haskell against some expe

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Andrew Coppin
Brent Yorgey wrote: The algorithm you use to compute primes is actually quite inefficient, and in particular, it is NOT the same algorithm that the C program is using, despite first appearances! The call to 'filter' in the sieve function works by checking *every* number for divisibility by p,

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Olivier Boudry
On 11/27/07, Sebastian Sylvan <[EMAIL PROTECTED]> wrote: > > That is indeed a nice and clear version that's pretty fast. It's > basically the same as the C version except "backwards" (i.e. examine a > number and work backwards through its divisors, rather than filling in > a "map" of all multiples

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Yitzchak Gale
Andrew Coppin wrote: > On the other hand, I must relay to you how much fun I had with certain > other problems. You may want to look at: http://haskell.org/haskellwiki/Euler_problems and make some contributions. But be very careful what you peek at, so don't spoil your own fun. Regards, Yitz __

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Andrew Coppin
On the other hand, I must relay to you how much fun I had with certain other problems. For example, problem #12. I started with this: triangles = scanl1 (+) [1..] divisors n = length $ filter (\x -> n `mod` x == 0) [1..n] answer = head $ dropWhile (\n -> divisors n < 500) triangles Sadly,

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Sebastian Sylvan
On Nov 27, 2007 8:44 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: > Andrew Coppin wrote: > > Also, I'm stuck with problem #10. (Find the sum of all primes less > > than 1 million.) I've let the program run for well over 15 minutes, > > and still no answer is forthcomming. It's implemented using the

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Sebastian Sylvan
On Nov 27, 2007 8:54 PM, Olivier Boudry <[EMAIL PROTECTED]> wrote: > > Hi Andrew, > > I don't remember who I stole this prime computation from, but it is very > fast (10001's prime in 0.06 sec with Int and 0.2 with Integer on my machine) > and not overly complex: > > primes :: [Integer] > prime

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Andrew Coppin
Andrew Coppin wrote: Also, I'm stuck with problem #10. (Find the sum of all primes less than 1 million.) I've let the program run for well over 15 minutes, and still no answer is forthcomming. It's implemented using the same primes function as above, with a simple filter and sum. (The type has

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Yitzchak Gale
Brent Yorgey wrote: > For more information you might want to read this paper, which includes a > much better primes implementation: > www.cs.hmc.edu/~oneill /papers/Sieve-JFP.pdf > In fact, this very same topic was discussed on the list a while back, you > can read it here: > http://thread.gmane.or

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Henning Thielemann
On Tue, 27 Nov 2007, Andrew Coppin wrote: > So, now I have a Haskell version that's "only" several hundred times > slower. Neither program is especially optimised, yet the C version is > drastically faster. This makes me sad. :-( I think the C version is so much faster because it does not need a

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Don Stewart
andrewcoppin: > Hi guys. > > Somebody just introduced me to a thing called "Project Euler". I gather > it's well known around here... > > Anyway, I was a little bit concerned about problem #7. The problem is > basically "figure out what the 10,001st prime number is". Consider the > following t

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Olivier Boudry
On 11/27/07, Andrew Coppin <[EMAIL PROTECTED]> wrote: > > Hi guys. > > Somebody just introduced me to a thing called "Project Euler". I gather > it's well known around here... > > Anyway, I was a little bit concerned about problem #7. The problem is > basically "figure out what the 10,001st prime n

Re: [Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Brent Yorgey
On Nov 27, 2007 2:34 PM, Andrew Coppin <[EMAIL PROTECTED]> wrote: > Hi guys. > > Somebody just introduced me to a thing called "Project Euler". I gather > it's well known around here... > > Anyway, I was a little bit concerned about problem #7. The problem is > basically "figure out what the 10,00

[Haskell-cafe] A tale of Project Euler

2007-11-27 Thread Andrew Coppin
Hi guys. Somebody just introduced me to a thing called "Project Euler". I gather it's well known around here... Anyway, I was a little bit concerned about problem #7. The problem is basically "figure out what the 10,001st prime number is". Consider the following two programs for solving this