Archive for July, 2007

thumper: My Ugly Little Ocaml Web Server

Spent the afternoon cobbling together a simple Ocaml web server and - to spite my normally reclusive attitude when it comes to personal projects - I thought I’d share the results. The interesting stuff is in thumper.ml. Refer to line 215 if you’re interested in implementing your own handlers for primitive resources.

I’m sure there’s room for improvement, so I’ve uploaded a tarball of the bzr repository. You can get it here: Thumper v0.1. Any suggestions/patches?

Note: This is not the next part of my series of Ocaml tutorials. That’s likely to come after I’ve finished moving. :)

The Many Humps of Ocaml, Part 1

DRAFT: I’m still learning Ocaml myself, so some of what I say here may be plain wrong or misguided. I’m relying on those more experienced with Ocaml to correct my mistakes! If you have plenty of Ocaml experience, please correct my mistakes. By all means, be brutal!

Welcome to the very first part of my (hopefully enlightening) series of tutorials on the Ocaml programming language! In this tutorial we’ll be learning the Ocaml flavour of everybody’s favourite program: Hello World.

Since those interested in Ocaml will likely be coming from such a background, please note that this tutorial is written under the assumption that readers will have experience with other, imperative languages such as Java or C++. Now, on with the code:

let main () =
print_endline "Hello, World!" ;;

main () ;;

Save this file as tmhoo01.ml. You can run this program from the command-line using the following:

$ ocaml tmhoo01.ml

Or compile it to native code:

$ ocamlopt -o tmhoo01 tmhoo01.ml

Predictably, running this program displays “Hello, World!” in the console window. It’s not the output we’re interested in, however: it’s Ocaml’s weird ass syntax! Let’s take this line by line:

let main () =

This declares a function called “main”. let is an Ocaml keyword which defines named expressions (sometimes called let-bindings). In this case, the name of our expression/let-binding is “main”.

main takes a single parameter: the parentheses () represent what is known as the “unit value”. It is the only possible value for the unit type, and has a similar use and meaning to what void has in C/C++. In this case, it means the function main accepts no other parameters. This is necessary because Ocaml functions must always be applied to one or more parameters: thus, when we have no parameters to pass we must resort to using (). If we do not accept this unit parameter at a minimum, the expression in main is evaluated at the next double semi-colon ;;. This is not our intention.

  print_endline "Hello, World!" ;;

The print_endline call does as you would expect. Similar to System.out.println or printf. print_endline returns the unit value. Implicitly, our main function has a unit type. This distinction may not completely make sense just yet, but the importance of this will make sense later once we start doing more work with different types.

The ;; keyword is used to separate multiple top-level constructs (e.g. let-bindings and class definitions). Later, you will use ; to separate expressions within other constructs.

main () ;;

As you would expect, this calls our main function.

So that’s our first Ocaml program dissected. Thanks for reading! If you have any questions or comments, please post them here: I’ll surely be revising this article based on your recommendations.

UPDATE 1: Correction to the descriptions of ; and ;;. Thanks Paul!
UPDATE 2: Removed my haughty claim that Ocaml references are more like C++ pointers than Java references. Cheers Chris!
UPDATE 3: Tried to otherwise simplify the tutorial. Less “blah blah blah”!
UPDATE 4: Remove introduction to references all together. Several people felt it was out of place to introduce a concept like references in the first tutorial. I happen to agree. :)

Moving to Melbourne

I’ll be packing up and shipping off to Melbourne, Australia at the end of July to be a little closer to my Melbournian girlfriend. Exciting - if stressful - times ahead with the big move! I’m also looking forward to learning all I can from the top-notch Java developers at Shine Technologies when I start my new job there on the 6th of August.

I’ve started writing my Ocaml tutorial after tinkering further with the language over the past few days. I was hoping to have part 1 sorted out tonight, but it’s late and I’m exhausted. Sorry for the delay. It is coming. Honest.

CodeIgniter: A PHP Framework That Doesn’t Suck

I stumbled across CodeIgniter for the first time today. It seems people have been using it for a while, but this is the first time I’ve been made aware of it. I had a chance to give it a run through the gauntlet on a recent project.
I have to say it’s actually pretty decent as far as PHP frameworks go. It’s unobtrusive without being half-assed. It’s powerful without feeling overwhelming. It just damn well works and doesn’t try to do more than it should.

This is high praise coming from me. Despite the six years of commercial PHP development behind me, I think PHP sucks. Further, I’ve rejected outright frameworks like CakePHP and Symfony, most of which feel like they were too busy wishing they were Rails and doing a little too much “magic”. Ruby makes the magic in Rails beautiful and manageable. In PHP, things feel far less elegant and the “Rails-esque” PHP frameworks often reflect that awkwardness. CodeIgniter avoids this pitfall by providing a solid foundation on which to build web applications and sites without getting the way.

It’s not all roses though. Like any other framework, CodeIgniter has its warts: The validation and pagination libraries are a little awkward to configure, the “Model” portion of the MVC framework feels relatively weak, it wouldn’t hurt to merge some libraries/helpers and add others with more utility. Inevitably you still suffer from PHP’s shortcomings: a flat namespace, shit OOP support (for PHP4, anyway) and the unpredictable naming conventions of the standard library. Not that this is CodeIgniter’s fault, just another failing of the PHP as a development language and platform.

On the whole, however, CodeIgniter gives a big bang for its buck and, given a few years, could prove to be a formidable framework indeed should Zend come up short. I highly recommend giving it a careful look and evaluating it for your next web project.