[Haskell-beginners] Wrapping random

Isaac Dupree isaacdupree at charter.net
Fri Nov 28 18:59:20 EST 2008


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


More information about the Beginners mailing list