I want the function to take in string, then assign a value to its corresponding symbol. An implementation using ToExpression is
mySet[str_String, val_] :=
ToExpression["Set[" <> str <> "," <> ToString[val] <> "]"]
How can I do it without ToExpression? I wonder how this can be done with purely evaluation control.
I have read the following posts: Generating assignments and transforming code inside held forms when generating code
How do I evaluate only one step of an expression?
I think the main problem is to evaluate Symbol["x"] partially to x(it can have OwnValues), which can't be achieved with first post's code; and second post's code is indeed returning a HoldForm expression, leaving me generally the same problem.
ToExpression– vapor Jun 02 '16 at 07:33Symbol[]can't handle symbols with values:x = 5; Symbol["x"]– Kuba Jun 02 '16 at 07:36OwnValuesof that symbol – vapor Jun 02 '16 at 07:38Block– vapor Jun 02 '16 at 07:41ToExpressionduring assignment you have to make MMA understand"x"isxwhich means you have to convert it to an expression (held or not) at some point, and whether you will use something different fromToExpressiondoesn't matter, you will effectively do this. E.g. you can export "x" as a text file and import/get as expression, but deeper this will still be ToExpression. – Kuba Jun 02 '16 at 07:47ToExpression. I will think about it. – vapor Jun 02 '16 at 07:49ToExpression? – Kuba Jun 02 '16 at 07:50ToExpressioncode like what I wrote in the question, though it is easier to understand/write and it works. I believe there must be some other reason than it's ugly(but I don't know the reason, so I simply avoided the use ofToExpressionas I can). – vapor Jun 02 '16 at 07:54ToExpressionbut a working with Strings what is not popular. Not only it's ugly but"x"misses current$Contextfor example. – Kuba Jun 02 '16 at 08:02my[a_,b_]:=ToExpression[TemplateApply["Set[``,``]",{a,b}]].Of course,you don't likeToExpression. :) – yode Jun 15 '16 at 05:44