Lists: The Build­ing Blocks of Max­ima

This 20th ar­ti­cle in our se­ries on Math­e­mat­ics in Open Source show­cases the list ma­nip­u­la­tions in Max­ima, the pro­gram­ming lan­guage with an AL­GOL-like syn­tax but Lisp-like se­man­tics.

OpenSource For You - - FOR U & ME LET’S TRY -

Lists are the ba­sic build­ing blocks of Max­ima. The fun­da­men­tal rea­son is that Max­ima is im­ple­mented in Lisp, the build­ing blocks of which are also lists. To be­gin with, let us walk through the ways of cre­at­ing a list. The sim­plest method to get a list in Max­ima is to just de­fine it, us­ing []. So, [x, 5, 3, 2*y] is a list con­sist­ing of four mem­bers. How­ever, Max­ima pro­vides two pow­er­ful func­tions for au­to­mat­i­cally gen­er­at­ing lists: make­list() and cre­ate_list().

make­list() can take two forms. make­list (e, x, x0, xn) cre­ates and re­turns a list us­ing the ex­pres­sion ‘e’, eval­u­ated for ‘x’ us­ing the val­ues rang­ing from ‘x0’ to ‘xn’. make­list(e, x, L) cre­ates and re­turns a list us­ing the ex­pres­sion ‘e’, eval­u­ated for ‘x’ us­ing the mem­bers of the list L. Check out the ex­am­ple below for bet­ter clar­ity:

Note the in­ter­est­ing us­age of con­cat() to just con­cate­nate its ar­gu­ments. Note that make­list() is lim­ited by the vari­a­tion it can have, which to be spe­cific, is just one – ‘i’ in the first two ex­am­ples and ‘x’ in the last one. If we want more, the cre­ate_list() func­tion comes into play.

cre­ate_list(f, x1, L1, ..., xn, Ln) cre­ates and re­turns a list with mem­bers of the form ‘f’, eval­u­ated for the vari­ables x1, ..., xn us­ing the val­ues from the cor­re­spond­ing lists L1, ..., Ln. Here is just a glimpse of its power:

Note that ‘all pos­si­ble com­bi­na­tions’ are cre­ated us­ing the val­ues for the vari­ables ‘x’, ‘y’ and ‘z’.

Once we have cre­ated the lists, Max­ima pro­vides a host of func­tions to play around with them. Let’s take a look at these.

Test­ing the lists

The fol­low­ing set of func­tions demon­strates the var­i­ous checks on lists: atom(v) - re­turns ‘true’ if ‘v’ is an atomic el­e­ment; ‘false’ oth­er­wise listp(L) - re­turns ‘true’ if ‘L’ is a list; ‘false’ oth­er­wise mem­ber(v, L) - re­turns ‘true’ if ‘v’ is a mem­ber of list L; ‘false’ oth­er­wise some(p, L) - re­turns ‘true’ if pred­i­cate ‘p’ is true for at least one mem­ber of list L; ‘false’ oth­er­wise ev­ery(p, L) - re­turns ‘true’ if pred­i­cate ‘p’ is true for all mem­bers of list L; ‘false’ oth­er­wise

List recre­ations

Next is a set of func­tions op­er­at­ing on list(s) to cre­ate and re­turn new lists: cons(v, L) - re­turns a list with ‘v’, fol­lowed by mem­bers of L end­cons(v, L) - re­turns a list with mem­bers of L fol­lowed by ‘v’ rest(L, n) - re­turns a list with mem­bers of L, ex­cept the first ‘n’ mem­bers (if ‘n’ is non-neg­a­tive), oth­er­wise ex­cept the last ‘-n’ mem­bers. ‘n’ is op­tional, in which case, it is taken as 1 join(L1, L2) - re­turns a list with mem­bers of L1 and L2 in­ter­spersed delete(v, L, n) - re­turns a list like L but with the first ‘n’ oc­cur­rences of ‘v’ deleted from it. ‘n’ is op­tional, in which case all oc­cur­rences of ‘v’ are deleted ap­pend(L1, ..., Ln) - re­turns a list with mem­bers of L1, ..., Ln, one af­ter the other unique(L) - re­turns a list ob­tained by re­mov­ing the du­pli­cate mem­bers in the list L re­verse(L) - re­turns a list with mem­bers of the list L in re­verse order

Note that the list L is still not mod­i­fied. For that mat­ter, , even L1, L2, L3 are not mod­i­fied. In fact, that is what is meant when we state that all these func­tions recre­ate new mod­i­fied lists, rather than mod­ify the ex­ist­ing ones.

List ex­trac­tions

Here is a set of func­tions ex­tract­ing the var­i­ous mem­bers of a list. first(L), sec­ond(L), third(L), fourth(L), fifth(L), sixth(L), seventh(L), eight(L), ninth(L), and tenth(L), re­spec­tively re­turn the first, sec­ond, ... mem­ber of the list L. last(L) re­turns the last

mem­ber of the list L.

Again, note that the list L is still not mod­i­fied. How­ever, we may need to mod­ify the ex­ist­ing lists, and none of the above func­tions will do that. It could be achieved by as­sign­ing the re­turn val­ues of the var­i­ous list recre­ation func­tions back to the orig­i­nal list. How­ever, there are a few func­tions, which do mod­ify the list right away.

List ma­nip­u­la­tions

The fol­low­ing are the two list ma­nip­u­lat­ing func­tions pro­vided by Max­ima: push(v, L) - in­serts ‘v’ at the begin­ning of the list L pop(L) - re­moves and re­turns the first el­e­ment from list L L must be a sym­bol bound to a list, not the list it­self, in both the above func­tions, for them to mod­ify it. Also, these func­tion­al­i­ties are not avail­able by de­fault, so we need to load the ba­sic Max­ima file. Check out the demon­stra­tion below.

We may dis­play L af­ter do­ing these op­er­a­tions, or even check the length of L to ver­ify the ac­tual mod­i­fi­ca­tion of L. In case we need to pre­serve a copy of the list, the func­tion copy­list() can be used.

Ad­vanced list op­er­a­tions

And fi­nally, here is a bonus of two so­phis­ti­cated list op­er­a­tions: sub­list_indices(L, p) - re­turns the list in­dices for the mem­bers of the list L, for which pred­i­cate ‘p’ is ‘true’. as­soc(k, L, d) - L must have all its mem­bers in the form of x op y, where op is some bi­nary op­er­a­tor. Then, as­soc() searches for ‘k’ in the left op­er­and of the mem­bers of L. If found, it re­turns the cor­re­spond­ing right op­er­and, oth­er­wise it re­turns‘d’; or it re­turns false, if ‘d’ is miss­ing. Check out the demon­stra­tion below for both the above op­er­a­tions

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.