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

  • Home
  • SEARCH
  • 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 8398691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:06:52+00:00 2026-06-09T21:06:52+00:00

I have the following function: sendq :: Socket -> B.ByteString -> String -> IO

  • 0

I have the following function:

sendq :: Socket -> B.ByteString -> String -> IO PortNumber -> IO ()
sendq s datastring host port = do
     hostAddr <- inet_addr host
     sendAllTo s datastring (SockAddrInet port hostAddr)

whereas sendAllTo has the function signature

sendAllTo :: Socket -> ByteString -> SockAddr -> IO ()

The problem is the from previous functions I get handed down an IO PortNumber where SockAddr takes a PortNumber only. I tried to make these two compatible by promoting sendAllTo into the IO monad:

liftM sendAllTo s datastring (SockAddrInet port hostAddr)

but no joy. Tells me something about to many arguments. Is this a case for liftM? How do I apply it correctly?

  • 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-09T21:06:53+00:00Added an answer on June 9, 2026 at 9:06 pm

    liftM and friends serve the purpose of jacking up pure functions to a monadic setting, much as the Applicative combinators do.

    liftM :: Monad m => (s -> t) -> m s -> m t
    

    There are two issues with the code you tried. One is just a lack of parentheses.

    liftM sendAllTo :: IO Socket -> IO (ByteString -> SockAddr -> IO ())
    

    which is not what you meant. The other issue is that

    sendAllTo :: Socket -> ByteString -> SockAddr -> IO ()
    

    is a monadic operation, so lifting it will deliver two layers of IO. The usual method is to parenthesize the pure prefix of the application, like so

    liftM (sendAllTo s datastring) :: IO SockAddr -> IO (IO ())
    

    You can then build the argument with liftM2.

    liftM2 SockAddrInet ioport (inet_adder host) :: IO SockAddr
    

    That gives you

    liftM (sendAllTo s datastring) (liftM2 SockAddrInet ioport (inet_adder host))
      :: IO (IO ())
    

    which will achieve precisely nothing as it stands, because it explains how to compute an operation but doesn’t actually invoke it! That’s where you need

    join (liftM (sendAllTo s datastring) (liftM2 SockAddrInet ioport (inet_addr host)))
      :: IO ()
    

    or, more compactly

    sendAllTo s datastring =<< liftM2 SockAddrInet ioport (inet_adder host)
    

    Plug. The Strathclyde Haskell Enhancement supports idiom brackets, where

    (|f a1 .. an|) :: m t if f :: s1 -> ... -> sn -> t and a1 :: m s1 … an :: m sn.

    These do the same job for Applicative m as the liftM family do for monads, treating f as a pure n-ary function and a1..an as effectful arguments. Monads can and should be Applicative too, so

    (|SockAddrInet ioprot (inet_addr host)|) :: IO SockAddr
    

    and

    (|(sendAllTo s datastring) (|SockAddrInet ioprot (inet_addr host)|)|) :: IO (IO ())
    

    The notation then allows you to invoke computed monadic computations like the above, with a postfixed @.

    (|(sendAllTo s datastring) (|SockAddrInet ioprot (inet_addr host)|) @|) :: IO ()
    

    Note that I’m still parenthesizing the pure prefix of the application, so that the f of the template is the whole of (sendAllTo s datastring). The notation allows you to mark pure arguments in any position with a ~, so you can write this

    (|sendAllTo ~s ~datastring (|SockAddrInet ioprot (inet_addr host)|) @|) :: IO ()
    

    if the mood takes you.

    Rant. We spend far too much energy on figuring out the right liftM, join, =<<, do, (|...~...@|) punctuation in order to explain how to cut up a type as a value-explaining kernel (() here) in an effect-explaining context (IO here). If this up-cutting were made more explicitly in types, we should need less noise in our programs to slap values and computations into alignment. I should much prefer the computer to infer where the ~ and @ marks go, but as things stand, Haskell types are too ambiguous a document to make that feasible.

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

Sidebar

Related Questions

I have the following function using Excel 2010: Private Function MakeAllSheetsValuesOnly(targetBookName As String) If
I have following code to send files to a FTP server. function FtpUploader( [string]$uri,
I have the following code in an HTML file: socket.on('message',function(data) { console.log('Received a message
I have the following function which send packets over raw socket. #include <unistd.h> #include
I have following function in one Activity public void AppExit() { Editor edit =
In my MVC application in Controller i have following function to add and focus
have the following function on my collection : getFiltered: function (status, city) { return
I have the following function set up to sequentially fade in divs (with class
I have the following function to remove a DOM element div, $('#emDiv').on(click, ':button[data-emp-del=true]', function
I have the following function //simple function with parameters and variable function thirdfunction(a,b,c,d){ console.log(the

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.