We’ve seen how to make lists and arrays with Table. There’s also a way to do it using pure functions—with Array.

Array[f,10]

Array[#^2&,10]

Table[n^2,{n,10}]

Array[f,4] makes a single list of 4 elements. Array[f,{3,4}] makes a 3×4 array.

Array[f,{3,4}]

Array[f,{3,4}]//Grid

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.

f[#1,#2]&[55,66]

f[#2,#1,{#2,#2,#1}]&[55,66]

Now we can use #1 and #2 inside a function in Array.

Array[#1*#2&,{5,5}]//Grid

Array[If[#1==#2,x,#1*#2]&,{5,5}]//Grid

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[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.

FoldList[f,x,{1,2,3,4,5}]

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.

FoldList[#1+#2&,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}

{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}

FoldList[#1+#2&,0,{a,b,c,d,e}]

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.

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.

FoldList[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.

Use Prime and Array to generate a list of the first 100 primes. »

Use Prime and Array to find successive differences between the first 100 primes. »

Use Array and Grid to make a 10 by 10 addition table. »

Use FoldList, Times and Range to successively multiply numbers up to 10 (making factorials). »

Use FoldList and Array to compute the successive products of the first 10 primes. »

Use FoldList to successively ImageAdd regular polygons with between 3 and 8 sides, and with opacity 0.2. »

Use Array to make a 10 by 10 grid where the leading diagonal and everything below it is 1 and the rest is 0. »

What does # alone mean?

It’s equivalent to #1—a 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 written—the “#” 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?

Yes—as 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.

Tech Notes