[Haskell-beginners] Why is this function that slow?

Andreas Flierl andreas at flierl.eu
Wed Jul 28 18:29:56 EDT 2010


Hello everyone,

I am a newbie to Haskell coming from a Java/Scala/Ruby background. As first few exercises, I was trying to translate my Scala solutions for some Project Euler [1] problems to Haskell. The function that solves problem 4 turns out to be quite slow (6s in Haskell as opposed to 400ms in Scala) and I cannot understand why. Here's the function:

euler4 = solve 100 999
  where solve min max = maximum palindromes
          where palindromes    = [n | a <- range, b <- range, let n = a * b, isPalindrome n]
                range          = [max, max - 1 .. min]
                isPalindrome n = (n :: Int) == (read $ reverse $ show n)

Using +RTS -sstderr I see that the allocation rate is 12M/s, which seems rather high to me. My guess would be that this is somehow related to lazy evaluation but all in all, I've got no real clue and would be thankful for any advice.

Thank you
Andreas

[1] http://www.projecteuler.net


More information about the Beginners mailing list