
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
Find Product of Two Numbers Using Recursion in Haskell
In Haskell, we can find the Product of Two Numbers by using recursion along with recursive repeated addition. In the first example we are going to use (product' x y | y == 0 = 0 | y == 1 = x | otherwise = x + product' x (y-1)) function. And in the second example, we are going to use recursive repeated addition.
Algorithm
Step 1 ? The recursive product' function is defined as,
For example 1 and 2 ?
product' x y | y == 0 = 0 | y == 1 = x | otherwise = x + product' x (y-1).
For example 3 ?
product' x y | y == 0 = 0 | y == 1 = x | otherwise = x + product' x (y-1).
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, we are defining x and y, and using the print function to output the result of product' x y.
Step 3 ? The variables named, "x" and "y" are being initialized. It will hold the number whose product is to be computed.
Step 4 ? The resultant product of two numbers is printed to the console using ?print' function, after the product' function is called.
Example 1
In this example, we have defined a function product' that takes two integers x and y as input. The function uses recursion to find the product of the two numbers. The base case is when y is equal to 0, in which case the function returns 0. The next base case is when y is equal to 1, in which case the function returns x. If neither of these base cases are met, the function recursively calls itself with the input x and y-1, and adds the result to x.
product' :: Integer -> Integer -> Integer product' x y | y == 0 = 0 | y == 1 = x | otherwise = x + product' x (y-1) main :: IO () main = do let x = 2 let y = 5 print (product' x y)
Output
10
Example 2
In this example, we are going to see that how we can find the product of the two numbers. This can be done by using tail-recursion. Here, the function uses a helper function called productTailHelper which takes three arguments: x, y, and an accumulator acc. The base case of the recursion is when y is equal to 0, in which case the function returns acc. In the recursive case, the function calls itself with the updated arguments x, y-1, and acc+x, which updates the accumulator with the next value in the product calculation. The final result is returned when the recursion reaches the base case.
productTail :: Integer -> Integer -> Integer productTail x y = productTailHelper x y 0 where productTailHelper x y acc | y == 0 = acc | otherwise = productTailHelper x (y-1) (acc+x) main :: IO () main = do let x = 3 let y = 4 print (productTail x y)
Output
12
Example 3
This example uses the fact that multiplication is simply repeated addition. It starts by checking if the second number (y) is 0, in which case the product is 0. If y is 1, then the product is simply x. Otherwise, the function calls itself with x and y-1, and adds x to the result, effectively adding x to itself y times.
product' :: Integer -> Integer -> Integer product' x y | y == 0 = 0 | y == 1 = x | otherwise = x + product' x (y-1) main :: IO () main = do let x = 3 let y = 4 print (product' x y)
Output
12
Conclusion
In Haskell, the product of two numbers can be calculated by using recursion along with helper function as tail recursion or by using repeated addition.