-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththe_winner.hs
More file actions
39 lines (29 loc) · 847 Bytes
/
the_winner.hs
File metadata and controls
39 lines (29 loc) · 847 Bytes
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
import Data.List (sort)
import Data.Set
type Party = String
type Ballot = [Party]
count :: (Eq a) => a -> [a] -> Int
count _ [] = 0
count a (x:xs)
| a == x = 1 + count a xs
| otherwise = count a xs
test1 = count 'a' "ababca"
--rmdups :: (Eq a) -> [a] -> [a]
rmdups [] = []
rmdups (x:xs)
| x `elem` xs = rmdups xs
| otherwise = x : rmdups xs
test2 = rmdups "ababca"
setRemove :: (Ord a) => [a] -> Set a -> [a]
setRemove [] _ = []
setRemove (x:xs) sofar
| x `member` sofar = setRemove xs sofar
| otherwise = x : (setRemove xs (insert x sofar))
rmdups' :: (Eq a) -> [a] -> [a]
rmdups' xs = setRemove xs empty
test3 = rmdups' "ababca"
frequency :: (Eq a) => [a] -> [(Int, a)]
frequency xs = [(count x xs, x) | x <- rmdups' xs]
test4 = frequency "ababca"
--TODO finish
--http://www.cs.nott.ac.uk/~gmh/fun-cwk2.pdf