我试图找出问题所在。似乎类型有问题,但单独使用相同的表达式可以正常工作。
这是代码:
a = [9, 4, 12, 0, -6, 16] :: [Int]
qsort:: [Int] -> [Int]
qsort [] = []
qsort [x] = [x]
qsort xs = (qsort l)++(qsort r)
where m = (realToFrac(sum xs)) / (realToFrac(length xs))
l = filter (<=m) xs
r = filter (>m) xs
main::IO()
main = do
print (show (qsort a))
它抛出:
main.hs:7:36:
No instance for (Fractional Int)
arising from a use of `/'
Possible fix: add an instance declaration for (Fractional Int)
In the expression: (realToFrac (sum xs)) / (realToFrac (length xs))
In an equation for `m':
m = realToFrac (sum xs) / (realToFrac (length xs))
In an equation for `qsort':
qsort xs
= (qsort l) ++ (qsort r)
where
m = realToFrac (sum xs) / (realToFrac (length xs))
l = filter (<= m) xs
r = filter (> m) xs
但这很好用:
a = [9, 4, 12, 0, -6, 16] :: [Int]
main::IO()
main = do
print (show ( (realToFrac(sum a)) / (realToFrac(length a)) ))