Clojure is a Lisp for the JVM. I don’t normally like Lisps - the code tends to feel cluttered, and the libraries are scattered and difficult to penetrate. Whilst in Python you’d perform a ‘import string’, in PLT Scheme you’d have to ‘(require (lib "string.ss" "srfi" "13"))’.
Clojure solves that by having a small, functional core, and farming everything else out to Java. It works surprisingly well, and the code ends up looking rather clean. It’s rather similar to Arc in style; with short function names, a small core, and liberal amounts of simple syntax sugar. Where Clojure differs is it’s more functional style, it’s advanced concurrency primitives, and it’s integration with the Java.
Neverwinter Nights and its sequel use a custom scripting language called NWScript. Why Bioware didn’t use something like Lua, I’m uncertain, but reinventing the wheel isn’t exactly a rarity in the software industry.
NWScript is very much like a cut down version of C. Unfortunately, it lacks arrays, or any form of list-like data structure. This makes designing complicated scripts in it rather trickier than it otherwise would be.
To this end, I’ve created nwn.ss, a PLT Scheme module for turning a custom S-Expression into valid NWScript.
When I program in my spare time, I usually do enough work to cover all of the interesting parts, and then abandon the project to move onto something new. I guess this is because I put more of a focus on the problem than the finished result.
Because of this, it’s something of a pleasant surprise that I am continuing to work on my Haskell HTTP server over a month after I started on it. I’ve decided on a name, Haffle, and I’ve just finished version 0.0.3. Haffle 0.0.3 can read POST variables and GET query strings, so it’s starting to become almost useful.
I’m a little dissatisfied with the libraries available for authoring web pages in Haskell. I want something that takes full advantage of Haskell’s do-notation to produce simple, readable code. Something like this:
main = httpServe 8080 $ do GET "/hello" ==> index index :: Reader HttpRequest (HttpResponse -> IO HttpResponse) index = do name <- param "name" setHeader "Content-Type" "text/plain" output "Hello " output name
I’m still a way from finishing it, but I’ve managed to create a HTTP request parser and a basic server. I’m aiming for a initial 0.0.1 release that will allow me to specify fixed outputs for any valid HTTP request:
main = httpServe 8080 (output "Hello World")
From there, I hope to add features with each release, until I have something usable. The code will be available under a MIT license.
On Ubuntu Gutsy, the embedded terminal plugin for gedit didn’t seem to work for me. It may not work for you, either. If it doesn’t, you can fix it with the following steps:
At the terminal, type: sudo gedit /usr/lib/gedit-2/plugins/terminal.py
Find the line that says:
self._vte.fork_command()
It should be around line 95.
Replace the line with:
shell = os.getenv("SHELL") self._vte.fork_command(shell, argv = [shell], directory = os.getcwd())
Save and quit. When you next start up gedit, the embedded terminal should work.
Sometimes, when working in Ruby on Rails, I find myself writing code like:
foobar = FooBar.find(:first) default = foobar.name if foobar
In Haskell, this would be a classic usage case for the Maybe monad. In Ruby, we don’t quite have the capabilities to make it implicit, but we can approximate the behavior explicitly:
class NilClass def maybe(*a); self; end end class Object alias :maybe :send end default = FooBar.find(:first).maybe(:name)
A small hack perhaps, but it saves a bit of typing.
A while ago I posted some Haskell code for sorting a table of hierarchical ordered data into a tree. Since then, I’ve learned a little more Haskell, and about half of the code I wrote is covered by standard libraries, or not necessary. Here’s an slimmed down version that makes greater use of GHC’s wealth of libraries, and for bonus marks, it’s also point free:
I’m not a very organised person. I try to be, but it takes a lot of effort, and, being a programmer, I’m a naturally lazy sort of person. Whenever there’s a repetitive problem, I lean toward automating it in the most generally applicable way possible.
There are any number of tools for organising information, but they all tend to be either highly specific, such as a todo list, or very difficult to automatically organise and sort, such as a wiki. Ideally, I’d like a system that gives me the best of both worlds; the ability to restrict data to a specific form, but a form of my choosing. Essentially, I want a database.
However, most databases aren’t designed for quick, user-friendly, interactive use; that’s why we spend so much effort putting pretty frontends on them. Unfortunately, these facades often limit what we can do. Once we wrap our database in a GUI, we can no longer execute abitrary queries to pull out the data we want.
So what I really want is an interface that allows me to retain all the flexibility of the underlying database. In other words, I want a CLI, a REPL, an interactive prompt which I can use to query and build my database. And for this I need a query language that is both concise and succinct, something I can use to create queries in seconds, and something whose entire syntax can fit on the back of a postcard.
As a general rule, I find I’m not happy with a software project until at least the third ground-up rewrite. Rewriting is usually discouraged in business, and not without some good reason. But in my spare time, I often find myself happily refactoring my code, each design better than the last, but never quite getting any further before I spot some flaw that inspires another redesign.
Haskell seems to encourage this constant honing, not just for complex modules or sophisticated libraries, but even for the smallest function. It’s refactoring in miniature; a sort of code bonsai.
I was recently trying to create a netstrings implementation for Haskell, and I started off with a function that would strip the first length indicator off. So for instance, “5:hello,” would become (5, “hello,”)
readLength :: String -> (Int, String) readLength s = (read n, tail s') where (n, s') = break (== ':') s
This was my first attempt. Did it work? Yep! Could I do better? Maybe…
Ever heard of ISBL?
You’d be forgiven if you hadn’t. Wikipedia devotes only a single sentence to it, and you’re unlikely to find it mentioned in any database textbook. It’s a relational database language that was developed during the 1970s at IBM’s UK Scientific Centre, around about the time that SQL was being spawned in IBMs US labs.
ISBL never took off, whilst SQL became ubiquitous. This is a shame, because ISBL was an elegant system that used only six query operators, and was firmly based on Codd’s relational model; conversely, SQL was a mixed up mess of inconsistent keywords and terrible design decisions.
In today’s SQL dominated world, modern programmers may be sceptical of my opinions. Sure, SQL may have it’s quirks, but it’s not that bad a language - is it? Rather than lecture you, I’ll provide an example…