I’m curious about the expression flip id (It’s not homework: I found it in the getOpt documentation).
I wonder why it has this type:
Prelude> :t (flip id)
(flip id) :: b -> (b -> c) -> c
For example, (flip id) 5 (+6) gives 11.
I know why id (+6) 5 gives 11, but I don’t “get” the flip id thing.
I tried to figure this out myself using pen and paper but couldn’t. Could anybody please explain this to me? I mean, how does flip id come to have the type b -> (b -> c) -> c ?
The
idfunction has this type:You get an instance of this type, when you replace
abya -> b:which, because of currying, is the same as:
Now apply
flipto this and you get:In the case of
id (+)the instance is:Now
flip idgives you:Side note: This also shows you how
($)is the same asid, just with a more restricted type: