// F# Parser Monad (http://www.cs.nott.ac.uk/~gmh/pearl.pdf)

type Parser<'a> = Parser of (string ->('a * string) list)

let extract(Parser(f)) = f

type ParserMonad() =

member b.Bind(p, f) = Parser (fun cs ->

let r = extract(p) cs

let r' = List.map (fun (a,cs') -> extract(f a) cs') r

List.concat r')

member b.Return(x) = Parser (fun cs -> [x,cs])

member b.Zero() = Parser (fun cs -> [])

let (++) p q = Parser(fun cs -> List.append (extract p cs) (extract q cs))

let (+++) p q = Parser(fun cs -> match (extract(p ++ q) cs) with

| [] -> []

| x::xs -> [x])

let parser = ParserMonad()

## Wednesday, 3 December 2008

### F# Parser Monad (http://www.cs.nott.ac.uk/~gmh/pearl.pdf)

Subscribe to:
Post Comments (Atom)

## No comments:

Post a Comment