模式匹配是最优雅的 Haskell 特性之一。
我最近一直在做一个需要队列数据结构的项目,所以我正在使用 Data.Sequence。但是,看起来我必须放弃模式匹配的优雅并求助于守卫:
floodFillWorker :: Image -> RGBAColor -> Double -> PixelQueue -> Image
floodFillWorker image base tolerance queue
| Seq.null queue = image
| otherwise = doSomeWork image
我可以对序列使用模式匹配还是需要使用 guard ?
最佳答案
ehemient 在 View 模式的正确轨道上,但我认为有一种方法可以做到这一点,实际上非常好。 Data.Sequence
实际上是在考虑 View 的情况下编写的,您应该使用 ViewL
或 ViewR
类型以便在数据结构上进行模式匹配。
{-# LANGUAGE ViewPatterns #-}
floodFillWorker image _ _ (Seq.viewl -> EmptyL) = image
floodFillWorker image base tolerance queue = doSomeWork image
关于haskell - Haskell 中的模式匹配序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2078042/