Wolfram Computation Meets Knowledge

The Wolfram Language:
Fast Introduction for Programmers

Show additional notes for:
All sections 
Get Started

Pure Functions

The Wolfram Language allows what it calls pure functions, indicated by ending with &
Their first argument is indicated by #

(These are also known as anonymous functions, lambda expressions, etc.)

Notes for Java programmers:

Pure functions work similarly to lambda expressions in Java, but the syntax is simpler and more consistent in the Wolfram Language.

Notes for Python programmers:

Pure functions in the Wolfram Language work similarly to lambda functions in Python.

Make a pure function for adding 1:

(# + 1) &

If a pure function is given as the head of an expression, the function is applied to the arguments:

(# + 1) &[50]

Here is a function of several arguments:

{#2, 1 + #1, #1 + #2} &[a, b]

This is an alternative way to specify the function:

Function[{x, y}, {y, 1 + x, x + y}][a, b]

Notes for Java programmers:

Wolfram Language pure functions provide a much simpler syntax for including multiple parameters than Java lambda expressions.

Notes for Python programmers:

Wolfram Language pure functions can indicate parameters either with # or by giving them explicit names. Python's lambda functions always require named parameters.

Lots of built-in functions commonly use pure functions:

Select[{1, 4, 6, 8, 10, 15}, # > 7 &]
NestList[f[#, #] &, a, 3]

Which of the following is a pure function that adds two numbers?

Which of these is the value of (# + 2) & [10]?

Which of these is the output of {#2, #1, #3} & [2, 3, 4, 5]?

© 2017 Wolfram. All rights reserved.