Skip to content

Commit ea2c9cd

Browse files
committed
Add solution to exercise four
1 parent 27becde commit ea2c9cd

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

12-monads-and-more/12.05-exercises.hs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@ instance Functor Tree where
99

1010
-----------------------------------------------------------------------------
1111
-- 2.
12-
--
13-
-- |
14-
-- Avoid conflict with library definition for `((->) r)`
15-
newtype R r a = R
16-
{ run :: r -> a }
12+
instance Functor ((->) a) where
13+
fmap = (.)
1714

18-
instance Functor (R r) where
19-
fmap f (R r) = R $ f . r
15+
-----------------------------------------------------------------------------
16+
-- 3.
17+
instance Applicative ((->) a) where
18+
pure x = \_ -> x
19+
f <*> g = \x -> f x $ g x
20+
21+
-----------------------------------------------------------------------------
22+
-- 4.
23+
newtype ZipList a = Z [a]
24+
deriving Show
2025

21-
applyR :: (Int -> Int) -> R Int Int -> Int -> Int
22-
applyR f r x = run (fmap f r) x
26+
instance Functor ZipList where
27+
fmap f (Z xs) = Z (fmap f xs)
2328

24-
runR :: Int
25-
runR = applyR (+100) (R $ (+2)) 3
29+
instance Applicative ZipList where
30+
pure x = Z $ repeat x
31+
Z fs <*> Z xs = Z [f x | (f, x) <- zip fs xs]

0 commit comments

Comments
 (0)