Haskell Program to Check Palindrome


This tutorial will help us in checking if any number is palindrome number or not using user-defined function and boolean functions in haskell. A palindrome number is a number that remains the same when its digits are reversed. The following example will give you a clear idea about Palindrome number.

For example, 121, 12321, and 1221 are palindrome numbers, while 123, 1234, and 1212 are not.

Algorithm

  • Step 1 − The isPalindrome function using reverse function is defined as, isPalindrome str = str == reverse str.

  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, a string is passed, which is then passed to the isPalindrome function. The result of the function is used to print a message indicating whether the string is a palindrome or not.

  • Step 3 − The variable named, “str” is being initialized. It will hold the string which is to be checked whether it is a palindrome number or not.

  • Step 4 − The result is printed to the console using ‘putStrLn’ statement after the function is called.

Example 1

In this example, the isPalindrome function takes a string as input and returns True if the string is a palindrome (i.e., the same when read forwards and backwards), and False otherwise. The function uses the reverse function to reverse the input string and compares it to the original string. If they are equal, the string is a palindrome, otherwise it is not.

isPalindrome :: String -> Bool
isPalindrome str = str == reverse str

main :: IO ()
main = do
   let str = "1331"
   if isPalindrome str
      then putStrLn "The string is a palindrome."
      else putStrLn "The string is not a palindrome."

Output

The string is a palindrome.

Example 2

In this example, the isPalindrome function checks if the input string is empty or consists of a single character, in which case it returns True as such strings are palindromes by definition. If the input string is longer, it checks if the first and last characters are equal, and if so, it recursively calls isPalindrome on the rest of the string (excluding the first and last characters). If both of these conditions are satisfied, the input string is a palindrome and the function returns True. Otherwise, it returns False.

isPalindrome :: String -> Bool
isPalindrome [] = True
isPalindrome [_] = True
isPalindrome (x:xs) = (x == last xs) && isPalindrome (init xs)

main :: IO ()
main = do
   let str = "1331"
   if isPalindrome str
      then putStrLn "The string is a palindrome."
      else putStrLn "The string is not a palindrome."

Output

The string is a palindrome.

Example 3

In this example, the isPalindrome function uses a list comprehension to create a list of pairs of corresponding characters from the beginning and end of the input string. The all function is then used to check if all of these pairs are equal. If all of the pairs are equal, the input string is a palindrome and the function returns True. Otherwise, it returns False.

isPalindrome :: String -> Bool
isPalindrome str = all (\(x,y) -> x == y) [(str !! i, str !! (len-i-1)) | i <- [0..(len `div` 2)]]
   where len = length str

main :: IO ()
main = do
   let str = "1331"
   if isPalindrome str
      then putStrLn "The string is a palindrome."
      else putStrLn "The string is not a palindrome."

Output

The string is a palindrome.

Conclusion

In Haskell, we can check if a number is palindrome or not using user-defined function with reverse function, list comprehension or recursion.

Updated on: 24-Apr-2023

412 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements