Programming Language for Old Timers

by David A. Moon
February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.

Previous page   Table of Contents   Next page

Curried Functions

The name _ (pronounced "blank") has a special meaning, indicating partial application, also known as curried functions. It acquires this meaning because its definition in the PLOT module is a macro that takes no arguments and expands into a special flag. The infix ( macro, which is the syntax for function calls, recognizes this flag and produces a function constructor instead of an invocation. The expression parser does the same thing for operator calls. The logic to recognize the curried function flag is centralized in a pseudo-constructor curryable-invocation which returns either an invocation, a spread-invocation, or a functation.

Thus a function call expression (whether written with parentheses or with an operator) where one or more of the arguments is the name _ does not call the function. Instead, it constructs a function containing one method with as many arguments as there are underscores, which when called will call the original function with its arguments substituted for the underscores, and non-underscore arguments having the values originally specified.

For example, _ + 5 is a function of one argument that adds 5 to its argument.

It is not easy to generalize this to e.g. _ + 3 * _ as a function of two arguments that adds the first to three times the second. We will not try. Instead, _ + 3 * _ is a function of one argument that adds its argument to another function of one argument that multiplies its argument by 3, as if it had been written _ + (3 * _). Presumably the + function has no method applicable to functions.

The keyword "...:", which cannot be written in source code but can be created by calling the constructor keyword("..."), serves as a flag to curryable-invocation to indicate that the preceding argument is to be spread.

Previous page   Table of Contents   Next page