Types “哈斯克尔错误”;无法推断因使用..而产生的(整数浮点);
我是Haskell的新手,正在尝试计算一系列浮点数的标准偏差。Ubuntu 18.04,ghc 8.0.2。我在谷歌上搜索到了以下错误,但仍然不明白“积分浮点”到底是什么Types “哈斯克尔错误”;无法推断因使用..而产生的(整数浮点);,types,Types,我是Haskell的新手,正在尝试计算一系列浮点数的标准偏差。Ubuntu 18.04,ghc 8.0.2。我在谷歌上搜索到了以下错误,但仍然不明白“积分浮点”到底是什么 *Main> let z = stdDev 0 1 y x <interactive>:250:9: error: • Could not deduce (Integral Float) arising from a use of ‘stdDev’ from the context: Floating a
*Main> let z = stdDev 0 1 y x
<interactive>:250:9: error:
• Could not deduce (Integral Float) arising from a use of ‘stdDev’
from the context: Floating a
bound by the inferred type of z :: Floating a => [a]
at <interactive>:250:5-38
• In the expression: stdDev 0 (length (head (x))) y x
In an equation for ‘z’: z = stdDev 0 (length (head (x))) y x
变量:
*Main> y
[380.0,1.0]
*Main> x
[[600.0,1.0],[400.0,1.0],[170.0,1.0],[430.0,1.0],[300.0,1.0]]
我找到了解决办法。我需要指定函数类型并添加“fromIntegral”,而我的一些算法逻辑是错误的
stdDev :: Int -> Float -> [Float] -> [[Float]] -> [Float]
stdDev i l ms xs
| fromIntegral(i) < length(head(xs)) = sqrt((sumOfMinusMeans i (ms!!i) xs)
/ l): (stdDev (i+1) l ms xs)
| otherwise = []
sumOfMinusMeans :: Int -> Float -> [[Float]] -> Float
sumOfMinusMeans i m (x:xs)
| xs == [] = (x!!i - m)**2
| otherwise = (x!!i - m)**2 + (sumOfMinusMeans i m xs)
stdDev::Int->Float->[Float]->[[Float]]]->[Float]
stdDev i l ms xs
|fromIntegral(i)Float->[[Float]]->Float
平方米(x:xs)
|xs=[]=(x!!i-m)**2
|否则=(x!!i-m)**2+(分钟数之和i m xs)
*Main> y
[380.0,1.0]
*Main> x
[[600.0,1.0],[400.0,1.0],[170.0,1.0],[430.0,1.0],[300.0,1.0]]
stdDev :: Int -> Float -> [Float] -> [[Float]] -> [Float]
stdDev i l ms xs
| fromIntegral(i) < length(head(xs)) = sqrt((sumOfMinusMeans i (ms!!i) xs)
/ l): (stdDev (i+1) l ms xs)
| otherwise = []
sumOfMinusMeans :: Int -> Float -> [[Float]] -> Float
sumOfMinusMeans i m (x:xs)
| xs == [] = (x!!i - m)**2
| otherwise = (x!!i - m)**2 + (sumOfMinusMeans i m xs)