From: Ramakrishnan Muthukrishnan Date: Sat, 27 Dec 2014 09:59:51 +0000 (+0530) Subject: hw10: exercises 4 and 5 X-Git-Url: https://git.rkrishnan.org/vdrive/%22file:/frontends/status?a=commitdiff_plain;h=0bb04c08d8232187dc73265e9a675f84c8e48d26;p=yorgey.git hw10: exercises 4 and 5 --- diff --git a/hw10/AParser.hs b/hw10/AParser.hs index f506fcb..04eb6ad 100644 --- a/hw10/AParser.hs +++ b/hw10/AParser.hs @@ -96,3 +96,17 @@ abParser_ = (\_ _ -> ()) <$> char 'a' <*> char 'b' intPair :: Parser [Integer] intPair = (\x _ z -> x : z : []) <$> posInt <*> char ' ' <*> posInt + +-- Exercise 4 +-- class Applicative f => Alternative f where +-- empty :: f a +-- (<|>) :: f a -> f a -> f a +instance Alternative Parser where + empty = Parser(\_ -> Nothing) + (Parser p1) <|> (Parser p2) = Parser (\inp -> case (p1 inp) of + Nothing -> (p2 inp) + Just v -> Just v) + +-- exercise 5: int or Uppercase +intOrUppercase :: Parser () +intOrUppercase = ((\_ -> ()) <$> posInt) <|> ((\_ -> ()) <$> (satisfy isUpper))