Haskell Program to Reverse a Number


This tutorial discusses writing a program to reverse a number in the Haskell programming language.

In this tutorial, we see two ways to implement a program in Haskell to reverse a number.

  • Program to reverse a number using the list function reverse.
  • Program to reverse a number using a recursive function.

Example 1

Program to reverse a number using the list function reverse.

-- function declaration 
reverseNumber :: Int->Int
-- function definition
reverseNumber n = k
   where
      temp = reverse (show n)
      k = read temp :: Int
main :: IO()
main = do
-- declaring and initializing a variable number
   let number = 123
-- invoking the function reverseNumber and printing the returned number
   print ("The reverse number for the number " ++ show number ++ " is:")
   print (reverseNumber number)

Output

"The reverse number for the number 123 is:"
321

In the above program, we declared a function reverseNumber as such it takes an integer as an argument and returns an integer. In its function definition, we are taking an argument n. We are parsing the integer n into a string using the function show, we invoked a function reverse for the value returned by the show function. And loaded it into a variable temp. As strings in Haskell are lists of characters. The function reverse takes input from a list and returns the reverse of it. We are parsing the string temp into an integer using the function read and loading it into a variable k which is returned by the function. I.e the function reverseNumber converts an integer into a string, reverses the string using string function reverse, reconverts it into a number, and returns the number.

In the main function, we declared and initialized a number. Finally, we invoked the reverseNumber function with this number as an argument and printed the returned output.

Example 2

Program to reverse a number using a recursive function.

-- function declaration
reverseNumber :: Int->Int->Int
-- function definition
-- base case
reverseNumber 0 revnum = revnum
reverseNumber num revnum = reverseNumber x y
   where
      k = mod num 10
      y = revnum * 10 + k
      x = div num 10                            
main :: IO()
main = do
-- declaring and initializing variable number
   let number = 123
-- invoking the function reverseNumber and printing the returned result
   print ("The reverse number for the number " ++ show number ++ " is:")
   print (reverseNumber number 0)

Output

"The reverse number for the number 123 is:"
321

In the above program, we declared a function reverseNumber as such it takes two integers as an argument and returns an integer. In its function definition, we are taking two integer arguments num and revnum. We are extracting the digit at one’s place in num using the function mod and loading it into a variable k. We are multiplying the argument revnum with 10 and adding the number k and loading it into a variable y. Dividing the argument rev with 10 and loading it into a variable x. Finally, we are returning  We are returning a recursive call to the function reverseNumber with arguments x and y. The recursive calls are invoked until the function attains base case, where the first argument is 0, and the function returns the second argument. In the main function, we declared and initialized a number. Finally, we invoked the reverseNumber function with this number as an argument and printed the returned output.

Conclusion

In this tutorial, we discussed two ways to implement a program to reverse a number in Haskell Programming Language.

Updated on: 14-Dec-2022

533 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements