home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lisp / interpre / xlispplu / lsp / common.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1992-01-14  |  10.8 KB  |  354 lines

  1. ;; functions missing that are part of common lisp, and commonly used
  2.  
  3. ;; It is assumed you are using XLISP with all Common Lisp related options
  4. ;; turned on before you load this file.
  5.  
  6. ;; Author either unknown or Tom Almy unless indicated.
  7.  
  8. ;; pairlis does not check for lengths of keys and values being unequal
  9.  
  10. (defun pairlis (keys values &optional list)
  11.        (nconc (mapcar #'cons keys values) list))
  12.  
  13. (defun copy-list (list) (append list 'nil))
  14.  
  15. (defun copy-alist (list)
  16.     (if (null list)
  17.         'nil
  18.         (cons (if (consp (car list))
  19.           (cons (caar list) (cdar list))
  20.           (car list))
  21.           (my-copy-alist (cdr list)))))
  22.  
  23. (defun copy-tree (list)
  24.     (if (consp list)
  25.         (cons (copy-tree (car list)) (copy-tree (cdr list)))
  26.         list))
  27.  
  28. (unless (fboundp 'list*)
  29. (defun list* (&rest list)    ;; There must be a better way...
  30.     (cond ((null list) 'nil)
  31.       ((null (cdr list)) (car list))
  32.       (t (do* ((head (cons (car list) 'nil))
  33.            (current head
  34.                 (cdr (rplacd current (cons (car tail) 'nil))))
  35.            (tail (cdr list) (cdr tail)))
  36.           ((null (cdr tail)) (rplacd current (car tail)) head)
  37.           ))))
  38. )
  39.  
  40. (defun identity (l) l)
  41.  
  42. (defun signum (x)
  43.    (cond ((not (numberp x)) (error "not a number" x))
  44.          ((zerop x) x)
  45.      (T (/ x (abs x)))))  
  46.  
  47. ; Cruddy but simple versions of these functions.
  48. ; Commented out since XLISP will now expand macros once, making
  49. ; good version much preferred.
  50.  
  51. ;(defmacro incf (var &optional (delta 1))
  52. ;    `(setf ,var (+ ,var ,delta)))
  53.  
  54. ;(defmacro decf (var &optional (delta 1))
  55. ;    `(setf ,var (- ,var ,delta)))
  56.  
  57. ;(defmacro push (v l)
  58. ;    `(setf ,l (cons ,v ,l))))
  59.  
  60. ;(defmacro pushnew (a l &rest args)
  61. ;  `(unless (member ,a ,l ,@args) (push ,a ,l) nil))
  62.  
  63. ;(defmacro pop (l)
  64. ;    `(prog1 (first ,l) (setf ,l (rest ,l)))))
  65.  
  66.  
  67. ; This is what one really needs to do for incf decf and
  68. ; (in common.lsp) push and pop. The setf form must only be evaluated once.
  69. ; But is it worth all this overhead for correctness?
  70. ; (By Tom Almy)
  71.  
  72. (defun |DoForm| (form) ; returns (cons |list for let| |new form|)
  73.        (let* ((args (rest form)) ; raw form arguments
  74.           (letlist (mapcan #'(lambda (x) (when (consp x)
  75.                            (list (list (gensym) x))))
  76.                    form))
  77.           (revlist (mapcar #'(lambda (x) (cons (second x) (first x)))
  78.                    letlist))
  79.           (newform (cons (first form) (sublis revlist args))))
  80.          (cons letlist newform)))
  81.  
  82. (defmacro incf (form &optional (delta 1))
  83.       (if (and (consp form) (some #'consp form))
  84.           (let ((retval (|DoForm| form)))
  85.            `(let ,(car retval) 
  86.              (setf ,(cdr retval)
  87.                    (+ ,(cdr retval) ,delta))))
  88.           `(setf ,form (+ ,form ,delta))))
  89.  
  90. (defmacro decf (form &optional (delta 1))
  91.       (if (and (consp form) (some #'consp form))
  92.           (let ((retval (|DoForm| form)))
  93.            `(let ,(car retval) 
  94.              (setf ,(cdr retval)
  95.                    (- ,(cdr retval) ,delta))))
  96.           `(setf ,form (- ,form ,delta))))
  97.  
  98. (defmacro push (val form)
  99.       (if (and (consp form) (some #'consp form))
  100.           (let ((retval (|DoForm| form)))
  101.            `(let ,(car retval) 
  102.              (setf ,(cdr retval)
  103.                    (cons ,val ,(cdr retval)))))
  104.           `(setf ,form (cons ,val ,form))))
  105.  
  106. (defmacro pop (form)
  107.       (if (and (consp form) (some #'consp form))
  108.           (let ((retval (|DoForm| form)))
  109.            `(let ,(car retval) 
  110.              (prog1 (first ,(cdr retval))
  111.                 (setf ,(cdr retval)
  112.                       (rest ,(cdr retval))))))
  113.           `(prog1 (first ,form)
  114.               (setf ,form (rest ,form)))))
  115.  
  116.  
  117. (defmacro pushnew (val form &rest rest)
  118.       (if (and (consp form) (some #'consp form))
  119.           (let ((retval (|DoForm| form)))
  120.            `(let ,(car retval) 
  121.              (setf ,(cdr retval)
  122.                    (adjoin ,val ,(cdr retval) ,@rest))))
  123.           `(setf ,form (adjoin ,val ,form ,@rest))))
  124.  
  125.  
  126. ;; Hyperbolic functions    Ken Whedbee  from CLtL
  127.  
  128. (defun logtest (x y) (not (zerop (logand x y))))
  129.  
  130. (defconstant imag-one #C(0.0 1.0))
  131.  
  132. (defun cis (x) (exp (* imag-one x)))
  133.  
  134.  
  135. (defun sinh (x) (/ (- (exp x) (exp (- x))) 2.0))
  136. (defun cosh (x) (/ (+ (exp x) (exp (- x))) 2.0))
  137. (defun tanh (x) (/ (sinh x) (cosh x)))
  138.  
  139. (defun asinh (x) (log (+ x (sqrt (+ 1.0 (* x x))))))
  140. (defun acosh (x)
  141.        (log (+ x
  142.                (* (1+ x)
  143.                     (sqrt (/ (1- x) (1+ x)))))))
  144. (defun atanh (x)
  145.        (when (or (= x 1.0) (= x -1.0))
  146.              (error "logarithmic singularity" x))
  147.        (log (/ (1+ x) (sqrt (- 1.0 (* x x))))))
  148.     
  149.  
  150.  
  151. ;; Additional Common Lisp Functions by Luke Tierney
  152. ;; from xlisp-stat
  153.  
  154. ;;
  155. ;; Defsetf and documentation functions
  156. ;;
  157.  
  158. (defun apply-arg-rotate (f args) 
  159.   (apply f (car (last args)) (butlast args)))
  160.  
  161. ; (defsetf) - define setf method
  162. (defmacro defsetf (sym first &rest rest)
  163.   (if (symbolp first)
  164.       `(progn (setf (get ',sym '*setf*) #',first) ',sym)
  165.       (let ((f `#'(lambda ,(append (car rest) first) ,@(cdr rest)))
  166.             (args (gensym)))
  167.         `(progn
  168.           (setf (get ',sym '*setf*) 
  169.                 #'(lambda (&rest ,args) (apply-arg-rotate ,f ,args)))
  170.           ',sym))))
  171.  
  172.   
  173. ;;;;
  174. ;;;;
  175. ;;;; Modules, provide and require:  Luke Tierney, from xlisp-stat
  176. ;;;;
  177. ;;;;
  178.  
  179. ; Uncomment these if you want them. It's non-standard, and nothing else
  180. ; in this distribution  uses them, so I'm commenting them out.  Tom Almy
  181.  
  182. ;(defvar *modules*)
  183.     
  184. ;(defun provide (name)
  185. ;  (pushnew name *modules* :test #'equal))
  186.   
  187. ;(defun require (name &optional (path name))
  188. ;  (let ((name (string name))
  189. ;        (path (string path)))
  190. ;    (unless (member name *modules* :test #'equal)
  191. ;            (if (load path)
  192. ;                t
  193. ;        (load (strcat *default-path* path))))))
  194.  
  195. ;;;;
  196. ;;;;
  197. ;;;; Miscellaneous Functions:  Luke Tierney
  198. ;;;;    from xlisp-stat
  199. ;;;;
  200.  
  201. ;(defun vectorp (x)
  202. ;  (and (arrayp x) (= (array-rank x) 1)))
  203.  
  204. ; equalp rewritten by Tom Almy to better match Common Lisp
  205. (defun equalp (x y)
  206.   (cond ((equal x y) t)
  207.       ((numberp x) (if (numberp y) (= x y) nil))
  208.       ((characterp x) (if (characterp y) (char-equal x y) nil))
  209.       ((and (or (arrayp x) (stringp x)) 
  210.             (or (arrayp y) (stringp y))
  211.             (eql (length x) (length y)))
  212.        (every #'equalp x y))))
  213.  
  214. ; also improved by TAA to use *terminal-io*
  215. (defun y-or-n-p (&rest args)
  216.   (do ((answer nil 
  217.            (let* ((*breakenable* nil)
  218.               (x (errset (read *terminal-io*) nil)))
  219.              (when (consp x) (car x)))))
  220.       ((member answer '(y n)) (eq answer 'y))
  221.       (when args (apply #'format *terminal-io* args))
  222.       (princ " (Y/N)" *terminal-io*)))
  223.  
  224. ; This implementation is questionable (says TAA), I'm commenting it out
  225.  
  226. ; (defun getf (place indicator &optional default)
  227. ;   (let ((mem (member indicator place :test #'eq)))
  228. ;    (if mem (second mem) default)))
  229.  
  230.  
  231. ; Improved by TAA to match common lisp definition
  232. (defun functionp (x)
  233.     (if (typep x '(or closure subr symbol))
  234.     t
  235.         (and (consp x) (eq (car x) 'lambda))))
  236.  
  237. (defmacro with-input-from-string (stream-string &rest body)
  238.   (let ((stream (first stream-string))
  239.         (string (second stream-string)))
  240.     `(let ((,stream (make-string-input-stream ,string)))
  241.        (progn ,@body))))
  242.  
  243.  
  244. (defmacro with-input-from-string
  245.       (stream-string &rest body)
  246.       (let ((stream (first stream-string))
  247.         (string (second stream-string))
  248.         (start (second (member :start (cddr stream-string))))
  249.         (end (second (member :end (cddr stream-string))))
  250.         (index (second (member :index (cddr stream-string)))))
  251.            (when (null start) (setf start 0))
  252.            (if index
  253.            (let ((str (gensym)))
  254.             `(let* ((,str ,string)
  255.                 (,stream (make-string-input-stream ,str 
  256.                                    ,start 
  257.                                    ,end)))
  258.                (prog1 (progn ,@body)
  259.                   (setf ,index 
  260.                     (- (length ,str)
  261.                        (length (get-output-stream-list 
  262.                              ,stream)))))))
  263.            `(let ((,stream (make-string-input-stream ,string ,start ,end)))
  264.              (progn ,@body)))))
  265.            
  266.  
  267. (defmacro with-output-to-string (str-list &rest body)
  268.   (let ((stream (first str-list)))
  269.     `(let ((,stream (make-string-output-stream)))
  270.        (progn ,@body)
  271.        (get-output-stream-string ,stream))))
  272.  
  273. (defmacro with-open-file (stream-file-args &rest body)
  274.   (let ((stream (first stream-file-args))
  275.         (file-args (rest stream-file-args)))
  276.     `(let ((,stream (open ,@file-args)))
  277.        (unwind-protect 
  278.            (progn ,@body)
  279.          (when ,stream (close ,stream))))))
  280.  
  281. ; (unintern sym) - remove a symbol from the oblist
  282. (defun unintern (symbol)
  283.   (let ((subhash (hash symbol (length *obarray*))))
  284.     (cond ((member symbol (aref *obarray* subhash))
  285.              (setf (aref *obarray* subhash)
  286.                    (delete symbol (aref *obarray* subhash)))
  287.              t)
  288.           (t nil))))
  289.  
  290.  
  291. ;; array functions.   KCW    from  Kyoto Common Lisp
  292.  
  293. (defun fill (sequence item
  294.              &key (start 0) end)
  295.        (when (null end) (setf end (length sequence)))
  296.        (do ((i start (1+ i)))
  297.        ((>= i end) sequence)
  298.        (setf (elt sequence i) item)))
  299.  
  300.  
  301. (defun replace (sequence1 sequence2
  302.                 &key (start1 0) end1
  303.                      (start2 0) end2)
  304.     (when (null end1) (setf end1 (length sequence1)))
  305.     (when (null end2) (setf end2 (length sequence2)))
  306.     (if (and (eq sequence1 sequence2)
  307.              (> start1 start2))
  308.         (do* ((i 0 (1+ i))
  309.               (l (if (< (- end1 start1) (- end2 start2))
  310.                      (- end1 start1)
  311.                      (- end2 start2)))
  312.               (s1 (+ start1 (1- l)) (1- s1))
  313.               (s2 (+ start2 (1- l)) (1- s2)))
  314.             ((>= i l) sequence1)
  315.           (setf (elt sequence1 s1) (elt sequence2 s2)))
  316.         (do ((i 0 (1+ i))
  317.              (l (if (< (- end1 start1)(- end2 start2))
  318.                     (- end1 start1)
  319.                     (- end2 start2)))
  320.              (s1 start1 (1+ s1))
  321.              (s2 start2 (1+ s2)))
  322.             ((>= i l) sequence1)
  323.           (setf (elt sequence1 s1) (elt sequence2 s2)))))
  324.  
  325.  
  326. (defun acons (x y a)         ; from CLtL
  327.    (cons (cons x y) a))
  328.  
  329.  
  330. ;; more set functions.  KCW    from Kyoto Common Lisp
  331.  
  332. ;; Modified to pass keys to subfunctions without checking here
  333. ;; (more efficient)
  334.  
  335. ;; (Tom Almy states:) we can't get the destructive versions of union
  336. ;; intersection, and set-difference to run faster than the non-destructive
  337. ;; subrs. Therefore we will just have the destructive versions do their
  338. ;; non-destructive counterparts
  339.  
  340. (setf (symbol-function 'nunion) 
  341.       (symbol-function 'union)
  342.       (symbol-function 'nintersection) 
  343.       (symbol-function 'intersection)
  344.       (symbol-function 'nset-difference) 
  345.       (symbol-function 'set-difference))
  346.  
  347. (defun set-exclusive-or (list1 list2 &rest rest)
  348.   (append (apply #'set-difference list1 list2 rest)
  349.           (apply #'set-difference list2 list1 rest)))
  350.  
  351. (defun nset-exclusive-or (list1 list2 &rest rest)
  352.   (nconc (apply #'set-difference list1 list2 rest)
  353.          (apply #'set-difference list2 list1 rest)))
  354.