[Haskell-beginners] What's the difference between those two solution?

Haisheng Wu freizl at gmail.com
Tue Dec 13 04:49:03 CET 2011


Hello,
  I'm trying to solve Euler problem 104 with the solution "My Solution"
below but it takes quite long time therefore I quite.
  Then I turn to haskell wiki for better solution which work well but I can
not figure out why it is better than mine.
  I'm wondering whether more function call decrease the performance.

  Could you please help a little?
  Thank you.

*-- | My Solution *
main = print $ snd $ head $ dropWhile (\(x,y) -> (not . bothNinePandigit
"123456789") x) (zip fibs [1..])

bothNinePandigit digits n = isFirstNinePandigit digits n &&
isLastNinePandigit digits n

isLastNinePandigit  digits n = digits == sort (lastDigits 9 n)
isFirstNinePandigit digits n = digits == sort (firstDigits 9 n)

firstDigits k n = take k (show n)
lastDigits  k n = show (n `mod` 10^k)

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

*-- | From Haskell Wiki *
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)

isFibPan n =
  let a = n `mod` 1000000000
      b = sort (show a)
      c = sort $ take 9 $ show n
  in  b == "123456789" && c == "123456789"

ex_104 = snd $ head $ dropWhile (\(x,y) -> (not . isFibPan) x) (zip fibs
[1..])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111213/1cecea7c/attachment.htm>


More information about the Beginners mailing list