Haskell beginner help
Name:
Anonymous
2009-02-25 16:03
I'm learning haskell and I stumbled upon a problem. Here's the code:
module Main
where
main = do putStr "Input your name: "
s <- getLine
putStrLn s
main
But after compiling and running, the prompt string doesn't get printed until I input stuff, like this:
foo
Input your name: foo
bar
Input your name: bar
When running the code in the interpreter, everything's alright. What's the problem?
Name:
Anonymous
2009-02-25 16:08
I'm learning haskell
Here's the real problem
Name:
Anonymous
2009-02-25 16:09
Buffering issues? I don't know how to flush the buffer in Haskell, but see about doing that before doing your getLine.
Name:
Anonymous
2009-02-25 16:22
>>3
Added
hFlush stdout and worked, thanks. :)
Name:
Anonymous
2009-02-25 16:34
Okay, one more thing. I want the program to stop when :q is entered. I wanted to do it like this:
main = do putStr "Input your name: "
hFlush stdout
s <- getLine
putStrLn s
if (s /= ":q") then main
But it doesn't work. How should I do that?
Name:
Anonymous
2009-02-25 16:37
>>5
Don't help me, I realised that if I break the loop, the function won't return anything (wouldn't be
:: IO()). It should be something like this:
if (s /= ":q") then main else putStrLn ""
Name:
notahaskellprogrammer
2009-02-25 16:43
>>3
I think your right, try a second putStr to flush the buffer before getting input.
Inelegant but it should work
Name:
Anonymous
2009-02-25 16:46
>>7
nevermind me i hadn't refreshed /prog/
Name:
Anonymous
2009-02-25 17:01
In case you're wondering, by default it only flushes the output buffer after \n. You can use hSetBuffering to change this.
Name:
Anonymous
2009-02-25 17:21
Also,
[asdf@konpyuta:~/hs/] file test.hs
test.hs: ASCII Java program text
what.
Name:
Anonymous
2009-02-25 19:30
LOLOL HASKELL
Name:
Anonymous
2009-02-25 19:36
>>6
else putStrLn ""
else return ()
Name:
Anonymous
2009-02-25 20:04
>>=12
when (s /= ":q") main
Name:
Anonymous
2009-02-25 20:24
>>13
>>=12
I see what you did there.
Name:
Anonymous
2009-02-25 21:29
>>11
I don't know what you're laughing about. I have a good job writing
facts and
fibs in Haskell.
Name:
Anonymous
2009-02-25 21:36
main=do s <- getLine
if s /= "q" then do {putStrLn "?"; hFlush stdout; main } else return ()
Name:
Anonymous
2009-02-25 22:13
>>2
Now you have two problems
fix'd.
Name:
Anonymous
2009-02-25 23:52
>>14
Monads are like meme burritos.
Name:
Anonymous
2009-02-26 2:13
>>16
main = do s <- getLine; when (s /= "q") $ putStrLn "?" >> main
Name:
Anonymous
2009-02-26 6:53
Name:
Anonymous
2009-02-26 9:20
>>20
They both make horrible and useless analogies!
Name:
Anonymous
2010-11-27 8:05
Name:
Anonymous
2011-02-04 13:10