我正在学习haskell并且在理解错误试图告诉我的内容时遇到一些问题。此代码产生以下错误
data Term = Mul { factor :: Term, factor2 :: Term }
| Div { dividend :: Term, divisor :: Term }
| Add { summand :: Term, summand2 :: Term }
| Sub { minuend :: Term, subtrahend :: Term }
| Mon { exponent :: Int }
value :: (Double, Term) -> Double
value x (Mul a b) = (value x a) * (value x b)
value x (Div a b) = (value x a) / (value x b)
value x (Add a b) = (value x a) + (value x b)
value x (Sub a b) = (value x a) - (value x b)
value x (Mon a) = x^a
错误:
Couldn't match expected type `Term -> (Double, Term)'
with actual type `Double'
The function `value' is applied to two arguments,
but its type `(Double, Term) -> Double' has only one
In the first argument of `(+)', namely `(value x a)'
In the expression: (value x a) + (value x b)
我做错了什么?
答案 0 :(得分:4)
问题在于,您的类型签名和定义并不同意他们如何接受论证。
您需要以咖喱风格(推荐)编写您的类型签名:
value :: Double -> Term -> Double
或以非常规风格(非惯用)编写您的函数:
value (x, Mul a b) = ...
我建议尝试两者,但前者几乎总是在野外完成。