Haskell zipWith
You can't *
two numbers having a different type, like Float
and Int
. You need to explicitly convert one of them so that they have the same type (Float
, in your case).
averageGrade :: [Float] -> [Int] -> Float
averageGrade a b
| a == [] = 0
| b == [] = 0
| otherwise = sum (zipWith (\ x y -> x * fromIntegral y) a b)
Note that you do not really need to check the ==[]
cases, since zipWith
returns []
is those cases, and sum [] == 0
.
averageGrade :: [Float] -> [Int] -> Float
averageGrade a b = sum (zipWith (\ x y -> x * fromIntegral y) a b)
You can (*)
different types given that those are members of Num
type class. You may add Num
and Eq
constraints to the type signature such as;
averageGrade :: (Num a, Eq a) => [a] -> [a] -> a
averageGrade a b | a == [] = 0
| b == [] = 0
| otherwise = sum $ zipWith (*) a b
*Main> averageGrade [0.75 , 0.25] [6, 4]
5.5
However as @chi mentions you don't really need the checks for empty lists hence we don't really need the Eq
constraint in the type signature. The following should be sufficient.
averageGrade :: Num a => [a] -> [a] -> a
averageGrade a b = sum $ zipWith (*) a b
*Main> averageGrade [0.75 , 0.25] [6, 4]
5.5