Ich versuche, eine Funktion zu definieren, die es mir erfordert, einem Doppeltyp einen fraktionalen Typ hinzuzufügen, aber ich scheint einen Fehler zu erhalten.Probleme, die fraktionale Typen hinzufügen
epsilon = 0.0000001
dif :: (Fractional a) => (a->a) -> a -> a
dif f x = (f(x+epsilon)-f(x))/epsilon
Haskell scheint Schwierigkeiten zu sein mit x + epsilon Interpretieren aber dies scheint seltsam, dass x Berücksichtigung definiert ein Fractional-Typ in der Funktionsdeklaration zu sein und epsilon ist ein Doppel (die einen Teil der fraktionierten Typklasse ist ?
Heres der Fehler erhalte ich:
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)’
Danke
Insbesondere ist 'epsilon' ein Double, also funktioniert' diff' nur mit 'a ~ Double'. Die Typensignatur verspricht jedoch, für * any * 'Fractional a' zu arbeiten, und der Compiler stellt fest, dass Ihre Implementierung dieses Versprechen nicht erfüllt, weil sie von' diff' abhängt. – amalloy