"Unholding" variables

Messages in english

Modérateur : xcasadmin

niccolo
Messages : 26
Inscription : dim. mars 02, 2014 2:13 pm

"Unholding" variables

Message par niccolo » mar. mars 28, 2023 8:15 pm

Let's say I store an expression in a variable:

exprsn := x^2 + 3

Now, I want to turn it into a function, so I do:

f(x) := exprsn

But then, if I type "f(7)", I get "exprsn" instead of "7^2 + 3". How to fix this?

I did "f(x) := eval(exprsn)", and it (seems to?) work.

My questions:
1. Is this the correct way to solve the problem?
2. Why is there a problem in the first place? I.e, why isn't the 'exprsn' varible resolved in "f(x) := exprsn"?
2. If I type "f" it says "x -> eval(exprsn)", and I would have prefered it to say "x -> x^2 + 3".

parisse
Messages : 5734
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: "Unholding" variables

Message par parisse » mer. mars 29, 2023 4:16 pm

You should use unapply to do that.
exprsn := x^2 + 3;
f:=unapply(exprsn,x)

Répondre