Maciek
Maciek

Reputation: 286

Type error Haskell, what's wrong?

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.

Upvotes: 0

Views: 266

Answers (2)

Nate
Nate

Reputation: 12829

The problem is that you are passing (n r) to chooseF. Hugs determines from this that the term n must be some function of type a -> b, into which you are passing r. The result of this would then be partially applied into chooseF.

Presumably, you wanted to call chooseF with both n and r as parameters instead. To fix this error, call chooseF n r instead.

Upvotes: 2

bzn
bzn

Reputation: 2392

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.

Upvotes: 4

Related Questions