From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Sat, 27 Dec 2014 06:15:50 +0000 (+0530)
Subject: Add functor instance of parser
X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/uri/frontends/flags/webapi.txt?a=commitdiff_plain;h=981fe0e31a3314dd7b7533f1c3eb6ae8409aa057;p=yorgey.git

Add functor instance of parser
---

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)