How to resolve the algorithm Palindrome detection step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

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 input x is a palindrome by comparing it to its reverse.

  • The isPalindrome function, using the Data.Bifunctor and Data.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 the quotRem function from the Data.Bifunctor library, checks if a string s is a palindrome by comparing the leftward characters with a reflection of the rightward characters.

  • The main function tests the isPalindrome and isPal functions with a list of prepared strings. The prepared function converts the strings to lowercase and removes spaces.

  • The showResult function takes a function f and a string s as input and returns a string representing the result of applying f to s.

  • The is_palindrome_r function recursively checks if a string x 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 returns False.

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