Partial application of functions in OCaml
In OCaml, functions can be partially applied. This means that if a function takes multiple arguments, you can apply some of them and get back a new function that takes the remaining arguments.
For example, the + operator is a function that takes two arguments and returns their sum:
(+) ;;
- : int -> int -> int = <fun>
You can partially apply this function by providing only the first argument. For example, consider the following code:
let add_five = (+) 5;;
val add_five : int -> int = <fun>
In this code, we have created a new function called add_five that takes a single argument and returns the sum of that argument and 5. We achieved this by partially applying the + operator with the argument 5.
Now we can use add_five like any other function. For example, add_five 10 will return 15.
Read other posts