Is there a `flip` function in the OCaml standard library?
ocaml string module
ocaml list to string
ocaml power function
In Haskell, we have a
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.
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
Is there a `flip` function in the OCaml standard library?, 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. This chapter describes the functions provided by the OCaml standard library. The modules from the standard library are automatically linked with the user’s object code files by the ocamlc command. Hence, these modules can be used in standalone programs without having to add any .cmo file on the command line for the linking phase.
The pain is somewhat mitigated by labeled arguments:
# let f ~x ~y = x - y;; val f : x:int -> y:int -> int = <fun> # f ~y:5;; - : x:int -> int = <fun> # f ~x:6;; - : y:int -> int = <fun>
Thus if you're willing to write down the labels (which some argue makes code more readable) then you may be able to get the behavior you want. It depends on the situation of course.
Module Stdlib, The Exit exception is not raised by any library function. being parsed by the compiler, with the standard error format of OCaml: "File %S, line %d, characters %d-%d". Reverse-application operator: x |> f |> g is exactly equivalent to g (f (x )) . There is a wealth of type-conversion functions in the Pervasives module. They all have (somewhat unidiomatic, English-wise) names of the form rtype _of_ dtype , where dtype is the type of the domain (i.e., the type to convert from ) and rtype is the type of the range (i.e. the type of to convert to , the result).
Fun.flip in included in the standard library.
Module Fun, flip f reverses the argument order of the binary function f . For any arguments x and y , (flip f) x y is f y x . Write an OCaml function string_of_tree which generates this string representation, if the tree is given as usual (as Empty or Node(x,l,r) term). Then write a function tree_of_string which does this inverse; i.e. given the string representation, construct the tree in the usual form.
Falling foul of the "|>" operator with a non-commutative function , Std library that there is a function called Fn.flip that seems to do what I'm looking for, allowing me to use non-commutative functions without too much bother: For example, the implementation of set union in the OCaml standard library in theory is asymptotically faster than the equivalent function in the standard libraries of imperative languages (e.g., C++, Java) because the OCaml implementation exploits the immutability of sets to reuse parts of input sets in the output (see persistent data structure).
99 problems – OCaml, Write a function last : 'a list -> 'a option that returns the last element of a list. (easy) . Solution. # let rec OCaml standard library has List.length but we ask that you reimplement it. Bonus for a tail HINT: a palindrome is its own reverse. Solution. Core – Jane Street Capital's full-fledged standard library overlay. A portable subset of Core is also available: Core_kernel. Base - Jane Street Capital's dependency-free, quick-compiling, fully-portable across any environment that can run OCaml code standard library. React – React is an OCaml module for functional reactive programming (FRP
Higher-order Programming, There is an obvious similarity between these two functions: what they do is apply a given It's called List.map in the OCaml standard library. the list first then use fold_left ; the operator will need to take its arguments in the reverse order, too : The PR provides a few functions that can be systematically found in containers' CCFun module, base's Fn module and the Haskell Prelude. These are exactly: const, id and flip. While preparing this PR I discovered that the function Bool.negate introduced by PR #2010 actually lives in Base under the name Fn.non.
- The OCaml equivalent of Hoogle doesn't seem to find anything, at least.
- In core the function is
- Thanks a lot. Lucky that it's been defined in Core.
flipis actually more useful in contexts of partial application. A simple example would be a function to subtract a number by 2:
flip (-) 2