hornbeck

thoughts on business, community and customer service

Posts Tagged ‘lisp

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 , ,

Getting Started with Clojure

leave a comment »

ClojureAfter talking to a few people this past week I decided that I would spend some of my July 4th weekend giving Clojure a try. There are a couple of good resources available for getting started, the PeepCode screencast and the Programming Clojure book from The Pragmatic Programmers. I picked up both of these and watched the video right away, it was a nice introduction that got me more interested and ready to dive into the book.

Before getting into the book though I knew that I wanted to get setup in my editor. Phil Hagelberg has a great blog post on getting setup if you are using his Emacs Starter Kit, however I’m not using elpa for my setup so I went about things a little different.

I found this page that describes setting up slime with Clojure and followed it pretty much all the way through except for the code repositories that he uses and the paths to where Clojure lives on his file system. Instead of using the clojure-mode mentioned in the article, I used Phil’s repo as his seems to be a little more up to date.

Getting Clojure itself installed is pretty easy, I grabbed the source from github along with clojure-contrib as it has a bunch of nice add-ons. The only other piece you will need to get going is Java and Ant.

So far I’m impressed with the language but I’m still not sure how it will fit into my tool belt. Outside of Ruby, Erlang is where I spend all of my time hacking and Clojure seems to fit into the same niche.

Written by hornbeck

July 3, 2009 at 7:42 pm

Posted in clojure

Tagged with , ,