Home > Software design >  Why is GHC on Replit printing my input multiple times?
Why is GHC on Replit printing my input multiple times?

Time:03-09

I'm using the Replit website to run a Haskell program using GHC. I'm typing code into the editor and running it using the Run button, not just running lines of code using GHCi. I'm running into weird problems with output whenever I use the getLine function, even in extremely simple programs. For example, running the program

main = do ans <- getLine
          putStrLn ans

and typing hi at the input prompt prints hi twice instead of once:

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

The problem gets even weirder when I try to disable input/output buffering in the main function (I needed to do this for a larger program I was working on):

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^Jhi

I have tested this code on other websites and the code works normally. What am I missing here?

CodePudding user response:

I can duplicate using the Haskell Cabal Template. Looks like a "replit" bug in the "Console" tab for this template. The Console is echoing back user input independent of the Haskell program. Try the program:

main = do
  putStrLn "Enter something"
  getLine
  putStrLn "I'm printing something else"

and the output will include echoed input. (The space before the first "foo" also shouldn't be there.)

Running Cabal-example...
Enter something
 foo   <-- what I entered
foo    <-- echoed by the Console
I'm printing something else

Try running your program in the "Shell" tab using "cabal v1-run", and it should work fine:

~/SympatheticVastHypothesis$ 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..
Running Cabal-example...
Enter something
This won't echo     <-- what I entered
I'm printing something else
~/SympatheticVastHypothesis$ 

I think you'll probably have to report it as a bug to the replit folks.

  • Related