我想实现的law of cosines功能,这里的法律是我的代码:Haskell的实现余弦
cosC :: [a] -> a
cosC sides
| length sides < 3 = 0
| otherwise = (x^2 + y^2 - z^2)/(2 * x * y)
where x = head(tail(tail(sides)))
y = head(tail(sides))
z = head(sides)
,但我得到两个错误:
No instance for (Fractional a)
arising from a use of `/'
In the expression: (x^2 + y^2 - z^2)/(2 * x * y)
In an equation for `cosC':
cosC sides
| length sides < 3 = 0
| otherwise = (x^2 + y^2 - z^2)/(2 * x * y)
where
x = head (tail (tail (sides)))
y = head (tail (sides))
z = head (sides)
和
No instance for (Num a)
arising from the literal `2'
In the first argument of `(*)', namely `2'
In the first argument of `(*)', namely `2 * x'
In the second argument of `(/)', namely `(2 * x * y)'
编辑:我已经修复了上述余弦定律中的符号错误。感谢Daniel Fischer指出了这一点。
你有一个符号错误,它应该是'(x^2 + y^2 - z^2)'的法律的余弦。 –
斑点!非常感谢。 – User3419