Skip to main content
added 628 characters in body
Source Link
Zeta
  • 19.6k
  • 2
  • 57
  • 90

Your code is fine, but I would suggest some small changes. Instead of head . transpose, I would use concatMap (take 1). This captures your intend to take the first (and therefore largest) number from each allPalindrome.

Next, I would use Int instead of (Integral a, Show a), since 999 * 999 is smaller than maxBound :: Int. Why? Because by default, Integer will be used for Integral types, if they were note specified. Therefore, you end up with maxPalindrome handled as a Integer, which is slower than Int.

And last, but not least, I would stop at 111, since 111 * 111 is a palindrome.

We end up with:

isPalindrome :: Show a => a -> Bool
isPalindrome n = l == reverse l
  where l = show n

maxPalindrome :: Int
maxPalindrome = maximum $ concatMap (take 1) $ allPalindrome <$> [999, 998 .. 111]
  where allPalindrome x = filter isPalindrome $ (x *) <$> [999, 998 .. x]

main :: IO ()
main = print maxPalindrome

Note that you should compile your code if you want to check its performance.

Alternatively, if you want to keep maxPalindrome's type, use :: Int:

maxPalindrome :: (Integral n, Show n) => n
maxPalindrome = maximum $ concatMap (take 1) $ allPalindrome <$> [999, 998 .. 111]
  where allPalindrome x = filter isPalindrome $ (x *) <$> [999, 998 .. x]

main :: IO ()
main = print (maxPalindrome :: Int)

Your code is fine, but I would suggest some small changes. Instead of head . transpose, I would use concatMap (take 1). This captures your intend to take the first (and therefore largest) number from each allPalindrome.

Next, I would use Int instead of (Integral a, Show a), since 999 * 999 is smaller than maxBound :: Int. And last, but not least, I would stop at 111, since 111 * 111 is a palindrome.

We end up with:

isPalindrome :: Show a => a -> Bool
isPalindrome n = l == reverse l
  where l = show n

maxPalindrome :: Int
maxPalindrome = maximum $ concatMap (take 1) $ allPalindrome <$> [999, 998 .. 111]
  where allPalindrome x = filter isPalindrome $ (x *) <$> [999, 998 .. x]

main :: IO ()
main = print maxPalindrome

Note that you should compile your code if you want to check its performance.

Your code is fine, but I would suggest some small changes. Instead of head . transpose, I would use concatMap (take 1). This captures your intend to take the first (and therefore largest) number from each allPalindrome.

Next, I would use Int instead of (Integral a, Show a), since 999 * 999 is smaller than maxBound :: Int. Why? Because by default, Integer will be used for Integral types, if they were note specified. Therefore, you end up with maxPalindrome handled as a Integer, which is slower than Int.

And last, but not least, I would stop at 111, since 111 * 111 is a palindrome.

We end up with:

isPalindrome :: Show a => a -> Bool
isPalindrome n = l == reverse l
  where l = show n

maxPalindrome :: Int
maxPalindrome = maximum $ concatMap (take 1) $ allPalindrome <$> [999, 998 .. 111]
  where allPalindrome x = filter isPalindrome $ (x *) <$> [999, 998 .. x]

main :: IO ()
main = print maxPalindrome

Note that you should compile your code if you want to check its performance.

Alternatively, if you want to keep maxPalindrome's type, use :: Int:

maxPalindrome :: (Integral n, Show n) => n
maxPalindrome = maximum $ concatMap (take 1) $ allPalindrome <$> [999, 998 .. 111]
  where allPalindrome x = filter isPalindrome $ (x *) <$> [999, 998 .. x]

main :: IO ()
main = print (maxPalindrome :: Int)
Source Link
Zeta
  • 19.6k
  • 2
  • 57
  • 90

Your code is fine, but I would suggest some small changes. Instead of head . transpose, I would use concatMap (take 1). This captures your intend to take the first (and therefore largest) number from each allPalindrome.

Next, I would use Int instead of (Integral a, Show a), since 999 * 999 is smaller than maxBound :: Int. And last, but not least, I would stop at 111, since 111 * 111 is a palindrome.

We end up with:

isPalindrome :: Show a => a -> Bool
isPalindrome n = l == reverse l
  where l = show n

maxPalindrome :: Int
maxPalindrome = maximum $ concatMap (take 1) $ allPalindrome <$> [999, 998 .. 111]
  where allPalindrome x = filter isPalindrome $ (x *) <$> [999, 998 .. x]

main :: IO ()
main = print maxPalindrome

Note that you should compile your code if you want to check its performance.