ClearAll@foo;
foo[tag_] := (
tag /: tag[i_] := tag[[i]]
);
a = {7, 8, 9};
foo[a];
a[2]
I wanted the above to output 8, but encountered an error:
TagSetDelayed::sym: "Argument {7,8,9} at position 1 is expected to be a symbol. "
I think I understand that. It's currently a List, not a Symbol. So, I tried adding
SetAttributes[foo, HoldFirst];
after which I encountered another error:
TagSetDelayed::tagnf: "Tag a not found in {7,8,9}[i$_]."
Okay, so tag evaluated, I believe because it was in the form _[___]. So next, I added Unevaluated:
foo[tag_] := (
tag /: Unevaluated[tag][i_] := tag[[i]]
);
But this resulted in
TagSetDelayed::tagpos: "Tag a in Unevaluated[a][i$_] is too deep for an assigned rule to be found."
This, I don't understand...
a[2] = 42to makea{7, 42, 9}. I'm essentially trying to write a function that implements @ssch's answer here: http://mathematica.stackexchange.com/questions/34850/is-there-anything-like-a-c-pointer-or-returning-a-reference-in-mathematica. I must retain the use of UpValues then, correct? (Perhaps I simplified the example too far.) – Andrew Cheong Jan 25 '14 at 18:13ain{a[x_], y_} ^:= x^y– Mr.Wizard Jan 26 '14 at 02:36