How to define <*> for applicative parser?

和自甴很熟 提交于 2019-12-11 05:15:22

问题


Suppose we define parser as a function

type Parser[A] = String => List(A, String) 

The parser takes an input string and yields a sequence of pairs. Each pair consists of the parsing result and unconsumed part of the input

As I understand, we can define the parser as an applicative and implement a seq parser combinator in terms of <*>.

The seq combinator is a function, which takes two parsers p and q as its arguments and returns a new parser, which applies p and q to the input sequentially. For example:

val pa  = symbol('a') // parser, which recognizes 'a' as the 1st char in the input
val pb  = symbol('b') // parser, which recognizes 'b' as the 1st char in the input  
val pab = seq(pa, pb) // recognizes "ab"

Obviously, we can easily define flatMap for the parser and then define <*> in terms of that flatMap. Can we define <*> differently and implement seq with that <*> ?

来源:https://stackoverflow.com/questions/30675240/how-to-define-for-applicative-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!