-
Notifications
You must be signed in to change notification settings - Fork 0
/
24dataMap.hs
67 lines (43 loc) · 2.24 KB
/
24dataMap.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
import qualified Data.Map as Map
-- we qualify the import above because Data.Map shares some functions with Prelude
-- now, every function we use from Data.Map must be prefaced with Map
-- Example from the book 'Will Kurt Get Programming in Haskell'
-- let us create a new data type, Organ:
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 = zip ids organs
organMap = Map.fromList organPairs
{-
Q18.2
Modify the Organ type so that it can be used as a key.
Then build a Map, organ-Inventory, of each organ to its count in the organCatalog.
-}
data OrganV2 = HeartX | BrainX | KidneyX | SpleenX deriving (Show, Eq, Ord)
myOrgans :: [OrganV2]
myOrgans = [HeartX,HeartX, HeartX, BrainX, KidneyX, SpleenX, SpleenX]
-- construct a OrganV2 list and Count list
getCount :: [OrganV2] -> OrganV2 -> Int
getCount myOrgans organ = foldl (+) 0 flipCorrectOrganOn
where flipCorrectOrganOn = map (checkOrganExistence organ) myOrgans
checkOrganExistence organ x = if x == organ then 1
else 0
getUniqueOrgans :: [OrganV2] -> [OrganV2]
getUniqueOrgans [] = []
getUniqueOrgans (firstOrgan:givenOrgans) = if elem firstOrgan givenOrgans then getUniqueOrgans(givenOrgans)
else firstOrgan : getUniqueOrgans(givenOrgans)
getUniqueOrganCountPairs :: [OrganV2] -> [(OrganV2,Int)]
getUniqueOrganCountPairs givenList = map (getUniqueOrganCount ) uniqueList
where uniqueList = getUniqueOrgans(givenList)
getUniqueOrganCount organ = (organ,countOfOrgan)
where countOfOrgan = getCount myOrgans organ
myOrgansUniqueCount = getUniqueOrganCountPairs myOrgans
myOrgansUniqueCountMap = Map.fromList myOrgansUniqueCount
getCountOfHeartX = Map.lookup HeartX myOrgansUniqueCountMap
{-
*Main> getCountOfHeartX
Just 3
-}
-- Works!