Archive for February, 2007

The Pleasure of Interactivity

Wednesday, February 21st, 2007

What’s so great about Lisp? This question is frequently asked by people who wonder what such a weird-looking language could offer.

The usual response is often “macros.” As a one-liner, this is probably a fair answer. However, it’s not very enlightening, since the questioner isn’t going to have any idea what a Lisp macro is, or what it can do. Telling someone that penicillin is great because it is an antibiotic isn’t very useful if the questioner has no idea what bacteria are or what role they play in causing illness.

The thing *I* like best about using Lisp is that it’s interactive.

My current project is a Mac OS X application written in Objective-C. I am using Apple’s Xcode developement environment, which contains numerous fancy features.

Yet, I still have an emacs running, talking to an inferior lisp via SLIME.

Sometimes I use the lisp as a calculator. Sometimes I’ll get a function working in Lisp and then re-write it in C for insertion into my application. Sometimes I’ll re-write some troublesome C function in Lisp and debug it from the lisp.

Having the ability to do this sort of work interactively and incrementally is a real pleasure. No recompiling files, no special debugger commands, just the whole Lisp environment at your fingertips all the time.

Probably prime numbers

Friday, February 9th, 2007

I was browsing through a copy of the New Turing Omnibus, and ran across the article on detecting primes.I grabbed an algorithms text, and implemented the Miller-Rabin primality test in Common Lisp. (more…)

Determining the default route

Monday, February 5th, 2007

A subscriber on cocoa-dev@lists.apple.com was asking whether there was some way to programmatically determine the default route.

The cheap and sleazy way would be to read the output of netstat, but that’s not very aesthetically appealing.

Here is some sample code, derived from the source for netstat, which will find and print the IPv4 default route.

Getting disk insertion/removal notifications

Monday, February 5th, 2007

On Mac OS X, there is a daemon called diskarbitrationd that can notifiy interested clients of the appearance of disks and filesystems. Users talk to diskarbitrationd via the Disk Arbitration framework.

For some reason, the current developer documentation doesn’t have much to say about this framework. It therefore appears to be necessary to grovel through the headers to figure out how to use it. Fortunately, the headers are well-commented.

I was able to throw together a trivial program in about 15 or 20 minutes from first looking at DiskArbitration.h. Maybe this will help someone out there.

#include <stdio.h>
#include <DiskArbitration/DiskArbitration.h>

void hello_disk(DADiskRef disk, void *context)
{
    printf("disk %s appeared\n", DADiskGetBSDName(disk));
}

void goodbye_disk(DADiskRef disk, void *context)
{
    printf("disk %s disappeared\n", DADiskGetBSDName(disk));
}

main()
{
    DASessionRef session;

    session = DASessionCreate(kCFAllocatorDefault);

    DARegisterDiskAppearedCallback(session, NULL, hello_disk, NULL);
    DARegisterDiskDisappearedCallback(session, NULL, goodbye_disk, NULL);

    DASessionScheduleWithRunLoop(session,
        CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

    CFRunLoopRun();

    CFRelease(session);
    exit(0);
}