DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Euler Problem 4 In Haskell
// A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99. // // Find the largest palindrome made from the product of two 3-digit numbers. // // There are shorter ways to do this, and I cheat a bit by counting down to 900 rather than 100, but this one short-circuits when it finds the answer, // and so is very efficient.
isPalindrome :: String -> Bool isPalindrome  = True isPalindrome str = let str2 = reverse str in (str2 == str) factors :: [(Int, Int)] factors = [(x,y) | x <- reverse [900..999], y <- reverse [900..999]] findPal :: [(Int, Int)] -> Int findPal  = 0 findPal ((x,y):xs) = let pal = isPalindrome $ show mult mult = x * y in case pal of True -> mult False -> findPal xs main = do return (findPal factors)