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.

Updated on: 01-Mar-2023

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements