MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Releasing several Holds simultaneously

  • To: mathgroup at
  • Subject: [mg82271] Re: Releasing several Holds simultaneously
  • From: "Szabolcs HorvÃt" <szhorvat at>
  • Date: Wed, 17 Oct 2007 03:46:17 -0400 (EDT)
  • References: <ff1po5$90n$> <>

On 10/16/07, Andrew Moylan <andrew.j.moylan at> wrote:
> Hi Szabolcs,
> My general expression is of the form Hold[SetDelayed[a, b]] where both a and
> b may contain one or more Holds. The expression is the result of using
> Mathematica to generate Mathematica code.
> I start with Hold[SetDelayed[a, b]] and then insert (via /. and
> placeholders) code into b. The reason b contains Holds is that some of the
> pieces of code that I insert into it are wrapped in Hold because they can't
> be evaluated:
> pieceofcode = Hold[z[[1]]];
> So I could generalise my question like this:
> Suppose I have Hold[a := b] and I have pieceofcode = Hold[z[[1]]]. How can I
> arrange to evaluate a := z[[1]]?
> Here's my present solution:
> SetAttributes[SuperHold, HoldAll];
> ReleaseSuperHold[expr_] := expr /. SuperHold[x_] :> x;
> pieceofcode = Hold[z[[1]]];
> SuperHold[a := b] /. b -> pieceofcode //
>   ReleaseHold // ReleaseSuperHold;
> Andrew

Here's an alternative:

In[1]:= z={1,2,3}
Out[1]= {1,2,3}

In[2]:= pieceofcode:=Hold[z[[1]]]

In[3]:= toBeAssginedTo=a
Out[3]= a

In[4]:= Function[{val},Evaluate[toBeAssginedTo]:=val,{HoldAll}]@@pieceofcode

In[5]:= ?a

You could also "Block out" (Block[{z}, ...]) 'z' and other variables
instead of Holding them, but this may not work for all types of code;
or wrap expressions containing 'z' in Unevaluated while you pass them
on.  It all depends one exactly what kind of Mathematica code are you
building (z[[1]] was of course a simplified example):

In[1]:= SetAttributes[fun,HoldRest]

In[2]:= fun[a_,z_]:=a:=Unevaluated[z[[1]]]

In[3]:= z={1,2,3}
Out[3]= {1,2,3}

In[4]:= fun[a,z]

In[5]:= ?a

  • Prev by Date: Re: Efficient creation of regression design matrix
  • Next by Date: Re: Recursion limit: a good idea to ignore?
  • Previous by thread: Re: Releasing several Holds simultaneously
  • Next by thread: RE: Releasing several Holds simultaneously