Problem 06

99 Haskell Problems

Problem 6

(*) Find out whether a list is a palindrome. A palindrome can be read forward or backward; e.g. (x a m a x).

Example in Haskell:

*Main> isPalindrome [1,2,3]
False
*Main> isPalindrome "madamimadam"
True
*Main> isPalindrome [1,2,4,8,16,8,4,2,1]
True

Solution:

isPalindrome x = x == reverse x

Note that the type signature for isPalindrome is

:t isPalindrome
isPalindrome :: Eq a => [a] -> Bool

The reason is that the type for the element of the list must be comparable in an equality.

Advertisements