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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:11:01+00:00 2026-05-30T03:11:01+00:00

If m2 and m are equal I want to do an IO operation (send

  • 0

If m2 and m are equal I want to do an IO operation (send out some stuff over the network) that returns nothing and update my map. I have crafted the following code and wonder (1) if this can be refactored into something less clunky (2) why my case expression that should send out the messages does not get evaluated or at least no messages do get send.

If I uncomment the code at the bottom then things do get send as I want to, but of course it is then send out disregarding what the value of m2 is.

main = withSocketsDo $ do
     s <- socket AF_INET Datagram defaultProtocol
     (q, m) <- newRq
     let m2 = appendMsg "first" key m
         _ = case m2 of        
                   val | m2 == m -> do let Just messages = Map.lookup ("192.168.1.1", 4711) m in sendq s (B.pack $ unwords messages) "192.168.1.1" 4711
         (q4, m4) = case m2 of 
                   val | m2 == m -> deleteRec key q m2
                       | otherwise -> (q, m2)
     --let Just messages = Map.lookup ("192.168.1.1", 4711) m
     --sendq s (B.pack $ unwords messages) "192.168.1.1" 4711

I know that _ is not defined for all cases where m2 and m are not equal, but sendq returns IO () and I could not think of something sensible to add here for | otherwise -> that would return the same type.

  • 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-30T03:11:03+00:00Added an answer on May 30, 2026 at 3:11 am

    The two lines of code starting with _ = case m2 of do exactly nothing! This is because the bound value _ never gets evaluated, because it is not used (or even mentioned) anywhere else.

    What you are looking for is, I guess, the function when from Control.Monad. It has type Monad m => Bool -> m () -> m(); you could use it as follows:

     when (m == m2) (send the messages ...)
    

    but you would have to use it outside the let block, i.e. as part of the main do expression.

    One more thing, your code:

         (q4, m4) = case m2 of 
                   val | m2 == m -> deleteRec key q m2
                       | otherwise -> (q, m2)
    

    can be simplified to:

         (q4, m4) = if (m2 == m) then deleteRec key q m2 else (q, m2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a equal distribution on APPID and want to use HASH
I want to set the listview datacontext equal to an observable collection so that
I want to find all files/dirs that are not equal to .git* , so
I have two button name A, and B want to click them equal to
Let's say I have some code that does some floating point arithmetic and stores
I want to write a function that returns the nearest next power of 2
I have two Maps that contain the same type of Objects: Map<String, TaskJSO> a
I have 3 images E1,E2,E3 of equal size 256*256*3. Now by some any arbitrary
I have a big List that may have some 50,000 or more items and
Suppose that I have a comments form, and I want to prevent bad users

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.