scotfl.ca

April, 2008

Pseudo-functional C

print( plus(&ten, &three) ); print( times(plus(times(&two, &three), four_fn), &ten) );

Result:

13
100

Reverse Polish Notation and a plethora of parentheses does not a functional language make. But they do impart a superficial resemblance. However, add a little context and suddenly it becomes a lot more ugly:

#include "functional.h"
#include "integers.h"


DType* plus (DType* a, ...)
{
    TWO_ARGS_ATOMIZED(a, b)
    return new_atom(a->atom + b->atom);
}


DType* times (DType* a, ...)
{
    TWO_ARGS_ATOMIZED(a, b)
    return new_atom(a->atom * b->atom);
}


DType* _four (DType *a, ...)
{
    return new_atom(4);
}


int main (int argc, char* argv[])
{
    DType* four_fn = new_nonatom(&_four);
    print( plus(&ten, &three) );
    print( times(plus(times(&two, &three), four_fn), &ten) );
}

This really needs lists and the usual suspects (car, cdr, cons, map, filter, and reduce). As well as some sort of call(List(fn, arg1, ...)), which I am pretty sure is as close to functional notation as I can get. As a bonus, lists will allow me to drop the varargs magic. Maybe I’ll add that next weekend. I had forgotten how much fun C can be, it was nice to go back to it for a while.

Git repo here.

Posted on 13 April 2008 in Uncategorized

1 Comment »

April Fools Day Is Played Out

Anil Dash is completely right on this. But it wasn’t always this way. For example here’s an oldie from Adam C Engst of TidBITS:

New PowerBooks — Just after the release of the PowerBook 3400, dubbed “the world’s fastest portable” by Apple, comes the PowerBook 1000, codenamed Falcon. Announced on 01-Apr-97 and bearing the affectionate slogan “the fastest hunk of junk in the galaxy,” the PowerBook 1000 is based on the diminutive PowerBook 100 design and features a 320 MHz low-power PowerPC 620 CPU, 80 MB of RAM, a 2 GB hard disk, a hot-swappable removable storage bay that supports a CD-ROM drive, Zip drive, or floppy drive, and ten hours of battery life on trilithium resin battery technology. Unique to the PowerBook 1000 is generalized wireless communication technology that enables the machine to act as a pager or cellular telephone, or to connect to the Internet via a wireless modem at speeds up to 53 Kbps. Prices are expected to start at $1,500. [ECA]

– From TidBITS issue 373

I remember that showing up in Claris Emailer at about 3am. And the feeling that came over me as I scanned the issue, half-asleep, and caught the phrases “PowerPC”, “PowerBook 100 form-factor” and “$1,500″ — it was wonderful. But then my brain started working again and I realized it was 1 April. That realization was like being punched in the stomach. Reading it now, 10 years later, I can only assume that I was really, really tired.

(For pricing context, I should point out the low-end PowerBook 1400 I got a few months later was a $4000 machine. I love that 1400.)

Posted on 1 April 2008 in Uncategorized

Comments Off