Haskell: Couldn't match expected type ‘(a, b0)’ with actual type ‘(a, b, h)’
My code:
g :: (a, b, h) > (c, d, i) > ((a, c))
g x y = ((fst x, fst y))
Why does it print the following errors?:
ax.hs:11:15: error:
â€¢ Couldn't match expected type â€˜(a, b0)â€™
with actual type â€˜(a, b, h)â€™
â€¢ In the first argument of â€˜fstâ€™, namely â€˜xâ€™
In the expression: fst x
In the expression: ((fst x, fst y))
â€¢ Relevant bindings include
x :: (a, b, h) (bound at ax.hs:11:3)
g :: (a, b, h) > (c, d, i) > (a, c) (bound at ax.hs:11:1)

11  g x y = ((fst x, fst y))
 ^
ax.hs:11:22: error:
â€¢ Couldn't match expected type â€˜(c, b1)â€™
with actual type â€˜(c, d, i)â€™
â€¢ In the first argument of â€˜fstâ€™, namely â€˜yâ€™
In the expression: fst y
In the expression: ((fst x, fst y))
â€¢ Relevant bindings include
y :: (c, d, i) (bound at ax.hs:11:5)
g :: (a, b, h) > (c, d, i) > (a, c) (bound at ax.hs:11:1)

11  g x y = ((fst x, fst y))
Please help. I'm not sure why this is happening. How do I fix it? Thank you
Willem Van Onsem
answered question
Robin Zigmond
commented
fst
only applies to pairs (2tuples)
1 Answer
fst :: (a, b) > a
only works for 2tuples, not tuples with a different arity than 2.
You can use pattern matching to obtain the first item of the 3tuple, and personally I would use pattern matching for the second tuple as well, since this is  in my opinion  a more transparent syntax:
g :: (a, b, h) > (c, d, i) > (a, c)
g (x, _, _) (y, _, _) = (x, y)
Note that ((a, c))
is the same as (a, c)
, so we can remove the extra parenthesis.
Willem Van Onsem
posted this
Have an answer?
JD
fst :: (a,b) > a
. You need something like anfst3
, which would work on triples, but that's not in the Prelude. Pretty easy to write yourself, though.