Search code examples
listmaxima

Maxima: cummulatively apply function to a list's atoms


is there a way to apply a function cumulatively to each atom of a list ? For instance :

wishedFunction("+",[1,2,4,7,3])

Would compute (((1+2)+4)+7)+3 and hence return 17

In other words I am looking for a maxima equivalent to the python reduce() function.

I have read the documentation for the map() and scanmap() functions, but they do not behave the same way…

Any suggestions ?


Solution

  • Actually what you are looking for is indeed one of the built-in reduce functions (lreduce, rreduce, xreduce, tree_reduce).

    (%i1) xreduce ("+", [1,2,4,7,3]);
    (%o1)                                 17
    

    Sorry, I misunderstood your question. I was thinking that you want a function which returns a list with the accumulating partial results. Obviously that's incorrect. Sorry for the confusion.