Skip to content

Commit 27becde

Browse files
committed
Add first two solutions to chapter 12 exercises
1 parent 144efaf commit 27becde

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- 1.
2+
data Tree a = Leaf
3+
| Node (Tree a) a (Tree a)
4+
deriving Show
5+
6+
instance Functor Tree where
7+
fmap f (Node l x r) = Node (fmap f l) (f x) (fmap f r)
8+
fmap _ Leaf = Leaf
9+
10+
-----------------------------------------------------------------------------
11+
-- 2.
12+
--
13+
-- |
14+
-- Avoid conflict with library definition for `((->) r)`
15+
newtype R r a = R
16+
{ run :: r -> a }
17+
18+
instance Functor (R r) where
19+
fmap f (R r) = R $ f . r
20+
21+
applyR :: (Int -> Int) -> R Int Int -> Int -> Int
22+
applyR f r x = run (fmap f r) x
23+
24+
runR :: Int
25+
runR = applyR (+100) (R $ (+2)) 3

0 commit comments

Comments
 (0)