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,
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] ).
flip f takes its (first) two arguments in the reverse order of f.
OCaml doesn't have a return keyword — the last expression in a function becomes the result of the function automatically.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With