Perl vs Lisp

Yesterday, a redditor judged important to resurrect a 7 year-old post by the colorful Usenet troll, Erik Naggum. The post itself is about Perl vs. Lisp and isn’t very interesting because Mr. Naggum is probably the most biased person to post on comp.lang.lisp.

This post generated a significant amount of comments on Reddit and I found one comment particularly interesting

Perl vs. Lisp? Seriously? They aren’t even remotely similar languages. Why not Basic vs. Assembly? Sheesh.

He has a point: syntactically, Perl and (Common) Lisp are probably the languages least similar. They do however share a significant amount of semantics, and those similarities are certainly more important than syntax. Both languages are garbage collected, are dynamically typed, can treat code as data, are lexically scoped, have closures, and many other less important features or features I am forgetting about. In fact, I think the only thing that Lisp has that Perl lacks are macros (even then, Perl 6 will have macros.)

Let’s look at some simple examples of Perl and Common Lisp code. Here is a counter function. It takes a starting value as a parameter and returns a function that increments the counter by one and returns the new value (actually, that’s not quite exact for the Perl version.)


# Perl
sub counter {
    my $start = shift;
    return sub {
        return $start++;
    }
}

my $counter = counter(0);
for (1..10) {
    print $counter->(), "\\n";
}

; Common Lisp
(defun make-counter (start)
  (decf start)
  #'(lambda ()
      (incf start)))

(let ((counter (make-counter 0)))
  (dotimes (_ 10)
    (format t "~a~%" (funcall counter))))

When you run these two programs, they return the exact same output. Now of course, you could write a program that returns the same output in any language. The real interest of these programs is that they use the same, exact method to accomplish their task. Because functions are data just like strings or numbers in both languages, a function can return another function. The solutions also depend on the language having lexical scoping, so that the values are kept around between each call. You can convert the Common Lisp code to Emacs Lisp and you’ll see that it doesn’t work, Emacs Lisp will complain that the start variable does not exist.

I could give plenty more examples of how Perl and Lisp are similar but Mark-Jason Dominus has done a better job than I could ever hope to with his Perl book, Higher-Order Perl.

Knowing a language

Adam Barr talks about what is “knowing” a language on his blog. He argues that knowing a language does not involve knowing the built-in functions (or the standard library.) I must strongly disagree with this view, because if you deal with the lowest level of a language — in this case Python — you don’t write idiomatic code, which means that people who know the language cannot easily understand your code.

Let’s look at a simple Python example, determining the largest element of a list. The person who doesn’t know Python would write:


max = my_list[0]
for my element in my_list[1:]:
    if element > max:
        max = element
print max

This works (sort of; it would fail for an empty list), but no Python programmer is ever going to use that. The proper way is to use the built-in max() function:


print max(my_list)

There are many other core functions that should be known before one can claim he/she knows Python and a lot of modules too. Don’t try to create your own temporary files, just use the tempfile module! If you try to do it yourself, you expose yourself to a bunch of problems that have already been solved.

Knowing a language is more than just knowing the syntax and the reserved keywords, it’s knowing the core library, the standard library, it’s knowing which features to use and which to avoid. Otherwise, I could claim that I know Java, although my lack of knowledge of the libraries would make me extremely unproductive.

Changed the name of my blog

You may have noticed that I changed the title of my blog from “Uncentered Mind” to “Occasionally sane”. I was talking on IRC about Perl 6 (to a Perl-hating crowd), and here’s something one of the people there said to me:

<jemfinch> and you, whom I consider to be occasionally sane, completely disappoint me by being interested in Perl6.

I thought it was so awesome that I decided to make that the new name of my blog :)