Skip to content

Instantly share code, notes, and snippets.

@mitsuji
Created January 10, 2016 08:10
Show Gist options
  • Save mitsuji/a4e077fdf2a05e2d7a73 to your computer and use it in GitHub Desktop.
Save mitsuji/a4e077fdf2a05e2d7a73 to your computer and use it in GitHub Desktop.
type MemberId = Int
type ItemId = Int
data Sex = Man | Woman deriving (Show,Eq)
data Member = Member {
memberId :: MemberId
,memberName :: String
,memberSex :: Sex
} deriving (Show)
type MemberDB = [Member]
membersBySex :: MemberDB -> Sex -> [Member]
membersBySex db sex = filter (\m -> memberSex m == sex) db
data Item = Item {
itemId :: ItemId
,itemName :: String
} deriving (Show)
type MemberItemDB = [(MemberId,Item)]
itemsByMember :: MemberItemDB -> MemberId -> [Item]
itemsByMember db memberId =
let items = filter (\(id,_) -> id == memberId) db
in map snd items
itemsBySex :: MemberDB -> MemberItemDB -> Sex -> [String]
itemsBySex mdb idb sex =
concat $ map (\member->
map (\item ->
(memberName member) ++ ":" ++ (itemName item)
) $ itemsByMember idb $ memberId member
) $ membersBySex mdb sex
itemsBySexM :: MemberDB -> MemberItemDB -> Sex -> [String]
itemsBySexM mdb idb sex = do
member <- membersBySex mdb sex
item <- itemsByMember idb $ memberId member
return $ (memberName member) ++ ":" ++ (itemName item)
main = do
print $ itemsBySex
[
Member 101 "1st" Man,
Member 102 "2nd" Woman,
Member 103 "3rd" Man,
Member 104 "4th" Woman
]
[
(101, Item 1001 "item1001"),
(101, Item 1002 "item1002"),
(101, Item 1003 "item1003"),
(102, Item 1004 "item1004"),
(102, Item 1005 "item1005"),
(102, Item 1006 "item1006"),
(103, Item 1007 "item1007"),
(103, Item 1008 "item1008"),
(103, Item 1009 "item1009"),
(104, Item 1010 "item1010"),
(104, Item 1011 "item1011"),
(104, Item 1012 "item1012")
]
Man
print $ itemsBySexM
[
Member 101 "1st" Man,
Member 102 "2nd" Woman,
Member 103 "3rd" Man,
Member 104 "4th" Woman
]
[
(101, Item 1001 "item1001"),
(101, Item 1002 "item1002"),
(101, Item 1003 "item1003"),
(102, Item 1004 "item1004"),
(102, Item 1005 "item1005"),
(102, Item 1006 "item1006"),
(103, Item 1007 "item1007"),
(103, Item 1008 "item1008"),
(103, Item 1009 "item1009"),
(104, Item 1010 "item1010"),
(104, Item 1011 "item1011"),
(104, Item 1012 "item1012")
]
Man
-- print $ membersBySex [
-- Member 101 "1st" Man,
-- Member 102 "2nd" Woman,
-- Member 103 "3rd" Man,
-- Member 104 "4th" Woman
-- ] Man
--
-- print $ itemsByMember [
-- (101, Item 1001 "item1001"),
-- (101, Item 1002 "item1002"),
-- (101, Item 1003 "item1003"),
-- (102, Item 1004 "item1004"),
-- (102, Item 1005 "item1005"),
-- (102, Item 1006 "item1006"),
-- (103, Item 1007 "item1007"),
-- (103, Item 1008 "item1008"),
-- (103, Item 1009 "item1009"),
-- (104, Item 1010 "item1010"),
-- (104, Item 1011 "item1011"),
-- (104, Item 1012 "item1012")
-- ] 101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment