[Haskell-cafe] Simple program. Simple problem?

michael rice nowgate at yahoo.com
Sun Oct 11 19:24:55 EDT 2009


There's still something screwy going on here with the random generation or passing the gen to cointoss. Shouldn't newStdGen be updating getStdGen?

I've been running it and entering 'h' four (4) times, then aborting with CNTL-C.
Each time I either get

You lose!
You lose!
You win!
You lose!

or

You win!
You win!
You lose!
You win!

That's all, should be getting some variation.

Michael

======================

import System.Random

coinToss :: StdGen -> IO ()
coinToss gen = putStrLn "What's your guess, heads or tails ('h' or 't')?"
               >> fmap head getLine
               >>= \c -> let (randInt, _) = randomR(0,1) gen :: (Int, StdGen)
                         in if c == ((!!) "ht" randInt) then putStrLn "You win!" else putStrLn "You lose!"

main = do
  gen <- getStdGen
  coinToss gen
  gen <- newStdGen
  main


--- On Sun, 10/11/09, Felipe Lessa <felipe.lessa at gmail.com> wrote:

From: Felipe Lessa <felipe.lessa at gmail.com>
Subject: Re: [Haskell-cafe] Simple program. Simple problem?
To: haskell-cafe at haskell.org
Date: Sunday, October 11, 2009, 7:08 PM

On Mon, Oct 12, 2009 at 12:42:16AM +0200, Peter Verswyvelen wrote:
> btw I always find it amusing to play with interact and lazy IO:

I always find it frightening to play with lazy IO :).

--
Felipe.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091011/748cca4d/attachment.html


More information about the Haskell-Cafe mailing list