An Elementary Introduction to the Wolfram Language
29More about Pure Functions
Weve seen how to make lists and arrays with Table. Theres also a way to do it using pure functionswith Array.
Generate a 10-element array with an abstract function f:
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
Table can give the same result, though you have to introduce the variable n:
In[3]:=
Click for copyable input
Out[3]=
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:
In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=
If the function is Times, Array makes a multiplication table:
In[6]:=
Click for copyable input
Out[6]=
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:
In[7]:=
Click for copyable input
Out[7]=
#1 always picks out the first argument, and #2 the second argument:
In[8]:=
Click for copyable input
Out[8]=
Now we can use #1 and #2 inside a function in Array.
Use a pure function to make a multiplication table:
In[9]:=
Click for copyable input
Out[9]=
Use a different pure function that puts in x whenever the numbers are equal:
In[10]:=
Click for copyable input
Out[10]=
Heres the equivalent computation with Table:
In[11]:=
Click for copyable input
Out[11]=
NestList takes a single function, say f, and successively nests it:
In[12]:=
Click for copyable input
Out[12]=
In[13]:=
Click for copyable input
Out[13]=
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.
Heres FoldList with an abstract function f:
In[14]:=
Click for copyable input
Out[14]=
Including Framed makes it a little easier to see whats going on:
In[15]:=
Click for copyable input
Out[15]=
At first, this may look complicated and obscure, and it might seem hard to imagine how it could be useful. But actually, its very useful, and surprisingly common in real programs.
FoldList is good for progressively accumulating things. Lets 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):
In[16]:=
Click for copyable input
Out[16]=
Heres the computation its doing:
In[17]:=
Click for copyable input
Out[17]=
Or equivalently:
In[18]:=
Click for copyable input
Out[18]=
In[19]:=
Click for copyable input
Out[19]=
Of course, this case is simple enough that you dont actually need a pure function:
In[20]:=
Click for copyable input
Out[20]=
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:
In[21]:=
Click for copyable input
Out[21]=
Finally, lets 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:
In[22]:=
Click for copyable input
Out[22]=
The concept of FoldList is not at first the easiest to understand. But when youve got it, youve learned an extremely powerful functional programming technique thats 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?
Its equivalent to #1a slot to be filled from the first argument of a function.
How does one say #1?
Can one name arguments to a pure function?
Yes. Its done with Function[{x, y}, x+y], etc. Its sometimes nice to do this for code readability, and its 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.
 
Download Notebook Version