添加小数类型的问题

ora*_*ice 1 haskell

我试图定义一个函数,要求我将一个小数类型添加到double,但我似乎收到一个错误.

epsilon = 0.0000001
dif :: (Fractional a) => (a->a) -> a -> a

dif f x = (f(x+epsilon)-f(x))/epsilon
Run Code Online (Sandbox Code Playgroud)

Haskell似乎在解释x + epsilon时遇到了麻烦,但考虑到x在函数声明中定义为Fractional类型并且epsilon是double(它是Fractional类型类的一部分),这似乎很奇怪?

这是我得到的错误:

Couldn't match expected type ‘a’ with actual type ‘Double’
  ‘a’ is a rigid type variable bound by
      the type signature for dif :: Fractional a => (a -> a) -> a -> a
      at dif.hs:3:8
Relevant bindings include
  x :: a (bound at dif.hs:5:7)
  f :: a -> a (bound at dif.hs:5:5)
  dif :: (a -> a) -> a -> a (bound at dif.hs:5:1)
In the second argument of ‘(+)’, namely ‘epsilon’
In the first argument of ‘f’, namely ‘(x + epsilon)’
Run Code Online (Sandbox Code Playgroud)

谢谢.

Dan*_*ner 6

给出epsilon一个适当的多态类型签名:

epsilon :: Fractional a => a
Run Code Online (Sandbox Code Playgroud)

您可能也喜欢"单形态限制"中的解释.