Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finish the race_ function?

Tags:

haskell

I want to finish the race_ function but I don't know how to do that.

This is the code I have right now:

mainLoop :: Handle -> String -> IO ()
mainLoop handle username = do
    _ <- race_ fromServer toServer

    putStrLn "Race is done"
    return ()

    where
        fromServer = forever $ do
            line <- hGetLine handle
            putStrLn $ "Server: " ++ line

        toServer = do
            line <- getLine
            case line of
                ":quit" -> do
                    --hPutStrLn handle "blabla"
                    return ()
                _       -> do
                    hPutStrLn handle $ line
                    toServer

If I uncomment the line hPutStrLn handle "blabla" the user will see Race is done when he types :quit. Without that line the program just hang when the user types :quit.

I feels wrong to use hPutStrLn handle "blabla" just to finish the race_. I assume that there exists a better function to finish the race_ function. Any idea?

I find it strange that a simple return () doesn't work since both hPutStrLn and return returns an empty IO action.

Full context can be found here. The handle is created by handle <- connectTo "localhost" (PortNumber 4000). The server is created in server.hs. I'm running this code on Windows 7 in "Git bash" (msysgit) with runhaskell.

like image 392
Sawny Avatar asked Dec 13 '25 22:12

Sawny


1 Answers

Used in context

import System.IO
import Control.Concurrent.Async
import Control.Monad

main = do
    h <- openFile "./a" ReadWriteMode
    mainLoop h "user"
    hClose h

this works fine also without hPutStrLn - what did you do? (where ./a is a large file, like "dmesg > a")

like image 58
sdx23 Avatar answered Dec 16 '25 02:12

sdx23