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

Brent Yorgey byorgey at seas.upenn.edu
Thu Aug 14 15:13:22 EDT 2008

On Thu, Aug 14, 2008 at 03:33:26PM +0100, Alex Watt wrote:
> If I call "main" in ghci, my program runs as it should, but if I try
> compiling it, and then running the result of that compilation, i.e.:
> $ ghc -o animal_game game.hs
> $ ./animal_game
> unexpected things occur, particularly "getChar" does not work correctly, and

When you say 'getchar does not work correctly', exactly what do you
mean?  I'm not sure of the problem, but this smells to me of something
having to do with buffering and/or newline characters not getting
handled properly.
> What would also be nice is any comments on the actual code; as a begginner,
> it's nice to get some feedback, and I might as well also ask the following
> question. Originally I wanted to the types to be as such:
> data Animal = Animal String
> data Question a b = Question String a b -- where a is a Question or an
> Animal, as is b
> The problem is I wanted methods that had types that were able to accept
> an argument of type Animal or Question, and for it to act appropriately on
> that type, I tried adding them to common type classes (a new one I created
> called 'Askable' which contained a method 'ask'), but I ran into a whole
> bunch of problems. Is there any easy way to do it?

Creating an 'Askable' type class sounds like it would be a great
solution in certain instances; the problem to me looks like the fact
that you want to create a data structure (a QuestionTree) which
contains both Animals and Questions.  This quickly leads into
existential types---doable, but probably best left until later.  At
any rate, your current solution---making Animal and Question
constructors of a data type, and implementing play' and other such
functions by pattern matching---seems quite nice.  It's certainly the
approach I would take.  In general your code looks very nice.  The
only comment I have is that I would separate out play' and
getNewAnimal as separate top-level functions, rather than defining
them in a 'where' block.  Functions which are not top-level are
difficult to test by themselves.


More information about the Beginners mailing list