Reputation: 21
I'm trying to write a function that recursive counts a Taylor:
mcloren x = log (1+x)/(1-x)
compareWithDelta' :: (Num a, Floating a, Integral a, Ord a) => a -> a -> Bool
compareWithDelta' x acc = abs (acc - mcloren x) < 1.0e-10
mcl :: (Floating a, Integral a, Num a) => a -> a
mcl x =
let
mcl' x acc y
| compareWithDelta' x acc = acc
| otherwise = mcl' x (2*(x^(2*y+1)/(2*y+1))) (y+1)
in
mcl' x 0 0
But i have these error messages:
No instance for (Num a0) arising from a use of 'mcl'
The type variable 'a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Num Double — Defined in 'GHC.Float'
instance Num Float — Defined in 'GHC.Float'
instance Integral a => Num (GHC.Real.Ratio a)
— Defined in 'GHC.Real'
...plus three others
In the expression: mcl 1
In an equation for 'it': it = mcl 1
What does it mean and how to fix it ?
Upvotes: 2
Views: 311
Reputation: 60463
The short answer is that you're getting this "ambiguous type variable" error because your constraints (Integral a, Floating a)
are inconsistent. There is no type that satisfies this, so when defaulting tries to find a type for 1
(in your input expression), it can't.
It gets these constraints from the power operator (^)
taking an integral argument on the left (2.5 ^ 2
is not valid, use (^^)
for that), and from comparing with the float 1.0e-10
. I'm guessing just want to use (^^)
instead of (^)
and remove the Integral
constraint.
If you're doing numerics, you will almost certainly need fromIntegral
and realToFrac
eventually, which are the sledgehammer conversion functions between different numeric types. Just preparing you for what's ahead.
There is one more exponential operator, (**)
, which takes fractional numbers both on the left and right. The reason Haskell has three different exponentiation operators is fodder for another question if you are curious.
Upvotes: 6