It always helps to put a Debug.Trace.trace:
in if trace (show (fromEnum c)) $ c == ((!!) "ht"
randInt) then p
What's your guess, heads or tails ('h' or 't')?
h
104
You win!
What's your guess, heads or tails ('h' or 't')?
*10*
You lose!
What's your guess, heads or tails ('h' or 't')?
So getChar also receives the linefeed character.
An easy way to get around this, is to use getLine instead and just use the
first character, as in
>> fmap head getLine
But of course we're hacking away here :-)
On Mon, Oct 12, 2009 at 12:10 AM, michael rice <[email protected]> wrote:
> What is going wrong here?
>
> Michael
>
> =======
>
> import System.Random
>
> coinToss :: StdGen -> IO ()
> coinToss gen = putStrLn "What's your guess, heads or tails ('h' or 't')?"
> >> getChar
> >>= \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
>
> =======
>
> [mich...@localhost ~]$ runhaskell cointoss.hs
> What's your guess, heads or tails ('h' or 't')?
> h
> You win!
> What's your guess, heads or tails ('h' or 't')?
> You lose!
> What's your guess, heads or tails ('h' or 't')?
> h
> You lose!
> What's your guess, heads or tails ('h' or 't')?
> You lose!
> What's your guess, heads or tails ('h' or 't')?
> ^Ccointoss.hs: cointoss.hs: interrupted
> [mich...@localhost ~]$
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe