Sunday, October 16, 2005

Tiny Lisp course - Lisp in 10 bullets.

With my final year project coming up and with my decision to use Common Lisp for the project for both the compiler and the virtual machine, I had to give my friend and project mate venky a simple introduction to get him to the proficiency of reading and understanding lisp code. So I devised this " Lisp in 10 bullets" so that we can get him to start hacking around common lisp.

Since we are using both scheme and common lisp in the project, this 10 bullets had to be general to include both the popular dialects. Also, since he is not a complete newbie to programming (he's a decent javascript/C/C++ programmer), I don't have the need to explain things like what an 'if' does or what a list is. So here is the list.

  1. In Lisp everything thats written is called a s-expression, which are basically of 2 types. Atoms and Forms.
  2. Atoms are stuff like numbers (10, 234.56, etc..), characters , strings, boolean literals like T (true) and NIL (false) and variable names (called symbols in Lisp, which are associated with other values).
  3. Forms, the 2nd type of s-expressions are always enclosed within paranthesis.
  4. Lisp works by evaluating the s-expression and printing the output, which is the value of the s-expression. Each s-expression's value depends on what it is (atom/form) and what type of an atom or a form it is. The process of finding the value of an s-expression is called evaluation.
  5. Literals (atoms like 12, #\a , "Hey", etc.. basically numbers, chars and strings) evaluate to themselves, i.e 10 evaluates to the number 10. Atoms like symbols evaluate to the value that they are associated to.
  6. Forms are of two types - Functional application and Special forms. And they are always enclosed within paranthesis (this is just rule 3 again, but to drive home the point!)
  7. Special forms are written as (special-form-name arg1 arg2 arg3 arg4 ... argN) and special forms dictates how and when (even wether) each of arg1 to argN is evaluated. Functional Application is simpler, even though written in the same manner as (function-name arg1 arg2... argN), it works by evaluating all the values from arg1 to argN first and then passing them to the function 'function-name'.
  8. Functions in lisp are a bit different and there is a 2 step process to work with them. First the action of creating a function which is done using the lambda special form and then process of associating the function with a name. (i.e, its upto you to assign functions a name, you can leave it if you don't need it).
  9. Lisp has a data structure called the list, whose written literal representation is indistinguishable from that of forms. Its enclosed within paranthesis and its elements seperated by whitespaces. So basically you can see any lisp program as a bunch of atoms (which are data anyway) and lists. This means that lisp code and lisp data are indistinguishable.
  10. Since code is indistinguishable from data, you can write programs to change/generate code because its basically working by modifying lists. Since Lisp provides you with an eval function you can evaluate your generated code at runtime, and Lispniks call this feature 'Macros'.

Hmm, thats my simple 10 bullet guide to lisp, hope it was helpful to all those readers who are interested.

Update 1: As far as point 10 goes, thats not what Lispniks call macros, Macros are simply programs which change/generate code, but this happens during compile time. (however the point is still valid, you can evaluate your code at runtime using eval.)

Signing Off,
Vishnu Vyas

3 comments:

Anonymous said...

Just a quibble with point 2. Symbols are not just variable names, and calling them such loses some of what makes lisp so unique and strange. It would be more accurate to say that symbols are just that, symbols, which can be bound to values.

Anonymous said...

Hello anonymous,

Totally agreed.
Yeah, i know they aren't, but its easier for newbies who aren't used to what symbols to first identify with variable names in the begining, this is just a small tutorial after all.

Jacques Mattheij said...

minor nitpick, it's parenthesis