[Haskell-cafe] Simple program. Simple problem?

michael rice nowgate at yahoo.com
Sun Oct 11 18:56:53 EDT 2009


Thanks for the tip, and the lazy example.



I think I'm finally beginning to "get" monads, so I decided to test my
understanding with this small example. So far so good, except for that
little bump in the road.



Onward and upward.



Michael


--- On Sun, 10/11/09, Peter Verswyvelen <bugfact at gmail.com> wrote:

From: Peter Verswyvelen <bugfact at gmail.com>
Subject: Re: [Haskell-cafe] Simple program. Simple problem?
To: "michael rice" <nowgate at yahoo.com>
Cc: haskell-cafe at haskell.org
Date: Sunday, October 11, 2009, 6:42 PM

btw I always find it amusing to play with interact and lazy IO:
guess :: [Char] -> [String] -> [String]guess (c:cs) ~(i:is) =   "What's your guess, heads or tails ('h' or 't')?" : 
  (if [c]==i then "You win!" else "You lose!") :  guess cs is 
main = do  gen <- getStdGen  let rs = randomRs  (0,1::Int) gen
      cs = map ("ht"!!) rs  interact $ unlines . guess cs . lines


On Mon, Oct 12, 2009 at 12:24 AM, Peter Verswyvelen <bugfact at gmail.com> wrote:

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')?
h104You win!What's your guess, heads or tails ('h' or 't')?10You 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 <nowgate at yahoo.com> 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

=======

[michael at 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
[michael at localhost ~]$ 






      
_______________________________________________

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/e2487c7c/attachment.html


More information about the Haskell-Cafe mailing list