Tuesday, November 22, 2005

 

Lisp with a different syntax

I've been reading Ajax in Action, and have been reminded of the quote "Javascript is Lisp with a different syntax". It's usually used to stress the functional and closure features of Javascript, but there's another part that's also very close - attaching arbritrary properties to an object, including functions.

Compare

>var x = new Object()
>x.one = 1
1
>x.two = 2
2
>x.doubler = function(n) { return n * 2; }
function (n) { return n * 2; }
>x.two
2
>x.doubler(3)
6

with

CL-USER> (setf (get 'x 'one) 1)
1
CL-USER> (setf (get 'x 'two) 2)
2
CL-USER> (setf (get 'x 'doubler) #'(lambda (x) (* x 2)))
#<CLOSURE :LAMBDA (X) (* X 2)>
CL-USER> (get 'x 'two)
2
CL-USER> (funcall (get 'x 'doubler) 3)
6


In old Lisp textbooks I've seen this referred to as "data driven programming", used before anyone was talking about object-oriented programming. Now, no one would do OO this way in Lisp, but the quote didn't say "Javascript is Common Lisp with a different syntax".

Comments:
You might want to try KR which is a prototype-based object system for Common Lisp. It is a mixture of what you describe and Kenny Tilton's Cells.
 
Thats cool. I have done some javascript but didn't know that one could do that.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?