Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell zipWith

I want to sum a zipped list.

averageGrade :: [Float] -> [Int] -> Float
averageGrade [0.75 , 0.25] [6, 4] , result: 0,75*6 + 0.25*4 = 5.5 

when I go to ghci and do the following:

sum(zipWith (*) [0.75, 0.25] [6, 4])

I get excactly what i want to.

But in code I'm getting an error, and I don't know why.

    averageGrade :: [Float] -> [Int] -> Float
    averageGrade a b
                | a == [] = 0
                | b == [] = 0
                | otherwise = (sum(zipWith (*) a b))

If I want to compile this I'm getting the following failure:

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.
like image 479
Sorun Avatar asked Nov 14 '19 09:11

Sorun


3 Answers

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)
like image 162
chi Avatar answered Oct 19 '22 00:10

chi


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
like image 42
Redu Avatar answered Oct 19 '22 00:10

Redu


Alternative implementation:

averageGrade :: [Float] -> [Float] -> Float
averageGrade [] _ = 0
averageGrade _ [] = 0
averageGrade (x:xs) (y:ys) = x * y + (averageGrade xs ys)

Usage:

averageGrade [1.0, 2.0] [2.0, 2.0]
like image 1
Thomas Cook Avatar answered Oct 19 '22 00:10

Thomas Cook