From 0bb04c08d8232187dc73265e9a675f84c8e48d26 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Sat, 27 Dec 2014 15:29:51 +0530 Subject: [PATCH] hw10: exercises 4 and 5 --- hw10/AParser.hs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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)) -- 2.37.2