Wrapping random

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Wrapping random

Torsten Otto
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"
 >findAnswer (x:xs)
 > | (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.

Regards,
Torsten Otto
Reply | Threaded
Open this post in threaded view
|

Wrapping random

Steven Ashley
Hi Torsten,


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
have used.

Control.Monad.Random would take care of this stuff for you however it
requires a simple understanding of monads.


Best of luck,

Steven

2008/11/29 Torsten Otto <[hidden email]>:

> 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"
>>findAnswer (x:xs)
>>       | (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.
>
> Regards,
> Torsten Otto
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
Reply | Threaded
Open this post in threaded view
|

Wrapping random

Michael Snoyman
In reply to this post by Torsten Otto
On Fri, Nov 28, 2008 at 1:53 PM, Torsten Otto <[hidden email]> wrote:

> 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"
> >findAnswer (x:xs)
> >       | (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.
>

I believe you are looking for unsafePerformIO (
http://haskell.org/ghc/docs/latest/html/libraries/base/System-IO-Unsafe.html#v%3AunsafePerformIO).
I'm not sure if it will work properly for random number generation,
however,due to optimization issues.

Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081128/5fc09b0c/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Wrapping random

Isaac Dupree
In reply to this post by Torsten Otto
Torsten Otto wrote:

> 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!"

could be more concisely
 > randomAnswer r = ["Nope!", "Absolutely!"] !! r
and 0-based rather than 1-based:
 > main = do
 >   randomNumber <- randomRIO (0::Int,1)
 >   print (randomAnswer randomNumber)

for example, although it worked fine as-is, and it's
possible to abstract more nicely than what I showed here too

-Isaac
Reply | Threaded
Open this post in threaded view
|

Wrapping random

Andrew Sackville-West
In reply to this post by Torsten Otto
On Fri, Nov 28, 2008 at 10:53:32PM +0100, Torsten Otto wrote:
> 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.

Perhaps instead of using a random number, you could simulate
randomness with some other technique. For example, if there is no good
response in a particular situation, you could hash the input and map
it to some predetermined responses. This would keep you in the IO
monad (I think. I'm a total rookie at this). You could then useit as a
vehicle to teach some other stuff such as the meaning of random in a
computer environment (vs true randomness) and concepts of hashing
(even looking into collisions in a simple way because the students
will see that different situations reliably give rise to the same
outout).

.02

A
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/beginners/attachments/20081129/21f097d8/attachment.bin
Reply | Threaded
Open this post in threaded view
|

Wrapping random

Torsten Otto
Thanks a bunch for the replies, they were much appreciated. In the  
meantime we talked in more depth than I had initially planned to about  
randomness, statefulness and even monads to some extent.
(And the chatbot gives its random replies as was planned :-) )

Kind regards,
Torsten