Re: DelayedRule assembly
- To: mathgroup at smc.vnet.net
- Subject: [mg131614] Re: DelayedRule assembly
- From: Alex Krasnov <akrasnov at cory.eecs.berkeley.edu>
- Date: Thu, 12 Sep 2013 02:24:05 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
- References: <20130909040609.9789D6A14@smc.vnet.net> <20130910073508.938986A4E@smc.vnet.net>
One can use the HoldRest attribute of RuleDelayed or the Hold wrapper and
insert the evaluated components using one of several constructs. Here are
four possible solutions:
In: ToRuleDelayed[c2_, c3_, c4_] := c1 :> (c2 = c3; c4); ToRuleDelayed[c2, c3, c4]
Out: f[y_] :> ({x, z} = g[]; h[x, y, z])
In: With[{c2 = c2, c3 = c3, c4 = c4}, c1 :> (c2 = c3; c4)]
Out: f[y_] :> ({x, z} = g[]; h[x, y, z])
In: c1 :> (c2 = c3; c4) /. Thread[Thread[HoldPattern[{c2, c3, c4}]] -> {c2, c3, c4}]
Out: f[y_] :> ({x, z} = g[]; h[x, y, z])
In: ReleaseHold[c1 :> Evaluate[Hold[c2 = c3; c4] /. Thread[Thread[HoldPattern[{c2, c3, c4}]] -> {c2, c3, c4}]]]
Out: f[y_] :> ({x, z} = g[]; h[x, y, z])
See the documentation for these constructs.
Alex
On Tue, 10 Sep 2013, Dave Snead wrote:
> Hi,
>
> I'm trying to assemble a delayed rule from components.
>
> The rule I want is:
>
> In[1]:= rule0 = f[y_] :> ({x, z} = g[]; h[x, y, z])
> Out[1]= f[y_] :> ({x, z} = g[]; h[x, y, z])
>
> The components I have are:
> In[2]:= c1 = f[y_];
> c2 = {x, z};
> c3 = g[];
> c4 = h[x, y, z];
>
> Some attempts that do not work are:
>
> In[6]:= rule1 = RuleDelayed[c1, (c2 = c3; c4)]
> Out[6]= f[y_] :> (c2 = c3; c4)
>
> In[7]:= rule2 = RuleDelayed[c1, Evaluate@(c2 = c3; c4)]
> Out[7]= f[y_] :> h[x, y, z]
>
> How can I write a RuleDelayed expression using c1,c2,c3,c4
> which returns an expression identical to rule0 above?
>
> Thanks in advance,
> Dave Snead
>
>
>
- References:
- "Nice" complex form
- From: sam.takoy@yahoo.com
- DelayedRule assembly
- From: "Dave Snead" <dsnead6@charter.net>
- "Nice" complex form