define nwt(a, var)=
prgm
disp a|#var=2
endprgm
Indirection is a programming ability that has been borrowed from the
likes of C and C++. What it does is allow you to have one variable
point, or refer, to a different variable. It's a very confusing
ability, but quite nice once you know how to use it. Why is this
useful, you may ask. Because sometimes we don't know which variable
we want to look at, and this saves code.
On the Nspire, how indirection (#) works is you create a variable that
contains a string. This string should be the name of an existing
variable. When you use indirection on the string, it refers you to
the variable whose name it contains. For example, define the following:
x:=2
var:="x"
then "disp #var" would display 2, or in other words, what's stored in
the variable x. #var says look at the name that var is holding. Look
at that variable.
It's like saying you're at a party with Alan and Bob and you ask Bob
for his opinion on something. And Bob's response is "ask Alan."
Here, Bob is var and Alan is x.
So a program like the following:
define indirection(var)=
prgm
disp #var
endprgm
Would display the contents of whatever variable name you pass as the argument.
indirection("x") would display the contents of the variable called x.
indirection("hello") would display the contents of the variable called hello.
indirection("list") would display the contents of the variable called list.
That's assuming these variables exist. I haven't tried it with an
undefined variable, but I assume it returns an error.
Where you might use this in a program is if you want the user to tell
you which variable to use (like in the first example), or if you have
several variables and you want a numerical result to determine which
one you use:
local ind
if num=2 then
ind:="x1"
else if num=3 then
ind:="g5"
else
ind:="default"
endif
#ind blah blah blah...
So if we look back at my original program:
define nwt(a, var)=
prgm
disp a|#var=2
endprgm
what the user provides is an algebraic expression (like x^2 or 3n-5,
etc) and a string which contains the variable in the expression you
want to replace ("x" or "n", etc).
Suppose we look at José's example of a = x^2 and var = "x".
The program then takes the line "disp a|#var=2" and goes "wait a
minute, var is telling me to look somewhere else." the content of var
is "x", so the program goes "ah ha! It should read 'disp a|x=2',
using x like a variable." Then it replaces a with its contents, so we
get "disp x^2|x=2" which is, "display x^2 with x=2." And it will
display "4".
Better? Any questions?
Hope this helps.
--Eric
--- Original Message ---
> Dear Eric,
>
> Is it okay if you can expound on how your redefined program works? I’m
> confused about the “#” sign as well as adding a second variable. When
> you run the program, are you defining the variables as strings?
>
> Sincerely,
> -TJ
>
>
> thisIsAString must contain a string, and in all cases, the string must
> contain the name of an existing variable.
Doesn't need to be existing, but must be valid. That is, even if
variable x isn't defined, #"x" is a valid indirection string; on the
other hand, #"1+1", #"2a" or #"-x" are not valid.
Valid variable names have up to 16 characters, the first character
must be a letter (uppercase, lowercase, greek letter, international
symbol like á, ã, etc.); other characters can also be numbers or
underscore.
Nelson