Kapitel 2

Lisp I: Lisp, AutoLisp, einfache Funktionen


2.1 LISP

LISP is a computer programming language and it is considered by many to be easy to learn and fun to use. A programming language is defined by a set of valid expressions or statements. Each valid expression may be made up of a number of valid operators and symbols that can occur only in defined patterns. A series of valid statements may be combined into a computer program that, when executed, performs a specific task.

A valid LISP statement is expressed as a list enclosed in parentheses. A statement in LISP can be executed directly by an interpreter program and results displayed on the screen. Thus two steps are involved in this process: writing LISP expressions and passing them to the LISP interpreter for evaluation.

Setting Up

In this course, the LISP interpreter available with the AutoCad program will be used. Whenever you start AutoCad, this interpreter is running in the background. When you type a valid LISP expression, it is passed to the LISP interpreter for evaluation. Since AutoCad is designed to be an interactive graphics application, it provides a number of specialized graphics functions in LISP which are not part of the standard LISP language. The enhanced version of the language available in AutoCad is known as AutoLisp.

Start AutoCad by typing acad in any shell window. Select a new drawing option and enter any name for the drawing file name. Once AutoCad is properly loaded, you will see a graphic window with a screen menu and some pull-down menus at the top. At the bottom, you see a prompt command: in a small window. Here is where you can enter LISP statements for evaluation. If you hit F1 key, a bigger window comes to the foreground on your screen. This is the same command: prompt window which was only partially visible earlier. With this larger window available, proceed with the following sections.

When you are finished with the following sections, remember to quit AutoCad, exit OpenWindows, and log out.

2.1.1 Atoms and Lists

An expression in LISP is made up of atom(s) or list(s). Atoms could be integer numbers like 3, 27, -88; floating-point numbers like 2.34, -0.9, 123.333; or symbols like X, name, My_Name. A list consists of a left parenthesis, followed by zero or more atoms, and a right parenthesis as in the following: (2 name -3.56) or ().

2.1.2 Arithmetic Operations

A number of operations are predefined in LISP like the following arithmetic operations. An operation is defined by a symbol (e.g. +, -) which signifies a procedure to be performed (e.g. addition, subtraction) on specified atoms.
(+ num1 num2)
This operation returns the sum of all specified numbers.
(- num1 num2)
This operation subtracts the second number from the first and returns the result.
(* num1 num2)
This operation returns the product of two numbers.
(/ num1 num2)
This function divides the first number with the second and returns the quotient.
In the above operations, symbols +, -, *, and / specify procedures, and numbers could be any numeric values on which the procedures are carried out by the LISP interpreter. If you mix integer and floating-point numbers in an arithmetic operation, the result returned will be a floating-point number! Further, note that the number of arguments to these operations need not be limited to only two numbers as shown above. Try to evaluate these operations with different number of arguments.

Examples: Enter the following expressions and check the results.

Try these operations with more than two numbers as arguments.

2.1.3 Variables

