1

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 <*> ?

Michael
  • 41,026
  • 70
  • 193
  • 341
  • Why a _sequence_ of pairs? Can you include e.g. your `symbol` or `flatMap` implementations to show how you're using this? – Travis Brown Jun 05 '15 at 22:00
  • I took this parser defintion from http://eprints.nottingham.ac.uk/237/1/monparsing.pdf – Michael Jun 05 '15 at 22:11
  • I will add the `symbol` and `flatMap` implementations to the question to make it clear. – Michael Jun 05 '15 at 22:13
  • @TravisBrown Decided to post a new question rather than update this one. Hope the new question is clear. http://stackoverflow.com/questions/30685920/applicative-parser-example-in-scala – Michael Jun 06 '15 at 17:59

0 Answers0