Tuple bang patterns

谁说我不能喝 提交于 2019-12-05 05:45:25

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!