Personal tools

99 questions/Solutions/6

From HaskellWiki

< 99 questions | Solutions(Difference between revisions)
Jump to: navigation, search
 
Line 4: Line 4:
 
isPalindrome :: (Eq a) => [a] -> Bool
 
isPalindrome :: (Eq a) => [a] -> Bool
 
isPalindrome xs = xs == (reverse xs)
 
isPalindrome xs = xs == (reverse xs)
 +
</haskell>
 +
 +
<haskell>
 +
isPalindrome' []  = True
 +
isPalindrome' [x] = True
 +
isPalindrome' xs  = (head xs) == (last xs) && (isPalindrome' $ init $ tail xs) 
 
</haskell>
 
</haskell>

Revision as of 07:28, 17 November 2010

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

isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome xs = xs == (reverse xs)
isPalindrome' []  = True
isPalindrome' [x] = True
isPalindrome' xs  = (head xs) == (last xs) && (isPalindrome' $ init $ tail xs)