gpt4 book ai didi

haskell - Haskell 中的模式匹配序列

转载 作者:行者123 更新时间:2023-12-03 20:19:42 25 4
gpt4 key购买 nike

模式匹配是最优雅的 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 的情况下编写的,您应该使用 ViewLViewR类型以便在数据结构上进行模式匹配。

{-# LANGUAGE ViewPatterns #-}

floodFillWorker image _ _ (Seq.viewl -> EmptyL) = image
floodFillWorker image base tolerance queue = doSomeWork image

关于haskell - Haskell 中的模式匹配序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2078042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com