Haskell Program To Check Whether The Input String Is A Palindrome


This tutorial discusses writing a program to check whether the input string is a palindrome in the Haskell programming language.

A string is said to be a palindrome when it results in an exact string after reversing it. For example, the string “level” is a palindrome because it results in an exact string even after reversing it.

In this tutorial we see,

  • Program to check whether the string is a palindrome using a built-in function reverse.

  • Program to check whether the string is a palindrome using a recursive function.

Method 1: Checking The String Is A Palindrome Using A Built-In Function

Algorithm

  • We declare a function isPalindrome, this function returns the output of the comparison which is a boolean value.

  • Then we declare the main function, where a variable str is initialized with the value “kayak”. The function isPalindrome is invoked with the initialized string str as an argument. The output of the function is loaded into a variable status.

  • Finally, the value of the status is checked with the if statement, if the value of the status is true the program prints that the string is a palindrome, else the program prints the string as not a palindrome.

Example

-- function declaration
isPalindrome :: [Char]->Bool

-- function definition
isPalindrome str = (str==(reverse str))

main :: IO()
main = do
-- initializing variable str
   let str = "kayak"
-- invoking the function isPalindrome
   let status = isPalindrome str
-- printing the status
   if (status)
      then print ("The string " ++ str ++ " is a Palindrome")
   else print ("The string " ++ str ++ " is not a Palindrome")

Output

"The string kayak is a Palindrome"

Method 2: Check Whether The String Is A Palindrome Using A Recursive Function

Algorithm

  • Declare a function StringReverse, this function returns the reverse of the string argument.

  • Declare a function isPalindrome, this function returns the output of the comparison which is a boolean value.

  • Create a main function and initialize the string to be checked for Palindrome

  • Using the isPalindrome function and StringReverse function check whether both the strings match

  • Print the result

Example

-- function declaration
stringReverse :: [Char]->[Char]

-- function definition
-- base case
stringReverse [] = []
stringReverse (ch:str) = (stringReverse str) ++ [ch]
-- function declaration
isPalindrome :: [Char]->Bool
-- function definition
isPalindrome str = (str==(stringReverse str))

main :: IO()
main = do
-- initializing variable str
   let str = "kayak"
-- invoking the function isPalindrome
   let status = isPalindrome str
-- printing the status
   if (status)
      then print ("The string " ++ str ++ " is a Palindrome")
   else print ("The string " ++ str ++ " is not a Palindrome")

Output

"The string kayak is a Palindrome"

Conclusion

In this tutorial, we discussed two ways to implement a program to check whether a string is a palindrome in the Haskell programming Language.

Updated on: 15-Dec-2022

730 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements