I'm using hugs to compile a simple Haskell function calculating the number of permutations. I would like it to return an Integer, but I need to operate on floats. I've tried to calculate the answer as a Float and then truncate it, but for some reason it's not working out.
This is the Function:
choose :: Float -> Float -> Integer
choose n r = truncate (chooseF (n r))
where
chooseF::Float->Float->Float
chooseF n r | n==r = 1
| otherwise = n / (n-r) * chooseF(n-1) r
This is the Error (Line 35 is the second line of the function):
ERROR "/homes/mb4110/SimpleMath":35 - Type error in application
*** Expression : n r
*** Term : n
*** Type : Float
*** Does not match : a -> b
It's probably something obvious that I'm missing but I've been at this for a good while and can't think of the solution.
chooseF
takes two arguments, but because of the parenthesis n r
is parsed as a single argument. Thus, remove the parenthesis around n r
and it should be fine.