- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Haskell Program to calculate the power using Recursion

In Haskell, we can calculate the power by using recursion along with cases and also by using tail-recursion. In the first example we are going to use recursion along with base case, (power _ 0 = 1) and recursive case, (power x y = x * power x (y-1)). In the second example, we are going to use recursive cases as (power' x y | y == 0 = 1| otherwise = x * power' x (y-1)) and in third example, we are going to use tail-recursion.

## Algorithm

**Step 1**− The user defined recursive power function is defined as,

For example 1 −

power _ 0 = 1 power x y = x * power x (y-1).

For example 2 −

power' x y | y == 0 = 1 | otherwise = x * power' x (y-1).

For example 3 −

power x y = power' x y 1 where power' x y acc | y == 0 = acc | otherwise = power' x (y-1) (acc*x).

**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 program sets x to 2 and y to 5, and then calls the power function with those values as arguments.**Step 3**− The variables named, “x” and “y” are being initialized. It will hold the base and exponent values respectively.**Step 4**− The resultant power is printed to the console using ‘print’’ function, after the function is called.

## Example 1

In this example, a function power is defined that takes in two integers x and y as arguments. The function uses recursion to calculate the value of x raised to the power of y. The base case is when y is 0, in which case the function returns 1.

power :: Integer -> Integer -> Integer power _ 0 = 1 power x y = x * power x (y-1) main :: IO () main = do let x = 2 let y = 5 print (power x y)

## Output

32

## Example 2

This is a simple implementation of a recursive function in Haskell that calculates the power of a number using recursion. The function takes two arguments, x and y, and calculates x raised to the power of y. The base case is when y is equal to 0, in which case the function returns 1. In the recursive case, the function multiplies x with the result of calling the function with the arguments x and y-1, effectively raising x to the power of y.

power' :: Integer -> Integer -> Integer power' x y | y == 0 = 1 | otherwise = x * power' x (y-1) main :: IO () main = do let x = 2 let y = 5 print (power' x y)

## Output

32

## Example 3

In this example, a tail-recursive approach is implemented for calculating the power of a number. The function takes two parameters, x and y, where x is the base number and y is the exponent. It defines an inner function power' which takes three parameters, x, y, and acc, where acc is used to store the intermediate result. The function checks if y is equal to 0, if so it returns acc which is the final result. If y is not equal to 0, it calls itself with y decremented by 1 and acc multiplied by x.

power :: Integer -> Integer -> Integer power x y = power' x y 1 where power' x y acc | y == 0 = acc | otherwise = power' x (y-1) (acc*x) main :: IO () main = do let x = 2 let y = 5 print (power x y)

## Output

32

## Conclusion

In Haskell, power can be calculated using recursion and by using cases with it. Also, it can be calculated by using tail-recursion.

- Related Articles
- Java Program to calculate the power using recursion
- Golang Program to Calculate The Power using Recursion
- C++ Program to Calculate Power Using Recursion
- Java program to calculate the power of a Given number using recursion
- Haskell Program to Calculate the Power of a Number
- Haskell Program to Find G.C.D Using Recursion
- Haskell Program to Reverse a Sentence Using Recursion
- Haskell Program to convert the decimal number to binary using recursion
- How to calculate Power of a number using recursion in C#?
- Haskell Program to Find the Sum of Natural Numbers using Recursion
- Haskell Program to Find the Product of Two Numbers Using Recursion
- Haskell Program to convert the Binary number to Gray code using recursion
- Haskell Program to Find Factorial of a Number Using Recursion
- Haskell Program to Find Sum of N Numbers Using Recursion
- Haskell Program to find the reverse of a given number using recursion