-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter_12.hs
More file actions
75 lines (46 loc) · 1.66 KB
/
Copy pathChapter_12.hs
File metadata and controls
75 lines (46 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE DeriveGeneric #-}
module Chapter_12 where
import Prelude hiding (Maybe, Nothing, Just)
import GHC.Generics
-- Exercise 12.WarmUp1
-- Consider following type:
data Maybe a = Nothing | Just a
deriving (Eq, Ord, Show, Read)
-- Provide instance definitions to make this type into instances of the Functor, Applicative and Monad classes.
-- Here are some examples of how these instance definitions can be used:
-- >>> fmap (+1) Nothing
-- Nothing
-- >>> fmap (+1) (Just 1)
-- Just 2
-- >>> pure (+1) <*> Just 1
-- Just 2
-- >>> pure (+) <*> Just 1 <*> Just 3
-- Just 4
-- >>> pure (+) <*> Nothing <*> Just 2
-- Nothing
-- >>> safediv n m = if (m == 0) then Nothing else (Just (div n m))
-- >>> do {n <- pure 10; m <- pure 2; safediv n m}
-- Just 5
-- >>> safediv n m = if (m == 0) then Nothing else (Just (div n m))
-- >>> do {n <- pure 10; m <- pure 0; safediv n m}
-- Nothing
instance Functor Maybe where
-- fmap :: ???
fmap = undefined
instance Applicative Maybe where
-- pure :: ???
pure = undefined
-- (<*>) :: ???
(<*>) = undefined
instance Monad Maybe where
-- (>>=) :: ???
(>>=) = undefined
-- Exercise 12.1 (*)
-- Define an instance of the Functor class for the following type of binary trees that have data in their nodes:
data Tree a = Leaf | Node (Tree a) a (Tree a)
deriving (Read, Show, Eq, Generic)
instance Functor Tree where
-- fmap :: ???
fmap = undefined
-- Note: Just in case you are wondering, this type of tree does not have an appropriate instance of the Applicative or Monar class. Take a look at https://dkalemis.wordpress.com/2014/03/22/trees-as-monads/ for an explanation.