Monadic相当于适用<*

gaw*_*awi 4 monads haskell parsec applicative

在阅读了Anthony样式相关解析器问题的回应之后,我试图说服自己编写monadic解析器仍然可以相当紧凑.

而不是

reference :: Parser Transc
reference = try $ do string "#{"
                     a <- number
                     char ','
                     b <- number
                     char ','
                     c <- number
                     char '}'
                     return $ Outside (a,b,c)
Run Code Online (Sandbox Code Playgroud)

我们可以简单地:

reference3 :: Parser Transc
reference3 = liftM3 (((Outside .).) .  (,,)) 
             (string "#{" >> number <<! char ',') 
             number
             (char ',' >> number <<! char '}') where 
               (<<!) = liftM2 const
Run Code Online (Sandbox Code Playgroud)

这与Anthony提供的应用版非常相似:

reference2 :: Parser Transc
reference2 = ((Outside .) .) . (,,) 
             <$> (string "#{" *> number2 <* char ',') 
             <*> number2 
             <*> (char ',' *> number2 <* char '}')
Run Code Online (Sandbox Code Playgroud)

......除了<<!概念上类似的运算符,<*其定义为liftA2 const"序列但丢弃值和使用值提供给左侧".

当然<<会为一个坏的名字liftM2 const,它会认为<<是相当于flip >>如果我们按照相同的逻辑>>==<<.

我没有在一个名字下找到"liftM2 const".这是因为它不是那么有用吗?

Hei*_*mus 10

我不太明白这个问题.每个monad也是一个applicative functor,所以你也可以简单地使用(*>)monadic表达式.

(在此答案(2011年)时,Applicative不是超类Monad,因此可能需要添加相应的类实例.)