Why does filter odd.fst [(1,2), (2,3)]
give me a compile error? odd.fst
should take in a tuple of ints and output a boolean, so I am confused as to why the compiler is telling me it can't match types.
Asked
Active
Viewed 100 times
4

boneofmysword
- 43
- 4
2 Answers
14
For the same reason that 2 * 3+4
is 10
, not 14
. Operator precedence does not care about spacing: 2 * 3+4
parses as (2 * 3) + 4
.
Similarly,
filter odd.fst [(1,2), (2,3)]
parses as
(filter odd) . (fst [(1,2), (2,3)])
no matter how you space it. This is because function application has higher precedence than any infix operator.
You want
filter (odd . fst) [(1,2), (2,3)]
instead.

melpomene
- 84,125
- 8
- 85
- 148
6
The reason you're getting a type mismatch compiler error is because the order of precedence is not as you're expecting. If you add more brackets then you will get the desired result:
filter (odd.fst) [(1,2), (2,3)]

Micha Wiedenmann
- 19,979
- 21
- 92
- 137

silleknarf
- 1,219
- 6
- 22