Help with division

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with division

Daniel Carrera-2
Hello all,

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.

--//--
Prelude> :l test.hs
Compiling Main             ( test.hs, interpreted )

test.hs:13:20:
     No instance for (Fractional Int)
       arising from use of `/' at test.hs:13:20
     Probable fix: add an instance declaration for (Fractional Int)
     In the definition of `choose':
         choose n k = (fac (n)) / ((fac (k)) * (fac (n - k)))
Failed, modules loaded: none.
--//--

The problem is that it doesn't like the / sign. For example, if I
replace the / by * the function is compiled just fine.

Does anyone know what I'm missing?

Cheers,
Daniel.
--
      /\/`) http://oooauthors.org
     /\/_/  http://opendocumentfellowship.org
    /\/_/
    \/_/    I am not over-weight, I am under-tall.
    /
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Help with division

Greg Buchholz-2
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))


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Help with division

Daniel Carrera-2
Greg Buchholz wrote:
>   Integral division is spelled "div"...

Thanks, that worked.

The "course material" on the website uses a "/".  See the second link
from the top, English notes. On page 5, right at the bottom. This course
material teaches "Gofer" which it claims is "essentially a subset of
Haskell". I guess that division must be one of the differences between
Gofer and Haskell.

Cheers,
Daniel.
--
      /\/`) http://oooauthors.org
     /\/_/  http://opendocumentfellowship.org
    /\/_/
    \/_/    I am not over-weight, I am under-tall.
    /
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe