Something simple

Marco Túlio Gontijo e Silva marcot at riseup.net
Thu Jul 17 12:39:47 EDT 2008


Hi.

Em Qui, 2008-07-17 às 11:56 -0400, Chad Wilson escreveu:
> Very simple and to the point.  Now I need to dig into the manual and
> figure out the next simple progression...taking 2 arguments and doing
> something to them akin to:
> 
> roll 1 6
> 
> This would roll 1, 6-sided die, or 1d6 in gamer terms and print the
> result.

> import Control.Monad
> import Random
> import System.Environment
> main :: IO ()
> main
>   = do

To get the first two arguments you can use:

>     (n : s : _) <- getArgs

The randomRIO [2] function is a good option, with replicateM [3] to run
more than one dice.  read [4] is needed to parse the argument into an
Int value.  You have to specificy the type of 1, cause randomRIO has to
be sure of it.

>     replicateM (read n) (randomRIO (1 :: Int, read s)) >>= print

[1]:
http://haskell.org/ghc/docs/latest/html/libraries/random/System-Random.html
[2]:
http://haskell.org/ghc/docs/latest/html/libraries/random/System-Random.html#v%3ArandomRIO
[3]:
http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html#v%3AreplicateM
[4]: http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Aread

Hope it helps.

-- 
Marco Túlio Gontijo e Silva
Página: http://marcotmarcot.googlepages.com/
Blog: http://marcotmarcot.blogspot.com/
Correio: marcot at riseup.net
XMPP: marcot at jabber.org
IRC: marcot at irc.freenode.net
Telefone: 25151920
Celular: 98116720
Endereço:
 Rua Turfa, 639/701
 Prado 30410-370
 Belo Horizonte/MG Brasil



More information about the Beginners mailing list