Find Factorial of a Number Using Recursion in Haskell



In Haskell, we Find Factorial of a Number Using Recursion by using recursion along with foldl and product function. In the first example we are going to use recursion along with base and recursive case and in the second example, we are going to use factorial n = foldl (*) 1 [1..n] function and third example, we are going to use factorial n = product [1..n] function.

Algorithm

  • Step 1 ? The user defined recursive factorial function is defined as,

  • For example 1 & 2 ?

factorial 0 = 1
factorial n = n * factorial (n-1).
  • For example 3 ?

factorial n = foldl (*) 1 [1..n].
  • For example 4 ?

factorial n = product [1..n].
  • 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 test the factorial function by passing the number 5, which should output 120 (5*4*3*2*1).

  • Step 3 ? The variable named, "num" is being initialized. It will hold the number whose factorial is to be computed.

  • Step 4 ? The resultant factorial is printed to the console using ?print'' function, after the function is called.

Example 1

In this example, we are going to see that how we can find the factorial of a number using recursion. This can be done by using user-defined recursive function.

factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n-1)

main :: IO ()
main = do
   let num = 5
   print (factorial num)

Output

120

Example 2

In this example, we are going to see that how we can find the factorial of a number using recursive case. The function factorial' takes an integer as input and returns the factorial of that number. The function uses a base case where if the input is 0, it returns 1. Otherwise, it calls itself with the input decremented by 1 and multiplies that result by the original input. The main function then assigns the value 5 to a variable num, and calls factorial' function with num as an argument and prints the result..

factorial' :: Integer -> Integer
factorial' n
   | n == 0 = 1
   | otherwise = n * factorial' (n-1)

main :: IO ()
main = do
let num = 5
print (factorial' num)

Output

120

Example 3

In this example, we are going to see that how we can find the factorial of a number using recursion. This can be done by using foldl function.

factorial :: Integer -> Integer
factorial n = foldl (*) 1 [1..n]

main :: IO ()
main = do
   let num = 5
   print (factorial num)

Output

120

Example 4

In this example, we are going to see that how we can find the factorial of a number using recursion. This can be done by using product function.

factorial :: Integer -> Integer
factorial n = product [1..n]

main :: IO ()
main = do
   let num = 5
   print (factorial num)

Output

120

Conclusion

In Haskell, to find the factorial of a number using recursion, we can use user-defined functions, or foldl and product functions.

Updated on: 2023-03-27T11:31:56+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements