-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaybe.hs
80 lines (52 loc) · 2.14 KB
/
maybe.hs
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
73
74
75
76
77
78
79
80
-- INTRODUCING MAYBE: SOLVING MISSING VALUES WITH TYPES
import qualified Data.Map as Map
-- qualified statement lets you give the module you're importing a name so it doesn't
-- conflist with existing functions
data Organ = Heart | Brain | Kidney | Spleen deriving (Show, Eq)
organs :: [Organ]
organs = [Heart,Heart,Brain,Spleen,Spleen,Kidney]
ids :: [Int]
ids = [2,7,13,14,21,24]
organPairs :: [(Int,Organ)]
organPairs = zip ids organs
organCatalog :: Map.Map Int Organ
organCatalog = Map.fromList organPairs
-- GHCi> Map.lookup 7 organCatalog
-- Just Heart
-- Map.lookup :: Ord k => k -> Map.Map k a -> Maybe a
possibleDrawers :: [Int]
possibleDrawers = [1 .. 50]
-- Next you need a function to get the contents of each drawer.
-- The following maps this list of possible drawers with the lookup function.
getDrawerContents :: [Int] -> Map.Map Int Organ -> [Maybe Organ]
getDrawerContents ids catalog = map getContents ids
where getContents = \id -> Map.lookup id catalog
availableOrgans :: [Maybe Organ]
availableOrgans = getDrawerContents possibleDrawers organCatalog
countOrgan :: Organ -> [Maybe Organ] -> Int
countOrgan organ available = length (filter
(\x -> x == Just organ)
available)
-- GHCi> countOrgan Brain availableOrgans
-- 1
-- GHCi> countOrgan Heart availableOrgans
-- 2
isSomething :: Maybe Organ -> Bool
isSomething Nothing = False
isSomething (Just _) = True
justTheOrgans :: [Maybe Organ]
justTheOrgans = filter isSomething availableOrgans
-- GHCi>justTheOrgans
-- [Just Heart,Just Heart,Just Brain,Just Spleen,Just Spleen,Just Kidney]
isJust and isNothing
-- The Data.Maybe module contains two functions, isJust and isNothing, that solve the general case of handling Just values.
-- isJust is identical to the isSomething function but works on all Maybe types.
-- With Data.Maybe imported, you could’ve solved this problem as follows:
justTheOrgans' = filter isJust availableOrgans
showOrgan :: Maybe Organ -> String
showOrgan (Just organ) = show organ
showOrgan Nothing = ""
-- GHCi> showOrgan (Just Heart)
-- "Heart"
-- GHCi> showOrgan Nothing
-- ""