— A Vote is a list of Country, Vote Count pairs.
— A Vote will be comparable to a dictionary.
type Vote = [(String, Integer)]
— In the European talent competition, EuroVision,
— Countries are voted upon using weighted votes.
— Voters might submit sets of votes of votes that
— appear like the following:
sub1 :: Vote sub1 = [("Lithuainia", 12), ("Spain", 10), ("Finland", 8), ("Lichtenstein", 7), ("Greece",6), ("Serbia",5), ("Cyprus",4), ("Poland",3), ("Malta",2), ("Germany",1)] sub2 :: Vote sub2 = [("Portugal", 12), ("Finland", 10), ("Greece", 8), ("Sweden", 7), ("Albania",6), ("Cyprus",5), ("Malta",4), ("Poland",3), ("Spain",2), ("Bulgaria",1)] sub3 :: Vote sub3 = [("Belgium", 12), ("Poland", 10), ("Portugal", 8), ("Lithuainia", 7), ("Netherlands",6), ("Iceland",5), ("Spain",4), ("Greece",3), ("Germany",2), ("Bulgaria",1)] sub4 :: Vote sub4 = [("Lithuainia", 12), ("Malta", 10), ("Germany", 8), ("Iceland", 7), ("Lichtenstein",6), ("Poland",5), ("Portugal",4), ("Cyprus",3), ("Malta",2), ("Serbia",1)] sub5 :: Vote sub5 = [("Iceland", 12), ("Germany", 10), ("Lithuainia", 8), ("Sweden", 7), ("Lichtenstein",6), ("Cyprus",5), ("Spain",4), ("Poland",3), ("Albania",2), ("Greece",1)] sub6 :: Vote sub6 = [("Poland", 12), ("Bulgaria", 10), ("Portugal", 8), ("Malta", 7), ("Germany",6), ("Iceland",5), ("Albania",4), ("Greece",3), ("Cyprus",2), ("Sweden",1)] ex1 :: [Vote] ex1 = [sub1] ++ [sub2] ++ [sub3] ++ [sub4] ++ [sub5] ++ [sub6]
— We will build up a set of functions to perform useful
— operations on Lists of Votes.
— 1. For a vote, return the vote count for Country, C.
-- c lkupCtry :: Vote -> String -> Vote lkupCtry v c =
— Then, write a function that given a vote
— and a country’s name, it returns just the
— vote count of that country. Return 0 if the country is not present.
— Use lkupCtry to help.
voteCount :: Vote -> String -> Integer voteCount v c = undefined
— 2. Calculate the total vote values for a list of Votes
-- First, write a function that adds a tuple of a -- Country and its Vote Count to a Vote. This should -- append a country if it is not in the list, and it should -- add a country's vote count if it is already present -- -- ex: -- INPUT: combine [("Germany", 5), ("Albania", 3)] ("Germany", 2) -- OUTPUT: [("Germany", 7), ("Albania", 3)] -- INPUT: combine [("Germany", 5), ("Albania", 3)] ("France", 2) -- OUTPUT: [("Germany", 5), ("Albania", 3), ("France", 2)]
combine :: Vote -> (String, Integer) -> Vote combine vote v@(c,i) = undefined
-- Secondly, write voteCombine, which which combines two -- Votes into one, using combine. -- ex: -- INPUT: voteCombine [("Germany", 5), ("Albania", 3), ("France", 2)] [("Germany", 5), ("Albania", 3), ("France", 2)] -- OUTPUT: [("Germany", 10), ("Albania", 6), ("France", 4)] -- INPUT: voteCombine [("Germany", 5), ("Albania", 3), ("France", 2)] [("Germany", 5), ("Belgium", 3), ("Malta", 2)] -- OUTPUT: [("Germany", 10), ("Albania", 3), ("France", 2), ("Belgium", 3), ("Malta", 2)] voteCombine :: Vote -> Vote -> Vote voteCombine v1 (v2:v2s) = undefined
— Now, use foldr() to create a function that takes a list of
— Votes and creates one vote, with the values summed.
—
— Remember, foldr does the following:
— foldr < function f > < data d > < list of data ds >
— applies f(ds[i-1], f(ds[i-2], …..f(ds[1], f(ds[0], d)))
— ex:
— INPUT: totalVote [ [(“Germany”,2), (“France”,1)],
— [(“Germany”,2), (“France”,1)],
— [(“Germany”,2), (“France”,1)] ]
— OUTPUT: [(“Germany”, 6), (“France”, 3)]
totalVote :: [Vote] -> Vote totalVote (v:vs) = undefined -- foldr (voteCombine) ???? ?????
— 3. From a list of Votes, sort the total Vote such that the first
— Country in the list is the Country with the most votes
—
— Hint: QuickSort is easily implemented in functional languages
— thanks to List Comprehensions and Anonymous Functions and filters
sortVote :: Vote -> Vote sortVote vs = undefined
— 4. Write a function that takes a list of votes, vs,
— and an integer, n, and returns the countries with
— the n most votes in descending order.
—
— First, write a function that returns the first N
— (Country, Vote Count) pairs of a Vote
天才代写 CS代写 CS作业代写 haskell代写
takeVotes :: Integer -> Vote -> Vote takeVotes n vs = undefined
-- Then, use totalVote, sortVote and takeVotes to implement -- bestCountries, which returns the n most voted-for -- Countries and their Vote Countes. -- You can use the operator "$" to string functions together. -- For example, -- sqrt(abs(x)) = sqrt $ abs x -- -- ex : -- INPUT: bestCountries [("Germany", 5), ("Albania", 3), ("France", 4), ("Belgium", 2), ("Malta", 2)] 3 -- OUTPUT: [("Germany", 5), ("France", 4), ("Albania", 3)] bestCountries :: [Vote] -> Integer -> Vote bestCountries votes n = undefined