Thursday, March 6, 2008

On Parentheses

People think about lisp parentheses as most hard thing in the world. When I started learning lisp it was a bottleneck for me too. I used to think what you do with these parentheses? Why it is like this. But now when I have started feeling a bit confident about lisp parentheses I felt I should put a note for absolute beginners of the language.

Parentheses while coding lisp

While you are coding always use a lisp editor for your parentheses handling. Best option is Emacs or XEmacs. If you are using it in windows please install and configure your Emacs in Windows (for some idea you can see here). Understand that good editor (like Emacs) will not only help you in coding in lisp but will increase your speed of translating your idea into code.

Use good key combinations to move around the parentheses as s-expression. Do not waste your time with bad editors which does not support any lisp mode; the faster you unlearn (thoes editors) the better you will be in coding lisp.

Parenthesis while reading lisp codes

There is no parentheses in the lisp code when you are reading a code. Yes, it may sound crazy for anyone starting out in lisp but it is true. Here is an example showing the concept:

(defun show-file(filename)
    (let ((in (open filename :if-does-not-exist nil)))
     (when in
      (format t "~a~%" (read-line in))
     (close in)))

Here is what you get when you removed all the parentheses:

defun show-file filename
let in open filename :if-does-not-exist nil
when in
format t “~a~%” read-line in
close in

It reads like:

Define function show-file which will take filename as argument
Let in be the file handler with filename; if the file does not exist do nothing
When there is a file handler in “in”
Format output as each line in the file comes
And yes close the file “in” before quitting.

That’s all! I hope this will help.