程序代写代做代考 Haskell algorithm – Voting algorithm example from chapter 7 of Programming in Haskell, – cscodehelp代写
— Voting algorithm example from chapter 7 of Programming in Haskell,
— , Cambridge University Press, 2016.
import Data.List
— First past the post
votes :: [String]
votes = [“Red”, “Blue”, “Green”, “Blue”, “Blue”, “Red”]
count :: Eq a => a -> [a] -> Int
count x = length . filter (== x)
rmdups :: Eq a => [a] -> [a]
rmdups [] = []
rmdups (x:xs) = x : filter (/= x) (rmdups xs)
result :: Ord a => [a] -> [(Int, a)]
result vs = sort [(count v vs, v) | v <- rmdups vs]
winner :: Ord a => [a] -> a
winner = snd . last . result
— Alternative vote
ballots :: [[String]]
ballots = [[“Red”,”Green”],
[“Blue”],
[“Green”,”Red”,”Blue”],
[“Blue”,”Green”,”Red”],
[“Green”]]
rmempty :: Eq a => [[a]] -> [[a]]
rmempty = filter (/= [])
elim :: Eq a => a -> [[a]] -> [[a]]
elim x = map (filter (/= x))
rank :: Ord a => [[a]] -> [a]
rank = map snd . result . map head
winner’ :: Ord a => [[a]] -> a
winner’ bs = case rank (rmempty bs) of
[c] -> c
(c:cs) -> winner’ (elim c bs)