0

I was surprised to find out the names of arguments of a function definition are not "temporary name/formal name"

a=1;b={1,2};c="abc";
x;
f[lzy_]:=lzy+1
Names["Global`*"]

(* answer is {"a", "args", "b", "c", "dims", "f", "lzy", "x"} *)

this recovery makes me feel unsafe and confuses me —— why a symbol to the left of the = will be admitted to a Symbol. As far as my gut feeling goes, "right value" should not be allocated memory.

and i did more tests i found almost wherever you use a new symbolic name, that name will appear in the output of Names[]. (for example, i call f[ssssss], then ssssss will appear in Names[])

so how to understand is a question for me, and another question, will this phenomenon cause some problems in some cases especially for newbie? I know in simple case will not have problems(because delayed-equal :=) as following

lzy=777
f[1](*ans is 2 not 778*)
user64494
  • 26,149
  • 4
  • 27
  • 56
Aerterliusi
  • 353
  • 1
  • 5
  • 1
    Yes, whenever a symbol is encountered, it will be "remembered". Don't think of it as allocating memory, however, because symbols don't have types that require a specific amount of memory. Sure, the list of symbols itself requires memory, but each new symbol is trivial in that respect. – lericr Jan 17 '23 at 20:11
  • 1
    Although it appears in Names[], it does not have any "values" assigned, which you can see with ?lzy. Furthermore, evaluate just abcdefg, outside any function or definition or whatsoever. After the evaluation, it will appear in Names[], even though you did nothing with it. – Domen Jan 17 '23 at 20:13
  • 1
    I don't think there's much to say in terms of "how to understand". You seem to already understand. But as for causing problems, yes, there might be some situations to be careful of. You've already identified the Set versus SetDelayed distinction, which is probably the most likely place to cause "problems". The only example of yours that seems to cause you concern is the pattern one. But there's really no need to worry, because the whole pattern matching domain handles this gracefully. I guess what I'm really saying is "don't worry about it too much". – lericr Jan 17 '23 at 20:14
  • 1
    Possible duplicate: 8295, closely related: 43381, what do you think? – Kuba Jan 17 '23 at 21:20

0 Answers0