Using "get" inside a function




On Sun, 6 Mar 2011, Bruce Linnell wrote:

Bruce,
The problem you are struggling with is this: when does the
argument to a function get evaluated? Before the function
looks at it:

(%i1) g(x) := block([], get(x,'Trank));
(%o1)                  g(x) := block([], get(x, 'Trank))
(%i2) put(X,true,'Trank);
(%o2)                                true
(%i3) g(x);
(%o3)                                false
(%i4) g(X);
(%o4)                                true

That looked good, now assign a value to X:

(%i5) X:1;
(%o5)                                  1
(%i6) g(X);

get: argument must be a symbol or a string; found: 1
#0: g(x=1)
 -- an error. To debug this try: debugmode(true);
 (%i7) g('X);
 (%o7)                                true


In %i6, X evaluates to 1 before g sees it, hence the
error. In %i7, 'X evaluates to X, which is a symbol.
%i4 is similar, because an unbound symbol evaluates
to itself.

If you want to stuff stuff into a symbol, I suggest
using hashhes:

(%i8) H[Trank] := true ;

(%o8) H[Trank]:=true
(%i9) H[metric] := matrix([2,1],[1,2]);

(%o9) H[metric]:=matrix([2,1],[1,2])
(%i10) H[Trank];

(%o10) true
(%i11) H[metric];

(%o11) matrix([2,1],[1,2])

Leo

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.