Haskell 中的模式匹配 Seq
模式匹配是 Haskell 最优雅的功能之一。
我最近一直在做一个项目,我需要一个队列数据结构,所以我使用 Data.Sequence。然而,看起来我必须放弃模式匹配的优雅并求助于守卫:
floodFillWorker :: Image -> RGBAColor -> Double -> PixelQueue -> Image
floodFillWorker image base tolerance queue
| Seq.null queue = image
| otherwise = doSomeWork image
我可以将模式匹配与序列一起使用还是需要使用守卫?
Pattern matching is one of the most elegant Haskell features.
I've been working on a project recently where I need a queue data structure so I'm using Data.Sequence. However, it looks like I have to give up the elegance of pattern matching and resort to guards:
floodFillWorker :: Image -> RGBAColor -> Double -> PixelQueue -> Image
floodFillWorker image base tolerance queue
| Seq.null queue = image
| otherwise = doSomeWork image
Can I use pattern matching with sequences or do I need to use guards?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ehemient 的视图模式走在正确的轨道上,但我认为有一种方法可以做到这一点,这实际上非常好。
Data.Sequence
实际上是在考虑视图的情况下编写的,您应该使用ViewL
或ViewR
类型以便对数据结构进行模式匹配。ephemient is on the right track with view patterns but I think there's a way to do it which is actually quite nice.
Data.Sequence
was actually written with views in mind and you should use either theViewL
orViewR
types in order to patternmatch on the data structure.您可以使用查看模式而不是守卫,但实际上它并没有更好(IMO)。守卫我觉得还不错...
You could use view patterns instead of guards, but actually it isn't any better (IMO). The guards look fine to me...