This polymorphic function allows us to flip the order of the arguments of an arbitrary curried function:
# let flip f x y = f y x ;;
val flip : ('a -> 'b -> 'c) -> 'b -> 'a -> 'c
That is flip
takes a function of type 'a -> 'b -> 'c and returns a function of type b -> 'a -> 'c. But I actually don't get it, why it is correct? how the order of a,b,c are determined by ? Pretty confused about it, can anyone explain for me, thank you.
a
andb
are determined by the order ofx
andy
, andc
is determined by the return type off
. – Jeffrey Scofieldf
doesn't change. So the last type variable will be the same in both cases. Only the type variables forx
andy
will change, because the order of the parameters is inverted. – Jeffrey Scofieldflip
is a function of typea -> b -> c
. So the type off
isa -> b -> c
. The return type offlip
is also a function. Think of what this returned function is going to return itself. It's the same as the return type of the input function. I.e., it'sc
. So your type can't be right. Your proposed type would return a type that is one of the parameters off
. – Jeffrey Scofield