1

I thought the purpose of a block in Maxima was to protect global variables from being accidentally reassigned; however, I am not totally clear on the roles / rules for the different parts of a block.

For example, if one defines the function:

function():=
    block(
    [
     a:1,
     b:2,
     c:a,
     d:b
    ],
     [c,d]);
      );

executing this function yields function(); >> [ a, b ]. Initially, I expected [ 1 , 2 ]; however, after consulting the documentation, I saw that it says:

Note that the assignments in the block variables, are done in parallel.

Which I take to mean that, in the above, c:a and d:b are being assigned to the as-yet-undeclared variables a and b.

As such, if I wanted to have variables that depend on other variables, within a block, would the correct way of doing this be:

function():= block([ a:1, b:2 ], block([ c:a, d:b], [ c, d]) );

It seems to give the correct result, but this seemed a bit circuitous to me, so I couldn't help but wonder if there were a more direct approach? It's also not clear to me when it is important to declare variables in the [ ] section (do the parts of the block have a name?). Any insight would be greatly appreciated.

Rax Adaam
  • 790
  • 3
  • 11
  • 1
    The way you showed, `block([a:1, b:2], block([c:a, d:b], ...))` is a valid, workable way to organize the code. Equivalently you can write `block([a: 1, b: 2, c, d], c: a, d: b, ...)`. The main point is just that any variables for which you want local values need to be named in the list of variables for a block, and in the right-hand side for an assignment in `block([a: something], ...)`, that `something` takes its values from outside the block. You don't have to declare variables as local variables; if not declared local, then any assignments are global and persist after the end of the block. – Robert Dodier Nov 24 '20 at 20:16

0 Answers0