- 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 find the power of a number using library function

This tutorial will help us to find the power of a number using library function. The base value and the exponent value is passed as an argument that is used to find the exponent power of the base value passed. And the final output is displayed.

For example, For base = 2 ; exponent = 3, it would return 8.

## Syntax

power x y = product (replicate (fromInteger y) x)

Product function computes a product of all elements in the list

power x y = foldl (*) 1 (replicate (fromInteger y) x)

Foldl takes the first item with the list along with the second argument to apply the function to them, after that it feeds the function with this result and the second argument and continues it till the last iteam

power x y = foldr (*) 1 (replicate (fromInteger y) x)

Foldr function will take the second argument and the last item in the list for applying the function and then it will take the penultimate item from the end and give the result and continues this till the end iteam.

## Algorithm

**Step 1**− Define Power Function**Step 2**− Program execution will be started from main function. The main() function has whole control of the program.**Step 3**− The variables named, “base” and “exponent” are initialized. It will contain the base value and the exponent value.**Step 4**− The final resultant value is displayed by using ‘putStrLn’ statement.

## Using Product Function and List

In this example, replicate function is used to create a list of x repeated y times and product is used to calculate the product of the elements of that list which is the x raised to the power of y.

### Example 1

power :: Integer -> Integer -> Integer power x y = product (replicate (fromInteger y) x) main :: IO() main = do let base = 5 let exponent = 3 let result = power base exponent putStrLn ("Result: " ++ show result)

### Output

Result: 125

## Using foldl Function

In this example, foldl function is used to perform the operation on each element of the list created by replicate and accumulate the result.

### Example 2

power :: Integer -> Integer -> Integer power x y = foldl (*) 1 (replicate (fromInteger y) x) main :: IO() main = do let base = 5 let exponent = 3 let result = power base exponent putStrLn ("Result: " ++ show result)

### Output

Result: 125

## Using Library Function and by Using foldr Function

In this example, foldr function is used to perform the operation on each element of the list created by replicate and accumulate the result in the reverse order.

### Example 3

power :: Integer -> Integer -> Integer power x y = foldr (*) 1 (replicate (fromInteger y) x) main :: IO() main = do let base = 5 let exponent = 3 let result = power base exponent putStrLn ("Result: " ++ show result)

### Output

Result: 125

## Using Power Function

In this example, the function is used by calling power x y, where x is the base and y is the exponent. For example, power 2 3 would return 8.

### Example 4:

power :: Integer -> Integer -> Integer power x 0 = 1 power x y = x * power x (y-1) main :: IO() main = do let base = 5 let exponent = 3 let result = power base exponent putStrLn ("Result: " ++ show result)

### Output

Result: 125

## Conclusion

In Haskell, there are various ways to find the power of a number without using library function. This can be achieved by using the product function and list, by using foldl , foldr function and even power function.

- Related Articles
- Haskell Program to find the GCD using library function
- Haskell Program to find the LCM using library function
- Haskell Program to get the predecessor of an integer number using library function
- Haskell Program to get the successor of an integer number using library function
- Haskell Program to check the given number is an EVEN number using library function
- Haskell Program to check the given number is an Odd number using library function
- Haskell Program to get total Bits Required for the Given Number using Library Function
- Haskell Program to get the remainder of float numbers using library function
- Haskell Program to get the Division and Remainder using library function
- Haskell Program to Calculate the Power of a Number
- Haskell Program to compare numbers and strings using library function
- Haskell Program to Find Factorial of a Number Using Recursion
- Haskell Program to calculate the power using Recursion
- Haskell Program to find the reverse of a given number using recursion
- Golang Program To Get The Predecessor Of An Integer Number Using Library Function