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 6235459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:40:19+00:00 2026-05-24T10:40:19+00:00

How can sort a [MVar a] list? using a as the element to compare

  • 0

How can sort a [MVar a] list? using a as the element to compare in the sort. E.g:

sortList :: [MVar Int] -> [MVar Int]

I cannot think a ways without breaking other threads.

Update:
I need to sort the list, because I want to implement a reference count like MVar and return always the one with least references. Something like:

getLeastUsed :: [MVar Int] -> MVar Int
getLeastUsed = head . sortList

And in the thread I want to increase the ‘Int’.

Update:
I was notice by answers that the rigth signature need IO because MVar

  • 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-05-24T10:40:19+00:00Added an answer on May 24, 2026 at 10:40 am

    First of all, your type signature is impossible; reading an MVar is not referentially transparent (as should hopefully be obvious–that’s what they’re for!). This has two consequences:

    • Your sort function must return an IO action
    • The list will be sorted according to the values seen when each MVar was read; not only may it be invalid by the time you use the list, it may change halfway through such that the first value is out of date before you read the last value.

    The former is unavoidable, and assuming the latter is acceptable for your purposes, you can do essentially what @hammar demonstrated.

    However, given that the sorting will be quickly out of date, and that you seem to be mostly interested in the least element, you might find something like this more directly useful since there’s little use to sorting otherwise:

    import Control.Applicative
    import Data.List
    import Data.Ord
    
    leastUsed :: [MVar Int] -> IO (MVar Int)
    leastUsed vars = fst . minimumBy (comparing snd) . zip vars <$> mapM readMVar vars
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can sort a list using Sort or OrderBy. Which one is faster? Are
I can sort a int* array using stl, plain and simple like std::sort(myarray, myarray
I have a list of fields which a user can sort using jquery. I
I'm using jquery ui to process a list that you can sort within and
Can I sort a list of objects by a property of an associated object?
It seems that I can sort a std::vector<std::pair<int, std::string>> , and it will sort
I have a NSTableView with 3 columns. I can sort using any of the
I'm using a standard GridView with a LinqDataSource. The user can sort and page
I using a genric list(m_equipmentList ) which is collection of objects (Schedule_Payitem). How can
I'm using JSF and I have a complex datatable where the user can sort

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.