A symbolic atom like X, name, My_Name, may be used to store some value which may be another symbolic atom like a number, character value, or a list. A symbolic atom with some value assigned to it may be evaluated, returning the value it is assigned. Such atoms are often referred to as variables since they can be assigned different values.
(setq sym val)
This operation assigns the value of the second argument to the the first argument; this is known as an assignment operation. Examples: Enter the following expressions exactly as shown. Note the quote (') character before the list. Its meaning will be explained in Kapitel 5; for the moment remember to put a quote before any list that is to be passed to a LISP procedure. A variable may be evaluated by simply passing it to the interpreter. In AutoLisp, you can retrieve the value of a variable by typing exclamation mark (!), followed by the variable name, without any spaces between the two. For example: The examples above should make it clear that once a variable is assigned some value, that variable can be used whenever the value it refers to is needed.

2.1.4 List Operations: car, cdr

A list may be made up of zero of more atoms, for example: (3 2.6 -8.9). If we want to access or change any of the atoms in the list, two LISP operations are provided.
(car a_list)
This operation returns the first element of an argument list.
(cdr a_list)
This operation returns everything in an argument list except its first element. Examples: Enter the following expressions exactly as shown.
    (car '(a b c)) ^ A
    (cdr '(a b c)) ^ (B C)
    (car '((3 b) c)) ^
    (cdr '((3 b) c)) ^
    (cdr '(a)) ^
What happens if you evaluate the following expressions?
    (setq a_list '((23.9 ab)(apples oranges cherries) (dogs cats cows))) ^
    (car (car a_list)) ^
    (cdr (cdr a_list)) ^
    (car (cdr a_list)) ^
    (cdr (car a_list)) ^
When a number of list operations involving car and cdr are needed, it is possible to combine them into compound operations by joining ar (from car) and dr (from cdr). For example, the previous four examples could be rewritten as follows:
    (caar a_list) ^
    (cddr a_list) ^
    (cadr a_list) ^
    (cdar a_list) ^

2.1.5 NIL

It is also possible to set up variables that do not have any value, this is signified by a special symbol nil. Note that assigning 0 (zero) is not the same as assigning nil to a variable since zero is still a valid value!

Example: (setq a_variable nil) will set up a symbol a_variable that does not have any value assigned to it. Since it is declared, it can be evaluated and the result of evaluation will be nil.

It is also possible to have lists which are empty, i.e. they do not contain any atoms, just left and right parentheses. These are known as null lists.

Example: (setq an_empty_list '()) will set up a null list. See what happens if you use car or cdr on such a list.


2.2 Uebung 2

In this exercise, you have to evaluate the following expressions and save the results in a text file by using copy-and-paste operations. It provides drill for using AutoLisp environment as well as working simultaneously with multiple windows and UNIX commands.
  1. Log in to your account.
  2. Create a directory called u02.
  3. Change working directory to u02.
  4. Open a text window by selecting the Text Editor... option from the Programs submenu of the WorkSpace menu in OpenWindows.
  5. Start AutoCad and a new drawing file. Bring the text window in the foreground by hitting F1 key.
  6. Enter each of the following expressions at the AutoCad command: prompt.

      (+ 3 4.0 8 99.99) ^
      (- 234 34 89 10) ^
      (* 10.0 2 5 6.0) ^
      (/ 335 12 8 9.9) ^
      (car (cdr '((ab cd) (ef gh ij) (kl mn)))) ^
      (cdr (car '((ab cd) (ef gh ij) (kl mn)))) ^
      (setq a_list '((ab cd) (ef gh ij) (kl mn))) ^
      (car (cdr a_list)) ^
      (cdr (car a_list)) ^
      !a_list ^
      (setq num1 25) ^
      (setq num2 10) ^
      (+ num1 num1 num2 num2) ^
      (* num1 num2) ^
      (/ num2 num1) ^

  7. Select the entire AutoCad text window by clicking at the top of the window with left mouse button and then clicking at the bottom of the window with middle mouse button. This should make the entire text grey out.
  8. Copy the selected text by hitting the Copy function key on the keyboard. This puts the selected text in computer memory.
  9. Move the mouse over to the text window and hit the Paste function key on the keyboard.
  10. Use the right mouse button to open the File menu of the text window, select the Store as New File... option, complete the path name indicated next to Dirctory: by adding u02. Type 02_name.txt (where name is your name) on the line next to File:. Complete the proceedure by clicking on the Store as New File button.
  11. Quit AutoCad. Copy the file 02_name.txt to directory /homes2/prog/abgabe.
  12. Exit OpenWindows and log out.


To the next chapter

Prog Content Vorwort ..1.. ..2.. ..3.. ..4.. ..5.. ..6.. ..7.. ..8.. ..9.. ..10.. ..11.. ..12.. ..13.. Appendix


@ by Architektur und CAAD 1994.......... The Teacher Team