home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-12-10 | 7.1 KB | 227 lines | [TEXT/ROSA] |
- ; File: sample.lisp
- ; Contents: sample lisp function definition.
- ;
-
- ;
- ; rev function
- ; reverses a list recursively
- ;
- (defun rev (l)
- (cond
- ((null l) nil)
- ((append (rev (cdr l)) (list (car l))))))
-
- ;
- ; fast-rev function
- ; reverses a list iteratively
- ;
- (defun fast-rev (list)
- (let ((result nil))
- (dolist (i list result)
- (push i result))))
-
-
- ;
- ; define alphabet just for something to manipulate
- ;
- (setq alpha '(a b c d e f g h i j k l m n o p q r s t u v w x y z))
-
- (defun vector-start (stream char)
- (apply #'vector (read-delimited-list #\])))
- (defun vector-end (stream char) nil)
- (set-macro-character #\[ #'vector-start)
- (set-macro-character #\] #'vector-end)
-
- ;
- ; Function 'write-chars'
- ; Writes n characters to std output
- ;
- (defun write-chars (c n &optional stream)
- "write-chars char num
- Usage: (write-chars char numchars)
- Writes characters to standard output by default,
- or to any supplied stream."
- (do
- ((i 0 (+ i 1)))
- ((>= i n) nil)
- (if stream
- (write-char c stream)
- (write-char c))))
-
- ;
- ; Function 'dump-file
- ;
- (defun dump-file (filename)
- "Usage: (dump-file FILENAME)
- Writes the contents of a file to standard output."
- (let ((stream (open filename)) c)
- (loop
- (setq c (read-char stream))
- (if (eq c 'Eof) (return))
- (write-char c))))
-
-
- ;
- ; read-function is useful for reading a specific named function from
- ; a file when you don't want to load the whole file
- ;
- (defun read-function (func &optional stream)
- "read-function (function-name &optional stream)
- Usage: (read-function func-name (open \"filename\"))
- This causes the function 'func-name' to be read from the file 'filename'.
- Returns the function definition, or NIL if not found. It is the responsibility
- of the caller to evaluate it, in order to create the function.
- i.e. (eval (read-function func-name stream))"
- (do ((in)) ((eq in 'Eof))
- (if stream (setq in (read stream)) (setq in (read)))
- (if (eq in 'Eof) (return nil))
- (if (and (listp in) (eq (car in) 'defun) (eq (cadr in) func)) (return in))))
-
- ;--------------------------------------------------------------------------
- ;
- ; print-function
- ; This function is useful for printing out a readable definition of a function.
- ; It is printed in the context of the function's own package, so that package
- ; qualifiers are not printed before private variables.
- ;
- (defun print-function (a &optional stream)
- "print-function (function-name &optional stream)
- Usage: (print-function func-name (open \"filename\"))
- This causes the function 'func-name' to be printed to the file 'filename'.
- Omitting the stream argument causes the function to be printed to the
- screen.
- Returns NIL."
-
- (let ((save-package (package-name *package*))
- (save-print-escape *print-escape*)
- (funcdef (function-definition (eval (list 'function a)))))
- (in-package (package-name (symbol-package a)))
- (setq *print-escape* t)
-
- ; replace "macro" with "defmacro name"
- ; and "lambda" with "defun name"
- (if (and (symbolp a) funcdef (consp funcdef))
- (if (eq (car funcdef) 'macro)
- (setq funcdef (cons 'defmacro (cons a (cdr funcdef))))
- (if (eq (car funcdef) 'lambda)
- (setq funcdef (cons 'defun (cons a (cdr funcdef)))))))
-
- (if stream
- (progn
- (print (list 'in-package (package-name (symbol-package a))) stream)
- (print funcdef stream))
- (progn
- (print (list 'in-package (package-name (symbol-package a))))
- (print funcdef)))
- (in-package save-package)
- (setq *print-escape* save-print-escape)
- nil))
-
- ;--------------------------------------------------------------------------
- ;
- ; dump-hash-table is useful for printing out the contents of a hash table
- ;
- (defun dump-hash-table (table)
- "dump-hash-table (hash-table)
- Usage: (dump-hash-table hash-table)"
- (maphash #'(lambda (key val)
- (write "pair: ")
- (write key)
- (write " ")
- (write val)
- (terpri)) table))
-
- ;--------------------------------------------------------------------------
- ;
- ; show-lisp-symbols is useful for printing out the names of all the common
- ; lisp symbols
- ;
- (defun show-lisp-symbols ()
- "Usage: (show-lisp-symbols)
- Displays all the symbols in the COMMON-LISP package."
- (dump-hash-table (package-hash-table (find-package "COMMON-LISP"))))
- ;
- ; read-function is useful for reading a specific named function from
- ; a file when you don't want to load the whole file
- ;
- (defun read-function (func &optional stream)
- "read-function (function-name &optional stream)
- Usage: (read-function func-name (open \"filename\"))
- This causes the function 'func-name' to be read from the file 'filename'.
- Returns the function definition, or NIL if not found. It is the responsibility
- of the caller to evaluate it, in order to create the function.
- i.e. (eval (read-function func-name stream))"
- (do ((in)) ((eq in 'Eof))
- (if stream (setq in (read stream)) (setq in (read)))
- (if (eq in 'Eof) (return nil))
- (if (and (listp in) (eq (car in) 'defun) (eq (cadr in) func)) (return in))))
-
- ;--------------------------------------------------------------------------
- ;
- ; print-function
- ; This function is useful for printing out a readable definition of a function.
- ; It is printed in the context of the function's own package, so that package
- ; qualifiers are not printed before private variables.
- ;
- (defun print-function (a &optional stream)
- "print-function (function-name &optional stream)
- Usage: (print-function func-name (open \"filename\"))
- This causes the function 'func-name' to be printed to the file 'filename'.
- Omitting the stream argument causes the function to be printed to the
- screen.
- Returns NIL."
-
- (let ((save-package (package-name *package*))
- (save-print-escape *print-escape*)
- (funcdef (function-definition (eval (list 'function a)))))
- (in-package (package-name (symbol-package a)))
- (setq *print-escape* t)
-
- ; replace "macro" with "defmacro name"
- ; and "lambda" with "defun name"
- (if (and (symbolp a) funcdef (consp funcdef))
- (if (eq (car funcdef) 'macro)
- (setq funcdef (cons 'defmacro (cons a (cdr funcdef))))
- (if (eq (car funcdef) 'lambda)
- (setq funcdef (cons 'defun (cons a (cdr funcdef)))))))
-
- (if stream
- (progn
- (print (list 'in-package (package-name (symbol-package a))) stream)
- (print funcdef stream))
- (progn
- (print (list 'in-package (package-name (symbol-package a))))
- (print funcdef)))
- (in-package save-package)
- (setq *print-escape* save-print-escape)
- nil))
-
- ;--------------------------------------------------------------------------
- ;
- ; dump-hash-table is useful for printing out the contents of a hash table
- ;
- (defun dump-hash-table (table)
- "dump-hash-table (hash-table)
- Usage: (dump-hash-table hash-table)"
- (maphash #'(lambda (key val)
- (write "pair: ")
- (write key)
- (write " ")
- (write val)
- (terpri)) table))
-
- ;--------------------------------------------------------------------------
- ;
- ; show-lisp-symbols is useful for printing out the names of all the common
- ; lisp symbols
- ;
- (defun show-lisp-symbols ()
- "Usage: (show-lisp-symbols)
- Displays all the symbols in the COMMON-LISP package."
- (dump-hash-table (package-hash-table (find-package "COMMON-LISP"))))
-
- ; print n spaces to standard output
- (defun spaces (n)
- (dotimes (i n) (write " ")))
-
-