Re: problem writing debugging utility function
- To: mathgroup at smc.vnet.net
- Subject: [mg100308] Re: problem writing debugging utility function
- From: "dabrowsa at indiana.edu" <dabrowsa at indiana.edu>
- Date: Sun, 31 May 2009 06:37:36 -0400 (EDT)
- References: <gvq09k$mqv$1@smc.vnet.net>
Thanks a lot to Leonid, Szabolcs, and Ken for their solutions. I liked Leonid's the best, being both much simpler and more general than mine. SetAttributes[ShowIt, HoldAll]; ShowIt[code_] := Module[{y}, Print[ToString[Unevaluated[code]], " = ", y = code]; y]; My own solution was the hideous $PreRead=ReplaceAll[#,{{"dbgv","[",var_,"]"}:>{"Print","[",RowBox [{"\""<>var<>" = "<>"\"",",",var}],"]"}}]&; which makes dbgv work like a macro. The grotesqueness is because what $PreRead sees is a very raw input form, with expressions broken down into RowBox's. In a sense this is the most direct analog to Lisp's macros, but ... yechh. The Unevaluated[] function had slipped off my radar, probably because I never fully grokked the distinction between it and Hold[]. In fact I'm still a bit confused about it. At first I thought it might like the evaluation inhibitor ` in Lisp, but some examples disabused me of that. In[2]:= f@Unevaluated[5 + 6 + 7 + 8] Out[2]= f[Unevaluated[5 + 6 + 7 + 8]] In[11]:= ToString@Unevaluated[5 + 6 + 7 + 8] Out[11]= "5 + 6 + 7 + 8" I might have expected Out[11]= "Unevaluated[5 + 6 + 7 + 8]" In[13]:= sqr[x_] := x^2 In[14]:= sqr@Unevaluated[5 + 6 + 7 + 8] Out[14]= 676 I might have expected Out[14]= sqr[5 + 6 + 7 + 8] since sqr does not know what to do with the pattern _+_+_+_ or _+_ for that matter. Could anyone elucidate this for me?