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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:24:25+00:00 2026-05-30T23:24:25+00:00

I want to receive a string, convert it to a list and write every

  • 0

I want to receive a string, convert it to a list and write every element of the list into a TChan. For every string received I want to fork a new process.

My problem is that I get errors related to IO when I try to compile this:

Couldn't match expected type `IO ()' with actual type `[()]'

or

Couldn't match expected type `IO ()' with actual type `[IO ()]'

Whilst I completely understand the error and know where it originates from ( – at least the first error – ) I am now out of ideas how I could split IO an computation in my example and still achieve what I try to do.

write2TChan msg mtch = do
        let mymessages = words msg
        map (\x -> atomically $ writeTChan mtch x) mymessages
        return ()

main = withSocketsDo $ do
        s <- socket AF_INET Datagram defaultProtocol
        bindAddr <- inet_addr host
        bindSocket s (SockAddrInet port bindAddr)
        mtch <- newTChanIO
        let forever socket hosts = do
                (msg, host) <- receiveMessage socket
                return ()
                return (forkIO $ write2TChan msg mtch)
                --forkIO $ write2TChan msg mtch
                --tried w return () and above, same problem
                forever socket hosts
        forever s []
        sClose s
  • 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-30T23:24:26+00:00Added an answer on May 30, 2026 at 11:24 pm

    map has the type map :: (a -> b) -> [a] -> [b], so it always returns a list. However, every statement in a do block in the IO monad must have type IO a for some a.

    You want the related function mapM :: Monad m => (a -> m b) -> [a] -> m [b] which does the same thing in a monad. Since you want to ignore the result, we can use the variant mapM_ :: Monad m => (a -> m b) -> [a] -> m ().

    write2TChan msg mtch = do
        let mymessages = words msg
        mapM_ (\x -> atomically $ writeTChan mtch x) mymessages
    

    For your other function you should be able to use forkIO directly. Wrapping it in return makes no sense at all, as that would make the type IO (IO a), i.e. an action returning an action. Also, return () is a no-op when it’s not the last statement in a do block, so you don’t need that.

    This should work

    let forever socket hosts = do
             (msg, host) <- receiveMessage socket
             forkIO $ write2TChan msg mtch
             forever socket hosts
    

    though, the hosts argument is currently always [], so unless you plan to do something about that, you can get rid of that, too. Also, I would avoid the name forever since there is already a commonly used function in Control.Monad with that name.

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

Sidebar

Related Questions

I receive an html encoded string from server code and I want to convert
I want to list messages that received specific user from other users group by
I've received a new computer from my employer. This time I don't want to
I'm trying to convert a flat file into an Excel-readable format. We receive the
What's the best library to use to convert the HTTP POST string received from
I want to save a file with name Привет Мир.jpg I receive a string
I have an NSString which I want to convert into an NSDecimalNumber . The
I want a simple function that receives a string and returns an array of
I want to receive the following HTTP request in PHP: Content-type: multipart/form-data;boundary=main_boundary --main_boundary Content-type:
I want users to receive 'points' for completing various tasks in my application -

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.