# 29 More about Pure Functions

We’ve seen how to make lists and arrays with Table. There’s also a way to do it using pure functionswith Array.
Generate a 10-element array with an abstract function f:
Array[f, 10]

Use a pure function to generate a list of the first 10 squares:
Array[#^2 &, 10]

Table can give the same result, though you have to introduce the variable n:
Table[n^2, {n, 10}]

Array[f,4] makes a single list of 4 elements. Array[f,{3,4}] makes a 3×4 array.
Make a list of length 3, each of whose elements is a list of length 4:
Array[f, {3, 4}]

Display it in a grid:
Array[f, {3, 4}] // Grid

If the function is Times, Array makes a multiplication table:
Grid[Array[Times, {5, 5}]]

What if we want to use a pure function in place of Times? When we compute Times[3,4], we say that Times is applied to two arguments. (In Times[3,4,5], Times is applied to three arguments, etc.) In a pure function, #1 represents the first argument, #2 the second argument and so on.
#1 represents the first argument, #2 the second argument:
f[#1, #2] &[55, 66]

#1 always picks out the first argument, and #2 the second argument:
f[#2, #1, {#2, #2, #1}] &[55, 66]

Now we can use #1 and #2 inside a function in Array.
Use a pure function to make a multiplication table:
Array[#1*#2 &, {5, 5}] // Grid

Use a different pure function that puts in x whenever the numbers are equal:
Array[If[#1 == #2, x, #1*#2] &, {5, 5}] // Grid

Here’s the equivalent computation with Table:
Table[If[i == j, x, i*j], {i, 5}, {j, 5}] // Grid

Now that we’ve discussed pure functions with more than one argument, we’re in a position to talk about FoldList. You can think of FoldList as a 2-argument generalization of NestList.
NestList takes a single function, say f, and successively nests it:
NestList[f, x, 5]

NestList[Framed, x, 5]

NestList just keeps on applying a function to whatever result it got before. FoldList does the same, except that it also “folds in” a new element each time.
Here’s FoldList with an abstract function f:
FoldList[f, x, {1, 2, 3, 4, 5}]

Including Framed makes it a little easier to see what’s going on:
FoldList[Framed[f[#1, #2]] &, x, {1, 2, 3, 4, 5}]

At first, this may look complicated and obscure, and it might seem hard to imagine how it could be useful. But actually, it’s very useful, and surprisingly common in real programs.
FoldList is good for progressively accumulating things. Let’s start with a simple case: progressively adding up numbers.
At each step FoldList folds in another element (#2), adding it to the result so far (#1):
FoldList[#1 + #2 &, 0, {1, 1, 1, 2, 0, 0}]

Here’s the computation it’s doing:
{0, 0 + 1, (0 + 1) + 1, ((0 + 1) + 1) + 1, (((0 + 1) + 1) + 1) + 2, ((((0 + 1) + 1) + 1) + 2) + 0, (((((0 + 1) + 1) + 1) + 2) + 0) + 0}

{0, 0 + 1, 0 + 1 + 1, 0 + 1 + 1 + 1, 0 + 1 + 1 + 1 + 2, 0 + 1 + 1 + 1 + 2 + 0, 0 + 1 + 1 + 1 + 2 + 0 + 0}

It may be easier to see what’s going on with symbols:
FoldList[#1 + #2 &, 0, {a, b, c, d, e}]

Of course, this case is simple enough that you don’t actually need a pure function:
FoldList[Plus, 0, {a, b, c, d, e}]

A classic use of FoldList is to successively “fold in” digits to reconstruct a number from its list of digits.
Successively construct a number from its digits, starting the folding process with its first digit:
FoldList[10 #1 + #2 &, {8, 7, 6, 1, 2, 3, 9, 8, 7}]

Finally, let’s use FoldList to “fold in” progressive images from a list, at each point adding them with ImageAdd to the image obtained so far.
Progressively “fold in” images from a list, combining them with ImageAdd:

The concept of FoldList is not at first the easiest to understand. But when you’ve got it, you’ve learned an extremely powerful functional programming technique that’s an example of the kind of elegant abstraction possible in the Wolfram Language.
29.1Use Prime and Array to generate a list of the first 100 primes. »
Expected output:
 Out[]=
29.2Use Prime and Array to find successive differences between the first 100 primes. »
Expected output:
 Out[]=
29.3Use Array and Grid to make a 10 by 10 addition table. »
Expected output:
 Out[]=
29.4Use FoldList, Times and Range to successively multiply numbers up to 10 (making factorials). »
Expected output:
 Out[]=
29.5Use FoldList and Array to compute the successive products of the first 10 primes. »
Expected output:
 Out[]=
29.6Use FoldList to successively ImageAdd regular polygons with between 3 and 8 sides, and with opacity 0.2. »
Expected output:
 Out[]=
+29.1Use Array to make a 10 by 10 grid where the leading diagonal and everything below it is 1 and the rest is 0. »
Expected output:
 Out[]=
What does # alone mean?
It’s equivalent to #1a slot to be filled from the first argument of a function.
How does one say #1?
Either “slot 1” (reflecting its role in a pure function), or “hash 1” (reflecting how it’s writtenthe “#” is usually called “hash”).
Can one name arguments to a pure function?
Yes. It’s done with Function[{x, y}, x+y], etc. It’s sometimes nice to do this for code readability, and it’s sometimes necessary when pure functions are nested.
Can Array make more deeply nested structures?
Yesas deep as you want. Lists of lists of lists... for any number of levels.
What is functional programming?
It’s programming where everything is based on evaluating functions and combinations of functions. It’s actually the only style of programming we’ve seen so far in this book. In Section 38 we’ll see procedural programming, which is about going through a procedure and progressively changing values of variables.
• Fold gives the last element of FoldList, just like Nest gives the last element of NestList.
• FromDigits reconstructs numbers from lists of digits, effectively doing what we used FoldList for above.
• Accumulate[list] is FoldList[Plus, list].
• Array and FoldList, like NestList, are examples of what are called higher-order functions, that take functions as input. (In mathematics, they’re also known as functionals or functors.)
• You can set up pure functions that take any number of arguments at a time using ##, etc.
• You can animate lists of images using ListAnimate, and show the images stacked in 3D using Image3D.