File tree Expand file tree Collapse file tree 6 files changed +110
-0
lines changed Expand file tree Collapse file tree 6 files changed +110
-0
lines changed Original file line number Diff line number Diff line change
1
+ import Test.QuickCheck
2
+ import Test.QuickCheck.Function
3
+
4
+ functorIdentity :: (Functor f , Eq (f a )) =>
5
+ f a -> Bool
6
+ functorIdentity f = fmap id f == f
7
+
8
+ functorCompose :: (Functor f , Eq (f c )) =>
9
+ (a -> b )
10
+ -> (b -> c )
11
+ -> f a
12
+ -> Bool
13
+ functorCompose f g x =
14
+ (fmap g (fmap f x)) == fmap (g . f) x
15
+
16
+ functorCompose' :: (Eq (f c ), Functor f ) =>
17
+ f a
18
+ -> Fun a b
19
+ -> Fun b c
20
+ -> Bool
21
+ functorCompose' x (Fun _ f) (Fun _ g) =
22
+ (fmap g (fmap f x)) == fmap (g . f) x
Original file line number Diff line number Diff line change
1
+ data Possibly a =
2
+ LolNope
3
+ | Yeppers a
4
+ deriving (Eq , Show )
5
+
6
+ instance Functor Possibly where
7
+ fmap _ LolNope = LolNope
8
+ fmap f (Yeppers a) = Yeppers (f a)
Original file line number Diff line number Diff line change
1
+ data Sum a b =
2
+ First a
3
+ | Second b
4
+ deriving (Eq , Show )
5
+
6
+ instance Functor (Sum a ) where
7
+ fmap _ (First a) = First a
8
+ fmap f (Second b) = Second (f b)
Original file line number Diff line number Diff line change
1
+ 1 . What is the kind of a?
2
+
3
+ ```
4
+ a -> a
5
+ ```
6
+
7
+ Ans -
8
+ Kind of a is '\* '
9
+
10
+ 2 . What are the kinds of b and T?
11
+
12
+ ```
13
+ a -> b a -> T (b a)
14
+
15
+ Ans -
16
+ Kind of b is `* -> *`
17
+ Kind of T is `* -> *`
18
+
19
+ 3. What is the kind of c?
20
+ ```
21
+
22
+ c a b -> c b a
23
+
24
+ ```
25
+ Ans -
26
+ The kind of c is `* -> * -> *`
27
+ ```
Original file line number Diff line number Diff line change
1
+ -- Instructions
2
+ -- Add fmap, parentheses, and function composition to the expression
3
+ -- as needed for the expression to typecheck and produce the
4
+ -- expected result.
5
+
6
+ -- 1. a = (+1) $ read "[1]" :: [Int]
7
+ a :: [Int ]
8
+ a = fmap (+ 1 ) $ (read " [1]" :: [Int ])
9
+
10
+ -- 2. b = (++ "lol") (Just ["Hi,", "Hello"])
11
+ -- Expected value of b is Just ["Hi,lol", "Hellolol"]
12
+ b :: Maybe [ String ]
13
+ b = (fmap . fmap ) (++ " lol" ) (Just [" Hi" , " Hello" ])
14
+
15
+ -- 3. c = (*2) (\x -> x - 2)
16
+ -- Preluce> c 1
17
+ -- -2
18
+ -- Solution Note - This one is tricky since the fmap is happening over the function functor (yes, no typo there)
19
+ c :: Integer -> Integer
20
+ c = fmap (* 2 ) (\ x -> x - 2 )
21
+
22
+ -- 4. d = ((return "1" ++) . show) (\x -> [x, 1..3])
23
+ -- Prelude> d 0
24
+ -- 1[0, 1, 2, 3]
25
+ d :: Integer -> String
26
+ d = fmap ((" 1" ++ ) . show ) (\ x -> [x, 1 .. 3 ])
27
+
28
+ -- 5. e :: IO Integer
29
+ -- e = let ioi = readIO "1" :: IO Integer
30
+ -- changed = read ("123" ++) show ioi
31
+ -- in (*3) changed
32
+ -- Prelude> e
33
+ -- 3693
34
+ e :: IO Integer
35
+ e = let ioi = (readIO " 1" :: IO Integer )
36
+ changed = fmap (read . (" 123" ++ ) . show ) ioi
37
+ in fmap (* 3 ) changed
38
+
39
+ main :: IO Integer
40
+ main = do
41
+ print a
42
+ print b
43
+ print (c 1 )
44
+ print (d 0 )
45
+ e
You can’t perform that action at this time.
0 commit comments