关于 Haskell 中 ~ 和 @ 运算符的问题
他们到底是做什么的?我知道 @ 的一种可能用途(在模式匹配的开头分配一个名称),但在 ~ 上找不到任何内容。
我在以下代码片段中找到了它们,取自 http://www.haskell.org/haskellwiki/Prime_numbers< /a>,但本文假设您精通 Haskell 语法,并且不会费心解释其深奥的运算符(我感到困惑的部分是声明的开头sieve):
primesPT () = 2 : primes'
where
primes' = sieve [3,5..] primes' 9
sieve (p:xs) ps@ ~(_:t) q
| p < q = p : sieve xs ps q
| True = sieve [x | x<-xs, rem x p /= 0] t (head t^2)
任何有关此处使用的语法的解释(或链接)将不胜感激。
What exactly do they do? I know one possible use of @ (assigning a name at the start of a pattern match), but haven't been able to find anything on ~.
I found them in the following code snippet, taken from http://www.haskell.org/haskellwiki/Prime_numbers, but the article assumes that you're fluent in Haskell syntax and doesn't bother explaining its esoteric operators (the part I'm confused about is the start of the declaration for sieve):
primesPT () = 2 : primes'
where
primes' = sieve [3,5..] primes' 9
sieve (p:xs) ps@ ~(_:t) q
| p < q = p : sieve xs ps q
| True = sieve [x | x<-xs, rem x p /= 0] t (head t^2)
Any explanation (or link to one) about the syntax used here would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运算符
~
使匹配变得惰性。通常模式匹配会评估参数,因为需要检查模式是否失败。如果您为模式添加前缀~
,则在需要之前不会进行评估。此功能通常用在“打结”代码中,其中需要引用尚未创建的结构。如果模式在评估时失败,则结果为未定义
。下面是一个示例:
f []
生成False
,而g []
生成 true,因为第一个模式始终匹配。 (您实际上会收到此代码的警告)也就是说,您可以将
~
视为!
的反义词,它会强制对参数求值,即使它是不需要的。请注意,这些运算符仅在它们应用的级别上使事物变得严格/惰性,而不是递归地。例如:
元组上的模式匹配是严格的,但 cons 模式是惰性的。
The operator
~
makes a match lazy. Usually a pattern-match evaluates the argument, as there is a need to check whether the pattern fails. If you prefix a pattern with~
, there is no evaluation until it is needed. This functionality is often used in “Tying the knot” code, where one needs to refer to structures that are not yet created. If the pattern fails upon evaulation, the result isundefined
.Here is an example:
f []
yieldsFalse
, whileg []
yields true, because the first pattern always matches. (You actually get a warning for this code)That said, you can see
~
as the opposite of!
, which forces the evaluation of an argument even if it's unneeded.Note that these operators only make things strict/lazy at the level they are applied at, not recursively. For example:
The pattern match on the tuple is strict, but the cons pattern is lazy.
这是一个惰性模式匹配(也称为< em>无可辩驳的模式匹配,我认为这是更好的名字)。
本质上,
~(_:t)
将始终匹配,即使输入是空列表[]
也是如此。当然,如果您不知道自己在做什么,这是很危险的:It's a lazy pattern match (also known as irrefutable pattern match which I think is the better name).
Essentially,
~(_:t)
will always match, even if the input is the empty list[]
. Of course, this is dangerous if you don't know what you're doing: