
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
Reverse a Given Number Using Recursion in Haskell
In Haskell, we can find reverse of a given number by using recursion along with helper function. In the first example, we are going to use (reverseNumHelper n rev) function and in the second example, we are going to use (reverseNum n | n < 10 = n| otherwise = (n `mod` 10) * (10 ^ numDigits (n `div` 10)) + reverseNum (n `div` 10)) function.
Finding the reverse of a given number using recursion using the helper function
In this method, the reverseNumHelper function takes two arguments: n and rev. n is the number to reverse and rev is the reversed number so far. The function uses an accumulator rev to keep track of the reversed number so far. The function repeatedly takes the last digit of n (n mod 10), adds it to rev multiplied by 10 (rev * 10 + (n mod 10)), and then divides n by 10 (n div 10) to get the remaining digits.
Algorithm
Step 1 ? The user defined recursive reverse number function is defined with the help of helper function and its definition is written as,
reverseNumHelper n rev | n == 0 = rev | otherwise = reverseNumHelper (n `div` 10) (rev * 10 + (n `mod` 10)) .
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. In the main function, the user defined recursive function is being called by passing argument to it.
Step 3 ? The variable named, "num" is being initialized. It will hold the number whose reverse is to be printed.
Step 4 ? The resultant reversed number is printed to the console using ?show' function after the function is called.
Example 1
In this example, a helper function reverseNumHelper is used to reverse the number. The reverseNum function serves as a wrapper that calls reverseNumHelper with n as the first argument and 0 as the second argument.
reverseNum :: Integer -> Integer reverseNum n = reverseNumHelper n 0 reverseNumHelper :: Integer -> Integer -> Integer reverseNumHelper n rev | n == 0 = rev | otherwise = reverseNumHelper (n `div` 10) (rev * 10 + (n `mod` 10)) main :: IO () main = do let num = 12345 let reversedNum = reverseNum num putStrLn (show reversedNum)
Output
54321
Finding the reverse of a given number using recursion
In this method, the reverseNum function uses recursion to reverse the number by first multiplying the last digit of n (n mod 10) by 10 raised to the number of digits in the remaining digits of n (10 ^ numDigits (n div 10)). The reverse of the remaining digits (reverseNum (n div 10)) is then computed recursively and added to the result.
Algorithm
Step 1 ? The user defined recursive reverse function is defined as,
reverseNum n | n < 10 = n | otherwise = (n `mod` 10) * (10 ^ numDigits (n `div` 10)) + reverseNum (n `div` 10).
Step 2 ? The user defined numDigits function is defined as,
numDigits n | n < 10 = 1 | otherwise = 1 + numDigits (n `div` 10).
Step 3 ? Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, the user defined recursive function is being called recursively again and again by passing argument to it.
Step 4 ? The variable named, "num" is being initialized. It will hold the number whose reverse is to be printed.
Step 5 ? The resultant reversed number is printed to the console using ?show' function after the function is called.
Example 1
In this example, the two functions reverseNum and numDigits are used. The reverseNum function takes an Integer argument n and returns its reverse. The numDigits function takes an Integer argument n and returns the number of digits in n.
reverseNum :: Integer -> Integer reverseNum n | n < 10 = n | otherwise = (n `mod` 10) * (10 ^ numDigits (n `div` 10)) + reverseNum (n `div` 10) numDigits :: Integer -> Integer numDigits n | n < 10 = 1 | otherwise = 1 + numDigits (n `div` 10) main :: IO () main = do let num = 12345 let reversedNum = reverseNum num putStrLn (show reversedNum)
Output
54321
Conclusion
The reverse of a number is a new number formed by reading the digits of the original number in the opposite direction. For example, the reverse of the number 12345 is 54321.
In Haskell, the number can be reversed using recursion. Recursion allows to find concise and elegant solutions to problems. It is a way of defining a function with base and recursive cases. These functions can take any number as an argument and returns the reverse of the same to the console.