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
.
Get hands-on with 1400+ tech skills courses.