小编The*_*rog的帖子

为什么 Replit 上的 GHC 会多次打印我的输入?

我正在使用 Replit 网站使用 GHC 运行 Haskell 程序。我在编辑器中输入代码并使用“运行”按钮运行它,而不仅仅是使用 GHCi 运行代码行。getLine每当我使用该函数时,即使在极其简单的程序中,我也会遇到奇怪的输出问题。例如,运行程序

main = do ans <- getLine
          putStrLn ans
Run Code Online (Sandbox Code Playgroud)

hi在输入提示处键入会打印hi两次而不是一次:

cabal v1-run
Preprocessing executable 'Cabal-example' for Cabal-example-0.1.0.0..
Building executable 'Cabal-example' for Cabal-example-0.1.0.0..
[1 of 1] Compiling Main             ( Main.hs, dist/build/Cabal-example/Cabal-example-tmp/Main.o )
Linking dist/build/Cabal-example/Cabal-example ...
Running Cabal-example...
 hi
hi
hi
Run Code Online (Sandbox Code Playgroud)

当我尝试禁用函数中的输入/输出缓冲时,问题变得更加奇怪main(我需要为我正在开发的更大的程序执行此操作):

cabal v1-run
Preprocessing executable 'Cabal-example' for Cabal-example-0.1.0.0..
Building executable 'Cabal-example' for Cabal-example-0.1.0.0..
[1 of 1] Compiling Main             ( Main.hs, dist/build/Cabal-example/Cabal-example-tmp/Main.o )
Linking dist/build/Cabal-example/Cabal-example ...
Running …
Run Code Online (Sandbox Code Playgroud)

io haskell ghc replit

2
推荐指数
1
解决办法
133
查看次数

标签 统计

ghc ×1

haskell ×1

io ×1

replit ×1