4

How come I can't do

fst . fst (("Bob",12),10)

in Haskell?

:t fst . fst
Prelude> ((c,b),b1) -> c

Doesn't this make (("Bob",12),10) a good candidate for fst . fst since it's

(([Char],Integer),Integer)
asked Jan 4, 2014 at 4:06
3
  • 3
    Hint: It's parsed as fst . (fst (("Bob",12),10)) whereas you want (fst . fst) (("Bob",12),10). Commented Jan 4, 2014 at 4:07
  • @Vitus you really live up to your name as a Wunderkid (if your nick does stem from that movie) Thanks a lot :] Commented Jan 4, 2014 at 4:18
  • For the record, what was the actual GHCi error of your original line of code? Commented Jan 4, 2014 at 5:32

1 Answer 1

8

The highest precedence in Haskell is function application or f a. So

fst . fst ((a, b), a)

is parsed as

fst . (fst ((a, b), a))

which is obviously nonsense. You can fix this with the $ operator which is just function application with the lowest precedence, so f $ a == f a.

fst . fst $ ((a, b), a)

Or with some parens

(fst . fst) ((a, b), a)
answered Jan 4, 2014 at 4:15
Sign up to request clarification or add additional context in comments.

2 Comments

What's more haskell-esque - the $ operator or parentheses?
@JaneDoe $ in general

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.