Search code examples
listrecursionmaximawxmaxima

(wx)Maxima: does `makelist` work in parallel or serially?


I am interested in creating a list whose terms are defined recursively (i.e. term[i] is a function of term[i-1], not a function of i-1).

I figured that if makelist works serially, then calling a previous term should not be an issue; however, the documentation does not explicitly address how the terms are generated.

An initial attempt (NB: even though the output here could be achieved using a function of the index, the point was to create a simple example that tested the ability of calling previous terms, within makelist):

test:
makelist(block([ ], 
    
    /* list item set to 1 for first term of the list, and to previous list item, thereafter */  
    if i = 1 
    then addend 
    else test[i-1]
    )

, i, 5);

but this returns [1, test[1], test[2], test[3], test[4] ] so it doesn't seem to actually access the values of test.

I tried various experiments, including initializing test; including a call to test in the block, i.e. block([ test:test ], ... , and a few others, but haven't been able to get the desired result.


Solution

  • This is a great question. The result of makelist isn't associated with the variable to which it is assigned (test in the example above) so while makelist is constructing its result, there is no way to know about test[i - 1] being the previous item. I don't think there is any way to access the previous item from within makelist (e.g. by some Lisp programming or something).

    About makelist being serial or parallel, I'm 99% sure that elements are constructed in order. E.g. makelist(print(i), i, 1, 10) prints the numbers from 1 to 10.

    If a term depends on the previous one, one way to do it is to create an empty list (e.g. x: makelist(0, n)) and then loop over the elements (e.g. x[1]: FOO; for i:2 thru n do x[i]: x[i - 1] + something).