Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15423

Re: DelayedRule assembly

From Alex Krasnov <akrasnov@cory.eecs.berkeley.edu>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: DelayedRule assembly
Date 2013-09-12 06:23 +0000
Message-ID <l0rmlf$59t$1@smc.vnet.net> (permalink)
References <20130909040609.9789D6A14@smc.vnet.net> <20130910073508.938986A4E@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


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
>
>
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: DelayedRule assembly Alex Krasnov <akrasnov@cory.eecs.berkeley.edu> - 2013-09-12 06:23 +0000

csiph-web