Monday, August 21, 2006

Organizing Systems: ASDF

After working through some of the PCL and ACL books I was itching to write some of my own code. Using Darcs I created several repositories for the new projects and coded away. Lisp code is dense. It is easy to write a fairly interesting application in a couple hundred lines in one file. Soon I had utilities and applications spread across different Darcs repositories that I would like to load more easily than:

(load "/lisp/project/some-file.lisp")

ASDF (Another System Definition Facility) allows you to easily compile and load lisp code. It is widely used by Lisp projects on Cliki. To use ASDF you will need to compile/load the source into your Lisp image. Download and save this file, compile if you like, and load it when lisp starts. There are some howtos that show how to load ASDF into Lisp automatically when it starts.

ADSF-Install is an extension to ASDF that allows easy downloading and installation of code from Cliki. Both ASDF and ASDF-Install useful for getting open-source lisp code including utilities, web-servers, and just about anything you would need.

Sunday, August 20, 2006

Learning More Lisp

Beginning Lisp is about learning to type and read Lisp code. There are a basic vocabulary and visual patterns that must be learned in order to code in Lisp. The Common Lisp Hyper Spec (CLHS) is the primary page to learn the vocabulary.

Here are your vocabulary words for today:
The visual patterns are how Lisp should be indented to be human-readable. For instance if can be written either all on one line or on multiple lines.
(if (= 5 6) (print 'no) (print 'yes))
or

(if (= 5 6)
(print 'no)
(print 'yes))
If works by conditionally executing its second argument if the first argument is true. Otherwise the third argument is executed. When reading Lisp code the second pattern is easier to recognize. Many of the built-in functions have indenting patterns that make it easy to read lisp code.

Typing out examples is the best way to learn to write and read Lisp code. The book On Lisp by Paul Graham is full of excellent examples for the intermediate Lisp programmer. On Lisp is a good sequel to ANSI Common Lisp. On Lisp is also free to download.

Another route to learning Lisp is to work through the examples in Paradigms of Artificial Intelligence by Peter Norvig. This book is an excellent source of Lisp examples. Norvig's book is good for learning recursion and solving problems with Lisp. Graham's books are good for learning to extend Lisp through higher-order functions and macros.

Working through examples is these books is really the best way to learn Lisp on your own. Just reading the examples or just executing code downloaded from the website does not allow for full comprehension that does typing, running, debugging, and changing the examples.

More about lisp in the next post...

Learning Lisp

Here is a short guide to learn Lisp in a few months:

1) Find a implementation that works on your platform. Clisp works almost everywhere and is actively developed.

2) Start reading Practical Common Lisp (PCL) because it is free. Skip the part about SLIME and Emacs if you are not familiar with Emacs. Start Clisp and type out every example in PCL. You need to train your fingers to handle parentheses. Notice that Clisp does some amount of paren matching.

3) Soon you will want to save your work. So you will need to choose an editor. Either Emacs or Vim will work. If you are unfamiliar with either try to learn Emacs as it is the standard for Lisp. Vim is servicable if you have already trained your Vim reflexes. I am Vim-brain-washed so Emacs is doubly hard to learn. Vim supports Lisp very well with paren matching, automatic indenting, and syntax highlighting.

4) Choose either SLIME or a two-window environment. I prefer a two window environment with Vim in one and Clisp in the other. In vim create files instead of typing forms at the REPL. Run these files with: clisp file.lisp

5) Keep typing examples from Practical Common Lisp. At some point I switched to ANSI Common Lisp (ACL). I found it had more and better examples. Paul Graham's prose is clearer than Seibel's. Typing out examples is key for learning Lisp quickly. It forces you to put the time in to really understand the language and trains your hands for typing lisp.

6) Read, Type, Repeat. Again you cannot get around this. Read a section of PCL or ACL. Type out the examples. Reread the section and study the code.

7) Another good step is to add unit-tests to the examples in the books. This can be as simple as wrapping an example in a deftest or converting the REPL forms to tests. You can also use unit-tests to asks questions of the example for improved understanding. I wrote a unit-test package and a code-printing utility that have grown while I learned Lisp: unit-test and print-code (insert links here)

8) Get Darcs - At some point you will need a code repository to keep you from blowing away your precious code. Darcs seemed to be widely used in Lisp projects. It's a breath of fresh air if you have been using CVS.

More in the next post...