HP-50g Local variable and Global variable confusion
04-07-2017, 09:44 AM
Post: #1
 rui.c.sousa.99 Junior Member Posts: 4 Joined: Apr 2017
HP-50g Local variable and Global variable confusion
I have a question that happen in HP-50, and I think also on the HP-48
Local variables (LAMs) and global variables (ID) appear identically on the stack, and yet they are differente entities
if you define an expression and use a variable that exist as a local variable, that expresison will have a LAM in it.
if the local variable don't exist, it will have an ID in it.

For example running the following program

<<
'2*X' -- Simple expression
DUP
->S2 -- Decompile it
5 -> X << -- local variable with a dummy value
'2*X' -- Same expression, but a local variable exists
DUP
->S2 -- Decompile it
>>
>>

will produce this on the stack
'2*X'
"SYMBOL %2 ID X x* ; "
'2*X'
"SYMBOL %2 LAM X x* ; "

They appear iddentically, but internally are different

This poses-me a question. How do we store algebraics and then use then in a local variable environment. For example, the following won't work:

<<
'2*X' 'F' STO -- Store expression in variable F
5 -> X << -- Define local variable X
-- try different alternative ways to Recall F and evaluate it using X =5,
-- none work, because ID X is different from LAM X
F EVAL
'F' RCL EVAL
F ->NUM
-- This one work, but involves conversion to string, with also have side effects
F ->STR STR-> EVAL

>>
>>

This also prone to cause bugs. If in the above program there was a global variable X, for example, executing previously
10 'X' STO
and then run the above program, all the functions would return 20 (and the last one return 10, becauses the string conversion re-parses the algebraic and uses the local variable)

How to force algebraics to use local variables?
I also tried the | operator:
'2*X|X=X' without success

I know I can use the | operator, but I have to know what variables exist in the expression, and it is so complicated and slow:
For example
'2*X' 'F' STO --- algebraic with global symbol
{X} 'V' STO --- variable, in a list, X is also a global symbol
then in the local environment (X exist as a local variable):
F --- RCL F : '2*X'
V ---- RCL V : {X}
X ---- RCL X (local variable value) : for example 5
+ --- Add to the list : {X 5 }
| --- Replace X : '2*5'
And repeat to to every variable

Also, another question, the VARS command shows the global vars, how can we know the local vars that exist

Thanks
 « Next Oldest | Next Newest »

 Messages In This Thread HP-50g Local variable and Global variable confusion - rui.c.sousa.99 - 04-07-2017 09:44 AM RE: HP-50g Local variable and Global variable confusion - Han - 04-07-2017, 05:28 PM RE: HP-50g Local variable and Global variable confusion - TravisE - 04-08-2017, 09:28 PM RE: HP-50g Local variable and Global variable confusion - rui.c.sousa.99 - 04-10-2017, 10:30 AM

User(s) browsing this thread: 1 Guest(s)