Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 不能';t将expexted类型Double与实际Int匹配_Haskell_Math_Haskell Platform - Fatal编程技术网

Haskell 不能';t将expexted类型Double与实际Int匹配

Haskell 不能';t将expexted类型Double与实际Int匹配,haskell,math,haskell-platform,Haskell,Math,Haskell Platform,我试图在不使用cosh函数的情况下计算ch值 ch :: Double -> Int -> Double ch' :: Double -> Int -> Integer -> Double -> Double fac :: Integer -> Integer fac 0 = 1 fac k | k > 0 = k * fac (k-1) taylor :: Double -> Int -> Double taylor x n =

我试图在不使用
cosh
函数的情况下计算ch值

ch :: Double -> Int -> Double
ch' :: Double -> Int -> Integer -> Double -> Double

fac :: Integer -> Integer
fac 0 = 1
fac k | k > 0 = k * fac (k-1)

taylor :: Double -> Int -> Double
taylor x n = ((x^2*n))/ (2*(fac n))

ch x iter = ch' x iter 0 1
ch' x iter n sum | iter == fromIntegral n = sum
                 | iter /= fromIntegral n = ch' x iter (n+1) (sum + (taylor x n))
但我有一个错误:

Couldn't match expected type `Double` with actual type `Integer`
In the second argument of `(*)`, namely `n`
In the first argument of `(/)`, namely `((x ^ 2 * n))`

我想我试着除掉Double,但我得到了整数。我怎样才能解决这个问题


非常感谢

问题在于算术运算符
+
*
-
的类型为

Num a => a -> a -> a
其中,操作员两侧的
Num a
必须是相同的
a
Double
Integer
都实现了
Num
,但不能直接添加它们。相反,您必须将值转换为正确的类型。由于您正在从
taylor
返回一个
Double
,因此我猜您希望将
整数
值转换为
Double
值。使用
fromInteger
(它实际上是
Num
typeclass中的一个函数)可以很容易地做到这一点:

请注意,在此计算中,您必须转换两个
整数值。如果这看起来有点混乱,您可以始终使用
where
子句:

taylor x n = (x^2 * n')/ (2 * facn)
    where
        n' = fromInteger n
        facn = fromInteger $ fac n
可能重复的
taylor x n = (x^2 * fromInteger n) / (2 * fromInteger (fac n))
taylor x n = (x^2 * n')/ (2 * facn)
    where
        n' = fromInteger n
        facn = fromInteger $ fac n