Posts filed under “haskell”

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 […]

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 […]

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 […]

Haskell 'words' and Perl 'split'

Haskell’s prelude has a function words that splits a string by spaces. Prelude> words “nice cup of tea” ["nice","cup","of","tea"] Apparently the question comes up quite regularly on irc or haskell-cafe as to why this function is specialised to split only on whitespace. Perl’s split, for example, can split on any character, or indeed string or […]

For loops in Haskell

Functional programmers often seem to complain that for loops are better off done with maps, and that, in any case, they're just degenerate cases of the same thing, as all imperative constructs can be created in a fully functional way… That being the claim, I thought it might be a fun exercise to try to […]

More on Maybes and Haskell Ternary

I was playing with the Maybe type and realised that there are useful functions provided to play with these types. Then I realised that in my definition of ternary operator in haskell the definition of (!) > Nothing ! a = a > Just b ! _ = b was very similar to the predefined […]

Chapter 9: More about Higher Order Functions (part 1)

I looked at chapter 8 in terror and ran to the (comparative) familiarity of this chapter. Currying I remember a couple of years ago, when I was looking up Functional Programming early on, I came across the idea that functional programming languages didn't take multiple arguments, but returned a function at each step. I think […]