<br><br><div class="gmail_quote">On Fri, Nov 28, 2008 at 1:53 PM, Torsten Otto <span dir="ltr"><<a href="mailto:t-otto-news@gmx.de">t-otto-news@gmx.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br>
<br>
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.<br>
<br>
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?<br>
<br>
We have this working:<br>
<br>
> import System.Random<br>
<br>
> main =<br>
> do randomNumber <- randomRIO (1::Int,2)<br>
> print (randomAnswer randomNumber)<br>
<br>
> randomAnswer r<br>
> | (r == 1) = "Nope!"<br>
> | (r == 2) = "Absolutely!"<br>
> | otherwise = "Error!"<br>
<br>
Now, how can we use it for something like this:<br>
<br>
>findAnswer [] = "h"<br>
>findAnswer (x:xs)<br>
> | (z == "unknown") = findAnswer xs<br>
> | otherwise = z<br>
> where z = findWord x lexikon<br>
<br>
where instead of getting "h" we'd like to call a function that would give us one of the strings out of randomAnswer.<br>
(findAnswer looks through a list [(keyword,response)].<br>
<br>
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?<br>
<br>
Any help would be greatly appreciated.<br></blockquote><div><br>I believe you are looking for unsafePerformIO (<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/System-IO-Unsafe.html#v%3AunsafePerformIO">http://haskell.org/ghc/docs/latest/html/libraries/base/System-IO-Unsafe.html#v%3AunsafePerformIO</a>). I'm not sure if it will work properly for random number generation, however,due to optimization issues.<br>
<br>Michael<br> </div></div><br>