Haskell Program To Perform nCr (r-combinations)


This tutorial discusses writing a program to perform nCr combinations in the Haskell programming language.

The nCr is used to find the number of ways r items can be selected from n items given that order doesn’t matter. The nCr is defined as n! / ( r! (n-r)! ). For example number of ways in which 3 items can be selected from 5 items is 5! / ( 3! (5-3)! ), i.e 10 ways.

In this tutorial we see,

  • Program to find the factorial of a number (helper function to find the nCr combinations).
  • Program to find the nCr combination.

Algorithm Steps

  • Take input or initialize variable for n and r.
  • Implement logic to computer the nCr combinations.
  • Print or display the output.

Example 1

Program to find the factorial of a number.

-- function declaration
factorial :: Int -> Int

-- function definition
factorial n = product [1..n]
main :: IO()
main = do
-- declaring and initializing variable
   let n = 5
-- computing factorial of variable n
   let fact = factorial n
-- printing the output
   print ("The factorial of the number " ++ show n ++ " is:")
   print(fact)

Output

"The factorial of the number 5 is:"
120

In the above program, we declared a function factorial as such it takes an integer as argument and returns an integer. In its function definition, an integer argument n is accepted. This function computes the factorial of a number by invoking the function product with an argument a list of integers from 1 to n. The function product takes an input list of numbers as an argument and returns the product of all elements in the list. The list is generated using the operator “..”. The syntax to create a list from a to b is a..b, where a must be less than or equal to b. The successive elements have unit differences. This function returns the computed factorial. In the main function, we declared and initialized a variable n to which the factorial should be calculated. We invoked the function factorial with n as arguments and loaded the returned output into a variable fact. Finally, the computed factorial of the number n is printed using the print function.

Note − The function show takes the argument of a number and returns the parsed string of a number. “++” is an operator to concatenate strings in Haskell.

Example 2

Program to find the nCr combination.

-- function declaration
factorial :: Int -> Int

-- function definition
factorial n = product [1..n]

-- function declaration
findCombinations :: Int->Int->Int

-- function definition
findCombinations n r = div (factorial n) ((factorial r) * (factorial (n-r)))
main :: IO()
main = do
-- declaring and initializing variable
   let n = 5
   let r = 3
-- computing the nCr combinations
   let output = findCombinations n r
-- printing the output
   print ("The number of ways in which " ++ show r ++ " items can be selected from " ++ show n ++ " items is:")
   print(output)

Output

"The number of ways in which 3 items can be selected from 5 items is:"
10

In the above program, we declared and defined a function factorial as the previous program is a helper function to find the nCr combinations. We declared a function findCombinations as such it takes two integer arguments and returns an integer. In its function definition, two integer arguments are accepted n and r. nCr is computed using the logic n! / ( r! (n-r)! ). The function factorial is invoked to compute the factorial of a number. And the computed combinations are returned. In the main function, two integer arguments are declared and initialized (n and r). The function findCombinations is invoked with n and r as arguments. The returned output is loaded into a variable output. Finally, The output is printed using the print function.

Conclusion

In this tutorial, we discussed implementing a program to perform the nCr combinations in the Haskell programming language.

Updated on: 14-Dec-2022

273 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements