wagner.andrew at gmail.com
Thu Jul 17 12:22:26 EDT 2008
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:
x <- someFunction
...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
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!
On Thu, Jul 17, 2008 at 11:56 AM, Chad Wilson <chadrwilson at gmail.com> 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
> Beginners at haskell.org
More information about the Beginners