maxima

idiomatic iteration over range and set in maxima


beginner's question. what is the idiomatic expression in maxima to accomplish the equivalent R expression

for (i in c(1:5,29,155)) message(i)
1,2,3,4,5,29,155

Solution

  • If you want to call a function for each value in a list and you don't care about the return value (i.e., you're interested a side effect such as printing), you can say:

    for x in mylist do print(x);
    

    If you want the return value of, say, f, for each element,

    makelist (f(x), x, mylist);
    

    Maxima doesn't have a built-in range operator, so something like 1:5 is the slightly more verbose makelist(i, i, 1, 5).

    Also, while in R c(c(c(...), ...), ...) effectively collapses any nested vectors, Maxima lets nested lists stand (flatten flattens nested lists). In the example given, try append(mypartiallist, [29, 155]) where mypartiallist is the stuff from the previous makelist.