hornbeck

thoughts on business, community and customer service

Lisp-1 vs Lisp-2

with 5 comments

lisp books

While I’ve used Lisp for years off and on I never really grabbed onto the difference between Lisp-1 and Lisp-2. I think this is mainly because I have used Common Lisp which is a Lisp-2 and I have only toyed with Scheme which is a Lisp-1, so I never really thought about the differences. However, Clojure is a Lisp-1 so I figured it was about time to read up on it. After spending some time reading about it I figured it may be nice to document it here so that others may be able to understand it as well.

Lisp-1 and Lisp-2 refers to the different ways that namespaces for functions and variables are handled between the two. Common Lisp uses a separate namespace for functions and variables while Scheme uses the same namespace for both.

According to Richard P. Gabriel in his paper on the subject, the two are defined as follows

Lisp-1 has a single namespace that serves a dual role as the function namespace and value namespace; that is, its function namespace and value namespace are not distinct. In Lisp-1, the functional position of a form and the argument positions of forms are evaluated according to the same rules.

Lisp-2 has distinct function and value namespaces. In Lisp-2, the rules for evaluation in the functional position of a form are distinct from those for evaluation in the argument positions of the form. Common Lisp is a Lisp-2 dialect.

Knowing now the difference between the two, what does it really mean? The main difference for someone programming between the two is how you use functions inside of other functions and how you name your variables.

From the Wikipedia page on Common Lisp comes the example

(sort (list 5 2 6 3 1 4) #’>)

In this example the function “>” has to use the special form of “#’” in front to show that it is a function and not a value. This is similar to using (quote) or “‘” in front of a list to show that it is not to be evaluated.

In a Lisp-1, the sort would have been written

(sort (list 5 2 6 3 1 4) >)

Herein lies most of the debate between which is the better choice. While in this example it’s easy to see that “>” is a function with or without using “#’”, sometimes it’s not the case. Let’s look at another example, this time from the paper already mentioned by Richard P. Gabriel

(DEFUN PRINT-SQUARES (LIST)
  (DOLIST (ELEMENT LIST)
    (PRINT (LIST ELEMENT (EXPT ELEMENT 2)))))

In Lisp-2 this function will work just fine with ELEMENT being bound to the value of LIST in the DOLIST call and the final LIST is a function that will combine the element with the exponent of the element into a new list and print it to the terminal.

In Lisp-1 you would need to write the function as such

(DEFUN PRINT-SQUARES (LST)
  (DOLIST (ELEMENT LST)
    (PRINT (LIST ELEMENT (EXPT ELEMENT 2)))))

Using LST as the variable instead of LIST since you cannot use a variable with the same name as a function. Since I never really put much thought into it before and was just used to the way that Common Lisp did things, none of this ever bothered me. Now that I’m seeing the difference and knowing the reasoning behind each one I think I like the Lisp-1 approach a little better. I don’t like having the same name mean two different things within my entire codebase let alone two separate meanings within my same function. Maybe that comes from my experience with Erlang or just the way my brain works but it seems a lot cleaner to me. I liked this quote from Michael Arntzenius on the Arc Forum,

“The lisp-1 versus lisp-2 issue is an old one, as you note. I prefer lisp-1′s, because they are conceptually cleaner – they don’t make an arbitrary distinction between where to find values of a certain kind (functions) and where to find values of another kind (everything else). It reduces cognitive load not to have to think about “okay, is this going to be found in the function namespace or in the other-values namespace?”.”

Written by hornbeck

July 5, 2009 at 9:06 pm

Posted in lisp

Tagged with , ,

5 Responses

Subscribe to comments with RSS.

  1. Queinnec’s “Lisp in Small Pieces” covers the implementation implications of the choice between Lisp-1 and Lisp-2. It’s not just an aesthetic consideration.

    Zach Beane

    July 7, 2009 at 2:50 pm

    • Zach,

      I haven’t been able to pick that book up yet but will definitely check it out to see the implementation implications of this. Are there any online resources that go into this?

      hornbeck

      July 7, 2009 at 9:24 pm

  2. [...] What it means for Clojure to be a lisp-1 (instead of a lisp-2 as Common Lisp) (via @clarkgrubb) [...]

  3. As Zach suggests, the situation is more complicated than this, especially when module loading, namespaces and macros are added in. A good introduction to the history of the issue is here: http://www.dreamsongs.org/Separation.html

    Adam Solove

    September 24, 2009 at 12:11 pm

    • Thanks for this article! This is what I thought was the difference, but was unsure. I’m going to read the Separation page posted by Adam Solove above. I, too, was interested in knowing more as a result of interest in Clojure.

      Jerri Kohl

      April 7, 2010 at 8:27 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: