关于 Haskell 中 ~ 和 @ 运算符的问题

发布于 2024-12-06 00:10:09 字数 566 浏览 0 评论 0原文

他们到底是做什么的?我知道 @ 的一种可能用途(在模式匹配的开头分配一个名称),但在 ~ 上找不到任何内容。

我在以下代码片段中找到了它们,取自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-12-13 00:10:09

运算符~ 使匹配变得惰性。通常模式匹配会评估参数,因为需要检查模式是否失败。如果您为模式添加前缀 ~,则在需要之前不会进行评估。此功能通常用在“打结”代码中,其中需要引用尚未创建的结构。如果模式在评估时失败,则结果为未定义

下面是一个示例:

f (_:_) = True
f []    = False

g ~(_:_) = True
g []     = False

f [] 生成 False,而 g [] 生成 true,因为第一个模式始终匹配。 (您实际上会收到此代码的警告)

也就是说,您可以将 ~ 视为 ! 的反义词,它会强制对参数求值,即使它是不需要的。

请注意,这些运算符仅在它们应用的级别上使事物变得严格/惰性,而不是递归地。例如:

h ~((x,y):xys) = ...

元组上的模式匹配是严格的,但 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 is undefined.

Here is an example:

f (_:_) = True
f []    = False

g ~(_:_) = True
g []     = False

f [] yields False, while g [] 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:

h ~((x,y):xys) = ...

The pattern match on the tuple is strict, but the cons pattern is lazy.

菊凝晚露 2024-12-13 00:10:09

这是一个惰性模式匹配(也称为< em>无可辩驳的模式匹配,我认为这是更好的名字)。

本质上,~(_:t) 将始终匹配,即使输入是空列表 [] 也是如此。当然,如果您不知道自己在做什么,这是很危险的:

Prelude> let f ~(_:t) = t in f []
*** Exception: <interactive>:1:4-15: Irrefutable pattern failed for pattern (_ : 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:

Prelude> let f ~(_:t) = t in f []
*** Exception: <interactive>:1:4-15: Irrefutable pattern failed for pattern (_ : t)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文