Why is GHC on Replit printing my input multiple times? - haskell

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?

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

failed to compile Haskell project "helloworld"

I am a beginner of Haskell and I have downloaded ghc-8.8.1 from the official site. I tried to compile my first Haskell program with it and failed. Here is my program helloworld.hs:
main = putStrLn "hello world"
Then I compiled it with cmd (Windows 10):
ghc --make helloworld
And it failed with these words:
[1 of 1] Compiling Main ( helloworld.hs, helloworld.o )
Linking helloworld.exe ...
C:\Users\蔡XX\AppData\Local\Temp\ghc92756_0\ghc_6.rsp: commitBuffer: invalid argument (invalid character)
(蔡XX is my name)
Why is it not working? How can I solve this problem?
BTW, my ghci is working.
Edit: I created a new user in my computer with English name and copied my ghc, and this time it worked. It turns out that this problem was due to the character 蔡 in my path, as the comments said. Thanks guys!

How to debug a stack process being 'killed'?

I'm compiling a haskell file with
$ stack ghc --resolver lts-12.5 --package raw-strings-qq myscript.hs
and execute it with
$ ./myscript
Killed
$
on my Ubuntu machine. I get Killed as an error message and nothing else.
How can I debug this? Does this mean the process takes up to much memory? Are there other possible sources of error?
The response you get from terminal as Killed is because you didn't give some commands:
I use online IDE called Replit, so at the start it doesn't load needed libraries for you, you have to load manually:
ghci -o main main.hs
Sometimes, it automatically loads Main, sometimes it doesn't. So, if you don't see such thing like below, then you have to do it manually as well:
[1 of 1] Compiling Main ( main.hs, interpreted )
Ok, one module loaded.
*Main>
:l main
Maybe, it doesn't directly answer your question, but I got same exception like yours and dealt with it in that way. Well, same exception has the same problem (I guess)?

Getting Source Files to Run in Haskell Programming (WinGHCi)

I can't figure out how to get WinGHCi to load and compile my .hs file.
I have a file, C:\Users\Haskell\Source\hello.hs, that only contains the following line:
main = putStrLn "Hello, world!"
If, at the Prelude> prompt, I run
:cd C:\Users\Haskell\Source\
nothing happens, which I'm assuming means the command was successful. However, when I try to run
:load hello.hs
I get a "[1 of 1] Compiling Main. Ok, modules loaded: Main" message. My prompt then changes from "Prelude" to "*Main" and I type:
ghc -o hello hello.hs
After that, I will get a series of errors talking about how ghc, o, hello, hello, and hs are "Not in scope."
I am in the correct directory. Why won't my program run?
One of my problems is that I'm unable to navigate the directories. I know that :!dir lists the files, and I am in the right directory, but :load hello.hs still doesn't work and I keep getting the scope error.
Any help would be appreciated.
EDIT: A user pointed out that if I have gotten to the *Main prompt, then my program has been loaded and compiled and I do not need to run the ghc command. If that is the case, how would I run it? Haskell.org states that, "You can then run the executable (./hello on Unix systems, hello.exe on Windows)," but an exe has not been created.
I find it easier to first navigate to the directory then invoke ghci. Once in Prelude you can use :l and the file name.
Or, you could load ghci then use :l and use the fully qualified path for the file.
Edit:
After reading your edits, it is clear you are getting your code compiled fine. Once it says it has compiled, there is no reason to try and do so again with ghc (I don't think you can do that from within ghci anyhow).
Now that it is compiled, you can use any of the code and data types defined there in. So to use your main function, just type in main at the *Main> prompt.

Leksah default hello world is not working after installation Xubuntu 13.10

I've installed Leksah 0.12.1.3 on Xubuntu 13.10 from terminal.
sudo apt-get install leksah
Opened leksah, created new workspace and package.
Main.hs is created by default with 'Hello world' program.
module Main (
main
) where
import Control.Monad (unless)
import Data.List (stripPrefix)
import System.Exit (exitFailure)
import Test.QuickCheck.All (quickCheckAll)
-- Simple function to create a hello message.
hello s = "Hello " ++ s
-- Tell QuickCheck that if you strip "Hello " from the start of
-- hello s you will be left with s (for any s).
prop_hello s = stripPrefix "Hello " (hello s) == Just s
-- Hello World
exeMain = do
putStrLn (hello "World")
-- Entry point for unit tests.
testMain = do
allPass <- $quickCheckAll -- Run QuickCheck on all prop_ functions
unless allPass exitFailure
-- This is a clunky, but portable, way to use the same Main module file
-- for both an application and for unit tests.
-- MAIN_FUNCTION is preprocessor macro set to exeMain or testMain.
-- That way we can use the same file for both an application and for tests.
#ifndef MAIN_FUNCTION
#define MAIN_FUNCTION exeMain
#endif
main = MAIN_FUNCTION
Now, if I try to run package, or write anything in editor, in lower-right window
========== 127 ==========================
appears.
This happens to me a lot.... I don't know what the cause is, but (at least in my case) I know I can fix the problem by just using the command line. I just "cd" into the directory with the package (the one with the *.cabal file), and type
cabal configure
cabal build
after this is done, Leksah works properly. Clearly it is a Leksah bug, but it is easy to work around.
Problem was in my naive assumption that 'apt-get install leksah' will install all needed packages.
However, that's not correct.
After leksah installation you'll need:
apt-get install cabal-install
apt-get install ghc
cabal update
After that, as jamshidh mentioned, you need to click package->cofigure.
Now build brakes with (for program posted in question, which is leksah autogenerated default):
Couldn't match type `IO' with `[]'
Expected type: String
Actual type: IO ()
In the first argument of `putStrLn', namely `testMain'
In the expression: putStrLn testMain
In an equation for `main': main = putStrLn testMain
But I managed to build much simpler version:
module Main (
main
) where
main = putStrLn "Hello World"
The problem with the default hello world is the following line:
putStrLn (hello "World")
It's simply that the left quote is not in the correct place. Change that to
putStrLn ("hello World")
and it should work.

Monitoring the filesystem with Haskell

I'm using the FSNotify package to watch my filesystem for changes to markdown files, so I can run them through Pandoc automatically. However, I'm having trouble getting the manager to exit nicely.
As it stands, my code is based on Yesod's use of the same package:
module Main where
import System.FSNotify
import Filesystem (getWorkingDirectory)
import System.Exit
import Control.Concurrent
main = do
cwd <- getWorkingDirectory
putStrLn "Watching current directory, press RETURN to exit."
withManager $ \man -> do
_ <- forkIO $ do
watchTree man cwd (const True) $ \fp -> print fp
_ <- getLine
exitSuccess
When I use runhaskell, the program works perfectly, but if I compile it to an executable (using ghc --make), when I press enter, my terminal becomes unresponsive. I'm compiling this on Windows 7 64-bit.
EDIT: Completed code. Well, not entirely completed - there's still more I want to do with this - but working.
Whenever I see a question of the form "works in GHCi, hangs when compiled", I think to myself "you need the threaded runtime".
Try recompiling with -threaded and see if that helps.
(GHCi is compiled with this flag, which explains the difference. I do recall there's some difference in how external function calls work depending on whether you use the threaded runtime or not...)

Resources