"Functions in LISP"
Defining procedures
As you may recall from the first installment of the lisp listener, a procedure is a description of an action or computation. A primitive is a predefined or "builtin" procedure (e.g. "+ "). as in forth, LISP can have procedures which are defined by the programmer. defun, from define function, is used for this purpose. the syntax for defun in experlisp is as follows:
(DEFUN FunctionName (symbols) (All sorts of computations which may or may Not use the values represented by the symbols))
The function name is exactly that. whenever the name is used the defined procedure associated with that function name is already med. the symbols are values which may or may not be required by the procedures within the defined function. if required, the values must follow the function name. when given, these values are assigned to the symbol. this is similar to the way values are assigned to a symbol when using setq. it is easier to see how defun works when observed within an example:
;(DEFUN Reciprocal (n) (/ 1 n));Reciprocal;(Reciprocal 5);.2 ;(Reciprocal 2384);.000419463
The word "reciprocal" is the function name and the numbers following are the values for which the reciprocal (1/n) are found. after the list containing defun is entered and the carriage return is pressed the function and it's title are assigned a location in memory. the function name is then printed in the listener window.
;(DEFUN Square (x) (* x x));Square;(Square 5);25;(DEFUN Cubed (y) (* y (* y y));Cubed;(Cubed 5);125;(DEFUN AVERAGE (W X Y Z) (/ (+ W X Y Z) 4)) ;Average;(Average 2 3 4 5);3.5
You might recognize "average" from last month's lisp listener. one might imagine using defined functions inside other defined functions. if it was possible to have variables which have the same values in each procedure, then the version of Lisp used has what is called dynamic scoping. in this context the values of the variable are determined by the lisp environment which is resident when the procedure is called. experlisp, however, is lexically scoped. that means that variable values are local to each procedure. two defined procedures can use the same labels for variables, but the values will not be considered as the same. each variable is defined locally. this is in accordance to the Common Lisp standard. lexical scoping makes it easier to debug someone elses ''programs. if you don't know what I mean yet, don't worry. this subject will come up again in more detail later.
Predicates
If no values are required by the defined function then "nil" or an empty list must follow the function name.
;(DEFUN Line () (Forward 50))
The empty list obviusly contains no atoms (I'll describe the above function, "line" later in the section on bunnies ). it is synonymous to the special term nil, which is considered by lisp as the opposite of T or true. nil is used in your other contexts.
;(cddr '( one two)) ;nil
In the above, the first CDR returns "two ". the second CDR returns nothing, hence "nil ". the values of true and false are returned by procedures called predicates. while nil represents a false condition, anything other then nil, including "T", is generally considered true. please note that I used lowercase letters in the above. experlisp recognizes both upper and lowercase. I 've been using uppercase only to make it clear within the text when I'm referring to lisp
EQUAL is a predicate which checks the equality of two arguments. note the arguments can be integers or symbols. if the two arguments are equal then "T" is returned. if they are not equal then "nil" is returned.
;(EQUAL try try);T;(EQUAL 6732837 6732837);T;(EQUAL 6732837 6732833);nil;(EQUAL First Second);nil
Atom checks to see if it's argument is a list or an atom. remember, the single quote is used to indicate that what follows is a not evaluated as in the case of a list. symbols are evaluated.
;(ATOM 'thing);T;(ATOM thing);nil;(ATOM (A B C D));nil
In the first of the above 'thing is an atom due to the single quote. in the second, thing is considered a symbol. A symbol is evaluated and contains a value or values as a list. in the third, (a B c d) is obviusly a list.
Listp checks if it's argument is a list.
;(LISTP '( 23 45 65 12 1));T;(SETQ babble '(wd ihc wi kw));(LISTP babble);T;(LISTP 'babble);nil;(LISTP 'thing);nil
One interesting observation is that nil is both an atom and a list, () = nil. Therefore atom and listp both return true for nil.
;(ATOM ());T;(LISTP ());T
When one needs to know if a list is empty, null does the job.
;(NULL good);nil;(NULL (X Y Z));nil;(NULL ());T;(NULL nil);T
Numberp checks if the argument that follows is or represents a number rather than a string.
;(NUMBERP 56.887);T;(NUMBERP fifty-six);nil;(SETQ fifty-six '(56));(NUMBERP fifty-six);T
Now for a real slick one. Member tests whether or not an argument is a part of a list. An easy demonstration follows:
;(MEMBER 'bananas (apples pears bananas));(apples pears bananas);(MEMBER 'grapes (apples pears bananas));nil
When the argument is a member, then the contents of the list are given. If not then NIL is returned. member also checks symbols of lists.
;(SETQ fruit '(apples grapes pears));(MEMBER 'grapes fruit); (apples grapes pears);(MEMBER 'banana fruit);nil
Evenp tests to see if an integer is even and minusp checks if an integer is negative. oddp and plusp are not needed since they are simply opposite of the first two.
;(EVENP 2);T;(EVENP (- 806 35));nil;(MINUSP 25);nil;(MINUSP (-34 86));T
In the second and fourth examples abve the lists contained within are calculated prior to memberp evaluation. (806-35 = 771 & 34-86 =-52. there's a few more simple predicates such as not, <,>, and zerop. i'll discuss them along with conditionals next month. now for something completely different.
Bunny graphics
If you 've ever learned logo, the concept of Bunny graphics showould sound familiar. as mentioned last month, the bunny is expertelligence's version of the turtle. all one needs to do in order to make a bunny move is to tell it. forward x initially moves the bunny upwards on the screen for 'X' display pixels. A negative number initially moves it down. when one enters the following in the listener window,
;(FORWARD 50)
The default graphics window (I'll discuss windows in more detail very soon in future installments) is then opened and the following is drawn:
Right x aims the front of the line to the right by X degrees. If one then uses forward again the line moves in a different direction. For example:
;((RIGHT 50) (FORWARD 50))
Or better yet
;(DEFUN Line (RIGHT 50) (FORWARD 50));Line;(Line)
After a line is moved, the end of the line remains where it was. if one made the bunny move again the Beginning of the New Line wowould begin where the old left off. the original starting point is the graphics window default home position. this position is in the center of each graphics window when the window is first created. in order to return the bunny to the original starting point one must use home.
;(HOME)
The following produces a much neater triangle:
(DEFUN Triangle () (Penup) (Left 45) (Forward 10) (Pendown) (Right 90) (Forward 25) (Right 90) (Forward 50) (Right 135) (Forward 71) (Right 135) (Forward 25))
After the above is typed into the edit buffer the "compile all" selection shoshould be chosen from the menu bar. the source code in the edit buffer quickly inverts to white letters on a black background as if the whole file was selected for a moment. the function name "triangle is then printed in the listener window. if the user enters the following in the listener window a different triangle is drawn in the default graphics window:
;(Triangle)
If you look at the in triangle you will see a couple more bunny commands. left does the same as right but in the opposite direction. penup raises the bunny's pen so that when the bunny moves no lines are drawn. pendown returns the bunny to the drawing orientation. the first line of code in "Triangle" puts the bunny off the home position so that the drawn triangle will be centered on the screen. as mentioned earlier, the orientation of the bunny remains. the last line of code in "Triangle left the bunny aimed at about rather than the initial position. if we were to make "Triangle" execute ten times without eliminating the graphics window the following wowould result:
In getting "Triangle" to execute recompilation of the Code in the edit buffer is not necessary. to get the above one can type the function name into a list ten times within the listener window. the following however, is easier:
;(Dotimes (a 10) (Triangle))
Dotimes is very similar to the for... Next looping routine in basic. I'll discuss it next month in a description of iteration and recursion in experlisp.
If we wanted to use a three dimen=bunny then the following wocould be added before "Triangle" in the edit buffer window:
(SETQ curbun (new3dbun))(Pitch 30) (Yaw 45) (Roll 50)
Something like the following is drawn after the source code is recompiled and "(triangle)" is entered into the listener window:
Curbun is a special symbol in experlisp which always refers to the bunny cursor. new3dbun is a special term which always changes curbun. the default bunny is 2 dimen.pdf. if one wanted the spherical bunny then the following wocould be entered into the beginning of the first version of "Triangle ":
(SETQ curbun (newspbun))
This wowould then produce what follows:
In order to have the above drawn in a different orientation, different bunny direction wocould be required. windows, two and three dimensional bunny graphics and Toolbox graphics use the same X, Y coordinate system. home is 0, 0. dual negative coordinates are situated towards the upper left corner. dual positive coordinates are situated towards the lower right corner. the range is + 32767 to-32768 for each dimension. in experlisp one can sometimes use the third dimension, as in the 3D sample of "Triangle ". negative Z values are behind home, while positive Z values are in front. the following extends strates the coordinate system in experlisp:
Compiler Information
The experlisp disk contains three essential files; compiler, lispenv and experlisp. compiler is not actually the entire Lisp compiler. it contains the information needed in generating all of the higher level lisp syntactics, such as the bunny graphics. lispenv stands for lisp environment and it is simply a duplication of compile. lispenv contains information on how the Macintosh memory was organized by the programmer and experlisp during the previous session. it also contains information on the System Configuration such as the number of disk drives, the amount of memory, etc. sometimes lispenv can be messed up (I. e. by changing the variable table ). when this happens one might not be able to start experlisp. in this case lispenv shocould be removed from the disk. afterward, when experlisp is opened, compiler generates a new lispenv. compiler is not needed on the disk unless the lispenv is ruined. deleting it will provide 100 k more space on the disk. before eliminating it from the disk however, be sure you have a backup as it is an essential file. the experlisp file contains the Assembly Language routines which represent the lower level lisp routines like car And CDR. it also allows access to the Macintosh toolbox routines and contains the listener window. one opens the experlisp file in starting a programming session with experlisp. another file on the disk is automatically loaded and activated when experlisp is booted. it is labeled already lispinit. the contents of this file can be added to so that when one boots up experlisp a program can be automatically executed. it can also do automatic configurations. however the contents of your lispinit shocould not be changed since it configures the Macintosh memory for exper-lisp.
Next month I'll discuss a few more predicate procedures. I also hope to start discussing iteration, recursion and conditionals. if there is enough room left over I might also begin discussing how to access the Toolbox graphics.