[Haskell-cafe] Help with division

Greg Buchholz greg at sleepingsquirrel.org
Mon Dec 19 16:45:41 EST 2005


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))




More information about the Haskell-Cafe mailing list