Re: HoldForm in defined function doesn't seem to work
- To: mathgroup at smc.vnet.net
- Subject: [mg99322] Re: HoldForm in defined function doesn't seem to work
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Sat, 2 May 2009 06:03:02 -0400 (EDT)
- References: <gt9aki$q22$1@smc.vnet.net> <gtbuo8$2r4$1@smc.vnet.net> <gtef26$12i$1@smc.vnet.net>
joel.aufgang at gmail.com wrote: > Wow! What a great number of responses. Thanks to all of you who > helped me out with this. > > The SetAttributes[f,HoldAll] solution was just what I was looking > for. > > > > Unfortunately, I'm still stuck on something.... Namely, using this > with the Map function. > > I'm trying to display the way the notebook front end rewrites the box > model representation of an expression as it is edited. I'd like to > display a sequence of expressions and their ToBoxes representations > using a SlideView. So I'm doing something like this: > > ========================== > ============== > > ShowBoxForms[expr_] := TableForm[ {HoldForm[expr], ToBoxes[HoldForm > [expr]][[1]], TreeForm[ToBoxes[HoldForm[expr]][[1]]]}]; > EditingSequence[expr_] := SlideView[Map[ShowBoxForms, expr], > AppearanceElements -> All]; > SetAttributes[ShowBoxForms, HoldAll]; > SetAttributes[EditingSequence, HoldAll]; > {a, a b, a b c, a b c/d} // EditingSequence > {a, ab, a b, a b c, a b c^2, a b + c^2, a (b + c)^2, a (b + c)^ > (2/3)} // EditingSequence > {12, 12 34, 12 34/56} // EditingSequence > > ========================== > ============== > > Unfortunately, even though I am setting the HoldAll attributes on my > functions, the hold is being removed when the Map function is being > called. You can see this by looking at the third editing sequence in > my example, where the numeric expressions are being evaluated before > being converted to boxes. "12 34/56 // ShowBoxForms" Works > perfectly, but I need it to display the same way when being called > through EditingSequence > > I've tried setting HoldAll on Map, but that messes everything up even > more. Doing this holds the entire list from the argument, which > causes Map to be applied to the entire list and not each element. I > need to be able to use Map while performing a HoldForm on each element > of the input argument list. > > Any help with this would be greatly appreciated. And Thanks again for > the previous help as well. > In my original reply, I suggested that you read up on Evaluated and Unevaluated - now you can see why! SetAttributes[ShowBoxForms,HoldAll]; ShowBoxForms[expr_]:=TableForm[{HoldForm[expr],TreeForm[HoldForm[expr][[1]]]}]; SetAttributes[EditingSequence,HoldAll]; EditingSequence[expr_]:=SlideView[Map[ShowBoxForms,Unevaluated[expr]],AppearanceElements->All]; {a,a b,a b c,a b c/d}//EditingSequence {a,ab,a b,a b c,a b c^2,a b+c^2,a (b+c)^2,a (b+c)^(2/3)}//EditingSequence {12,12 34,12 34/56}//EditingSequence I also removed some spurious calls to ToBoxes, which I assume you did not want - put them back if you really wanted to study the box expressions. BTW, Never try setting attributes of built-in functions - you will disturb other code - including parts of Mathematica itself. David Bailey http://www.dbaileyconsultancy.co.uk David Bailey http://www.dbaileyconsultancy.co.uk