From 8a3653d8f30f7d0f79556f464ea781472a931b2e Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Tue, 9 Apr 2019 23:36:56 +0200 Subject: test.txt: Add apply and pure --- test.txt | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/test.txt b/test.txt index 072ac19..0fc86ab 100644 --- a/test.txt +++ b/test.txt @@ -1,10 +1,22 @@ fmap f p = Parser ((.) (fmapEither (fmap1st f)) (runParser p)); +pure x = Parser (\s -> Right (x, s)); +apply pf px = Parser (\s -> + bindEither (runParser pf s) (\res1 -> case res1 of { + (f, s') -> bindEither (runParser px s') (\res2 -> case res2 of { + (x, s'') -> Right (f x, s'') +})})); + fmapEither f e = case e of { Left x -> Left x; Right x -> Right (f x) }; +bindEither m f = case m of { + Left x -> Left x; + Right x -> f x +}; + fmap1st f p = case p of { (l, r) -> (f l, r) }; @@ -16,6 +28,9 @@ runParser p = case p of { (.) f g x = f (g x); id x = x; -v = fmap id (Parser g); -x = fmap ((.) f g) (Parser g); -y = ((.) (fmap f) (fmap g)) (Parser g); +fl1_a = fmap id (Parser g); +fl1_b = Parser g; +fl2_a = fmap ((.) f g) (Parser g); +fl2_b = ((.) (fmap f) (fmap g)) (Parser g); +al1_a = apply (pure id) v; +al1_b = v; -- cgit v1.2.3