Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a `flip` function in the OCaml standard library?

Tags:

ocaml

In Haskell, we have a flip function: flip f x y = f y x, which essentially takes a function and returns the same function except that the two arguments are swapped. I wonder if there is a counterpart in OCaml, since I could not find one and don't want to rewrite it every time.

Cheers,

like image 533
tfboy Avatar asked May 18 '13 18:05

tfboy


People also ask

What does :: do in OCaml?

It is usually used if you have a function or value that is very similar to some other, but is in some way new or modified. Regarding the :: symbol - as already mentioned, it is used to create lists from a single element and a list ( 1::[2;3] creates a list [1;2;3] ).

What is flip Haskell?

flip f takes its (first) two arguments in the reverse order of f.

How do I return something to OCaml?

OCaml doesn't have a return keyword — the last expression in a function becomes the result of the function automatically.


1 Answers

Many functions like this for generalized FP plumbing aren't defined in the standard OCaml library. I have always missed them.

However, nowadays there are good OCaml libraries that supply most or all of these missing functions. The OCaml Batteries Included project defines flip in the BatPervasives module. The Jane Street Core project defines flip in the Fn module.

like image 180
Jeffrey Scofield Avatar answered Nov 26 '22 07:11

Jeffrey Scofield