# How to save expression with its name

I want to save expressions as well as their names in a file.

  func[i_] := i;
Do[func[i] >>> out.m,{i,1,3}];


The output is

   cat out.m

1
2
3


However the desired output is

   cat out.m

func[1] = 1;
func[2] = 2;
func[3] = 3;


Save does not save here.

• What about DumpSave? – CA Trevillian Oct 13 '19 at 21:51
• I do not know, but with that also I am unable to produce the expected output. – Boogeyman Oct 14 '19 at 9:21
• What do you want to export, the function names alone, or the definitions? The syntax you have there would result in the out.m being overwritten each time. I think you’re looking for something like OpenWrite see here for some other examples. – CA Trevillian Oct 14 '19 at 9:31
• With DumpSave you should be able to use the third option down in the templated examples, for this question. – CA Trevillian Oct 14 '19 at 9:34
• The overwriting is a different thing. My issue here is to write the function name as well as its definition/expression. Save would have worked if I just define just say fun1=func[1], then it would have written inside out.m as fun1=1. But I want func[1]=1 in the output. And in a generic case this func definition may not be this simple. – Boogeyman Oct 14 '19 at 9:37

You can use Save in this way:

func[i_] := func[i] = i;
Do[func[i], {i, 1, 3}];
FullDefinition@func
(*
func[1] = 1

func[2] = 2

func[3] = 3

func[i_] := func[i] = i
*)

Save["out.m",func];
ClearAll[func]; Get["out.m"]; FullDefinition@func
(*
func[1] = 1

func[2] = 2

func[3] = 3

func[i_] := func[i] = i
*)


So, here, we show that memoization can be used as a method to record the defined expressions when one is going to save them externally.

Your suggested method of using Save was not exactly proper, which is why it would not work for you. Here, this plays out as expected. While the memoized func name cannot be avoided in this procedure, it seems to do as you would like.

Does this make sense?

func[i_] := i

Do[
"func["<>ToString@i<>"] = "<>ToString@func@i >>> "out.m"
, {i,1,3}
]


Edit 1: It's not clear to me what's your scope but, this does what you asked

"func["<>ToString@#<>"] = "<>ToString@func@# & /@ Range@3 //
Export["out.m", #~StringRiffle~"\n"<>"\n","Text"]&


Edit 2: Given that your scope is to create a file that contains only the DownValues of a symbol f, you do not need to define a substitution rule (:=) for f. So, let's just define the DownValues of f and export them in a text file

Remove@f

f@#~Set~# &/@ Range@3

ToString@Information[f]@"DownValues" //
Export["out.m",#,"Text"]&

• It produces "func[1]=1". Without running shell commands is it possible to remove " ? – Boogeyman Oct 13 '19 at 9:37
• @Boogeyman see Edit 1 – Fortsaint Oct 13 '19 at 9:45
• The scope is very clear if you want to create an output which you can directly translate from Mathematica to other languages eg. symbolic manipulation system like Form. :) – Boogeyman Oct 13 '19 at 9:49
• Why you define funcin advance? you could have just defined another function g[i_]:=1 and then append f[i] = g[i]. If you see @Fraccalo answer, it has all those Hold-related function solely because you had defined func in advance – Fortsaint Oct 13 '19 at 9:56
• Yes I can do that. But this is the whole point. You have to define just another function in order to save it in a nice form. My question is very simple. You had a function definition and you want to print it with its expression as well as its names. Now if you do define , its just extra definition which to me is not justified just for printing. This is why I wanted to ask if there is nicer way to handle. – Boogeyman Oct 13 '19 at 10:04
list = ToString[#] <> "=" <> ToString[ReleaseHold@#] <> ";" &@
HoldForm@func[#] & /@ Range[3]

(# >>> out.m) & /@ list