Skip to content

Commit eaef692

Browse files
committed
Ch 17 finished
1 parent 84bb1ae commit eaef692

File tree

2 files changed

+147
-11
lines changed

2 files changed

+147
-11
lines changed

ch17/ChapterExercises.hs

+135-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,144 @@
11
module ChapterExercises where
22

3-
-- 1.
3+
import Data.Monoid
4+
import Control.Applicative
5+
import Test.QuickCheck
6+
import Test.QuickCheck.Checkers
7+
import Test.QuickCheck.Classes
48

5-
pure :: a -> [a]
6-
(<*>) :: [(a -> b)] -> [a] -> [b]
9+
{-
10+
- 1.
11+
-
12+
-pure :: a -> [a]
13+
-(<*>) :: [(a -> b)] -> [a] -> [b]
14+
-
15+
- 2.
16+
-
17+
-pure :: a -> IO a
18+
-(<*>) :: IO (a -> b) -> IO a -> IO b
19+
-
20+
- 3.
21+
-
22+
-pure :: Monoid a => b -> (a, b)
23+
-(<*>) :: Monoid c => (c, a -> b) -> (c, a) -> (c, b)
24+
-
25+
- 4.
26+
-
27+
-pure :: a -> (->) e a == a -> (e -> a) == a -> e -> a
28+
-(<*>) :: ((->) e (a -> b)) -> ((->) e a) -> ((->) e b)
29+
-(<*>) :: (e -> (a -> b)) -> (e -> a) -> (e -> b)
30+
-f (<*>) g = \x -> f x $ g x
31+
-}
732

8-
-- 2.
933

10-
pure :: a -> IO a
11-
(<*>) :: IO (a -> b) -> IO a -> IO b
34+
main = do
35+
quickBatch $ functor $ (Pair (1, 2, 3) (1, 2, 3) :: Pair (Int, Int, Int))
36+
quickBatch $ applicative $ (Pair (1, 2, 3) (1, 2, 3) :: Pair (Int, Int, Int))
1237

13-
-- 3.
38+
quickBatch $ functor $ (Two "a" (1, 2, 3) :: Two String (Int, Int, Int))
39+
quickBatch $ applicative $ (Two "a" (1, 2, 3) :: Two String (Int, Int, Int))
1440

15-
pure :: Monoid a => b -> (a, b)
16-
(<*>) :: Monoid c => (c, a -> b) -> (c, a) -> (c, b)
41+
quickBatch $ functor $ (Three "a" 1 (1, 2, 3) :: Three String (Sum Int) (Int, Int, Int))
42+
quickBatch $ applicative $ (Three "a" 2 (1, 2, 3) :: Three String (Sum Int) (Int, Int, Int))
1743

18-
-- 4.
44+
quickBatch $ functor $ (Three' "a" (1, 2, 3) (1, 2, 3) :: Three' String (Int, Int, Int))
45+
quickBatch $ applicative $ (Three' "a" (1, 2, 3) (1, 2, 3) :: Three' String (Int, Int, Int))
1946

