Next Titel Inhalt Vorwort 1 2 3 4 5 6 7 8 9 10 11 12 Appendix @ CAAD Teachers

KAPITEL 3
Lisp II - Prädikate und Funktionen

3.1 True or False

Expressions in LISP are evaluated by an interpreter which may return either a value, or simply T or nil. If a value is returned, it is either assigned to some symbolic atom (e.g. a variable) or is a result of what a procedure returns (e.g. car returns the first element in a list). The other two results, T and nil, are special. They imply true or false. In very general terms, if an expression is evaluated successfully, it returns T, otherwise nil is returned. Everything that is not nil is T. Do not use T or t as a variable!

The following section introduces a set of predefined procedures, which allow you to test for truth or falsity of atoms and their values. The concept of defining functions in LISP is also introduced together with some more list operations.

For the following sections, set up AutoCad and text windows as you did for the last two exercises. Refer to the Setting Up section in either Kapitel 2 or 3.

3.1.1 Logical Operations

Quite often we may want to know what kind of atoms are in use, what values are assigned to them, comparing values of atoms, etc. In order to perform these operations, a number of operations are predefined in LISP.
(= atom1 atom2)
Returns T if all the atoms are equal, otherwise it returns nil. Atoms can be only numbers and strings, not lists.
(/= atom1 atom2)
Returns T if all the atoms are not equal, otherwise it returns nil. Atoms can be only numbers and strings, not lists.
(< atom1 atom2)
Returns T if the first atom is less than the second atom, otherwise it returns nil.
(<= atom1 atom2)
Returns T if the first atom is less than or equal to the second atom, otherwise it returns nil.
(> atom1 atom2)
Returns T if the first atom is greater than the second atom, otherwise it returns nil.
(>= atom1 atom2)
Returns T if the first atom is greater than or equal to the second atom, otherwise it returns nil.
(zerop item)
Returns T if item is either integer or floating-point number and evaluates to zero, otherwise it returns nil.
(minusp item)
Returns T if item is either integer or floating-point number and evaluates to a negative value, otherwise it returns nil.
(listp item)
Returns T if item is a list, otherwise it returns nil.
(atom item)
Returns T if item is anything other than a list, otherwise it returns nil.
(null item)
Returns T if item is bound to nil, otherwise it returns nil.
(equal expr1 expr2)
Returns T only if expr1 and expr2 evaluate to the same (equal) values. Otherwise it returns nil.
(eq expr1 expr2)
Returns T only if expr1 and expr2 are bound to the same object, i.e. they are identical. Otherwise it returns nil.
Examples: Evaluate the following expressions exactly as shown, including the quote (') character before the lists and strings.
	(= 3 3.0)	=>  T
	(/ 2 3 4 8)	=>  0
	(= "ab" "ab")	=>  T
	(< 1 3 7)	=> 
	(< 2 4 4 7)	=> 
	(setq a_num 4.5)	=> 
	(setq a_char 'w)	=> 
	(setq a_list '(a b c))	=> 
	(zerop a_num)	=> 
	(zerop a_char)	=> 
	(minusp a_num)	=> 
	(listp a_char)	=> 
	(listp a_list)	=> 
	(atom a_num)	=> 
	(atom a_list)	=> 
	(null a_list)	=> 
	(setq f1 '(a b c))	=> 
	(setq f2 '(a b c))	=> 
	(setq f3 f2)	=> 
	(equal f1 f2)	=> 
	(equal f1 f3)	=> 
	(eq f1 f2)	=> 
	(= f1 f2)	=> 
	(eq f2 f3)	=> 
	(= f2 f3)	=> 
3.1.2 Boolean Operations
(not item)
Returns T if item evaluates to nil, otherwise it returns nil. It has the effect of negating the truth or falsity of item.
(and item1 item2 ...)
Returns nil if any item is found to be nil expression (i.e. if any argument evaluates to nil), otherwise it returns T. This operation evaluates arguments from left to right.
(or item1 item2 ...)
Returns T if any item is found to be a non-nil expression (i.e. if any argument evaluates to non-nil), otherwise it returns nil. This operation evaluates arguments from left to right.

3.2 List Operations - cons, append, list

In order to insert new symbols into a list, following operations are available in LISP.
(list a_sym b_sym ...)
Returns a new list formed by putting all the argument into one big list.
(list 'a 'b) => (A B)
(cons new_sym a_list)
Returns a new list formed by inserting the first argument at the head of second argument list. The first argument can be an atom or a list.
(cons 'a '(b c)) => (A B C)
(append a_list b_list)
Returns a new list formed by inserting all arguments together into a new list. Arguments need to be lists.
(append '(a b) '(c d)) => (A B C D)
Examples: Evaluate the following expressions exactly as shown, including the quote (') character before the lists and strings.
	(not (< 0 1.5))	=>  nil
	(not (= 2.5 3.5))	=>  T
	(and (< 2 3 5) (/= 0 1))	=> 
	(and (< 3 2 4) (/ 1 1.9))	=> 
	(or  (> 2 3 4) (/= 3.5 6.5))	=> 
	(or  (> 2 3 4) (= "ab" "AB"))	=> 
	(list 'a '(b c) 3 '(a 3.9))	=> 
	(setq f1 '(a b c))	=> 
	(setq f2 '(d e f))	=> 
	(cons 3.5 f1)	=> 
	!f1	=> 
	(append f1 f2)	=> 
	!f1	=> 
	!f2	=> 
	(setq f1 (cons 3.5 f1))	=> 
	!f1	=> 
	(setq f2 (append f1 f2))	=> 
	!f2	=> 
	!f1	=> 

3.3 LISP Functions - defun

All the examples introduced so far use predefined operations in LISP, e.g. car, cdr, +, *, minusp, zerop, etc. These are defined as procedures or functions made up of nothing but LISP expressions. To define your own functions in LISP which you can reuse again and again, following form should be used.
	(defun function_name ()
	  lisp_expression1
	  lisp_expression2
	  ...
	)
function_name is written in italics to indicate that any symbol (usually a name suggesting what a function does) can be used, and expressions can be any valid LISP expression including AutoCad commands. The meaning of parentheses following the function name will be explained in the next lecture.

Example: For the following example, you should open a TextEdit window and enter the following text. Save the file as test.lsp and then load it into AutoCad by entering at the command prompt: (load "test")

	(defun test_function ()
	  (setq a 2.0)
	  (setq b 3.0)
	  (setq c '(This is a list))
	  (princ "Square of variable a: ") 
	  (princ (* a a))
	  (princ)
	  (princ "Cube of variable b: ") 
	  (princ (* b b b))
	  (princ)
	  (princ "Value of variable c: ") 
	  (princ c)
	  (princ)
	  (setq d "this is a string!")
	  (princ d)
	  (princ)
	)
If the file is successfully loaded, the LISP interpreter will return the atom test_function. Now you can use it just like any other predefined LISP operation. Since this function does not have arguments, simply enter function name to evaluate it:
	(test_function)

3.4 Übung 3

This exercise gives you an opportunity to use the programming constructs that were introduced earlier.

Each of the required functions draws some elements on the screen. You have to figure out how and where coordinate values will need to be changed so that each function generates elements in different parts of the screen (i.e. your drawing). The following three LISP functions are required:

* draw_pline draws a polyline defining a square of 1 x 1 unit. Then it creates a polar array of 12 squares forming a complete circle. Next, it changes the thickness of all elements to 1.5.
* draw_solid does the same thing as the previous function except that the command solid should be used instead of pline to draw the basic square shape. Note that solid requires a slightly different order for the specification of the vertices.
* draw_box also creates a circular polar array of box shapes. For this function use the solbox command. Note that this command generates a cube, i.e. you do not have to draw a square and then change its thickness as in pline or solid commands. You will first need to load the "ame" solid modeling unit into AutoCad. Once you make a cube, use the solmesh command to change its representation from wire-frame to meshed surfaces. After you generate an array of the cube, use the solunion command to join the solids together into a single composite solid. Details of how to incorporate solid modeling commands into the draw_box function will be given during the lecture - follow those instructions carefully!
* Add one more function to your file as follows; replace your_name in the 3rd line with your name:
	(defun test ()
	  (princ)
	  (princ "Testing functions for your_name")
	  (draw_pline)
	  (draw_solid)
	  (draw_box)
	  (command "vpoint" "-1,-1,1")
	  (setvar "shadedge" 1)
	  (command "shade")
	)
Save your file as 03_name.lsp and submit it by copying it into the directory /homes5/prog/abgabe.

Besides realizing that AutoCad commands are valid LISP expressions that can be used as part of functions, you will also see what the differences are between polylines, solid faces, and real solid volumes!

Next Titel Inhalt Vorwort 1 2 3 4 5 6 7 8 9 10 11 12 Appendix @ CAAD Teachers

This website has been archived and is no longer maintained.