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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:27:14+00:00 2026-06-15T05:27:14+00:00

I have a question about how to retrieve the minimum value of a Map

  • 0

I have a question about how to retrieve the minimum value of a Map.

I have a Map k (Maybe a,Maybe b) and I need to retrieve the key with the smallest value on the first element of the tuple (the smallest a ), but I couldn’t find any predefined function. Is there some function I can use or I should implement that by myself?
Thanks

  • 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-15T05:27:15+00:00Added an answer on June 15, 2026 at 5:27 am

    You’ll need the constraint Ord a. Also, you need to decide how to compare Just x and Nothing (because you’ll have to compare (Just x, y) with (Nothing, z)). I’ll assume you want Just x to be smaller than Nothing for all x. If you use toList to covert the map to a list, you can then use minimumBy on the list with a custom comparison function. Moreover, the second part of the tuple (Maybe b) is irrelevant, so we’ll just call consider Map k (Maybe a, b) and get a more general function.

    I suggest something like

    import qualified Data.Map as M
    import Data.List
    
    minimum' :: (Ord a) => M.Map k (Maybe a, b) -> k
    minimum' = fst . (minimumBy comp) . M.toList
        where
          comp (_, (Nothing, _)) (_, (Just _, _)) = GT
          comp (_, (Just _, _)) (_, (Nothing, _)) = LT
          comp (_, (Nothing, _)) (_, (Nothing, _)) = EQ
          comp (_, (Just x, _)) (_, (Just y, _)) = compare x y
    

    It’s hard to tell what kind of behavior you actually want with regards to the Nothings, but another, more reasonable(*), alternative would be for the minimum function to have return type Maybe k, with Nothing returned in case the smallest element found (as above) is (Nothing, _). One way to achieve this is to first filter the list returned by toList to eliminate all elements whose values are (Nothing, _).

    (*) Letting Just x be smaller than Nothing is kinda bad. You’ll want the opposite behavior when looking for the largest element, so it all gets rather inconsistent and ugly. In other words, when the type a is ordered, the type Maybe a is only partially ordered (in a natural way — of course you can force a total order, as above).

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

Sidebar

Related Questions

Have a question about stored procedure: How to retrieve the variable's value from stored
I have a question about Javascript's dictionary. I have a dictionary in which key-value
I have little question about how web browser retrieve webpage? I know this User
I have question about normalization. Suppose I have an applications dealing with songs. First
I have asked a question about how to retrieve data from different tables in
I have a question about using session, or maybe TempData to store data while
I'm new to Sitecore. I have a question about saving a value for a
the title might be too generic, but I have a very specific question about
I have got a question about http:request(...) in erlang. When I retrieve a page
I have a question about DataTable . I retrieve a DataTable from the database,

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.