Wednesday, February 24, 2010

The LISP-end of the computing

LISP never introduced until very late in school and that too in the form of ONE slide (yes true) one slide. So what is it in LISP that deserves so little concern. Looking at MIT OCW lectures it seems to be THE language of choice. So whats lisp infact? LISP (LISt Processing) is a language where the primary data structure is list and modus operandi is list operations.

Step 1:  Installation
Installation of lisp on aptitude based package manager (like Debian/ Ubuntu) can be achieved by the following command.
 $ sudo apt-get  install clisp
Alongside this its recommended to install the HTML version of the book  Common Lisp the language, 2nd Edition by Guy L. Steele Jr.
$ sudo apt-get install cltl
The book is installed in the /usr/share/doc/cltl/clm/index.html.

Step 2: Testing LISP functionality
Soon after installation clisp can be tested for functionality. The following sequence of commands test the lisp environment.
 To start clisp
$ clisp
Try out the lisp-expression to add 2 and 2 the result is in the second line. 
[1]> (+ 2 2)
4
Build on more complex lists.  The following expression is lisp way of (1*2)+ 6.

[2]> (+ (* 1 2) 6)
8
Now with the increase in comfort and confidence that lisp is installed and functional. Assigning values to variables is done via the following command
[3]> (setq a 10)
10
For an extremely simple loop using the print command to print hello world five times.
[4]> (do ((x 1 (+ x 1))) ((> x 5)) (print "hello world"))

"hello world"
"hello world"
"hello world"
"hello world"
"hello world"
NIL
Step 3: Doing something more than "hello world"
Having done with assignments and control structures. The next thing is procedures / functions. LISP supports functions via the keyword defun.
[5]> (defun increment (x) (+ x 1))
INCREMENT
[6]> (increment 10)
11
 The line 5 defines the function named increment which essentially has one argument and one line in which the argument is incremented by one. The line 6 calls the function increment with the argument passed as second member of the list.

The following code finds the square of a variable x.
(defun square(x) (x*x))
(setq x 10)
(square x)
Exploration of LISP on emacs is another really interesting topic which needs a dedicated post.

No comments: