If there’s a number
n, find the factorial of
In this solution, we use the following recursive expression to calculate the factorial of
fact(n) = n * fact(n-1)
The code implementation is as follows:
fact n = if n == 0 then 1 else n * fact(n-1) main = print (fact 5)
fact, that takes
nand recursively calculates the factorial of
5as the argument.
product function in Haskell multiplies all the elements in a given list.
The steps are as follows:
productfunction to multiply all the numbers in the list.
-- Generating the list of numbers from 1 to n -- and calling the product function to multiply all the numbers in the list. fact n = product [1..n] main = print (fact 5)
View all Courses