Reputation: 23
I am trying to write a function called check that when applied to n returns a Bool indicating whether the ∑r=0 to n (n choose r) = 2^n
I have made the factorial and choose functions which work but I am having difficulty understanding what the type would be for the check function. I have written what I have done for it and I think logically it might work but I am not sure. The error I get when running says 'couldn't match type a -> Bool to actual type Bool' but I am not entirely sure how to make it an actual Bool type.
factorial :: (Integral a) => a -> a
factorial 0 = 1
factorial n = n * factorial (n - 1)
choose :: (Integral a) => a -> a -> a
n `choose` r
| r < 0 = 0
| r > n = 0
| otherwise = factorial n `div` (factorial r * factorial (n-r))
check :: (Integral a) => a -> a -> Bool
check n = (if (factorial (choose n)) == 2^n then True else False)
Upvotes: 0
Views: 151
Reputation: 1732
The correct type declaration for check
is a -> Bool
The types are separated by ->
and grouped using parentheses.
The last type is the returned type, here Bool
.
The previous types are the input types, here a
.
Your error message is couldn't match type a -> Bool to actual type Bool
. What's happening here is that Haskell matches the a
to variable n
, but is then left with a -> Bool
, when actually the return type is Bool
.
Upvotes: 2