-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtutorial6.hs
95 lines (63 loc) · 2.29 KB
/
tutorial6.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
92
93
94
95
-- Informatics 1 Functional Programming
-- Tutorial 6
--
-- Due: 12/13 November
import System.Random
-- Importing the keymap module
import KeymapTree
-- Type declarations
type Barcode = String
type Product = String
type Unit = String
type Item = (Product,Unit)
type Catalogue = Keymap Barcode Item
-- A little test catalog
testDB :: Catalogue
testDB = fromList [
("0265090316581", ("The Macannihav'nmor Highland Single Malt", "75ml bottle")),
("0903900739533", ("Bagpipes of Glory", "6-CD Box")),
("9780201342758", ("Thompson - \"Haskell: The Craft of Functional Programming\"", "Book")),
("0042400212509", ("Universal deep-frying pan", "pc"))
]
-- Exercise 1
longestProductLen :: [(Barcode, Item)] -> Int
longestProductLen cat = maximum [length y | (x,(y,z)) <- cat]
formatLine :: Int -> (Barcode, Item) -> String
formatLine len (code, (prod, unit)) = code ++ "..." ++ prod ++ (replicate (len - length prod + 3) '.') ++ unit
showCatalogue :: Catalogue -> String
showCatalogue cat = foldr (++) "" [formatLine (longestProductLen (toList cat)) list ++ "\n"| list <- (toList cat)]
-- Exercise 2
maybeToList :: Maybe a -> [a]
maybeToList (Just a) = [a]
maybeToList Nothing = []
listToMaybe :: [a] -> Maybe a
listToMaybe [] = Nothing
listToMaybe xs = Just (head xs)
catMaybes :: [Maybe a] -> [a]
catMaybes [] = []
catMaybes (x:xs) = maybeToList x ++ catMaybes xs
-- Exercise 3
getItems :: [Barcode] -> Catalogue -> [Item]
getItems barcodes cat = catMaybes list
where
list = [get barcode cat | barcode <- barcodes]
-- Input-output ------------------------------------------
readDB :: IO Catalogue
readDB = do dbl <- readFile "database.csv"
let db = fromList (map readLine $ lines dbl)
putStrLn (size db >= 0 `seq` "Done")
return db
readLine :: String -> (Barcode,Item)
readLine str = (a,(c,b))
where
(a,str2) = splitUpon ',' str
(b,c) = splitUpon ',' str2
splitUpon :: Char -> String -> (String,String)
splitUpon _ "" = ("","")
splitUpon c (x:xs) | x == c = ("",xs)
| otherwise = (x:ys,zs)
where
(ys,zs) = splitUpon c xs
getSample :: Catalogue -> IO Barcode
getSample db = do g <- newStdGen
return $ fst $ toList db !! fst (randomR (0,size db - 1) g)