
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if Input String is a Palindrome in Haskell
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.