RSS Amplifier

naff.dev · Jul 11, 2026

A Look at the Example Code From Erlang: The Movie

0
Sign in to vote or save

naff.dev

Near the beginning of the universally acclaimed short film Erlang: The Movie, the following Erlang program is given to demonstrate the succinctness of the declarative style:

X=person(address("Big street",23),
         telno([1,2,4,6,7,9]))

This is contrasted with an equivalent imperative C program so large it has to be scrolled so the camera can capture it all. As the Erlang program only seems to be defining a record (I don’t know Erlang, apologies for any butchered nomenclature), I was curious how it was implemented in C.

I couldn’t find any existing transcriptions of the C program, so here’s my best attempt:

#define NULL 0

struct address {
    char *street;
    int number;
};

struct person {
    struct address *addr;
    char *telno;
};

struct address *make_address(street, num)
char *street;
{
    struct address *a;

    if ((a = (struct address *) malloc(sizeof (struct address))) == NULL)
        return (NULL);

    if ((a->street = (char *) malloc(strlen(street) + 1)) == NULL) {
        free(a);
        return (NULL);
    }
    strcpy(a->street, street);
    a->number = num;

    return (a);
}

free_address(addr)
struct address *addr;
{
    if (addr) {
        free((void *) addr->street);
        free((void *) addr);
    }
}
struct person *make_person(addr, tel)
struct address *addr;
char *tel;
{
    struct person *p;

    if ((p = (struct person *) malloc(sizeof (struct person))) == NULL)
        return (NULL);

    p->addr = addr;

    if ((p->telno = (char*) malloc(strlen(tel) + 1)) == NULL) {
        free(p);
        return (NULL);
    }
    strcpy(p->telno, tel);

    return (p);
}

do_something()
{
    struct address *a;
    struct person *p;

    ...

    if((a = make_address("Big Street", 25)) == NULL) {
        /* Do rest of error cleanup. */
        ...
    }

    if ((p = make_person(a, "124679")) == NULL) {
        free_address(a);

        /* Do rest of error cleanup. */
        ...
    }

    ...
}

The only frames where the text comes into clear focus are before and after the camera move, so there was a fair amount of extrapolation on my part, filling in the blurry shapes of the code with the variable names and patterns I could make out. I’m 99% sure the capitalisation of “Street” differs between the two, but only 60% sure the street number is also different.

I’ve not written any pre-ANSI C before, and whilst I knew about the different function definitions, the lack of pointer-coercion was new to me. If you remove the obvious syntactic errors from the ...s, add a main, and disable enough warnings, you can compile and run this with a modern GCC.

As to whether this a fair comparison, I have mixed feelings. Obviously, handling data-structures and the relevant memory needs more bookkeeping in C than a language with a garbage collector. Furthermore, the example program doesn’t really compute anything, so you could argue an empty C program is technically equivalent. On the other hand, this example doesn’t exactly show Erlang’s strongest suit—C would be sure to lose out on implementing actor-based concurrency.

Overall, I suppose, this example is more an argument for languages with automatic memory management than for declarative languages. The same example in Python would be similarly pithy as in Erlang. Perhaps that was once a much bigger selling point of Erlang, and it’s only amongst today’s crop of languages that concurrency and the BEAM are its more notable features.

Read the original on naff.dev

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.