Currying in Mathematica

Written by on May 19, 2019 in LISP, Mathematica, Programming with 0 Comments

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.

Function Call

Function Call

Mismatched arguments will not match the function definition and hence it will not be called. Obviously, this is the expected and correct behaviour.

Argument Mismatch

Argument Mismatch

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.

Currying

Currying

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.

Curry Variant

Curry Variant

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.

Currying in Lisp

Currying in Lisp

Here is how it can be used:

Lisp Currying Examples

Lisp Currying Examples

Hope I was able to shed some light on Mathematica’s “Curry” function. Have a nice weekend!

Tags: , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top