Dolist and Domatrix
After a function has been defined, it can be applied to a list of argument values even if the function is not defined for lists. This is accomplished using the dolist and domatrix keywords. These statements have the format:
For example, assume you have defined the function:
Next, you want to apply this function to every element in the list named Points:
You do this with the expression:
Which produces the result [9,17,27,39,53].
If the function you supply accepts only one argument, f(x), dolist and domatrix do the same thing. That is:
For example:
If the function you supply accepts more than one argument, f(x,y), domatrix causes all permutations of all arguments to be applied while dolist treats the argument lists as parallel:
dolist f(a,b) = [
f(a[0],b[0]),
f(a[1],b[1]),
f(a[2],b[2]),
...
]
domatrix f(a,b) = [
[f(a[0],b[0]),f(a[0],b[1]),f(a[0],b[2]),...],
[f(a[1],b[0]),f(a[1],b[1]),f(a[1],b[2]),...],
[f(a[2],b[0]),f(a[2],b[1]),f(a[2],b[2]),...],
...
]
For example, if you define f(x,y) as
then, the expression
produces the 10-element vector
However, the expression
produces the 100-element matrix
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
You can use the dolist and domatrix keywords with any functions, including primitives and user-defined functions. In addition, the list arguments can contain other lists or matrices as elements. That is, dolist F([A,B,C]) = [F(A),F(B),F(C)] even if A, B, and C are themselves lists or matrices.