我想总结一个压缩列表。
averageGrade :: [Float] -> [Int] -> Float
averageGrade [0.75 , 0.25] [6, 4] , result: 0,75*6 + 0.25*4 = 5.5
当我去 ghci 并执行以下操作时:
sum(zipWith (*) [0.75, 0.25] [6, 4])
我得到了我想要的。
但是在代码中我遇到了一个错误,我不知道为什么。
averageGrade :: [Float] -> [Int] -> Float
averageGrade a b
| a == [] = 0
| b == [] = 0
| otherwise = (sum(zipWith (*) a b))
如果我想编译它,我会遇到以下失败:
Couldn't match type ‘Int’ with ‘Float’
Expected type: [Float]
Actual type: [Int]
In the third argument of ‘zipWith’, namely ‘b’
In the first argument of ‘sum’, namely ‘(zipWith (*) a b)’
Failed, modules loaded: none.