Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does odd.fst not work with the filter function?

Tags:

haskell

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.

like image 313
boneofmysword Avatar asked Nov 30 '22 21:11

boneofmysword


1 Answers

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.

like image 107
melpomene Avatar answered Dec 05 '22 04:12

melpomene