1
我試圖計算ch值而不使用cosh
函數。無法匹配預期類型Double與實際Int
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))`
和
Couldn't match expected type `Double` with actual type `Integer`
In the second argument of `(*)`, namely `fac n`
In the first argument of `(/)`, namely `(2 *(fac n))`
我猜我試圖分裂雙,但我有整型。我如何解決這個問題?
非常感謝!
可能重複[哈斯克爾,乘以INT和函數內浮動(http://stackoverflow.com/questions/19019093/ Haskell的倍增-INT-和浮子中之函數) – amalloy 2014-09-19 22:43:03