Haskell Program to Reverse a Sentence Using Recursion


In Haskell, we can reverse a Sentence by using recursion along with concatenation and also by using list comprehension. In the first example we are going to use recursion along with base and recursive case and in the second example, we are going to use concatenation as ((last sentence) : reverseSentence (init sentence)) and in third example, we are going to use list comprehension as, reverseSentence s = [s !! i | i <- [len-1, len-2..0]] .

Algorithm

  • Step 1 − The user defined recursive reverseSentence function is defined as,

  • For example 1 −

reverseSentence [] = []
reverseSentence (x:xs) = reverseSentence xs ++ [x].
  • For example 2 −

reverseSentence [] = []
reverseSentence sentence = (last sentence) : reverseSentence (init sentence).
  • For example 3 −

reverseSentence s = [s !! i | i <- [len-1, len-2..0]]
where len = length s.
  • 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. The main function calls the reverseSentence function to reverse the string "Hello World!".

  • Step 3 − The variable named, “sentence” is being initialized. It will hold the sentence that needs to be reversed.

  • Step 4 − The resultant reversed sentence is printed to the console using ‘print’’ function, after the function is called.

Example 1

In this example, the reverseSentence function takes a string as input and uses recursion to reverse the order of the characters in the string. The base case of the recursion is when the input string is empty, in which case the function returns an empty string. In the recursive case, the function calls itself with the input string minus the first character, and then concatenates the first character of the input string to the end of the reversed string returned by the recursive call.

reverseSentence :: String -> String
reverseSentence [] = []
reverseSentence (x:xs) = reverseSentence xs ++ [x]

main :: IO ()
main = do
   let sentence = "Hello World!"
   print (reverseSentence sentence)

Output

"!dlroW olleH"

Example 2

In this example, the reverseSentence function takes in a string and uses recursion to reverse it. The base case is when the input string is empty, in which case the function returns an empty string. The recursive case is when the input string is not empty, where the last character of the sentence is concatenated to the result of reversing the rest of the sentence (excluding the last character) using init function.

reverseSentence :: String -> String
reverseSentence [] = []
reverseSentence sentence = (last sentence) : reverseSentence (init sentence)

main :: IO ()
main = do
   let sentence = "Hello World!"
   print (reverseSentence sentence)

Output

"!dlroW olleH"

Example 3

In this example, the list comprehension is used to reverse the sentence by iterating through the string backwards and appending each character to the final result.

reverseSentence :: String -> String
reverseSentence s = [s !! i | i <- [len-1, len-2..0]]
   where len = length s

main :: IO ()
main = do
   let sentence = "Hello World!"
   print (reverseSentence sentence)

Output

"!dlroW olleH"

Conclusion

In Haskell, a sentence can be reversed using recursion and by concatenating the result. Also, it can be reversed using list comprehension.

Updated on: 27-Mar-2023

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements