Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7808893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:16:02+00:00 2026-06-02T03:16:02+00:00

I have a high order function which would take: results [Red, Blue, Green, Blue,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:16:04+00:00Added an answer on June 2, 2026 at 3:16 am

    If you have the results function, a simple application of

    maximumBy :: (a -> a -> Ordering) -> [a] -> a
    

    would suffice

    import Data.List -- for maximumBy
    import Data.Ord  -- for comparing
    
    winner = snd . maximumBy (comparing fst) . results
    

    Seeing

    if two colours have the same occurrences it output two the colours

    you need a different type – and of course a different implementation, still easy with library functions, though:

    import Data.List
    import Data.Ord
    import Data.Function
    
    winners :: [Party] -> [Party]
    winners = map snd . head . groupBy ((==) `on` fst) . sortBy (flip $ comparing fst) . results
    

    Since results already 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 use

    winners = map snd . last . groupBy ((==) `on` fst) . results
    

    to produce the list of all most occurring elements. If only one most frequent element is desired, it could be obtained with

    winner = snd . last . results
    

    from the sorted frequency list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cant seem to find any information on a high order function that would
I have a need for a high-performance string hashing function in python that produces
I understand that in order to return a string from a function I have
I have searched high and low for documentation on how to use this feature.
I need somebody with high skills in threading and event raising. I have an
I'm wondering whether/what difference between First Class Function and High Order Function . I
A recent homework assignment I have received asks us to take expressions which could
I have a high-speed ADC data capture/analysis program which performs poorly on older computers.
I have a table that potentially will have high number of inserts per second,
We have a high security application and we want to allow users to enter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.