Questions 1,2,and 3 Write a Haskell function factorial cat that returns the fact
ID: 3836854 • Letter: Q
Question
Questions 1,2,and 3 Write a Haskell function factorial cat that returns the factorial of a number, n! is 1 * 2 * 3 * ellipsis * n. For example, factorial 4 returns 24. Show a signature or type definition for the function. Write a Haskell function combinations n kappa that returns the number of different ways you can make a group of kappa objects from a set of n objects. The number of combinations is defined as combinations = n!/kappa ! middot (n - kappa)! Write a Haskell function to implement the Ackermann function A(m, n) = {n + 1 if m = 0 A(m - 1, 1) if m > 0 and n = 0 A(m - 1, A(m, n - 1)) if m > 0 and n > 0.Explanation / Answer
1)
-- fac is a function mapping Integer to Integer
fac :: Integer -> Integer
-- fac(0) = 1. It means factorial of 0 equals 1.
fac 0 = 1
-- for numbers greater than 0, do recursive approach n * fac(n-1)
fac n | n > 0 = n * fac (n-1)
2)
The below code computes nCk. It is very simple.
combn _ 0 = 1
combn n k = combn n (k - 1) * (n - k + 1) `div` k
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.