Search code examples
haskellioprimes

outputting a result is much slower that just executing its function?


we are doing the project euler challenges with Haskell. Currently we're at no 7 "find the 10001st prime number" Now we have a simple filter algorithm, nothing fancy but the calculation time varies widely with how you call the function.

primes = filterPrime [2..]
  where filterPrime (p:xs) =
          p : filterPrime [x | x <- xs, x `mod` p /= 0]

when using ghci with primes and just stopping when you see the numbers go over 100000 or so it usually runs ~3 seconds or so when using primes !! 10001 we have significantly longer runtimes up to 20 or 25 seconds. Once the list is filled, you can call the 10001st element instantly both ways.

Does anyone have a good explanation on how this works or why it takes so long in the second method?


Solution

  • local problem, needs own troubleshooting