How to resolve the algorithm Palindrome detection step by step in the Haskell programming language
How to resolve the algorithm Palindrome detection step by step in the Haskell programming language
Table of Contents
Problem Statement
A palindrome is a phrase which reads the same backward and forward. Write a function or program that checks whether a given sequence of characters (or, if you prefer, bytes) is a palindrome. For extra credit:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Palindrome detection step by step in the Haskell programming language
The provided code is written in Haskell and contains functions for palindrome detection.
-
The
is_palindrome
function checks if a given inputx
is a palindrome by comparing it to its reverse. -
The
isPalindrome
function, using theData.Bifunctor
andData.Char
libraries, checks if a list of elements[a]
is a palindrome by comparing it to its reverse. It converts the characters to lowercase before comparison. -
The
isPal
function, using thequotRem
function from theData.Bifunctor
library, checks if a strings
is a palindrome by comparing the leftward characters with a reflection of the rightward characters. -
The
main
function tests theisPalindrome
andisPal
functions with a list of prepared strings. Theprepared
function converts the strings to lowercase and removes spaces. -
The
showResult
function takes a functionf
and a strings
as input and returns a string representing the result of applyingf
tos
. -
The
is_palindrome_r
function recursively checks if a stringx
is a palindrome by comparing the first and last characters. If they match, it calls itself on the remaining string, excluding the first and last characters. If the characters don't match, it returnsFalse
.
Source code in the haskell programming language
is_palindrome x = x == reverse x
import Data.Bifunctor (second)
import Data.Char (toLower)
------------------- PALINDROME DETECTION -----------------
isPalindrome :: Eq a => [a] -> Bool
isPalindrome = (==) <*> reverse
-- Or, comparing just the leftward characters with
-- with a reflection of just the rightward characters.
isPal :: String -> Bool
isPal s =
let (q, r) = quotRem (length s) 2
in uncurry (==) $
second (reverse . drop r) $ splitAt q s
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ putStrLn $
(showResult <$> [isPalindrome, isPal])
<*> fmap
prepared
[ "",
"a",
"ab",
"aba",
"abba",
"In girum imus nocte et consumimur igni"
]
prepared :: String -> String
prepared cs = [toLower c | c <- cs, ' ' /= c]
showResult f s = (show s) <> " -> " <> show (f s)
is_palindrome_r x | length x <= 1 = True
| head x == last x = is_palindrome_r . tail. init $ x
| otherwise = False
You may also check:How to resolve the algorithm String comparison step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Topological sort step by step in the Forth programming language
You may also check:How to resolve the algorithm Active Directory/Connect step by step in the smart BASIC programming language
You may also check:How to resolve the algorithm Farey sequence step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Window management step by step in the Icon and Unicon programming language