Oh my head. Is that toast I smell? I’m actually getting headaches from trying to understand Haskell. Functional programming is totally different fron Imperative. But its also very powerful, especially for mathematically inclined Euler problems. That being said, what the hell is a Monad?
Admittedly I found much of the following code online, but I changed it a bit for my purposes. I compiled it using GHC on a dual-core 6-year old PC and it ran in 13 minutes, 11 seconds! For those of you playing at home, that’s a 819% speed improvement over Python! (see previous post). Where’s the Advil?
import Data.Time oddDigits :: Integer -> Bool oddDigits n | n < 10 = mod n 2 == 1 | otherwise = (mod (mod n 10) 2) == 1 && oddDigits (div n 10) rev :: Integer -> Integer -> Integer rev n acc | n < 10 = n + (10 * acc) | otherwise = rev (div n 10) ((mod n 10) + (10 * acc)) isReverse :: Integer -> Bool isReverse n | mod n 10 == 0 = False | otherwise = oddDigits (n + (rev n 0)) main = do start <- getCurrentTime print (length (filter isReverse [1..10^9])) stop <- getCurrentTime print $ diffUTCTime stop start