Skip to content

Instantly share code, notes, and snippets.

@groz
Created November 19, 2015 08:59
Show Gist options
  • Save groz/ca57346e39c690ec6be1 to your computer and use it in GitHub Desktop.
Save groz/ca57346e39c690ec6be1 to your computer and use it in GitHub Desktop.
fp101x parsers lecture working example
module Parsers where
import Prelude hiding (fail, return, (>>=))
type Parser a = String -> [(a, String)]
item :: Parser Char
item [] = []
item (c:cs) = [(c, cs)]
fail :: Parser a
fail _ = []
return :: a -> Parser a
return v inp = [(v, inp)]
(>>=) :: Parser a -> (a -> Parser b) -> Parser b
p >>= f = \inp -> concat [f a out | (a, out) <- p inp]
p :: Parser (Char, Char)
p = item >>= \c1 ->
item >>= \_ ->
item >>= \c2 ->
return (c1, c2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment