-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookshelf.hs
46 lines (38 loc) · 1.31 KB
/
bookshelf.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
type BookName = String
type BookId = Int
type BookAuthor = String
type BookAuthors = [BookAuthor]
--data BookInfo = Book BookId BookName BookAuthors
-- | AnonimousBook BookId BookName
-- deriving(Eq, Show)
--bookId :: BookInfo -> BookId
--bookId (Book id _ _) = id
--bookId (AnonimousBook id _) = id
data BookInfo = Book {
bookId :: BookId,
bookName :: BookName,
bookAuthors :: BookAuthors }
| AnonymousBook {
bookId :: BookId,
bookName :: BookName }
deriving(Eq, Show)
--weird
sameId :: BookInfo -> BookInfo -> Bool
sameId (Book id1 _ _) (Book id2 _ _) = id1 == id2
sameId (Book id1 _ _) (AnonymousBook id2 _) = id1 == id2
sameId (AnonymousBook id1 _) (Book id2 _ _) = id1 == id2
sameId (AnonymousBook id1 _) (AnonymousBook id2 _) = id1 == id2
--much much better
sameId2 :: BookInfo -> BookInfo -> Bool
sameId2 a b = bookId(a) == bookId(b)
sameAuthors :: BookInfo -> BookInfo -> Bool
sameAuthors (AnonymousBook _ _) _ = undefined
sameAuthors _ (AnonymousBook _ _) = undefined
sameAuthors a b = bookAuthors(a) == bookAuthors(b)
hasAuthor :: BookAuthor -> BookInfo -> Bool
hasAuthor _ (AnonymousBook _ _) = undefined
hasAuthor author b = isIn (bookAuthors(b)) author
isIn :: (Eq a) => [a] -> a -> Bool
isIn [] _ = False
isIn xs x | head xs == x = True
isIn xs x | head xs /= x = isIn (tail xs) x