Archive for November, 2007

Monad Wars – code online

Chessguy pointed out that it's currently hard to play along with the monad wars code. It would be nice for the posts to be “literate haskell”, where sections preceded by “>“ characters are valid Haskell. The idea is great – that you can mix sections of introduction and description with sections of actual code, ending […]

Perl snippet – repeated arrays

On #moose, the channel for a popular modern dialect of Object Oriented Perl, Debolaz asked: <Debolaz> Is there a simple way to produce an array of array like [[1..4],[1..4]] without having to specify the [1..4] multiple times? Matt Trout's reply confused me at first <@mst> map { [ 1..4 ] } (1, 2) because I […]

Monad Wars – 3: Command line actions

After the last post, we have parser actions that can recognise an integer or an item of merchandise. Now we need to be able to process a command, like “jet bronx” or “buy 4 lambdas”. Let's start off with this basis: > parseCommand = parseMap commandMap > commandMap = getPrefixMap [ > ( "buy", cmdBuy […]

Monad Wars – 2.5: some comments and corrections

One of the advantages of demonstrating your ignorance in public is that you may receive useful corrections… thanks to everyone who replied on these recent posts, I found the comments very instructive, and thought it was worth writing up as a new post. Strict records ddarius got in touch to mention that I might want […]

Monad Wars – 2: the command line

This time around, we're going to look at how we'll turn user input into commands in Monad Wars. I think that the easiest option to implement will also be very convenient to play with: a command line where we issue commands like: $ buy 4 foo $ sell 20 bar $ jet bronx or with […]

Monad Wars – 1: the Prompt

A lot of learning projects involve writing games: people have written clones of Tetris, Asteroids, Space Invaders, and even first person shooters (Frag) in Haskell. As I'm far less clever than these people, I thought I'd start with something a bit simpler: Dope Wars. Dope Wars is basically a trading game. In 30 turns, you […]

Common roots

This morning, Ranguard asked an interesting question on #london.pm: 11:27 <@ranguard> What's the best way of finding the common root of two paths, e.g. for /a/b/c/d/e, /a/b/c/1/2/3 I want /a/b/c/ returned, Path::Class::Dir has 'contains', but I'm not sure that's quite right? In my copious free lunchtime, I thought I'd write a version of it in […]

Haskell snippet – recursive average

Greg confessed on #london.pm to having written a average function in Perl in FP style… recursively. I asked him about this, as a perfectly good average function (using List::Util would be: sub avg { sum(@_) / @_ } which is perfectly fine FP style too. As far as I could understand it, he was reducing […]

Perl snippet for getPrefixMap

Some interesting comments on yesterday's Haskell post.  I thought I'd write this in Perl to compare.  Of course, we don't have an "inits" function in the standard library, but that's easily written: #!/usr/bin/perluse strict; use warnings;use Data::Dumper;my %hash = ( "key1" => 1, "key2" => 2, "other" => 3,);sub inits { my $string = shift […]

Haskell snippet – getPrefixMap

Here's a little snippet I worked on yesterday, while preparing my talk for the London Perl Workshop.It takes a list of tuples ("string", whatever) and maps all the prefixes of the string ("string", "strin", "stri", etc.) to the whatever. I was quite impressed at how easily this came together.  The functional composition (pipelines connected with […]