One of the more recent additions to the core Wolfram Language is the “Curry” function. It was introduced in Mathematica ver 11.3 and is still (as of ver 12.0) considered Experimental. In this post, I would like to go over this function briefly.
Non-curried functions in Mathematica have to be invoked with the arguments (both number and types) correctly matched. See the following example.
Mismatched arguments will not match the function definition and hence it will not be called. Obviously, this is the expected and correct behaviour.
The “Curry” function allows us to explicitly define curried functions. The commonly used form of this function takes as arguments, a function and its arity, and returns its curried variant. The following examples illustrate this idea.
Notice the following:
1) A curried function can be invoked by passing all arguments, as in a normal function call
2) When it is invoked using partial arguments, it returns another function that remembers the passed arguments and uses them when invoked with further arguments
3) The order of arguments is the same as in the original (un-curried) function
If a function takes just two arguments and we want the second argument to be curried, then we can use a simpler variant of the “Curry” function.
Now that you understand how the “Curry” function works, let us see how we can implement it in a language that does not have this feature. For example, even though it has so many great features, Lisp (more precisely, Common Lisp), does not support currying by default. How can we implement Mathematica’s “Curry” function in Lisp? Quite simple, actually. See the following implementation and how it is used.
Here is how it can be used:
Hope I was able to shed some light on Mathematica’s “Curry” function. Have a nice weekend!
Recent Comments