Printing symbol name and value in Mathematica

Deadly 提交于 2019-12-08 15:46:22

问题


I'd like to create a function My`Print[args__] that prints the names of the symbols that I pass it, along with their values. The problem is that before the symbols are passed to My`Print, they're evaluated. So My`Print never gets to see the symbol names.

One solution is to surround every argument that I pass to My`Print with Unevaluated[], but this seems messy. Is there a way of defining a MACRO such that when I type My`Print[args__], the Mathematica Kernel sees My`Print[Unevaluated /@ args__]?


回答1:


You need to set the attribute HoldAll on your function, with SetAttribute[my`print].

Here's a possible implementation:

Clear[my`print]
SetAttributes[my`print, HoldAll]
my`print[args__] := 
 Scan[
  Function[x, Print[Unevaluated[x], " = ", x], {HoldAll}], 
  Hold[args]
 ]

I used lowercase names to avoid conflicts with built-ins or functions from packages.

EDIT:

Just to make it explicit: I have two functions here. One will print the value of a single symbol, and is implemented as a Function inside. You can just use this on its own if it's sufficient. The other is the actual my`print function. Note that both need to have the HoldAll attribute.




回答2:


ClearAll[My`Print]
SetAttributes[My`Print, HoldAll]
My`Print[args___] := 
 Do[
   Print[
      Extract[Hold[args], i, HoldForm], "=", List[args][[i]]
   ], {i, Length[List[args]]}
 ]

ape = 20;
nut := 20 ape;
mouse = cat + nut;

My`Print[ape, nut, mouse]

(* ==>
ape=20
nut=400
mouse=400+cat
*)



回答3:


SetAttributes[MyPrint, HoldAll];
MyPrint[var_] := 
  Module[
    {varname = ToString[Hold[var]]},
    Print[StringTake[varname, {6, StringLength[varname] - 1}], 
                " = ", Evaluate[var]]
  ]



回答4:


Coming late to the party - one can use Listability to get a rather elegant (IMO) solution avoiding explicit loops or evaluation control constructs:

ClearAll[prn];
SetAttributes[prn, {HoldAll, Listable}];
prn[arg_] := Print[HoldForm[arg], " = ", arg];
prn[args___] := prn[{args}]

Stealing the test case from @Sjoerd,

In[21]:= prn[ape,nut,mouse]

During evaluation of In[21]:= ape = 20
During evaluation of In[21]:= nut = 400
During evaluation of In[21]:= mouse = 400+cat

Out[21]= {Null,Null,Null}



回答5:


Here is another variation of My`Print to add to the mix:

ClearAll[My`Print]
SetAttributes[My`Print, HoldAll]
My`Print[expr_] := Print[HoldForm[expr], " = ", expr]
My`Print[exprs___] := Scan[My`Print, Hold[exprs]]

... and another...

ClearAll[My`Print]
SetAttributes[My`Print, HoldAll]
My`Print[args___] :=
  Replace[
    Unevaluated @ CompoundExpression @ args
  , a_ :> Print[HoldForm[a], " = ", a]
  , {1}
  ]

Either way, the use is the same:

$x = 23;
f[x_] := 1 + x

My`Print[$x, $x + 1, f[1]]

(* prints:
   $x = 23
   $x+1 = 24
   f[1] = 2
*)



回答6:


In addition to the other answers consider the functions DownValues, OwnValues and UpValues:

In[1] := f[x_] := x^2

In[2] := f[x_, y_] := (x + y)^2

In[3] := DownValues[f]

Out[3] = {HoldPattern[f[x_]] :> x^2, HoldPattern[f[x_, y_]] :> (x + y)^2}

http://reference.wolfram.com/mathematica/ref/DownValues.html



来源:https://stackoverflow.com/questions/7985052/printing-symbol-name-and-value-in-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!