元组刘海图案

发布于 2024-11-17 08:09:20 字数 502 浏览 0 评论 0原文

我明白,在:

fx = x + 1 where !y = undefined

中,爆炸模式的含义是 y 将在 f 之前评估>。

类似地:

fx = x + 1 其中 !(!a, !b) = (undefined, undefined)

对于 xy< 来说,含义是相同的/代码>。

但是 bang 模式的含义是什么:

fx = x + 1 where (!a, !b) = (undefined, undefined)

它似乎不会导致 undefined 被评估。元组内爆炸模式何时生效?如果模式的元组是强制的?谁能举一个 (!a, !b) = (..)(a, b) = (..) 不同的例子?

I understand that in:

f x = x + 1 where !y = undefined

the meaning of the bang pattern is that y is to be evaluated before f.

Similarly:

f x = x + 1 where !(!a, !b) = (undefined, undefined)

the meaning is the same, w.r.t x and y.

But what do the bang patterns mean in:

f x = x + 1 where (!a, !b) = (undefined, undefined)

It doesn't seem to cause undefined to be evaluated. When do in-tuple bang patterns come into effect? If the pattern's tuple is forced? Can anyone give an example where (!a, !b) = (..) differs from (a, b) = (..)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

故事↓在人 2024-11-24 08:09:20

元组本身的爆炸模式将强制评估元组而不是其元素。每当元组本身被评估时,元组元素上的爆炸模式都会强制它们。

以下是不同行为的示例:

Prelude> let x = a + 1 where (a, b) = (1, undefined)
Prelude> x
2
Prelude> let x = a + 1 where (!a, !b) = (1, undefined)
Prelude> x
*** Exception: Prelude.undefined

A bang pattern on the tuple itself will force evaluation of the tuple but not its elements. Bang patterns on the tuple elements will force them whenever the tuple itself is evaluated.

Here's an example of the differing behavior:

Prelude> let x = a + 1 where (a, b) = (1, undefined)
Prelude> x
2
Prelude> let x = a + 1 where (!a, !b) = (1, undefined)
Prelude> x
*** Exception: Prelude.undefined
不必在意 2024-11-24 08:09:20

如果将其翻译为 let

f x = let (!a, !b) = (undefined, undefined) in x + 1

在这里,您创建一个包含 (a, b) 的元组,并且当对元组求值时,同时包含 a和 b 是。

但由于元组从未被求值,所以 ab 都不会。这与编写基本相同:

f x = let y = undefined `seq` 4 in x + 1

由于 y 从未被评估,因此也不是未定义的。

If you translate it to let:

f x = let (!a, !b) = (undefined, undefined) in x + 1

Here, you create a tuple containing (a, b), and when the tuple is evaluated, both a and b are.

But because the tuple is never evaluated, neither a nor b are. This is basically the same as writing:

f x = let y = undefined `seq` 4 in x + 1

Since y is never evaluated, neither is undefined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文