
I am a gamer. Table top RPGs, war games, board games, etc. Since my laptop is pen-based, I has been handier to keep it at the table than paper. As a result, I have been experimenting with making dice rollers to have yet another item not needed on the table. Sure, it takes away some of the tradition, but it makes for carrying fewer things. I have written various dice roll simulators in xblite, ruby and some other dynamics, but this seems like an interesting exercise to try in Haskell. My first goal is to figure out how to get the command-line arguments. As I did with the other languages, my first program in this development simply retrieved the arguments and printed them. So, here it is. import System.Environment main = do args <- getArgs print args 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. I figure the best way to learn a new language is to apply it something I might actually use it for. -- Chad Wilson

Chris, Welcome to Haskell! I'm not sure exactly what kind of comments you're looking for on this, but here's a discussion that I thought might pique your curiosity: Your main can actually be simplified even further to "main = getArgs
= print". Here's why:
In do-notation, whenever you see something of this form:
do
x <- someFunction
foo
...this gets de-sugared to "someFunction >>= \x -> foo". Don't get
scared yet.That >>= (pronounced 'bind') is just another Haskell
function, written in infix form (that means between its inputs, which
are "someFunction" and "\x -> foo" here). The "\x -> foo" is just an
anonymous function that takes a parameter, calls it x, and evaluates
'foo'. In your particular case, it becomes 'getArgs >>= \args -> print
args'. In this case, the 'foo' is 'print args'. So the anonymous
function is saying 'take the input, call it x, and pass it to the
print function', which is just what you want. The >>= is just saying
that it's going to take the result of getArgs, and use the output of
that, to feed into the input of the right side (the "\args -> print
args").
Now in haskell, whenever you see an anonymous function (also called a
lambda) of the form "\x -> f x", because of the way Haskell treats
functions, you can always simply replace this with 'f'. See where this
is going? Now your '\args -> print args' just becomes 'print', and
voila! So what you're saying, basically is, "take getArgs, and feed
the output of it into the print function". That's it!
Don't worry if you don't get this all right now, my intention was just
to whet your appetite. Have fun!
Andrew
On Thu, Jul 17, 2008 at 11:56 AM, Chad Wilson
I am a gamer. Table top RPGs, war games, board games, etc. Since my laptop is pen-based, I has been handier to keep it at the table than paper. As a result, I have been experimenting with making dice rollers to have yet another item not needed on the table. Sure, it takes away some of the tradition, but it makes for carrying fewer things. I have written various dice roll simulators in xblite, ruby and some other dynamics, but this seems like an interesting exercise to try in Haskell.
My first goal is to figure out how to get the command-line arguments. As I did with the other languages, my first program in this development simply retrieved the arguments and printed them. So, here it is.
import System.Environment
main = do args <- getArgs print args
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.
I figure the best way to learn a new language is to apply it something I might actually use it for.
-- Chad Wilson _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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#... [3]: http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html#v%... [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@riseup.net XMPP: marcot@jabber.org IRC: marcot@irc.freenode.net Telefone: 25151920 Celular: 98116720 Endereço: Rua Turfa, 639/701 Prado 30410-370 Belo Horizonte/MG Brasil

Chad,
You may want to take a look at the Random library to do that.
Random number generation depends (somewhat) on a state monad.
Regards
Rafael
---------- Forwarded message ----------
From: Chad Wilson

Hi Generally I would write a function to deal with arguments such that they needn't necessarily come from the command-line. So I write a function of type :: [ String ] -> IO () My general pattern is therefore main :: IO () main = getArgs >>= processArgs processArgs :: [ String ] -> IO () processArgs [ times, dice ] = {- logic for what you want here -} processArgs _ = error "You must provide exactly two arguments" attached is a sample script which I always start from when writing a simple program (it's actually in 'yi' as a template). regards allan Chad Wilson wrote:
I am a gamer. Table top RPGs, war games, board games, etc. Since my laptop is pen-based, I has been handier to keep it at the table than paper. As a result, I have been experimenting with making dice rollers to have yet another item not needed on the table. Sure, it takes away some of the tradition, but it makes for carrying fewer things. I have written various dice roll simulators in xblite, ruby and some other dynamics, but this seems like an interesting exercise to try in Haskell.
My first goal is to figure out how to get the command-line arguments. As I did with the other languages, my first program in this development simply retrieved the arguments and printed them. So, here it is.
import System.Environment
main = do args <- getArgs print args
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.
I figure the best way to learn a new language is to apply it something I might actually use it for.
-- Chad Wilson
participants (5)
-
Allan
-
Andrew Wagner
-
Chad Wilson
-
Marco Túlio Gontijo e Silva
-
Rafael Gustavo da Cunha Pereira Pinto