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.
- In Lisp everything thats written is called a s-expression, which are basically of 2 types. Atoms and Forms.
- 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).
- Forms, the 2nd type of s-expressions are always enclosed within paranthesis.
- 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.
- 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.
- 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!)
- 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’.
- 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).
- 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.
- 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.)