Skip to content

Commit 45838a9

Browse files
committed
Attempt some of the exercises in Chapter 17
1 parent 94f455a commit 45838a9

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

ch17/Constant.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Constant where
2+
3+
-- Exercise: Constant Instance
4+
5+
newtype Constant a b
6+
= Constant { getConstant :: a }
7+
deriving (Eq, Ord, Show)
8+
9+
instance Functor (Constant a) where
10+
fmap _ (Constant a) = Constant a
11+
12+
-- This was confusing to define but the examples helped
13+
instance Monoid a => Applicative (Constant a) where
14+
pure _ = Constant mempty
15+
(Constant x) <*> (Constant y) = Constant $ mappend x y

ch17/Identity.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Identity where
2+
3+
-- Exercise: Identity Instance
4+
5+
newtype Identity a
6+
= Identity a
7+
deriving (Eq, Ord, Show)
8+
9+
instance Functor Identity where
10+
fmap f (Identity a) = Identity (f a)
11+
12+
instance Applicative Identity where
13+
pure a = Identity a
14+
(Identity f) <*> (Identity a) = Identity (f a)

ch17/fixer-upper.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module FixerUpper where
2+
3+
-- Exercise: Fixer Upper
4+
5+
-- 1
6+
f :: Maybe String
7+
f = const <$> Just "Hello" <*> pure "World"
8+
9+
10+
-- 2
11+
g :: Maybe (Int, Int, String, [Int])
12+
g = (,,,) <$> Just 90
13+
<*> Just 10
14+
<*> Just "Tierness"
15+
<*> pure [1, 2, 3]

ch17/lookups.hs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module Lookups where
2+
3+
import Data.List (elemIndex)
4+
5+
-- Exercises: Lookups
6+
7+
-- 1
8+
added :: Maybe Integer
9+
added = (+3) <$> (lookup 3 $ zip [1, 2, 3] [4, 5, 6])
10+
11+
12+
-- 2
13+
y :: Maybe Integer
14+
y = lookup 3 $ zip [1, 2, 3] [4, 5, 6]
15+
16+
z :: Maybe Integer
17+
z = lookup 2 $ zip [1, 2, 3] [4, 5, 6]
18+
19+
tupled :: Maybe (Integer, Integer)
20+
tupled = (,) <$> y <*> z
21+
-- Try replacing y and/or z with Nothing
22+
-- That's so cool how we've abstracted the possibility of failure
23+
-- Look how simple the code for tupled has become
24+
-- Though it takes some thinking to understand why it works
25+
26+
27+
-- 3
28+
x' :: Maybe Int
29+
x' = elemIndex 3 [1, 2, 3, 4, 5]
30+
31+
y' :: Maybe Int
32+
y' = elemIndex 4 [1, 2, 3, 4, 5]
33+
34+
max' :: Int -> Int -> Int
35+
max' = max
36+
37+
maxed :: Maybe Int
38+
maxed = max' <$> x' <*> y'
39+
40+
41+
-- 4
42+
as = [1, 2, 3]
43+
bs = [4, 5, 6]
44+
45+
a :: Maybe Integer
46+
a = lookup 3 $ zip as bs
47+
48+
b :: Maybe Integer
49+
b = lookup 2 $ zip as bs
50+
51+
summed :: Maybe Integer
52+
summed = sum <$> ((,) <$> a <*> b)
53+
-- Is this correct?
54+
-- sum for 2-tuple just returns the second element in the tuple

0 commit comments

Comments
 (0)