Unexpected results with simple IO

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Unexpected results with simple IO

Maurí­cio
   Dear Haskell users,

   I have a problem using IO. The small test program below asks the user
to guess from a list of random numbers between 1 and 10. Everything
works well excepts for one problem: all the messages ("Guess a
number...", "Right..." and "Wrong...") are printed after the program
finishes, i.e., I have to use it blind. I'm afraid I misunderstand
something important about lazyness or monads... What am I doing wrong?

   Thanks,
   Maurício

module Main where
import Random

main = do
    r_gen <- getStdGen --random generator
    let r_list = (randomRs (1,10) r_gen) --random list
    guess_loop (r_list)

guess_loop (r:r_others) = do
    putStrLn "Guess a number between 1 and 10:"
    n <- readLn
    if n==r
       then do
          putStrLn "Right! :)"
          return ()
       else do
          putStrLn "Wrong... :("
          guess_loop r_others

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected results with simple IO

Cale Gibbard
That doesn't happen for me at all, it works just fine. Maybe it's
something wrong with your terminal? You could possibly try playing
with the buffering settings on stdout, using hSetBuffering in
System.IO.

 - Cale

On 17/02/06, Maurício <[hidden email]> wrote:

>    Dear Haskell users,
>
>    I have a problem using IO. The small test program below asks the user
> to guess from a list of random numbers between 1 and 10. Everything
> works well excepts for one problem: all the messages ("Guess a
> number...", "Right..." and "Wrong...") are printed after the program
> finishes, i.e., I have to use it blind. I'm afraid I misunderstand
> something important about lazyness or monads... What am I doing wrong?
>
>    Thanks,
>    Maurício
>
> module Main where
> import Random
>
> main = do
>     r_gen <- getStdGen --random generator
>     let r_list = (randomRs (1,10) r_gen) --random list
>     guess_loop (r_list)
>
> guess_loop (r:r_others) = do
>     putStrLn "Guess a number between 1 and 10:"
>     n <- readLn
>     if n==r
>        then do
>           putStrLn "Right! :)"
>           return ()
>        else do
>           putStrLn "Wrong... :("
>           guess_loop r_others
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected results with simple IO

Maurí­cio
   You're right... I was running the example in rxvt, in cygwin. Now I
tried in Windows command shell and it works.

   Thanks,
   Maurício

Cale Gibbard wrote:

> That doesn't happen for me at all, it works just fine. Maybe it's
> something wrong with your terminal? You could possibly try playing
> with the buffering settings on stdout, using hSetBuffering in
> System.IO.
>
>  - Cale
>
> On 17/02/06, Maurício <[hidden email]> wrote:
>
>>   Dear Haskell users,
>>
>>   I have a problem using IO. The small test program below asks the user
>>to guess from a list of random numbers between 1 and 10. Everything
>>works well excepts for one problem: all the messages ("Guess a
>>number...", "Right..." and "Wrong...") are printed after the program
>>finishes, i.e., I have to use it blind. I'm afraid I misunderstand
>>something important about lazyness or monads... What am I doing wrong?
>>
>>   Thanks,
>>   Maurício
>>
>>module Main where
>>import Random
>>
>>main = do
>>    r_gen <- getStdGen --random generator
>>    let r_list = (randomRs (1,10) r_gen) --random list
>>    guess_loop (r_list)
>>
>>guess_loop (r:r_others) = do
>>    putStrLn "Guess a number between 1 and 10:"
>>    n <- readLn
>>    if n==r
>>       then do
>>          putStrLn "Right! :)"
>>          return ()
>>       else do
>>          putStrLn "Wrong... :("
>>          guess_loop r_others
>>
>>_______________________________________________
>>Haskell-Cafe mailing list
>>[hidden email]
>>http://www.haskell.org/mailman/listinfo/haskell-cafe
>>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re: Unexpected results with simple IO

Emil Axelsson
What version of GHC are you using?
Your code works for me in rxvt in Cygwin, with GHC 6.4.1. But I remember having
that same problem earlier (in some earlier GHC version, so it may be fixed by now).

The solution was to run hFlush after each putStr, like so:

   import System.IO (hFlush, stdout)

   do putStr "..."
      hHlush stdout
      ...

If I remember correctly, the problem only occurred in GHCi and Hugs -- not when
compiling the code.

/ Emil



Maurício skrev:

>   You're right... I was running the example in rxvt, in cygwin. Now I
> tried in Windows command shell and it works.
>
>   Thanks,
>   Maurício
>
> Cale Gibbard wrote:
>> That doesn't happen for me at all, it works just fine. Maybe it's
>> something wrong with your terminal? You could possibly try playing
>> with the buffering settings on stdout, using hSetBuffering in
>> System.IO.
>>
>>  - Cale
>>
>> On 17/02/06, Maurício <[hidden email]> wrote:
>>
>>>   Dear Haskell users,
>>>
>>>   I have a problem using IO. The small test program below asks the user
>>> to guess from a list of random numbers between 1 and 10. Everything
>>> works well excepts for one problem: all the messages ("Guess a
>>> number...", "Right..." and "Wrong...") are printed after the program
>>> finishes, i.e., I have to use it blind. I'm afraid I misunderstand
>>> something important about lazyness or monads... What am I doing wrong?
>>>
>>>   Thanks,
>>>   Maurício
>>>
>>> module Main where
>>> import Random
>>>
>>> main = do
>>>    r_gen <- getStdGen --random generator
>>>    let r_list = (randomRs (1,10) r_gen) --random list
>>>    guess_loop (r_list)
>>>
>>> guess_loop (r:r_others) = do
>>>    putStrLn "Guess a number between 1 and 10:"
>>>    n <- readLn
>>>    if n==r
>>>       then do
>>>          putStrLn "Right! :)"
>>>          return ()
>>>       else do
>>>          putStrLn "Wrong... :("
>>>          guess_loop r_others
>>>
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> [hidden email]
>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected results with simple IO

Maurí­cio
   I'm also using GHC 6.4.1 and rxvt v2.7.10. The problem does occur in
compiled code, but everything is OK in ghci!
   hFlush stdout did solve the problem, as expected.
   I've just started using rxvt. If you have tips on how to make ghci
work well with rxvt, please share them with me (for instance, how to set
the top arrow to repeat the last line, instead of moving the cursor one
live above. I don't understand very well how those applications handle
keyboard).

   Best,
   Maurício

Emil Axelsson wrote:

> What version of GHC are you using?
> Your code works for me in rxvt in Cygwin, with GHC 6.4.1. But I remember
> having that same problem earlier (in some earlier GHC version, so it may
> be fixed by now).
>
> The solution was to run hFlush after each putStr, like so:
>
>   import System.IO (hFlush, stdout)
>
>   do putStr "..."
>      hHlush stdout
>      ...
>
> If I remember correctly, the problem only occurred in GHCi and Hugs --
> not when compiling the code.
>
> / Emil
>
>
>
> Maurício skrev:
>
>>   You're right... I was running the example in rxvt, in cygwin. Now I
>> tried in Windows command shell and it works.
>>
>>   Thanks,
>>   Maurício
>>
>> Cale Gibbard wrote:
>>
>>> That doesn't happen for me at all, it works just fine. Maybe it's
>>> something wrong with your terminal? You could possibly try playing
>>> with the buffering settings on stdout, using hSetBuffering in
>>> System.IO.
>>>
>>>  - Cale
>>>
>>> On 17/02/06, Maurício <[hidden email]> wrote:
>>>
>>>>   Dear Haskell users,
>>>>
>>>>   I have a problem using IO. The small test program below asks the user
>>>> to guess from a list of random numbers between 1 and 10. Everything
>>>> works well excepts for one problem: all the messages ("Guess a
>>>> number...", "Right..." and "Wrong...") are printed after the program
>>>> finishes, i.e., I have to use it blind. I'm afraid I misunderstand
>>>> something important about lazyness or monads... What am I doing wrong?
>>>>
>>>>   Thanks,
>>>>   Maurício
>>>>
>>>> module Main where
>>>> import Random
>>>>
>>>> main = do
>>>>    r_gen <- getStdGen --random generator
>>>>    let r_list = (randomRs (1,10) r_gen) --random list
>>>>    guess_loop (r_list)
>>>>
>>>> guess_loop (r:r_others) = do
>>>>    putStrLn "Guess a number between 1 and 10:"
>>>>    n <- readLn
>>>>    if n==r
>>>>       then do
>>>>          putStrLn "Right! :)"
>>>>          return ()
>>>>       else do
>>>>          putStrLn "Wrong... :("
>>>>          guess_loop r_others
>>>>
>>>> _______________________________________________
>>>> Haskell-Cafe mailing list
>>>> [hidden email]
>>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>>>
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> [hidden email]
>> http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re: Unexpected results with simple IO

Emil Axelsson
Unfortunately, I don't know how to make the arrow keys work in rxvt. I'm not the
right person to ask about such things...
I don't think it's possible (unless GHC is built for Cygwin, or something).
Does anybody else know?

I use an alias

   alias ghciW='cmd /c start ghci'

That way I can start GHCi in a new DOS window from rxvt. I usually want two
windows anyway -- one for the interactive session and one to compile other modules.

/ Emil



Maurício skrev:

>   I'm also using GHC 6.4.1 and rxvt v2.7.10. The problem does occur in
> compiled code, but everything is OK in ghci!
>   hFlush stdout did solve the problem, as expected.
>   I've just started using rxvt. If you have tips on how to make ghci
> work well with rxvt, please share them with me (for instance, how to set
> the top arrow to repeat the last line, instead of moving the cursor one
> live above. I don't understand very well how those applications handle
> keyboard).
>
>   Best,
>   Maurício
>
> Emil Axelsson wrote:
>> What version of GHC are you using?
>> Your code works for me in rxvt in Cygwin, with GHC 6.4.1. But I
>> remember having that same problem earlier (in some earlier GHC
>> version, so it may be fixed by now).
>>
>> The solution was to run hFlush after each putStr, like so:
>>
>>   import System.IO (hFlush, stdout)
>>
>>   do putStr "..."
>>      hHlush stdout
>>      ...
>>
>> If I remember correctly, the problem only occurred in GHCi and Hugs --
>> not when compiling the code.
>>
>> / Emil
>>
>>
>>
>> Maurício skrev:
>>
>>>   You're right... I was running the example in rxvt, in cygwin. Now I
>>> tried in Windows command shell and it works.
>>>
>>>   Thanks,
>>>   Maurício
>>>
>>> Cale Gibbard wrote:
>>>
>>>> That doesn't happen for me at all, it works just fine. Maybe it's
>>>> something wrong with your terminal? You could possibly try playing
>>>> with the buffering settings on stdout, using hSetBuffering in
>>>> System.IO.
>>>>
>>>>  - Cale
>>>>
>>>> On 17/02/06, Maurício <[hidden email]> wrote:
>>>>
>>>>>   Dear Haskell users,
>>>>>
>>>>>   I have a problem using IO. The small test program below asks the
>>>>> user
>>>>> to guess from a list of random numbers between 1 and 10. Everything
>>>>> works well excepts for one problem: all the messages ("Guess a
>>>>> number...", "Right..." and "Wrong...") are printed after the program
>>>>> finishes, i.e., I have to use it blind. I'm afraid I misunderstand
>>>>> something important about lazyness or monads... What am I doing wrong?
>>>>>
>>>>>   Thanks,
>>>>>   Maurício
>>>>>
>>>>> module Main where
>>>>> import Random
>>>>>
>>>>> main = do
>>>>>    r_gen <- getStdGen --random generator
>>>>>    let r_list = (randomRs (1,10) r_gen) --random list
>>>>>    guess_loop (r_list)
>>>>>
>>>>> guess_loop (r:r_others) = do
>>>>>    putStrLn "Guess a number between 1 and 10:"
>>>>>    n <- readLn
>>>>>    if n==r
>>>>>       then do
>>>>>          putStrLn "Right! :)"
>>>>>          return ()
>>>>>       else do
>>>>>          putStrLn "Wrong... :("
>>>>>          guess_loop r_others
>>>>>
>>>>> _______________________________________________
>>>>> Haskell-Cafe mailing list
>>>>> [hidden email]
>>>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>>>>
>>>
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> [hidden email]
>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe