next up previous
Contents Next: Using Your Own Up: Defining LISP functions Previous: Local and Global

Using an Editor

Simple function definitions, like square, are easily typed directly at the interpreter's prompt. However, for longer functions this soon becomes inadequate, because you will want to slightly modify definitions without having to completely retype them, not to mention the likelihood that you will make typographic errors when typing them in.

The solution is to write your functions in a program editor and then transfer them to the interpreter for evaluation. Exactly how you do this will vary from system to system and editor to editor. One of the most popular editors for writing LISP code on UNIX tm systems is emacs, since it provides convenient methods for transferring code between editor to interpreter. Additionally, most LISP interpreters allow you to directly call an editor from the interpreter prompt by typing (ed ``filename'').

If you have lots of function definitions, they will need to be stored in a file and then loaded into the interpreter using the instruction (load ``filename''). Once again, the details on how to save files and specify their names for loading will vary from system to system.

When typing code in an editor, two elements of good programming practice should be followed.

LISP code should be indented in a way which reveals its structure. Some editors, such as emacs, will automatically indent LISP code in a reasonable way. If your editor will not do this, follow examples in these chapters as a guide to one common indenting practice.

All code should be commented to improve readability. In LISP, anything on a line following a semi-colon is treated as a comment and is ignored by the interpreter. Opinions vary on the best use of comments. Too many comments can make code as hard to read as too few. Choosing mnemonic names for functions and variables can cut down on the number of comments needed for readability. You might wonder why you need comments if you are to be the only person to read your own code. Write some code and then come back to it two months later and you will understand why comments are important.

You are not recommended to go any further in this chapter until you know how to use a program editor on your computer.

next up previous
Contents Next: Using Your Own Up: Defining LISP functions Previous: Local and Global

© Colin Allen & Maneesh Dhagat
November 1999