public
Created

  • Download Gist
majority.dylan
Dylan
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
module: majority
 
define function winner (votes :: <collection>)
let winner
= reduce1(method (a, b) if (a.last > b.last) a else b end end,
map(method (candidate)
pair(candidate, size(choose(curry(\=, candidate), votes)));
end,
remove-duplicates(votes)));
unless (zero?(round/(winner.tail, votes.size)))
winner.head;
end unless;
end function winner;
 
define function print-winner (votes :: <collection>)
let winner = winner(votes);
if (winner)
format-out("The winner is: %s\n", winner);
else
format-out("There's no winner\n");
end;
end function print-winner;
 
define function main()
print-winner(#['a', 'b', 'a', 'b', 'a']);
print-winner(#['a', 'a', 'a', 'c', 'c', 'b', 'b', 'c', 'c', 'c', 'b', 'c', 'c']);
print-winner(#['a', 'b', 'c', 'a', 'b', 'a']);
exit-application(0);
end function main;
 
main();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.