haskell - 如何完成 race_ 功能?

标签 haskell

我想完成 race_ 功能,但我不知道该怎么做。

这是我现在的代码:

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

如果我取消注释行 hPutStrLn handle "blabla"用户将看到 Race is done当他输入 :quit .如果没有这一行,当用户输入 :quit 时,程序就会挂起。 .

我觉得用hPutStrLn handle "blabla"不对只是为了完成 race_ .我假设存在一个更好的函数来完成 race_功能。有什么想法吗?

我觉得一个简单的 return () 很奇怪不起作用,因为 hPutStrLnreturn返回一个空的 IO 操作。

可以找到完整的上下文 here .句柄由 handle <- connectTo "localhost" (PortNumber 4000) 创建.服务器创建于 server.hs .我在 Windows 7 的“Git bash”(msysgit) 中使用 runhaskell 运行此代码.

最佳答案

在上下文中使用

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

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

即使没有 hPutStrLn 也能正常工作 - 你做了什么? (其中 ./a 是一个大文件,如“dmesg > a”)

关于haskell - 如何完成 race_ 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30291109/

相关文章:

haskell - GTK2HS 无法安装最新的 cabal 版本

haskell - 如何直接调用 Monad 函数?

haskell - 使用 F# 中的 Applicative 功能构建记录

haskell - 定义 Data.Void(Hackage 上的包 "void")的不必要类型杂耍?

haskell - 用户应该使用/避免哪些 Haskell (GHC) 扩展?

haskell - 转换为 Pointfree 风格?

c - Haskell FFI : Successful compile and link, 应用程序与 BEX64 崩溃

haskell - 如何推断 Scott 编码的 List 构造函数的类型?

haskell - 无法安装 c2hs 和语言-c

haskell - 第一个仿函数定律是否遵循第二个?