Evaluate binomial coefficients: Difference between revisions

(→‎{{header|Scala}}: added recursive implementation)
Line 544:
<lang haskell>
choose :: (Integral a) => a -> a -> a
choose n k = product( [k+1..n]) `div` product( [1..n-k])
</lang>
 
<lang haskell>> 5 `choose` 3
10</lang>
 
Or, generate the binomial coefficients iteratively to avoid computing with big numbers:
 
<lang haskell>
choose :: (Integral a) => a -> a -> a
choose n k = foldl (\z i -> (z * (n-i+1)) `div` i) 1 [1..k]
</lang>
 
=={{header|HicEst}}==
Anonymous user