我正在编写一个 xml-conduit 解析器,并且我更喜欢应用语法而不是单子(monad)语法。由于需要结合很多论点,我在应用方面有些迷失。我当前的问题有 8 个参数,我只想使用第 4 个和第 6 个参数来构造结果。
我可以让它发挥作用的唯一方法如下:不过,对于扁平的解决方案来说,应该有奇特的星星排列:
import Control.Applicative
a1 :: Applicative Text
a2 :: Applicative Text
a3 :: Applicative Text
a4 :: Applicative Text
a5 :: Applicative Text
a6 :: Applicative Text
a7 :: Applicative Text
a8 :: Applicative Text
data Data = Data Text Text
f :: Text -> Text -> Data
parser :: Applicative Data
parser = a1 *> a2 *> a3 *> (f <$> a4 <* a5 <*> a6) <* a7 <* a8
有什么方法可以在没有括号的情况下执行相同的操作吗?
parser = f <$> a1 ?? a2 ?? a3 ?? a4 ?? a5 ?? a6 ?? a7 ?? a8
最佳答案
啊哈,建议的链接Applicative style parser for constructor with two arguments让我找到了答案:使用 (<$)
,不要使用(*>)
.
parser = f <$ a1 <* a2 <* a3 <*> a4 <* a5 <*> a6 <* a7 <* a8
关于haskell - 忽略 Control.Applicative 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16050719/