'Why am I getting type mismatch when my answer is a fraction?

I am trying to find the dot quotient of two tuples through pattern matching in Haskell, which is dot(q,p) = q1/p1 + q2/p2 + q3/p3.

I keep getting this error in terminal when I run tupleDotQuotient (1, 2, 3) (1, 2, 3):

Couldn't match expected type ‘[p]’
                  with actual type ‘(a0, b0, c0)’
    • In the first argument of ‘tupleDotQuotient’, namely ‘(1, 2, 3)’
      In the expression: tupleDotQuotient (1, 2, 3) (1, 2, 3)
      In an equation for ‘it’: it = tupleDotQuotient (1, 2, 3) (1, 2, 3)
    • Relevant bindings include it :: p (bound at <interactive>:10:1)

I am new to Haskell and am not sure what the problem is. If I change it to Num p => [p] -> [p] -> p, I get an error in (x / y) since (/) is a part of Fraction type class. What can I do to improve this pattern matching to find the dot quotient of two tuples/vectors?



Solution 1:[1]

[p] is a list of ps, not a tuple. Call your function with actual lists:

tupleDotQuotient [1, 2, 3] [1, 2, 3]

You could implement a tupleDotQuotient for tuples, but you'd have to provide an overload for each tuple size - so don't. (On the other hand, using arbitrarily-sized lists means that you could pass two lists of different lengths to your function, so you'll need to handle that case).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bergi