Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Console IO in notepad++

I've recently started to learn Haskell. I have this code

module Main
    where

import IO

main =  do 
            hSetBuffering stdin LineBuffering
            putStrLn "Please enter your name: "
            name <- getLine
            putStrLn ("Hello, " ++ name ++ ", how are you?")

I'm using the GHC compiler together with the notepad++ editor. The problem is the interaction goes like this:

Process started >>>
Vlad
Please enter your name:
Hello, Vlad, how are you?
<<< Process finished.

As you can see, output is only written after I input something. This was a bit unexpected, as I was sure the program would first ask for my name, then I'd get to enter it and then it would say hello. Well, that's exactly what happens if I run the exe manually, yet not if I run it with notepad++ and use its console wrapper...

How can I make notepad++ display the output when it should, and not all of it just before the program terminates? Is this even possible?

like image 749
IVlad Avatar asked Jan 23 '23 02:01

IVlad


2 Answers

Try setting stdout to LineBuffering! Also, loading your program in ghci instead runnign the compiled version doesn't seem to need any buffering at all...

By the way, I didn't know about the console in NPP - thanks for pointing me to it!

like image 101
yatima2975 Avatar answered Feb 11 '23 11:02

yatima2975


I'm not familiar with notepad++, but a quick and hacky method would probably be to do

hFlush stdout

after each putStrLn. You could even make the following method:

nppPutStrLn s = putStrLn s >> hFlush stdout
like image 35
sfultong Avatar answered Feb 11 '23 13:02

sfultong