-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson19.hs
91 lines (65 loc) · 2.62 KB
/
lesson19.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
81
82
83
84
85
86
87
88
89
90
91
import qualified Data.Map as Map
import qualified Data.List as Lst
import qualified Data.List as Lst
data Organ = Heart | Brain | Kidney | Spleen deriving (Show , Eq)
organs :: [Organ]
organs = [Heart,Spleen,Brain,Spleen,Kidney]
keys :: [Int]
keys = [2,5,54,44,53]
orgPairs :: [(Int,Organ)]
orgPairs = zip keys organs
organCatelog :: Map.Map Int Organ
organCatelog = Map.fromList orgPairs
possibleDrawers :: [Int]
possibleDrawers = [1..50]
getDrawerContents :: [Int] -> Map.Map Int Organ -> [Maybe Organ]
getDrawerContents newkeys organCatelog = map getContentsOrgans newkeys
where getContentsOrgans x = Map.lookup x organCatelog
availableOrgans :: [Maybe Organ]
availableOrgans = getDrawerContents possibleDrawers organCatelog
countOrgan :: Organ -> [Maybe Organ] -> Int
countOrgan organ available = length ( filter (\x -> x == Just organ) available )
isSomething :: Maybe Organ -> Bool
isSomething Nothing = False
isSomething (Just _) = True
justTheOrgans :: [Maybe Organ]
justTheOrgans = filter isSomething availableOrgans
showOrgans :: Maybe Organ -> String
showOrgans (Just organ) = show organ
showOrgans Nothing = ""
organList :: [String]
organList = map showOrgans justTheOrgans
cleanList :: String
cleanList = Lst.intercalate ", " organList
numOrZero :: Maybe Int -> Int
numOrZero Nothing = 0
numOrZero (Just x) = x
data Container = Vat Organ | Cooler Organ | Bag Organ
instance Show Container where
show (Vat organ) = show organ ++ " in a vat"
show (Cooler organ) = show organ ++ " in a cooler"
show (Bag organ) = show organ ++ " in a bag"
data Location = Lab | Kitchen | Bathroom deriving Show
organToContainer :: Organ -> Container
organToContainer Brain = Vat Brain
organToContainer Heart = Cooler Heart
organToContainer organ = Bag organ
placeInLocation :: Container -> (Location,Container)
placeInLocation (Vat a) = ( Lab , Vat a )
placeInLocation (Cooler a) = ( Lab , Cooler a )
placeInLocation (Bag a) = ( Kitchen , Bag a )
process :: Organ -> (Location,Container)
process organ = placeInLocation ( organToContainer organ )
report :: (Location,Container) -> String
report (location , container) = show container ++ " in the " ++ show location
processRequest :: Int -> Map.Map Int Organ -> String
processRequest id catelog = processAndReport organ
where organ = Map.lookup id catelog
processAndReport :: Maybe Organ -> String
processAndReport (Just organ) = report $ process organ
processAndReport Nothing = "error, id not found"
emptyDrawers :: [Maybe Organ] -> Int
emptyDrawers organs = length $ filter (== Nothing) organs
maybeMap :: (a -> b) -> Maybe a -> Maybe b
maybeMap f (Just v) = Just $ f v
maybeMap _ Nothing = Nothing