From: Ramakrishnan Muthukrishnan Date: Sat, 27 Dec 2014 15:23:26 +0000 (+0530) Subject: hw11: starting files X-Git-Url: https://git.rkrishnan.org/pf/components/com_hotproperty/css/simplejson/htmlfontify-example.html?a=commitdiff_plain;h=456f59f9d65871460c92e8ef32c6936d2177993b;p=yorgey.git hw11: starting files --- diff --git a/hw11/11-applicative2.pdf b/hw11/11-applicative2.pdf new file mode 100644 index 0000000..7bacd5e Binary files /dev/null and b/hw11/11-applicative2.pdf differ diff --git a/hw11/AParser.hs b/hw11/AParser.hs new file mode 100644 index 0000000..bb78a5a --- /dev/null +++ b/hw11/AParser.hs @@ -0,0 +1,44 @@ +module AParser (Parser, runParser, satisfy, char, posInt) where + +import Control.Applicative +import Data.Char + +newtype Parser a = Parser { runParser :: String -> Maybe (a, String) } + +satisfy :: (Char -> Bool) -> Parser Char +satisfy p = Parser f + where + f [] = Nothing + f (x:xs) + | p x = Just (x, xs) + | otherwise = Nothing + +char :: Char -> Parser Char +char c = satisfy (== c) + +posInt :: Parser Integer +posInt = Parser f + where + f xs + | null ns = Nothing + | otherwise = Just (read ns, rest) + where (ns, rest) = span isDigit xs + +inParser f = Parser . f . runParser + +first :: (a -> b) -> (a,c) -> (b,c) +first f (x,y) = (f x, y) + +instance Functor Parser where + fmap = inParser . fmap . fmap . first + +instance Applicative Parser where + pure a = Parser (\s -> Just (a, s)) + (Parser fp) <*> xp = Parser $ \s -> + case fp s of + Nothing -> Nothing + Just (f,s') -> runParser (f <$> xp) s' + +instance Alternative Parser where + empty = Parser (const Nothing) + Parser p1 <|> Parser p2 = Parser $ liftA2 (<|>) p1 p2 diff --git a/hw11/SExpr.hs b/hw11/SExpr.hs new file mode 100644 index 0000000..78ae980 --- /dev/null +++ b/hw11/SExpr.hs @@ -0,0 +1,46 @@ +{- CIS 194 HW 11 + due Monday, 8 April +-} + +module SExpr where + +import AParser +import Control.Applicative + +------------------------------------------------------------ +-- 1. Parsing repetitions +------------------------------------------------------------ + +zeroOrMore :: Parser a -> Parser [a] +zeroOrMore p = undefined + +oneOrMore :: Parser a -> Parser [a] +oneOrMore p = undefined + +------------------------------------------------------------ +-- 2. Utilities +------------------------------------------------------------ + +spaces :: Parser String +spaces = undefined + +ident :: Parser String +ident = undefined + +------------------------------------------------------------ +-- 3. Parsing S-expressions +------------------------------------------------------------ + +-- An "identifier" is represented as just a String; however, only +-- those Strings consisting of a letter followed by any number of +-- letters and digits are valid identifiers. +type Ident = String + +-- An "atom" is either an integer value or an identifier. +data Atom = N Integer | I Ident + deriving Show + +-- An S-expression is either an atom, or a list of S-expressions. +data SExpr = A Atom + | Comb [SExpr] + deriving Show