haskell - (l, r)之前的~是什么意思
问题描述
我读了 Haskell 的图书馆
partitionEithers :: [Either a b] -> ([a],[b])
partitionEithers = foldr (either left right) ([],[])
where
left a ~(l, r) = (a:l, r)
right a ~(l, r) = (l, a:r)
~
以前是什么意思(l, r)
?
解决方案
这是一个惰性模式匹配。这意味着假定模式匹配是成功的,并且仅在需要其数据时才实际执行。
ghci> strictPat (a,b) = "hello"
ghci> lazyPat ~(a,b) = "hello"
ghci> strictPat undefined
"*** Exception: Prelude.undefined
ghci> lazyPat undefined
"hello"
ghci> strictPat2 (a,b) = "the values are " ++ a ++ " and " ++ b
ghci> lazyPat2 ~(a,b) = "the values are " ++ a ++ " and " ++ b
ghci> strictPat2 undefined
"*** Exception: Prelude.undefined
ghci> lazyPat2 undefined
"the values are *** Exception: Prelude.undefined
它在这里被使用,因此partitionEithers
可以成为一个很好的流光。否则,它必须先评估整个列表,然后才能返回其任一结果的第一个元素(因为例如left
会强制传入对,这是由递归调用生成的,这将不得不强制其传入对,等等...)。
推荐阅读
- jmeter - 在每次调用中重用 HTTP 标头管理器
- android - Flutter:ListView中带有点指示器的PageView不起作用
- javascript - 未使用的表达式,需要赋值或函数调用 (Angular)
- snowflake-cloud-data-platform - 升级到 mac Big Sur 后,SnowSQL 因“找不到库安全性”而失败
- javascript - 从nodejs中的promise返回JavaScript对象的问题
- android - 左视图宽度由右视图宽度决定
- typescript - 如何强制 Dukat 生成声明的类型?
- soap - 如何从 ABAP 入站适配器生成自定义 SOAP 故障消息详细信息
- amazon-web-services - Elastic BeanStalk 无法加载 url
- javascript - 在 JavaScript 中是否有一种(接近)线性的方式来按天对数组中的日期进行分组?