[Haskell-beginners] Ruby Quiz Solution #15 - won't work after compilation

Daniel Fischer daniel.is.fischer at web.de
Thu Aug 14 17:29:55 EDT 2008


Am Donnerstag, 14. August 2008 22:12 schrieb Alex Watt:
> Brent: getChar acts like getLine from what I can tell, it's a bit odd
>

Buffering indeed, in ghci, IIRC, stdin and stdout aren't buffered by default, 
for binaries the default is line buffering, so the char won't be gotten until 
you type a newline. Changing your code to

module Main where

import System.IO

main :: IO ()
main = do hSetBuffering stdin NoBuffering
          play (Animal "Dog")
          return ()
 
solves the problem:

Think of an animal, I will try to guess what it is...
Are you thinking of a Dog? (y/n)
n
I give up, you win!
Please help me improve my guesses!
What is the name of the animal you were thinking of?
Cat
Now please enter a question that answers yes for a Cat and no for a Dog
Does it meow?
Do you want to play again? (y/n)
y
Think of an animal, I will try to guess what it is...
Does it meow? (y/n)
n
Are you thinking of a Dog? (y/n)
n
I give up, you win!
Please help me improve my guesses!
What is the name of the animal you were thinking of?
Cow
Now please enter a question that answers yes for a Cow and no for a Dog
Is it useful?
Do you want to play again? (y/n)
y
Think of an animal, I will try to guess what it is...
Does it meow? (y/n)
n
Is it useful? (y/n)
y
Are you thinking of a Cow? (y/n)
y
I win this time.
Do you want to play again? (y/n)
n
Thanks for playing..

On the other count, I have to agree with Brent, the code is very nice overall, 
but play' and getNewAnimal as top level functions would be better, and I 
don't really like the line

play' question@(Question s y n) = do ans <- ask $ show question

I'd prefer do ans <- ask s

but that's purely a matter of taste.

HTH,
Daniel


More information about the Beginners mailing list