1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
|
; common2.lsp. Fill in "missing" Common Lisp definitions
; However, those that concern features of Common Lisp not implemented in
; XLISP (such as the compiler and arrays) are not implemented here.
; You might want to only include those functions your application needs.
; Tom Almy, 7/93, revised 4/97
#-:common (load "common") ; We need these always!
(in-package "XLISP")
(export
'(array-in-bounds-p assert assoc-if assoc-if-not
ccase char-code-limit char-name character check-type
copy-seq copy-symbol ctypecase delete-duplicates ecase
etypecase fifth sixth seventh eighth ninth tenth float-sign
hash-table-p isqrt keywordp packagep ldiff make-list
make-string make-sequence member-if member-if-not merge
mismatch nbutlast nreconc prin1-to-string princ-to-string
probe-file rassoc rassoc-if rassoc-if-not read-from-string realp
revappend tailp typecase sleep with-open-stream byte byte-size
byte-position ldb ldb-test mask-field dpb deposit-field the ))
(defconstant char-code-limit 128)
;; Byte functions Added by Tom Almy, 4/95
;; "alias" these to common list functions
(setf (symbol-function 'byte) (symbol-function 'cons))
(setf (symbol-function 'byte-size) (symbol-function 'car))
(setf (symbol-function 'byte-position) (symbol-function 'cdr))
(defmacro bytetest (x)
`(unless (and
(consp ,x)
(typep (byte-size ,x) 'fixnum)
(typep (byte-position ,x) 'fixnum)
(>= (byte-size ,x) 0)
(>= (byte-position ,x) 0))
(error "arguments must be non-negative fixnums")))
(defun ldb (byte x)
(bytetest byte)
(logand (ash x (- (byte-position byte)))
(lognot (ash -1 (byte-size byte)))))
(defun ldb-test (byte x)
(bytetest byte)
(not (zerop (ldb byte x))))
(defun mask-field (byte x)
(bytetest byte)
(logand (ash (lognot (ash -1 (byte-size byte))) (byte-position byte))
x))
(defun dpb (new byte x)
(bytetest byte)
(let ((mask (lognot (ash -1 (byte-size byte)))))
(logior (logand x
(lognot (ash mask (byte-position byte))))
(ash (logand new mask) (byte-position byte)))))
(defun deposit-field (new byte x)
(bytetest byte)
(logxor x
(logand (ash (lognot (ash -1 (byte-size byte)))
(byte-position byte))
(logxor x new))))
(defsetf ldb (byte x) (new)
(let ((nbyte (gensym)) (nnew (gensym)))
(if (and (consp x) (some #'consp x))
(let ((retval (|DoForm| x)))
`(let ((,nbyte ,byte) ,@(car retval) (,nnew ,new))
(setf ,(cdr retval)
(dpb ,nnew ,nbyte ,(cdr retval)))
,nnew))
`(let ((,nbyte ,byte)(,nnew ,new))
(setf ,x (dpb ,nnew ,nbyte ,x)) ,nnew))))
(defsetf mask-field (byte x) (new)
(let ((nbyte (gensym)) (nnew (gensym)))
(if (and (consp x) (some #'consp x))
(let ((retval (|DoForm| x)))
`(let ((,nbyte ,byte) ,@(car retval) (,nnew ,new))
(setf ,(cdr retval)
(deposit-field ,nnew ,nbyte ,(cdr retval)))
,nnew))
`(let ((,nbyte ,byte)(,nnew ,new))
(setf ,x (deposit-field ,nnew ,nbyte ,x)) ,nnew))))
; Simplistic versions, probably goodenuf, the problem is that they
; do multiple evaluations
; (defsetf ldb (byte x) (new)
; `(progn (setf ,x (dpb ,new ,byte ,x)) ,new))
;(defsetf mask-field (byte x) (new)
; `(progn (setf ,x (deposit-field ,new ,byte ,x)) , new))
;; Apropos function contributed by Jan Kok, kok@cs.ColoState.edu
#-:packages
(defun apropos-list (string)
(let ((str (string string)) ; convert to string, if symbol is passed
(result nil))
(dotimes (i (length *obarray*) result)
(dolist (sym (elt *obarray* i))
(if (search str (string sym) :test #'char-equal)
(push sym result))))))
#-:packages
(defun apropos (string)
(mapcar #'(lambda (x)
(format t "~&~s" x)
(when (fboundp x) (princ " (defined)"))
(when (boundp x) (format t " value: ~s" x)))
(apropos-list string))
#+:mulvals (values)
#-:mulvals nil ; CL says return "nothing" but we need to return NIL
)
(defun array-in-bounds-p (a n) (and (>= n 0 (< n (length a)))))
(defmacro assert (test &optional forms &rest x &aux (loop (gensym)))
`(prog ((*print-length* 4) (*print-level* 4))
,loop
(when ,test (return nil))
(cerror "requests new values for ~*~s"
"~a"
,(if (null x) "" `(format nil ,@x))
',forms)
,@(mapcan #'(lambda (i)
`((format t "Enter new value for ~s:" ',i)
(setf ,i (read))))
forms)
(go ,loop)))
(defun assoc-if (test form &key (key #'identity))
(do ()
((endp form) nil)
(when (and (consp (car form))
(funcall test (funcall key (caar form))))
(return (car form)))
(setq form (cdr form))))
(defun assoc-if-not (test form &key (key #'identity))
(do ()
((endp form) nil)
(when (and (consp (car form))
(not (funcall test (funcall key (caar form)))))
(return (car form)))
(setq form (cdr form))))
(defmacro ccase (keyform &rest x)
(let ((val (gensym))
(loop (gensym))
(values (mapcan #'(lambda (x)
(if (listp (car x))
(copy-list (car x))
(list (car x))))
x)))
`(prog (,val) ,loop
(setf ,val ,keyform)
(return
(case ,val
,@x
(t (cerror
"requests new value for ~s"
"~*~s fell through ccase expression, wanted one of ~s"
',keyform
,val
',values)
(format t "New value:")
(setf ,keyform (read))
(go ,loop)))))))
(defun char-name (c)
(case c
(#\Space "Space")
(#\Newline "Newline")
(#\Rubout "Rubout")))
(defun character (arg) (coerce arg 'character))
(defmacro check-type (place type
&optional (string `',type)
&aux (loop (gensym)))
`(prog () ,loop
(when (typep ,place ',type) (return))
(cerror "requests new value for ~s"
"the value of ~s is ~s which is not of type ~a"
',place
,place
,string)
(format t "New value:")
(setf ,place (read))
(go ,loop)))
(defun copy-seq (seq) (subseq seq 0))
(defun copy-symbol (sym &optional all)
(unless (symbolp sym) (error "~s is not a symbol" sym))
(let ((x (make-symbol (symbol-name sym))))
(when all
(when (boundp sym) (setf (symbol-value x) sym))
(when (fboundp sym)
(setf (symbol-function x) (symbol-function sym)))
(setf (symbol-plist x) (symbol-plist sym)))
x))
(defmacro ctypecase (keyform &rest x)
(let ((val (gensym))
(loop (gensym))
(values (mapcan #'(lambda (x)
(if (listp (car x))
(list (copy-list (car x)))
(list (car x))))
x)))
`(prog (,val) ,loop
(setq ,val ,keyform)
(return
(typecase ,val
,@x
(otherwise (cerror
"requests new value for ~s"
"~*~s fell through ctypecase expression, wanted one of ~s"
',keyform
,val
',values)
(format t "New value:")
(setf ,keyform (read))
(go ,loop)))))))
(setf (symbol-function 'delete-duplicates) ; cheat on this one
(symbol-function 'remove-duplicates))
#-:packages
(defmacro do-all-symbols (arglist &rest body)
(when (not (consp arglist)) (error "Bad first argument"))
`(dolist ( ,(first arglist)
',(mapcan #'copy-list (coerce *obarray* 'list))
,@(rest arglist))
,@body))
(defmacro ecase (keyform &rest x)
(let ((tmp (gensym))
(values (mapcan #'(lambda (x)
(if (listp (car x))
(copy-list (car x))
(list (car x))))
x)))
`(let ((,tmp ,keyform))
(case ,tmp ,@x
(t (error "case ~s not one of ~s" ,tmp ',values))))))
(defmacro etypecase (keyform &rest x)
(let ((tmp (gensym))
(values (mapcan #'(lambda (x)
(if (listp (car x))
(list (copy-list (car x)))
(list (car x))))
x)))
`(let ((,tmp ,keyform))
(typecase ,tmp ,@x
(otherwise (error "type of ~s not one of ~s" ,tmp ',values))))))
;; These are terribly inefficient (see the awful defsetf code in common.lsp)
(defsetf first (x) (y) `(progn (rplaca ,x ,y) ,y)) ; same as "car"
(defsetf rest (x) (y) `(progn (rplacd ,x ,y) ,y)) ; same as "cdr"
(defsetf second (x) (y) `(progn (rplaca (cdr ,x) ,y) ,y))
(defsetf third (x) (y) `(progn (rplaca (cddr ,x) ,y) ,y))
(defsetf fourth (x) (y) `(progn (rplaca (cdddr ,x) ,y) ,y))
(defsetf fifth (x) (y) `(progn (rplaca (cddddr ,x) ,y) ,y))
(defsetf sixth (x) (y) `(progn (rplaca (nthcdr 5 ,x) ,y) ,y))
(defsetf seventh (x) (y) `(progn (rplaca (nthcdr 6 ,x) ,y) ,y))
(defsetf eighth (x) (y) `(progn (rplaca (nthcdr 7 ,x) ,y) ,y))
(defsetf ninth (x) (y) `(progn (rplaca (nthcdr 8 ,x) ,y) ,y))
(defsetf tenth (x) (y) `(progn (rplaca (nthcdr 9 ,x) ,y) ,y))
(defsetf cadr (x) (y) `(progn (rplaca (cdr ,x) ,y) ,y))
(defsetf caar (x) (y) `(progn (rplaca (car ,x) ,y) ,y))
(defsetf cdar (x) (y) `(progn (rplacd (car ,x) ,y) ,y))
(defsetf cddr (x) (y) `(progn (rplacd (cdr ,x) ,y) ,y))
(defsetf caaar (x) (y) `(progn (rplaca (caar ,x) ,y) ,y))
(defsetf caadr (x) (y) `(progn (rplaca (cadr ,x) ,y) ,y))
(defsetf cadar (x) (y) `(progn (rplaca (cdar ,x) ,y) ,y))
(defsetf caddr (x) (y) `(progn (rplaca (cddr ,x) ,y) ,y))
(defsetf cdaar (x) (y) `(progn (rplacd (caar ,x) ,y) ,y))
(defsetf cdadr (x) (y) `(progn (rplacd (cadr ,x) ,y) ,y))
(defsetf cddar (x) (y) `(progn (rplacd (cdar ,x) ,y) ,y))
(defsetf cdddr (x) (y) `(progn (rplacd (cddr ,x) ,y) ,y))
(defsetf caaaar (x) (y) `(progn (rplaca (caaar ,x) ,y) ,y))
(defsetf caaadr (x) (y) `(progn (rplaca (caadr ,x) ,y) ,y))
(defsetf caadar (x) (y) `(progn (rplaca (cadar ,x) ,y) ,y))
(defsetf caaddr (x) (y) `(progn (rplaca (caddr ,x) ,y) ,y))
(defsetf cadaar (x) (y) `(progn (rplaca (cdaar ,x) ,y) ,y))
(defsetf cadadr (x) (y) `(progn (rplaca (cdadr ,x) ,y) ,y))
(defsetf caddar (x) (y) `(progn (rplaca (cddar ,x) ,y) ,y))
(defsetf cadddr (x) (y) `(progn (rplaca (cdddr ,x) ,y) ,y))
(defsetf cdaaar (x) (y) `(progn (rplacd (caaar ,x) ,y) ,y))
(defsetf cdaadr (x) (y) `(progn (rplacd (caadr ,x) ,y) ,y))
(defsetf cdadar (x) (y) `(progn (rplacd (cadar ,x) ,y) ,y))
(defsetf cdaddr (x) (y) `(progn (rplacd (caddr ,x) ,y) ,y))
(defsetf cddaar (x) (y) `(progn (rplacd (cdaar ,x) ,y) ,y))
(defsetf cddadr (x) (y) `(progn (rplacd (cdadr ,x) ,y) ,y))
(defsetf cdddar (x) (y) `(progn (rplacd (cddar ,x) ,y) ,y))
(defsetf cddddr (x) (y) `(progn (rplacd (cdddr ,x) ,y) ,y))
(defun fifth (x) (nth 4 x))
(defun sixth (x) (nth 5 x))
(defun seventh (x) (nth 6 x))
(defun eighth (x) (nth 7 x))
(defun ninth (x) (nth 8 x))
(defun tenth (x) (nth 9 x))
(defun float-sign (x &optional (y 1.0))
(if (minusp x) (- (abs y))
(abs y)))
(defun hash-table-p (x) (typep x 'hash-table))
#-:bignums (defun isqrt (x) (floor (sqrt x)))
#+:bignums
(defun isqrt (x)
(if (and (typep x 'integer) (not (minusp x)))
(do* ((est (ash 1 (truncate (integer-length x) 2))
(truncate (+ est est2) 2))
(est2 (truncate x est) (truncate x est)))
((> 2 (abs (- est est2))) (min est est2)))
(floor (sqrt x))))
#+:packages
(defun keywordp (x) (eq (symbol-package x) (find-package "KEYWORD")))
#-:packages
(defun keywordp (x)
(and (symbolp x) (eq #\: (aref (symbol-name x) 0))))
#+:packages
(defun packagep (x) (typep x 'package))
(defun ldiff (l m)
(do ((list l (cdr list)))
((endp list) (copy-list l))
(when (eq m list) (return (butlast l (length list))))))
; This won't make really big lists in MS-DOS environments
(defun make-list (n &key initial-element &aux val)
(coerce (make-array n :initial-element initial-element) 'list))
; This will make bigger lists, but is about 5x slower
;(defun make-list (n &key initial-element &aux val)
; (dotimes (i n val) (push initial-element val)))
; These won't make really big strings/sequences in MS-DOS environments, but
; I'm not giving an alternative!
(defun make-string (n &key (initial-element #\space))
(coerce (make-array n :initial-element initial-element) 'string))
(defun make-sequence (type n
&key (initial-element
(if (eq type 'string) #\space nil)))
(coerce (make-array n :initial-element initial-element) type))
(defun member-if (test form &key (key #'identity))
(do ()
((endp form) nil)
(when (funcall test (funcall key (car form))) (return form))
(setq form (cdr form))))
(defun member-if-not (test form &key (key #'identity))
(do ()
((endp form) nil)
(unless (funcall test (funcall key (car form))) (return form))
(setq form (cdr form))))
(defun merge (type s1 s2 pred &key (key #'identity))
(let* ((i1 0)
(i2 0)
(i3 0)
(l1 (length s1))
(l2 (length s2))
(l3 (+ l1 l2))
(res (make-sequence type l3)))
(loop
(when (eql i3 l3) (return res))
(setf (elt res i3)
(cond
((eql i1 l1)
(prog1 (elt s2 i2) (incf i2)))
((eql i2 l2)
(prog1 (elt s1 i1) (incf i1)))
((funcall pred
(funcall key (elt s1 i1))
(funcall key (elt s2 i2)))
(prog1 (elt s1 i1) (incf i1)))
(t
(prog1 (elt s2 i2) (incf i2)))))
(incf i3))))
(defun mismatch (sequence1 sequence2
&key
(key #'identity)
test test-not
(start1 0) end1
(start2 0) end2)
(when (null end1) (setq end1 (length sequence1)))
(when (null end2) (setq end2 (length sequence2)))
(when (and test test-not)
(error "cannot specify both :test and :test-not"))
(if test-not
(setq test test-not test-not #'not)
(progn (setq test-not #'identity)
(unless test (setq test #'eql))))
(do* ((s1 start1 (1+ s1))
(s2 start2 (1+ s2)))
((or (>= s1 end1) (>= s2 end2))
(if (and (>= s1 end1) (>= s2 end2))
nil
s1))
(unless (funcall test-not
(funcall test
(funcall key (elt sequence1 s1))
(funcall key (elt sequence2 s2))))
(return s1))))
; There are numerous n* functions that I'm not implementing because
; They would be slower then the builtin, non-destructive equivalents.
(defun nbutlast (list &optional (n 1) &aux (l (length list)))
(if (< n l)
(prog2 (setf (cdr (nthcdr (1- (- l n)) list)) nil) list)
nil))
(defun nreconc (l1 l2) (nconc (nreverse l1) l2))
(defun prin1-to-string (arg) (format nil "~s" arg))
(defun princ-to-string (arg) (format nil "~a" arg))
(defun probe-file (arg) (open arg :direction :probe))
(defun rassoc (i form &key (key #'identity) test test-not)
(when (and test test-not)
(error "cannot specify both :test and :test-not"))
(if test-not
(do ()
((endp form) nil)
(when (and (consp (car form))
(not (funcall test-not i (funcall key (cdar form)))))
(return (car form)))
(setq form (cdr form)))
(progn (unless test (setq test #'eql))
(do ()
((endp form) nil)
(when (and (consp (car form))
(funcall test i (funcall key (cdar form))))
(return (car form)))
(setq form (cdr form))))))
(defun rassoc-if (test form &key (key #'identity))
(do ()
((endp form) nil)
(when (and (consp (car form))
(funcall test (funcall key (cdar form))))
(return (car form)))
(setq form (cdr form))))
(defun rassoc-if-not (test form &key (key #'identity))
(do ()
((endp form) nil)
(when (and (consp (car form))
(not (funcall test (funcall key (cdar form)))))
(return (car form)))
(setq form (cdr form))))
#+:mulvals
(defun read-from-string (arg &optional (e1 t) e2 &key (start 0) end)
(let* ((r nil)
(s (with-input-from-string (f arg :start start :end end :index r)
(read f e1 e2))))
(values s r)))
#-:mulvals ; Doesn't return second value (index into arg where parse stopped)
(defun read-from-string (arg &optional (e1 t) e2 &key (start 0) end)
(with-input-from-string (f arg :start start :end end)
(read f e1 e2)))
#+:bignums (defun realp (arg) (or (rationalp arg) (floatp arg)))
#-:bignums (defun realp (arg) (or (integerp arg) (floatp arg)))
(defun revappend (x y) (nconc (reverse x) y))
(defun tailp (s l)
(do ()
((endp l) nil)
(when (eq s l) (return t))
(setq l (cdr l))))
(defmacro typecase (keyform &rest x &aux (tmp (gensym)))
`(let ((,tmp ,keyform))
(cond
,@(mapcar #'(lambda (x)
(if (eq (first x) 'otherwise)
`(t ,@(rest x))
`((typep ,tmp ',(first x))
,@(rest x))))
x))))
(defun sleep (time)
(let ((endtime (+ time (/ (get-internal-real-time)
internal-time-units-per-second))))
(loop (when (> (/ (get-internal-real-time)
internal-time-units-per-second)
endtime)
(return nil)))))
(defmacro with-open-stream (stream-args &rest body)
`(let ((,(first stream-args) ,(second stream-args)))
(unwind-protect
(progn ,@body)
(when ,(first stream-args) (close ,(first stream-args))))))
; the function, contributed by Leo Sarasua, lsarasua@epo.e-mail.com
(defmacro the (type form)
(let ((eval-form (gensym "form")))
`(let ((,eval-form ,form))
(if (eql ',type (type-of ,eval-form))
,eval-form
(error "The value of ~s is not of type ~s" ',form ',type) ))))
|