In Haskell why can't we compose show and fst

  • A+
Category:Languages

I was expecting for show.fst (1,2) to work. But am surprised to see it give an exception.

On running individual fst (1,2) returns 1::Num a => a and show 1 prints out 1 as string

show.fst $ (1,2) 

works fine. Which is even more confusing as I do not see how the tuple can be reduced further (from what I understand the $ operator is used when we want the right part of expression to be evaluated.

 


In Haskell, function application has higher priority than any operators, so

show . fst (1,2) 

is parsed as

show . (fst (1,2)) 

And, since fst (1,2) == 1 is not a function, it is hard to compose it with anything.


The $ operator is actually just function application, but with a very low priority. This means that, in contrast, this

show . fst $ (1,2) 

if parsed as

(show . fst) $ (1,2) 

which seems to be what you want.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: