From 981fe0e31a3314dd7b7533f1c3eb6ae8409aa057 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Sat, 27 Dec 2014 11:45:50 +0530 Subject: [PATCH] Add functor instance of parser --- hw10/AParser.hs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/hw10/AParser.hs b/hw10/AParser.hs index 0ee3090..7860c07 100644 --- a/hw10/AParser.hs +++ b/hw10/AParser.hs @@ -57,3 +57,13 @@ posInt = Parser f ------------------------------------------------------------ -- Your code goes below here ------------------------------------------------------------ + +-- Exercise 1. functor instance for Parser +instance Functor Parser where + -- Parser a == String -> (a, String) + -- first :: (a -> b) -> (a, c) -> (b, c) + -- fmap :: (a -> b) -> Parser a -> Parser b + fmap f (Parser pf) = Parser (\s -> case (pf s) of + Nothing -> Nothing + Just v -> Just $ (uncurry (first f) v)) + where first f' a b = (f' a, b) -- 2.37.2