Lists: The Building Blocks of Maxima
This 20th article in our series on Mathematics in Open Source showcases the list manipulations in Maxima, the programming language with an ALGOL-like syntax but Lisp-like semantics.
Lists are the basic building blocks of Maxima. The fundamental reason is that Maxima is implemented in Lisp, the building blocks of which are also lists. To begin with, let us walk through the ways of creating a list. The simplest method to get a list in Maxima is to just define it, using . So, [x, 5, 3, 2*y] is a list consisting of four members. However, Maxima provides two powerful functions for automatically generating lists: makelist() and create_list().
makelist() can take two forms. makelist (e, x, x0, xn) creates and returns a list using the expression ‘e’, evaluated for ‘x’ using the values ranging from ‘x0’ to ‘xn’. makelist(e, x, L) creates and returns a list using the expression ‘e’, evaluated for ‘x’ using the members of the list L. Check out the example below for better clarity:
Note the interesting usage of concat() to just concatenate its arguments. Note that makelist() is limited by the variation it can have, which to be specific, is just one – ‘i’ in the first two examples and ‘x’ in the last one. If we want more, the create_list() function comes into play.
create_list(f, x1, L1, ..., xn, Ln) creates and returns a list with members of the form ‘f’, evaluated for the variables x1, ..., xn using the values from the corresponding lists L1, ..., Ln. Here is just a glimpse of its power:
Note that ‘all possible combinations’ are created using the values for the variables ‘x’, ‘y’ and ‘z’.
Once we have created the lists, Maxima provides a host of functions to play around with them. Let’s take a look at these.
Testing the lists
The following set of functions demonstrates the various checks on lists: atom(v) - returns ‘true’ if ‘v’ is an atomic element; ‘false’ otherwise listp(L) - returns ‘true’ if ‘L’ is a list; ‘false’ otherwise member(v, L) - returns ‘true’ if ‘v’ is a member of list L; ‘false’ otherwise some(p, L) - returns ‘true’ if predicate ‘p’ is true for at least one member of list L; ‘false’ otherwise every(p, L) - returns ‘true’ if predicate ‘p’ is true for all members of list L; ‘false’ otherwise
Next is a set of functions operating on list(s) to create and return new lists: cons(v, L) - returns a list with ‘v’, followed by members of L endcons(v, L) - returns a list with members of L followed by ‘v’ rest(L, n) - returns a list with members of L, except the first ‘n’ members (if ‘n’ is non-negative), otherwise except the last ‘-n’ members. ‘n’ is optional, in which case, it is taken as 1 join(L1, L2) - returns a list with members of L1 and L2 interspersed delete(v, L, n) - returns a list like L but with the first ‘n’ occurrences of ‘v’ deleted from it. ‘n’ is optional, in which case all occurrences of ‘v’ are deleted append(L1, ..., Ln) - returns a list with members of L1, ..., Ln, one after the other unique(L) - returns a list obtained by removing the duplicate members in the list L reverse(L) - returns a list with members of the list L in reverse order
Note that the list L is still not modified. For that matter, , even L1, L2, L3 are not modified. In fact, that is what is meant when we state that all these functions recreate new modified lists, rather than modify the existing ones.
Here is a set of functions extracting the various members of a list. first(L), second(L), third(L), fourth(L), fifth(L), sixth(L), seventh(L), eight(L), ninth(L), and tenth(L), respectively return the first, second, ... member of the list L. last(L) returns the last
member of the list L.
Again, note that the list L is still not modified. However, we may need to modify the existing lists, and none of the above functions will do that. It could be achieved by assigning the return values of the various list recreation functions back to the original list. However, there are a few functions, which do modify the list right away.
The following are the two list manipulating functions provided by Maxima: push(v, L) - inserts ‘v’ at the beginning of the list L pop(L) - removes and returns the first element from list L L must be a symbol bound to a list, not the list itself, in both the above functions, for them to modify it. Also, these functionalities are not available by default, so we need to load the basic Maxima file. Check out the demonstration below.
We may display L after doing these operations, or even check the length of L to verify the actual modification of L. In case we need to preserve a copy of the list, the function copylist() can be used.
Advanced list operations
And finally, here is a bonus of two sophisticated list operations: sublist_indices(L, p) - returns the list indices for the members of the list L, for which predicate ‘p’ is ‘true’. assoc(k, L, d) - L must have all its members in the form of x op y, where op is some binary operator. Then, assoc() searches for ‘k’ in the left operand of the members of L. If found, it returns the corresponding right operand, otherwise it returns‘d’; or it returns false, if ‘d’ is missing. Check out the demonstration below for both the above operations