将“With”与“Rules”列表一起使用 - 但不影响“With”的正常行为
规则
列表
rules = {a -> b, c -> d};
假设我有一个在整个笔记本中使用的 。然后,在某一时刻,希望规则在表达式中发生任何其他计算之前应用是有意义的。通常,如果您想要这样的东西,您会使用
In[2]:= With[{a=b,c=d}, expr[a,b,c,d]]
Out[2]= expr[b, b, d, d]
How can I take rules
并将其插入到With
的第一个参数中?
编辑
两者一些解决方案无法满足我正在寻找的所有要求 - 但我应该再强调一点。见上面粗体部分。
例如,让我们看看
rules = {a -> {1, 2}, c -> 1};
如果我在 With
中使用这些值,我会得到
In[10]:= With[{a={1,2},c=1}, Head/@{a,c}]
Out[10]= {List,Integer}
一些版本的 WithRules
产量
In[11]:= WithRules[rules, Head/@{a,c}]
Out[11]= {Symbol, Symbol}
(实际上,我没有注意到Andrew 的答案具有属性 HoldRest
- 所以它的工作原理就像我想要的那样。)
Say I have a list of Rules
rules = {a -> b, c -> d};
which I use throughout a notebook. Then, at one point, it makes sense to want the rules to apply before any other evaluations take place in an expression. Normally if you want something like this you would use
In[2]:= With[{a=b,c=d}, expr[a,b,c,d]]
Out[2]= expr[b, b, d, d]
How can I take rules
and insert it into the first argument of With
?
Edit
BothSome solutions fail do all that I was looking for - but I should have emphasised this point a little more. See the bold part above.
For example, let's look at
rules = {a -> {1, 2}, c -> 1};
If I use these vaules in With
, I get
In[10]:= With[{a={1,2},c=1}, Head/@{a,c}]
Out[10]= {List,Integer}
Some versions of WithRules
yield
In[11]:= WithRules[rules, Head/@{a,c}]
Out[11]= {Symbol, Symbol}
(Actually, I didn't notice that Andrew's answer had the Attribute HoldRest
- so it works just like I wanted.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要使用 Hold 来构建您的 With 语句。这是一种方法;可能有一个更简单的方法:
测试一下:(
我使用了 Print,这样任何涉及我过早意外评估 expr 的错误都会变得明显。)
You want to use Hold to build up your With statement. Here is one way; there may be a simpler:
Test it out:
(I used Print so that any bug involving me accidentally evaluating expr too early would be made obvious.)
我已经使用以下形式的
WithRules
很长时间了。与 Andrew Moylan 发布的相比,它是按顺序绑定的,因此您可以说WithRules[{a->b+1, b->2},expr]
并获取a
扩展为3
:这也作为 发布回答一个不相关的问题,正如那里所指出的,它已经在 usenet 上(至少)讨论过几次:
HTH
编辑:添加了
ReleaseHold,
Hold
对使expr
保持不计算状态,直到应用规则。I have been using the following form of
WithRules
for a long time. Compared to the one posted by Andrew Moylan, it binds sequentially so that you can say e.g.WithRules[{a->b+1, b->2},expr]
and geta
expanded to3
:This was also posted as an answer to an unrelated question, and as noted there, it has been discussed (at least) a couple of times on usenet:
HTH
EDIT: Added a
ReleaseHold
,Hold
pair to keepexpr
unevaluated until the rules have been applied.Andrew 的解决方案的一个问题是它将问题映射回 With,并且不接受下标变量。所以下面会生成消息。
鉴于
With
在其主体上执行语法替换,我们可以如下设置 WithRules:然后
编辑:解决 Leonid 提出的有效问题,以下版本是安全的:
然后
编辑2:即使WithRules3也不完全等同于Andrew的版本:
One problem with Andrew's solution is that it maps the problem back to With, and that does not accept subscripted variables. So the following generates messages.
Given that
With
performs syntactic replacement on its body, we can set WithRules alternatively as follows:Then
Edit: Addressing valid concerns raised by Leonid, the following version would be safe:
Then
Edit 2: Even WithRules3 is not completely equivalent to Andrew's version: