0

I have a program im Maxima CAS:

kill(all);
remvalue(all);


GivePart(n):=(
    [Part, iMax],

    if (n>20) then iMax:10
      else iMax : 250,
    Part : makelist(i, i, 0, iMax)   )$

GiveList(iMax):=(
    [Part, PartList ],
    PartList:[],
    for i:1 thru iMax step 1 do (
        Part:  GivePart(i),
        PartList : cons(Part, PartList)
    ),
    PartList
)$

pp:GiveList(60)$
length(pp);

It creates a list pp.

The length of pp should be 60 but is 21.

Program has 2 functions and iMax which is

  • parameter to second function
  • local variable in the first function

Program runs without any error messeges.

I have checked the source code of Maxima CAS

grep -wnR "iMax" 

and iMax is not used in Maxima CAS code

I know how to solve the problem: change name of local variable in first function:

kill(all);
remvalue(all);
GivePart(n):=(
    [Part, i_Max],

    if (n>20) then i_Max:10
      else i_Max : 250,
    Part : makelist(i, i, 0, i_Max)  )$

GiveList(iMax):=(
    [Part, PartList ],
    PartList:[],
    for i:1 thru iMax step 1 do (
        Part:  GivePart(i),
        PartList : cons(Part, PartList)
    ),
    PartList
)$


pp:GiveList(60)$

length(pp);

Now the length of pp is 60 ( good).

What is the cause of the problem ?

1 Answer 1

2

The problem appears to be

GivePart(n):=(
    [Part, iMax],

which is not correct, it should be

GivePart(n):=block(
    [Part, iMax],

Outside of block, [Part, iMax] is not recognized as a list of local variables, and iMax has the value that was bound when GiveList was called (this is a consequence of Maxima's "dynamic scope" policy).

I see that GiveList also has a missing block which needs to be corrected.

7
  • What is the reason that iMax is equal to 21 ? In GivePart it has value 10 or 250.
    – Adam
    Apr 6, 2018 at 17:09
  • 1
    Within GivePart, iMax is assigned a value (10 or 250). But iMax is also the loop variable in GiveList (because of dynamic scope). Changing the loop variable causes the loop to terminate. If you change if (n>20) ... to if (n>29) ... you'll get 30 items in the output list. Apr 6, 2018 at 20:43
  • @ Robert Dodier. Is it possible that Maxima parser find such error and give a warning?
    – Adam
    Apr 6, 2018 at 21:41
  • Not sure which error you mean. (1) Maybe you mean ([foo, bar], ...) instead of block([foo, bar], ...). Since ([foo, bar], ...) is a valid (if not very useful) construct, I don't know how we would give an error about it. (2) Maybe you mean changing a loop variable in a function called from the loop. The right way to fix that is to implement lexical scope instead of dynamic scope. Lexical scope has been a topic for discussion many times over the years. Perhaps someday it will be implemented. Apr 6, 2018 at 23:10
  • 2
    I've pushed commit 58a5a40 which implements a parser warning for ([foo, bar], ...). I expect it will appear in the next released version (5.42); in the meantime it is in the Git version. Apr 16, 2018 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.