20-
pure :: a -> (->) e a
47+
quickBatch $ functor $ (Four "a" "a" 1 (1, 2, 3) :: Four String String (Sum Int) (Int, Int, Int))
48+
quickBatch $ applicative $ (Four "a" "a" 2 (1, 2, 3) :: Four String String (Sum Int) (Int, Int, Int))
49+
50+
quickBatch $ functor $ (Four' "a" "a" (1, 2, 3) (1, 2, 3) :: Four' String (Int, Int, Int))
51+
quickBatch $ applicative $ (Four' "a" "a"(1, 2, 3) (1, 2, 3) :: Four' String (Int, Int, Int))
52+
53+
instance Arbitrary a => Arbitrary (Sum a) where
54+
arbitrary = fmap Sum arbitrary
55+
56+
data Pair a = Pair a a deriving (Eq, Show)
57+
58+
instance Arbitrary a => Arbitrary (Pair a) where
59+
arbitrary = liftA2 Pair arbitrary arbitrary
60+
61+
instance Eq a => EqProp (Pair a) where
62+
(=-=) = eq
63+
64+
instance Functor Pair where
65+
fmap f (Pair a b) = Pair (f a) (f b)
66+
67+
instance Applicative Pair where
68+
pure x = Pair x x
69+
Pair f g <*> Pair x y = Pair (f x) (g y)
70+
71+
data Two a b = Two a b deriving (Eq, Show)
72+
73+
instance Functor (Two a) where
74+
fmap f (Two x y) = Two x $ f y
75+
76+
instance (Eq a, Eq b) => EqProp (Two a b) where
77+
(=-=) = eq
78+
79+
instance (Arbitrary a, Arbitrary b) => Arbitrary (Two a b) where
80+
arbitrary = liftA2 Two arbitrary arbitrary
81+
82+
instance Monoid a => Applicative (Two a) where
83+
pure = Two mempty
84+
Two a f <*> Two a' x = Two (a `mappend` a') $ f x
85+
86+
data Three a b c = Three a b c deriving (Eq, Show)
87+
88+
instance Functor (Three a b) where
89+
fmap f (Three a b x) = Three a b $ f x
90+
91+
instance (Eq a, Eq b, Eq c) => EqProp (Three a b c) where
92+
(=-=) = eq
93+
94+
instance (Arbitrary a, Arbitrary b, Arbitrary c) => Arbitrary (Three a b c) where
95+
arbitrary = liftA3 Three arbitrary arbitrary arbitrary
96+
97+
instance (Monoid a, Monoid b) => Applicative (Three a b) where
98+
pure = Three mempty mempty
99+
Three a b f <*> Three a' b' x = Three (a `mappend` a') (b `mappend` b') $ f x
100+
101+
data Three' a b = Three' a b b deriving (Eq, Show)
102+
103+
instance Functor (Three' a) where
104+
fmap f (Three' a x x') = Three' a (f x) (f x')
105+
106+
instance (Eq a, Eq b) => EqProp (Three' a b) where
107+
(=-=) = eq
108+
109+
instance (Arbitrary a, Arbitrary b) => Arbitrary (Three' a b) where
110+
arbitrary = liftA3 Three' arbitrary arbitrary arbitrary
111+
112+
instance (Monoid a) => Applicative (Three' a) where
113+
pure x = Three' mempty x x
114+
Three' a f g <*> Three' a' x y = Three' (a `mappend` a') (f x) (g y)
115+
116+
data Four a b c d = Four a b c d deriving (Eq, Show)
117+
118+
instance Functor (Four a b c) where
119+
fmap f (Four a b c x) = Four a b c $ f x
120+
121+
instance (Eq a, Eq b, Eq c, Eq d) => EqProp (Four a b c d) where
122+
(=-=) = eq
123+
124+
instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d) => Arbitrary (Four a b c d) where
125+
arbitrary = Four <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
126+
127+
instance (Monoid a, Monoid b, Monoid c) => Applicative (Four a b c) where
128+
pure = Four mempty mempty mempty
129+
Four a b c f <*> Four a' b' c' x = Four (a `mappend` a') (b `mappend` b') (c `mappend` c') $ f x
130+
131+
data Four' a b = Four' a a b b deriving (Eq, Show)
132+
133+
instance Functor (Four' a) where
134+
fmap f (Four' a a' x x') = Four' a a' (f x) (f x')
135+
136+
instance (Eq a, Eq b) => EqProp (Four' a b) where
137+
(=-=) = eq
138+
139+
instance (Arbitrary a, Arbitrary b) => Arbitrary (Four' a b) where
140+
arbitrary = Four' <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
141+
142+
instance (Monoid a) => Applicative (Four' a) where
143+
pure x = Four' mempty mempty x x
144+
Four' a b f g <*> Four' a' b' x y = Four' (a `mappend` a') (b `mappend` b') (f x) (g y)

ch17/VowelsStops.hs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module VowelsStops where
2+
3+
import Control.Applicative (liftA3)
4+
5+
stops :: String
6+
stops = "pbtdkg"
7+
8+
vowels :: String
9+
vowels = "aeiou"
10+
11+
combos :: [a] -> [b] -> [c] -> [(a, b, c)]
12+
combos = liftA3 (,,)

0 commit comments

Comments
 (0)