[Santa Clara University]
Department of Mathematics
and Computer Science

Math 169

LISP Information

D. C. Smolarski, S.J.

[Return to Math 169 Homepage | Return to Supplement Listing Page]

Contents


LISP (Kyoto) on the HP Unix Server

  1. LISP (COMMON LISP) is interactive. One enters the LISP interpreter and then can load stored and edited files, or else enter expressions directly.
  2. To run a longer program, it is a better practice to create a source code (program) file using an editor of your choice (e.g. Emacs or vi). The extension should be lsp.
  3. After the source code file is created, one enters the LISP interpreter and then loads the file(s) using the command (LOAD "filename.lsp"). This is done as follows (assuming you wish to load file foo.lsp):
      math 22: lisp
      AKCL (Austin Kyoto Common Lisp)  Version(1.619) Thu Oct  7 15:46:49 PDT 1993
      Contains Enhancements by W. Schelter
    
      > (load "foo.lsp")
    
    (Also see the sample "typescript" session.)
  4. SPECIAL CHARACTERISTICS OF COMMON (KYOTO) LISP
    1. In the conditional statement (COND), e.g.,
      	(COND (condition1 action1)
           	      (condition2 action2)
      	      (condition3 action3)
           	 ...
      	      (condition_m action_m) )
      
      --the last condition can be replaced by T or omitted.
      --COMMON LISP allows more than one action per condition (without using a PROG construction).
      --NOTE: Parentheses are usually needed for the CONDITION and ACTION as required by the expressions, e.g., (EQ A B) or (SETQ C 3)--a second set surrounds each condition/action line.
    2. Function definition uses the keyword DEFUN (rather than DEFINE or DEF), e.g.,
      	(DEFUN function_name input_var_list action)
      
    3. INPUT/OUTPUT FUNCTIONS:
      (PRINT a) --- prints the value of a (on the terminal)

      (TERPRI) --- induces a carriage return

      To OPEN file FOO.LSP in your directory and prepare it as an INPUT file:
      	(SETQ IN1 (OPEN "FOO.LSP" :DIRECTION :INPUT))
      
      where IN1 can be any local atom.

      Then to READ from file FOO.LSP, use:

      	(READ IN1)
      
      reads one s-expression
      	(READ-CHAR IN1)
      
      reads one character

      To CLOSE the file:

      	(CLOSE IN1)
      
      To OPEN file BAR.LSP in your directory and prepare it as an OUTPUT file:
      	(SETQ OUT1 (OPEN "BAR.LSP" :DIRECTION :OUTPUT))
      
      Then to PRINT to BAR.LSP, use:
      (PRINT s_exp OUT1) -- prints in form which can later be used by READ with a preceding NEWLINE and followed by SPACE (i.e., with escape characters).

      (PRIN1 s_exp OUT1) -- prints in form which can later be used by READ without any surrounding spaces (i.e., with escape characters).

      (PRINC s_exp OUT1) -- like PRIN1 except special characters are printed without other marks (i.e., NO escape characters).

      (TERPRI OUT1) -- forces a carriage return in file OUT1.

    4. PROPERTY LISTS functions in COMMON LISP are:
      (SETF (GET x z) y) -- gives x the z-property of y.
      [OTHER LISPs may use (PUTPROP x y z)]
      E.g., (setf (get 'book 'date) 1988) gives the atom book a date property and assigns the value of 1988 to that property.

      (GET x z) -- gets the value of the z-property of x.

    5. ADDITIONAL INFORMATION:
      1. To LOAD in a file of LISP functions, say, FOO.LSP
        		(LOAD "FOO.LSP")
        
      2. COMMENTS: use ; and rest of the line is ignored.

      3. To EXIT:
        		(bye)
        
      4. To RESET after errors:
             		:q
        
      5. OTHER FUNCTIONS:
        (TRACE funct) turns on function call and return tracing.
        (UNTRACE funct) turns off tracing.
      6. To get help, type
        	(HELP) or (HELP 'Lisp_element)
        
        	or
        
        	(DESCRIBE 'Lisp_element), 
        		e.g., (DESCRIBE 'CAR) or (DESCRIBE 'PRINT)
        
        	or
        
        	(APROPOS "Lisp_concept"), e.g. (APROPOS "READ")
        
      7. EDITING:
        One can invoke the Unix "vi" editor on a file WHILE IN LISP through the command (ED "filename.LSP"). To return to LISP, use :w and :q.

Sample LISP Source Code

     
math 20: script
Script started on Wed Apr  7 21:11:37 1999
math 21: more foo.lsp
(defun for (a)
      (prog ()
	    loop (print (car a))
	         (setq a (cdr a))
		 (cond ((not (null a)) (go loop)))))
(defun rev (x)
      (cond ((null (cdr x)) (print (car x)))
	    ((prog () (rev (cdr x))
		      (print (car x))))))

Sample Execution

math 22: lisp
AKCL (Austin Kyoto Common Lisp)  Version(1.619) Thu Oct  7 15:46:49 PDT 1993
Contains Enhancements by W. Schelter

>(+ 1 2)
3

>(setq a 2)
2

>(setq b '(a 2 3))
(A 2 3)

>(car b)
A

>(cdr b)
(2 3)

>(cons '+ (cdr b))
(+ 2 3)

>(setq c (cons '+ (cdr b)))
(+ 2 3)

>c
(+ 2 3)

>(eval c)
5

>(load "foo.lsp")
Loading foo.lsp
Finished loading foo.lsp
T

>(setq d '(1 2 3 4 5 6 7))
(1 2 3 4 5 6 7)

>d
(1 2 3 4 5 6 7)

>(rev d)

7 
6 
5 
4 
3 
2 
1 
NIL

>(for d)

1 
2 
3 
4 
5 
6 
7 
NIL

>(bye)
Bye.
math 24: exit
math 25: 
script done on Wed Apr  7 21:13:45 1999


My email address is dsmolarski@math.scu.edu

This page last updated 14 March 2000.