<br><br><div class="gmail_quote">On Fri, Nov 28, 2008 at 1:53 PM, Torsten Otto <span dir="ltr">&lt;<a href="mailto:t-otto-news@gmx.de">t-otto-news@gmx.de</a>&gt;</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&#39;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&#39;t have &quot;good&quot; answer.<br>

<br>
Random in Haskell has its problems. I understand why you can&#39;t just call a function as you would in Java. I&#39;m not firm enough with monads myself (and certainly don&#39;t want to go there in the class beyond I/O) so I&#39;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>
&gt; import System.Random<br>
<br>
&gt; main =<br>
&gt; &nbsp; do randomNumber &lt;- randomRIO (1::Int,2)<br>
&gt; &nbsp; &nbsp; &nbsp;print (randomAnswer randomNumber)<br>
<br>
&gt; randomAnswer r<br>
&gt; &nbsp; &nbsp; &nbsp; | (r == 1) = &quot;Nope!&quot;<br>
&gt; &nbsp; &nbsp; &nbsp; | (r == 2) = &quot;Absolutely!&quot;<br>
&gt; &nbsp; | otherwise = &quot;Error!&quot;<br>
<br>
Now, how can we use it for something like this:<br>
<br>
&gt;findAnswer [] = &quot;h&quot;<br>
&gt;findAnswer (x:xs)<br>
&gt; &nbsp; &nbsp; &nbsp; | (z == &quot;unknown&quot;) = findAnswer xs<br>
&gt; &nbsp; &nbsp; &nbsp; | otherwise = z<br>
&gt; &nbsp; where z = findWord x lexikon<br>
<br>
where instead of getting &quot;h&quot; we&#39;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&#39;ve looked at realworldhaskell and the wikibook among other sources, but I can&#39;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&#39;m not sure if it will work properly for random number generation, however,due to optimization issues.<br>
<br>Michael<br> </div></div><br>