Friday, June 12, 2009

Emacs lisp basics

Lisp is the most simple programming language ever and I hope this post will prove that to you.

Lisp Lists

Lisp stands for LISt Processing and hence the list data type plays a pivotal role in any lisp program. So what is a list? In plain english, a list is a sequence of elements.By a sequence we mean that the order in which the elements appear is important.
For example 1,2,3,4,5 is a list of the first 5 natural numbers and is not the same as 5,4,3,2,1 which consists of the same elements but in reverse order.
To represent an arbitrary list of elements b0,b1,, we write down the elements of the list in order separated from each other by whitespaces and surrounded by parentheses.
     (b0 b1 b2 b3 ... bn)
Thus if you had a shopping list ordered as
   eggs, and you wanted to write it down in lisp then you would write it as
   (milk bread butter eggs) Since the number of white spaces between elements in a list does not matter, you can also write it as

All elements of the list need not be of the same type as the lisp interpreter does not enforce any type on the list. Thus we could have integers, words and lists inside a single list as in
  1. (this is a list of 7 elements)
  2. (An list that contains a (list inside a list))
Please note that the second list contains only 6 elements the list (list inside a list) is treated as a single element as far as the outermost list is concerned.

Lisp Interpreter

Now that we know how to write a list, the next thing to do is to write a lisp program. A lisp program is a special type of list where the first element is a function and the following elements are its arguments.

A lisp program is a list of the form (b0 b1, where b0 is the name of a function which takes 'n' arguments b1,b2 ... bn.

A function can be viewed as a black box thus,

For example there is a function called quote that takes a single argument and returns the argument as it is.
The list form for a quote argument is (quote arg1) where arg1 can be any number or string or list.
   (quote 5) ==> 5
   (quote "Hello World!") ==> "Hello World"
   (quote (this is a list of 7 elements)) ==> (this is a list of 7 elements)
   (quote (milk bread butter eggs)) ==> (milk bread butter eggs)

Now, if you want to "run" or "execute" the above programs, please copy them into emacs and place the cursor after the last parentheses of any program and press C-x C-e and you will notice the result of the program in the echo area.

P.S.:- If you are not able to comprehend what C-x C-e means please refer to my previous post and the one prior to the previous post where I give an intro to emacs.
The quote is a function that is very widely used and hence instead of writing (quote arg1) we can use 'arg1 which is a single apostrophe before arg1. As an exercise try out a few programs using 'arg1 instead of (quote arg1)
As another example, let's have a look at the function represented by '+' which takes arbitrary number of integers as arguments and returns the sum of all its arguments. Thus,
(+) ==> 0
(+ 1) ==> 1
(+ 1 2) ==> 3
(+ 21 45 22 200 212) ==> 500
and so on...
If you have a list (b0 b1 b2 ... bn) and you try evaluating it but b0 is not a function then what happens? As you would expect an error is generated.

For instance if you write a list
(this is not a function)
and try executing it in emacs 21 or later versions, you will get the following error :

To get out of the error press q.
(void-function this) is the error generated. It means that the element 'this' does not have a function definition associated with it.

That's it! This is all there is to any program in lisp. There are more details to programming in lisp but at the heart of the matter this is all there is.


Post a Comment