Clearly, functions in mathematica are not pure functions according to the definition on Wikipedia (no side effects - basically implementabe with table lookups):

x = 4;
y = 1;
a = Function[y = 0; x + #];
y
a[5]
y
x = 0;
a[5]

this produces

{1, 9, 0, 5}

Clearly it is affected by external changes (violating 1.) and it does have side-effects (violating 2.). Yet the manual keeps referring to functions as "pure functions" (c.f. e.g. "tutorial/PureFunctions"). What's the deal with this unnecessary and wrong wordiness? "Function" would have done fine...

  • 7
    Mathematics definition of pure function is completely different. "Pure functions allow you to give functions which can be applied to arguments, without having to define explicit names for the functions. ". Maybe that wiki pages needs an 'alternate uses' section. – george2079 Oct 31 '14 at 13:53
  • 2
    To expand on George's comment, a pure function in Mathematica does not describe a transformation rule where as any use of = or := associates a transformation rule with the symbol on the LHS, e.g. OwnValues, DownValues, SubValues, etc. So, in that sense, it is pure. – rcollyer Oct 31 '14 at 15:26
  • Clearly, this is a NKPF. – bobthechemist Oct 31 '14 at 16:56
  • 5
    Mathematica pure functions really should be called anonymous functions. en.wikipedia.org/wiki/Anonymous_function Java now has them, called there as lambda functions. C++ 11 has them also. The name "pure" is overloaded a little. The main thing is "is a function definition that is not bound to an identifier" from the above Wiki – Nasser Oct 31 '14 at 18:04
  • @Nasser, I think your comment would make a good answer. – Simon Woods Oct 31 '14 at 20:00
up vote 19 down vote accepted

The term pure function used in Mathematica is not being used in the same sense as the cited Wikipedia article. In Mathematica it refers to an anonymous function. In the Wikipedia article it is a term extracted by analogy from the increasingly popular term "purely functional" which refers (mainly) to deterministic programming free of side-effects.

The Mathematica usage largely predates the contemporary popular usage.

Pure Function in Mathematica

Section 4.1.4 of the Mathematica Book (1988), states:

There are various ways to specify the functions you give as arguments. If they are built-in functions, or are functions that you have explicitly defined, then you can refer to them simply by giving their names. In many cases, however, it is more convenient to be able to build up "pure functions" to which you do not have to give specific names.

This usage aligns with what are often called "anonymous functions" today. Note the use of quotation marks around "pure functions" suggesting that the term is being coined here. Later, it is said that:

This kind of Mathematica expression is directly analogous to the λ expressions that are used in LISP or in formal logic.

Again, this emphasizes their usage as anonymous functions. And then:

One goal of functional programming is completely to eliminate named variables.

So, in Mathematica, it appears that the "purity" of a function means that is it unencumbered by names. (I note in passing that eliminate named variables suggests the point-free programming style to me. Slots in pure functions do not conform to that style, but V10 is adding features that make point-free style a little easier.)

Pure Function in Wikipedia

The cited Wikipedia article defining the term "pure function" is tagged as controversial for lack of supporting citations. Neither of the two references that it cites define, or even mention, the term "pure function".

They do, however, speak of "pure functional" or "purely functional" programming languages. I suspect that "pure function" in that article is simply a coined reduction of the far more widespread term "purely functional".

Purely Functional in Popular Usage

My own impression is that the "pure" qualifier with respect to functional programming was relatively obscure until the Haskell programming language started to get some mindshare. Haskell emerged from the pure functional programming community in 1990, two years after The Mathematica Book, and even then did not really gain popularity until after the release of Haskell98.

I find it interesting to note that the following papers make no use of the term "pure" in connection with functional programming:

But we start to see usage of the term by the time of these:

If the Google books Ngram Viewer is to be believed, then the Mathematica usage of "pure" appears at the leading edge of an upsurge in wider usage:

ngram viewer screenshot

Summary

I interpret this (admittedly anecdotal) evidence to support the position that the Mathematica use of "pure function" is not a misapplication of the general usage. Rather, it is an independently coined term for "anonymous function" that predates the contemporary common understanding.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.