I have a high order function which would take:
results ["Red", "Blue", "Green", "Blue", "Blue", "Red"]
And return:
[(1,"Green"),(2,"Red"),(3,"Blue")]
I need to use the results function and create a new function called winner:
winner :: [Party ] -> Party
winner xs =
which would output the most occurred colour and remove first element within tuples, if two colours have the same occurrences it output two the colours, for example:
winner ["Red", "Blue", "Green", "Blue", "Blue", "Red"]
output:
"blue"
so far I’ve tried using snd and tail but I keep getting errors.
Thank you in advance.
If you have the
resultsfunction, a simple application ofwould suffice
Seeing
you need a different type – and of course a different implementation, still easy with library functions, though:
Since
resultsalready produces a sorted list – I didn’t want to rely on that just from the example output – it is not necessary to sort again, and we can useto produce the list of all most occurring elements. If only one most frequent element is desired, it could be obtained with
from the sorted frequency list.