从 Hold'd 表达式中提取内容的冒险
假设我有一个 param->value 规则列表,其中参数是可能分配有值的符号。例如:
{a, b, c} = {1, 2, 3};
x = Hold[{a->1, b->2, c->3}];
我需要将列表包装在 Hold 中,否则它将计算为 {1->1, 2->2, 3->3}。 (我愿意接受 Hold There 的任何替代方案,如果它能让剩下的事情变得更容易的话。)
现在假设我想将 x 转换成这样:
{"a"->1, "b"->2, "c"->3}
下面的函数将做到这一点:
f[h_] := Block[{a,b,c}, ToString[#[[1]]]->#[[2]]& /@ ReleaseHold@h]
我的问题:你能写一个 f 的版本吗?符号列表 {a,b,c} 不必显式提供?
Suppose I have a list of param->value rules where the params are symbols that might have values assigned to them. For example:
{a, b, c} = {1, 2, 3};
x = Hold[{a->1, b->2, c->3}];
I need the list wrapped in Hold otherwise it would evaluate to {1->1, 2->2, 3->3}. (I'm open to any alternatives to Hold there if it makes the rest of this easier.)
Now suppose I want to convert x into this:
{"a"->1, "b"->2, "c"->3}
The following function will do that:
f[h_] := Block[{a,b,c}, ToString[#[[1]]]->#[[2]]& /@ ReleaseHold@h]
My question: Can you write a version of f where the list of symbols {a,b,c} doesn't have to be provided explicitly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是使用 未评估 的方法:
Here is a way using Unevaluated:
Out:
也许不是很优雅,但似乎可行。
Out:
Perhaps not very elegant, but seems to work.
这是一个有点老的问题,但我认为有一个答案结合了两者的优点 Andrew Moylan 的回答 和 贝利撒留的回答。您确实希望左侧带有
HoldPattern
的规则列表,而不是包含整个事物的Hold
规则列表,这样您实际上可以使用规则而无需经历任何类型的ReleaseHold
过程。Unevaluated
也有助于构建您想要的列表类型:现在您可以通过规则替换来执行您想要的操作。这有点复杂,但我发现自己一遍又一遍地做这件事。您可能会注意到,此规则列表几乎与
OwnValues
或DownValues
列表的形式完全相同,因此能够操作它非常有帮助。诀窍是使用HoldPattern
和Verbatim
一致:Replace
的级别规范只是为了确保如果rhs
本身是一条规则或规则列表,不会发生意外情况。This is a bit of an old question, but I think there's an answer that combines the virtues of both Andrew Moylan's answer and belisarius' answer. You really want to have lists of rules with
HoldPattern
on the left-hand side, instead of lists of rules that haveHold
wrapped around the whole thing, so that you can actually use the rules without having to go through any sort ofReleaseHold
process.Unevaluated
can also be helpful in constructing the sort of list you want:Now you can do what you want with rule replacement. It's a bit involved, but it's something I find myself doing over and over and over again. You may notice that this list of rules has almost exactly the form of a list of
OwnValues
orDownValues
, so being able to manipulate it is very helpful. The trick is usingHoldPattern
andVerbatim
in concert:The level spec on
Replace
is just there to make sure nothing unexpected happens ifrhs
is itself a rule or list of rules.