Note: I’m going through draft posts that go back to 2014 and publishing where they still may have value. They may not be 100% upto date but better published late than never.
Currying
Currying leads to the ability to create partial applications.
Currying is the process of taking a function with more than one arguments and turning it into single argument functions, for example
let add a b = a + b // becomes let add a = let add' b = a + b add'
This results in a function syntax which looks like this
val add : a:int -> (int -> int)
The bracketed int -> int shows the function which takes an int and returns and int.
Partial Applications
A partial application is a way of creating functions which have some of their arguments supplied and thus creating new functions. For example in it’s simplest form we might have
let add a b = a + b let partialAdd a = add a 42
So, nothing too exciting there but what we can also do is, if we supply the first n arguments as per the following example
let add a b = a + b let partialAdd = add 42
notice how we’ve removed the arguments to partialAdd but we can call the function thus
partialAdd 10 |> printfn "%d"
we’re still supplying an argument because the function being called (the add function) requires an extra argument. The partialAdd declaration creates a new function which is partially implemented.
By declaring functions so that the last element(s) are to be supplied by a calling function we can better combine and/reuse functions.