Sunday, January 31, 2010

Thinlisp

Came across Thinlisp today while browsing cliki. Sounds interesting - develop in CL and then deploy in C. Its similar to ECL in the way of using C as a bridge but in reality its completely different. ECL is a CL implementation that uses C as a compiler. Thereby you can generate very small and very efficient standalone executables.

Thinlisp on the other hand, is supposed to work on any CL implementation. It uses CL to generate C. It works with a subset of lisp and doesn't support any garbage collection. I had a look at it today, its pretty old. The code looks like its been abandoned by Gensym. Nevertheless, I decided to give a try. Will post results as I get going it with, but I do expect some amount of bit-rot. Vladimir Sedach seems to have hacked at it a few years back, and has posted tarball. That would be my starting point.

Saturday, January 16, 2010

functions in plists

Common Lisp Plists are easy. A plist is just a list with properties. Any element with ":" prefixed is a property. The next element to a property is its value. We can then manipulate the plist using functions such as getf and setf. We give the property name to getf/setf and they fetch/set the appropriate property.

Here's a simple plist
'(:name "sid" :language "lisp")

Now, if we want to put in a function in the plist as a value, then the quote will not work.
'(:name "hello" :function (lambda () (format t "Hello World~%"))) ; Bad

The correct way is to backquote the plist and comma-escape the lambda.
`(:name "hello" :function ,(lambda () (format t "Hello World~%"))) 


We can then extract the function using getf and then finally call it using funcall. If we want to use an existing function, then we need to not only comma-escape but also add a #' so that the reader knows that a function symbol follows.

In C the equivalent would be to have a struct with a function pointer. What makes Lisp cool is that
  1. You can define an anonymous function in the plist itself
  2. You can have functions with different arity, inputs etc.. all in a list of plists. Lisp won't complain, but in C, the function pointer needs to be function of a particular signature only.
  3. Lisp also allows you to define structs which are more powerful than just plists, but a plist is good enough for this example.

Thursday, January 14, 2010

SBCL - FASL files

One aspect of SBCL that you *will* run into if you upgrade, is that older FASL files will no longer load. SBCL ties the FASL format to a specific version. That means that you recompile all the files when you upgrade. The manual gives a convienent snippet if the files are using ASDF.