Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. ghc --make Vs runhaskell (Madhu Babu)
2. Re: ghc --make Vs runhaskell (Brandon Allbery)
3. Re: ghc --make Vs runhaskell (Kim-Ee Yeoh)
4. Re: [Haskell-cafe] ghc --make Vs runhaskell (Madhu Babu)
----------------------------------------------------------------------
Message: 1
Date: Tue, 3 Feb 2015 22:23:42 -0500
From: Madhu Babu <[email protected]>
To: [email protected], [email protected]
Subject: [Haskell-beginners] ghc --make Vs runhaskell
Message-ID:
<calh-vv4afwugup3frxya1kurcsrr2plyxagg3n_b3v7buzh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi !,
I have the following program from ?Learn You a Haskell? (
http://learnyouahaskell.com/input-and-output#randomness ). When i run this
using runhaskell, code works perfectly fine. However, when i compile and
run the binary file, code behaves differently.
Basically, in the following code, we print a line first and the read the
line from stdin. This behavior works perfectly fine when using ?runhaskell
guess.hs?. But when i say ?./guess?, i had to type in a number directly (
i.e. first prompt is skipped ).
btw.. i am using mac yosomite command terminal.
Any help greatly appreciated.. Thanks a lot in advance..
import System.Random
import Control.Monad(when)
main = do
ranGen <- getStdGen
let (rand,_) = randomR (1,10) ranGen :: (Int,StdGen)
putStr "Guess a number between 1 and 10 : "
numStr <- getLine
when(not $ null numStr) $ do
let num = read numStr
if rand == num
then putStrLn "Yuhaa!! you are right baby!"
else putStrLn $ "Sorry dude!! it was " ++ show rand
newStdGen
main
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150203/28dfa686/attachment-0001.html>
------------------------------
Message: 2
Date: Tue, 3 Feb 2015 22:35:59 -0500
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Cc: haskell-cafe <[email protected]>
Subject: Re: [Haskell-beginners] ghc --make Vs runhaskell
Message-ID:
<cakfcl4wzjmgqmqv_qhknb86v0zzkoex8yvpoth8nmjosy3a...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Tue, Feb 3, 2015 at 10:23 PM, Madhu Babu <[email protected]> wrote:
> Basically, in the following code, we print a line first and the read the
> line from stdin. This behavior works perfectly fine when using ?runhaskell
> guess.hs?. But when i say ?./guess?, i had to type in a number directly (
> i.e. first prompt is skipped ).
>
runhaskell is presumably using unbuffered I/O for some reason, so
outputting directly to the terminal. The compiled program follows standard
Unix buffering conventions: line buffering on output to a terminal, so the
putStr is sitting in a buffer waiting to see a newline output.
C / C++ programs also do buffering, but there's a heinous hack which
detects reads on stdin and flushes stdout beforehand. (Heinous because
there is no guarantee that they are actually related --- but na?ve
programmers invariably do not learn about line buffering and expect all
output to be unbuffered(*), and C standard library programmers eventually
gave up and catered to them after years of trying to get them to pay
attention. I have a nasty suspicion we're going to end up with a similar
horrible hack in Haskell eventually.)
You can use hFlush from System.IO to flush the prompt out to the terminal,
or disable output buffering with hSetBuffering in the same module.
(*) At some point someone will pop up and say that on modern computers,
buffering is obsolete because it's fast enough that horribly inefficient
character-at-a-time I/O is good enough. Yet, I can *still* see visible
hesitations when character-at-a-time I/O is used on a modern Intel core
i5/i7. And your disk benchmarks will *tank* even with server-class disk
subsystems.
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150203/f1714d12/attachment-0001.html>
------------------------------
Message: 3
Date: Wed, 4 Feb 2015 10:45:07 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Cc: Haskell Cafe <[email protected]>
Subject: Re: [Haskell-beginners] ghc --make Vs runhaskell
Message-ID:
<CAPY+ZdQe8x1Sw8=L=ydkarzo4zggmbs8sjybncen70sddsb...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hey Madhu,
Right, this is another gotcha -- haskell has a bunch of them.
As Tristan and Brandon have remarked, the runhaskell interpreter and
compiled programs handle output buffering differently.
Here are changes to make your game work:
On Wed, Feb 4, 2015 at 10:23 AM, Madhu Babu <[email protected]> wrote:
> import System.Random
> import Control.Monad(when)
>
import System.IO
> main = do
> ranGen <- getStdGen
> let (rand,_) = randomR (1,10) ranGen :: (Int,StdGen)
>
hSetBuffering stdout NoBuffering
> putStr "Guess a number between 1 and 10 : "
>
You might want to take a look at the "Summing Two Numbers" section here:
https://www.haskell.org/onlinereport/io.html
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150204/0544d3eb/attachment-0001.html>
------------------------------
Message: 4
Date: Tue, 3 Feb 2015 23:57:16 -0500
From: Madhu Babu <[email protected]>
To: Tristan Seligmann <[email protected]>
Cc: [email protected], [email protected]
Subject: Re: [Haskell-beginners] [Haskell-cafe] ghc --make Vs
runhaskell
Message-ID:
<CALh-VV5k0Mm59BFis33RG+QifLV+xwVpyesV=qucZ+QmEsT=z...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Thanks a lot. .this really helps..
On Feb 3, 2015 10:35 PM, "Tristan Seligmann" <[email protected]>
wrote:
> On 4 February 2015 at 05:23, Madhu Babu <[email protected]> wrote:
> > Basically, in the following code, we print a line first and the read the
> > line from stdin. This behavior works perfectly fine when using
> ?runhaskell
> > guess.hs?. But when i say ?./guess?, i had to type in a number directly (
> > i.e. first prompt is skipped ).
>
> This is most likely due to stdio buffering; usually stdin/stdout are
> line buffered, so data you write with putStr may not actually be
> written until a newline is written. This post / thread details some
> ways to handle this problem:
>
> https://www.haskell.org/pipermail/beginners/2010-March/003692.html
> --
> mithrandi, i Ainil en-Balandor, a faer Ambar
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150203/632fa908/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 80, Issue 3
****************************************