[Haskell-beginners] Wrapping random
steven.e.ashley at gmail.com
Fri Nov 28 18:27:36 EST 2008
An option would be to use "randoms" to generate an infinite list of
random numbers before hand and pass these numbers to your functions.
Of course that will mean you have to keep track of what numbers you
Control.Monad.Random would take care of this stuff for you however it
requires a simple understanding of monads.
Best of luck,
2008/11/29 Torsten Otto <t-otto-news at gmx.de>:
> Hi all,
> I teach a high school class in Computer Science. The current programming
> goal is to implement chat-bots, and we're using Haskell of course. Now one
> of my students had the seemingly easy idea of having the bot answer with a
> random sentence if it doesn't have "good" answer.
> Random in Haskell has its problems. I understand why you can't just call a
> function as you would in Java. I'm not firm enough with monads myself (and
> certainly don't want to go there in the class beyond I/O) so I'm calling for
> help here: Is there a way to wrap the generation of random numbers so that
> for the students it works like a function?
> We have this working:
>> import System.Random
>> main =
>> do randomNumber <- randomRIO (1::Int,2)
>> print (randomAnswer randomNumber)
>> randomAnswer r
>> | (r == 1) = "Nope!"
>> | (r == 2) = "Absolutely!"
>> | otherwise = "Error!"
> Now, how can we use it for something like this:
>>findAnswer  = "h"
>> | (z == "unknown") = findAnswer xs
>> | otherwise = z
>> where z = findWord x lexikon
> where instead of getting "h" we'd like to call a function that would give us
> one of the strings out of randomAnswer.
> (findAnswer looks through a list [(keyword,response)].
> I've looked at realworldhaskell and the wikibook among other sources, but I
> can't manage to piece anything useful together. How do I manage to get
> something of type IO to represent itself as a String?
> Any help would be greatly appreciated.
> Torsten Otto
> Beginners mailing list
> Beginners at haskell.org
More information about the Beginners