WOLFRAM

29 More about Pure Functions

29More 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:
In[1]:=
Out[1]=
Use a pure function to generate a list of the first 10 squares:
In[2]:=
Out[2]=
Table can give the same result, though you have to introduce the variable n:
In[3]:=
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]:=
Out[4]=
Display it in a grid:
In[5]:=
Out[5]=
If the function is Times, Array makes a multiplication table:
In[6]:=
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]:=
Out[7]=
#1 always picks out the first argument, and #2 the second argument:
In[8]:=
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]:=
Out[9]=
Use a different pure function that puts in x whenever the numbers are equal:
In[10]:=
Out[10]=
Here’s the equivalent computation with Table:
In[11]:=
Out[11]=
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:
In[12]:=
Out[12]=
In[13]:=
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.
Here’s FoldList with an abstract function f:
In[14]:=
Out[14]=
Including Framed makes it a little easier to see what’s going on:
In[15]:=
Out[15]=
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):
In[16]:=
Out[16]=
Here’s the computation it’s doing:
In[17]:=
Out[17]=
In[18]:=
Out[18]=
It may be easier to see what’s going on with symbols:
In[19]:=
Out[19]=
Of course, this case is simple enough that you don’t actually need a pure function:
In[20]:=
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]:=
Out[21]=
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:
In[22]:=
Out[22]=
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.
Next Section