Use Currying for Function Chaining
Learn how we can use currying to chain functions to formulate multi-argument functions and how it enables partial application.
We'll cover the following...
Function with multiple arguments
One significant insight from lambda calculus is currying—we can formulate a function accepting multiple arguments as a nested chain of single-valued functions. Most functional programming languages, including OCaml, incorporate this technique into the language.
To illustrate, let’s look at OCaml’s built-in binary operator, +, to add two integers. Internally, OCaml implements + as a normal function. Although we normally use + in the infix notation like 1 + 2, OCaml allows us to use + in the prefix notation by placing it in parentheses (+). This means we can write (+) 1 2.
Except for its odd name, (+) is a normal function like any other function. It’s a binary function that accepts two integers and returns their sum. Its type is int -> int -> int.
Note: The
->operator is right-associative and so the type is ...