
19 Dec
2005
19 Dec
'05
4:45 p.m.
Daniel Carrera wrote:
Playing around with Haskell... I'm writing a 'choose' function: --//-- fac :: Int -> Int fac 0 = 1 fac n = n*fac(n-1)
choose :: Int -> Int -> Int choose n k = fac(n) / (fac(k) * fac(n-k)) --//--
I'm having problems with the last line.
Integral division is spelled "div"... http://www.zvon.org/other/haskell/Outputprelude/div_f.html ...the slash, "/", is for fractional numbers (Doubles, Rationals, etc.). Try one of...
choose :: Int -> Int -> Int choose n k = div (fac n) (fac(k) * fac(n-k))
-- or, as an infix function --
choose :: Int -> Int -> Int choose n k = (fac n) `div` (fac(k) * fac(n-k))