Ramzan Altair
Ramzan Altair

Reputation: 97

Declare a dynamic constant in Maxima

I need to declare a variable as constant, the variable is generated while the program is running, I tried this way:

foo(var) := declare(''var, constant)$

foo(x)$

facts();

But that doesn't work and I get:

[kind(var, constant)]

everytime.

instead:

[kind(x, constant)]

When I write code without a function, everything works fine:

var: x$

declare(''var, constant)$

facts();

I get:

[kind(x, constant)]

Does anyone know how to do this dynamically via a function?

Upvotes: 1

Views: 250

Answers (1)

Robert Dodier
Robert Dodier

Reputation: 17577

The conventional way to ensure that arguments are evaluated, even for functions which otherwise quote their arguments, is to apply the function to the arguments. E.g.:

apply (declare, [var, 'constant]);

Or, in a function:

foo(var) := apply (declare, [var, 'constant]);

apply evaluates its arguments, so the arguments are evaluated by the time the function sees them.

The quote-quote ''var doesn't have the expected effect in a function because quote-quote is applied only at the time the expression is parsed. So any later assignment to var has no effect.

I recommend against eval_string. There is almost always a better way to do anything than string processing; in this case that better way is apply.

Upvotes: 1

Related Questions