与某些图案不同的匹配头
我想匹配 who's head 与 f 不同的表达式。
这可行
[In] !MatchQ[t[3], x_ /; Head[x] == f]
[Out] True
但不行
[In] MatchQ[t[3], x_ /; Head[x] != f]
[Out] False
为什么第二个解决方案不起作用?我怎样才能让它发挥作用?
I want to match expression who's head differs from f.
This works
[In] !MatchQ[t[3], x_ /; Head[x] == f]
[Out] True
But not this
[In] MatchQ[t[3], x_ /; Head[x] != f]
[Out] False
Why does the second solution not work? How can I make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
为什么这不起作用:您必须使用
=!=
(<代码>UnsameQ),而不是!=
(不相等
)用于结构比较:通过计算可以看出原因:
运算符
==
(Equal) 和
!=
(不相等
) 当双方相等(或不相等)的事实无法成立时,会对自身求值。这在象征性环境中是有意义的。我在此处更详细地考虑了这个主题,其中还有SameQ 和
UnsameQ
。还有更优雅的方式来表达相同的模式,这也会更有效,例如:
Why this does not work: you must use
=!=
(UnsameQ
), rather than!=
(Unequal
) for structural comparisons:The reason can be seen by evaluating this:
The operators
==
(Equal
) and!=
(Unequal
) do evaluate to themselves, when the fact of equality (or inequality) of the two sides can not be established. This makes sense in a symbolic environment. I considered this topic in more detail here, where alsoSameQ
andUnsameQ
are discussed.There are also more elegant ways to express the same pattern, which will be more efficient as well, such as this: