jjared...@gmail.com wrote:
> Commenting out the definitions of birthdayparadox as well as test suppress
> all errors. I've tried to do add type annotations (Floating a) => a -> a,
> but truthfully I just want someone to point me in the right direction,
> because I know I'm just not able to read the error properly.
"No instance for Floating (x -> y)" means that the compiler tries to
use a function type as some (floating point) number. That usually
means you've forgotten to apply an argument to a function somewhere,
you need to add parenthesis, or you made a typo.
>
https://gist.github.com/1223492
The program is shorter than the error message, so why don't post it?
> pShare = 1 / 365
>
> uniquePairs p = logBase (1 - pShare) (1 - p)
> arcp n p = n^2 - n - uniquePairs p
>
> newtonRaphsonStep f f' ai = ai - f ai / f' ai
>
> newtonRaphson f f' ai epsilon =
> let ai' = newtonRaphsonStep f f' ai
> in if abs (ai' - ai) < epsilon then ai' else newtonRaphson f f' ai' epsilon
>
> birthdayparadox p =
> let a0 = sqrt . uniquePairs p
> f n = arcp n p
> f' = \x -> 2 * x - 1
> in newtonRaphson f f' a0 0.001
>
> test = birthdayparadox 0.5
I debugged this by commenting out the offending code, and then I used :t
in the toplevel to give my the types inferred by the compiler. Then I added
those in the source (which also means they serve as documentation for future
readers).
I got:
pShare :: Double
uniquePairs :: Double -> Double
arcp :: Double -> Double -> Double
newtonRaphson :: (Ord a, Fractional a) => (a -> a) -> (a -> a) -> a -> a -> a
So I can see that in birthdayparadox, a0 is supposed to be a Double (you
could specialize newtonRaphson to Double if you want, or keep it as it is
if you want to use it in other contexts, too).
However, (.) has a different binding priority than you think:
*Main> :t sqrt . uniquePairs 0.5
<interactive>:1:8:
Couldn't match expected type `a0 -> c0' with actual type `Double'
In the return type of a call of `uniquePairs'
Either add parenthesis:
*Main> :t (sqrt . uniquePairs) 0.5
(sqrt . uniquePairs) 0.5 :: Double
or use ($) instead:
*Main> :t sqrt $ uniquePairs 0.5
sqrt $ uniquePairs 0.5 :: Double
After that change, it compiles successfully.
HTH,
- Dirk