Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Calculate Power Using Recursion in Haskell



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.

Updated on: 2023-03-27T11:42:11+05:30

779 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements