Something simple

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

Something simple

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20080717/f089315c/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Something simple

Andrew Wagner
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 <[hidden email]> 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
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
Reply | Threaded
Open this post in threaded view
|

Something simple

Marco Túlio Gontijo e Silva
In reply to this post by Chad Wilson
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: [hidden email]
XMPP: [hidden email]
IRC: [hidden email]
Telefone: 25151920
Celular: 98116720
Endere?o:
 Rua Turfa, 639/701
 Prado 30410-370
 Belo Horizonte/MG Brasil

Reply | Threaded
Open this post in threaded view
|

Something simple

Allan Clark
In reply to this post by 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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: SampleScript.hs
Type: text/x-haskell
Size: 1646 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/beginners/attachments/20080717/414ac4fc/SampleScript.bin
Reply | Threaded
Open this post in threaded view
|

[Haskell-begin] Fwd: Something simple

Rafael Gustavo da Cunha Pereira Pinto
In reply to this post by Chad Wilson
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 <[hidden email]>
Date: Thu, Jul 17, 2008 at 12:56
Subject: Something simple
To: [hidden email]


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
[hidden email]
http://www.haskell.org/mailman/listinfo/beginners




--
Rafael Gustavo da Cunha Pereira Pinto
Electronic Engineer, MSc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20080717/f09d9452/attachment.htm