PageRenderTime 61ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/src/code/list.lisp

http://github.com/sbcl/sbcl
Lisp | 1453 lines | 1267 code | 87 blank | 99 comment | 34 complexity | 952eb6e03fe81709c0e31f9a03454ce8 MD5 | raw file
Possible License(s): CC0-1.0

Large files files are truncated, but you can click here to view the full file

  1. ;;;; functions to implement lists
  2. ;;;; This software is part of the SBCL system. See the README file for
  3. ;;;; more information.
  4. ;;;;
  5. ;;;; This software is derived from the CMU CL system, which was
  6. ;;;; written at Carnegie Mellon University and released into the
  7. ;;;; public domain. The software is in the public domain and is
  8. ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
  9. ;;;; files for more information.
  10. (in-package "SB-IMPL")
  11. ;;; Limitation: no list might have more than INDEX conses.
  12. ;;;; KLUDGE: comment from CMU CL, what does it mean?
  13. ;;;; NSUBLIS, things at the beginning broken.
  14. ;;;; -- WHN 20000127
  15. (declaim (maybe-inline
  16. tree-equal %setnth nthcdr nth
  17. tailp union
  18. nunion intersection nintersection set-difference nset-difference
  19. set-exclusive-or nset-exclusive-or subsetp acons
  20. subst subst-if
  21. ;; NSUBLIS is >400 lines of assembly. How is it helpful to inline?
  22. subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis))
  23. ;;; These functions perform basic list operations.
  24. (defun car (list) "Return the 1st object in a list." (car list))
  25. (defun cdr (list)
  26. "Return all but the first object in a list."
  27. (cdr list))
  28. (defun cadr (list) "Return the 2nd object in a list." (cadr list))
  29. (defun cdar (list) "Return the cdr of the 1st sublist." (cdar list))
  30. (defun caar (list) "Return the car of the 1st sublist." (caar list))
  31. (defun cddr (list)
  32. "Return all but the 1st two objects of a list."
  33. (cddr list))
  34. (defun caddr (list)
  35. "Return the 1st object in the cddr of a list."
  36. (caddr list))
  37. (defun caadr (list)
  38. "Return the 1st object in the cadr of a list."
  39. (caadr list))
  40. (defun caaar (list)
  41. "Return the 1st object in the caar of a list."
  42. (caaar list))
  43. (defun cdaar (list)
  44. "Return the cdr of the caar of a list."
  45. (cdaar list))
  46. (defun cddar (list)
  47. "Return the cdr of the cdar of a list."
  48. (cddar list))
  49. (defun cdddr (list)
  50. "Return the cdr of the cddr of a list."
  51. (cdddr list))
  52. (defun cadar (list)
  53. "Return the car of the cdar of a list."
  54. (cadar list))
  55. (defun cdadr (list)
  56. "Return the cdr of the cadr of a list."
  57. (cdadr list))
  58. (defun caaaar (list)
  59. "Return the car of the caaar of a list."
  60. (caaaar list))
  61. (defun caaadr (list)
  62. "Return the car of the caadr of a list."
  63. (caaadr list))
  64. (defun caaddr (list)
  65. "Return the car of the caddr of a list."
  66. (caaddr list))
  67. (defun cadddr (list)
  68. "Return the car of the cdddr of a list."
  69. (cadddr list))
  70. (defun cddddr (list)
  71. "Return the cdr of the cdddr of a list."
  72. (cddddr list))
  73. (defun cdaaar (list)
  74. "Return the cdr of the caaar of a list."
  75. (cdaaar list))
  76. (defun cddaar (list)
  77. "Return the cdr of the cdaar of a list."
  78. (cddaar list))
  79. (defun cdddar (list)
  80. "Return the cdr of the cddar of a list."
  81. (cdddar list))
  82. (defun caadar (list)
  83. "Return the car of the cadar of a list."
  84. (caadar list))
  85. (defun cadaar (list)
  86. "Return the car of the cdaar of a list."
  87. (cadaar list))
  88. (defun cadadr (list)
  89. "Return the car of the cdadr of a list."
  90. (cadadr list))
  91. (defun caddar (list)
  92. "Return the car of the cddar of a list."
  93. (caddar list))
  94. (defun cdaadr (list)
  95. "Return the cdr of the caadr of a list."
  96. (cdaadr list))
  97. (defun cdadar (list)
  98. "Return the cdr of the cadar of a list."
  99. (cdadar list))
  100. (defun cdaddr (list)
  101. "Return the cdr of the caddr of a list."
  102. (cdaddr list))
  103. (defun cddadr (list)
  104. "Return the cdr of the cdadr of a list."
  105. (cddadr list))
  106. (defun cons (se1 se2)
  107. "Return a list with SE1 as the CAR and SE2 as the CDR."
  108. (cons se1 se2))
  109. (declaim (maybe-inline tree-equal-test tree-equal-test-not))
  110. (defun tree-equal-test-not (x y test-not)
  111. (declare (type function test-not))
  112. (cond ((consp x)
  113. (and (consp y)
  114. (tree-equal-test-not (car x) (car y) test-not)
  115. (tree-equal-test-not (cdr x) (cdr y) test-not)))
  116. ((consp y) nil)
  117. ((not (funcall test-not x y)) t)
  118. (t ())))
  119. (defun tree-equal-test (x y test)
  120. (declare (type function test))
  121. (cond ((consp x)
  122. (and (consp y)
  123. (tree-equal-test (car x) (car y) test)
  124. (tree-equal-test (cdr x) (cdr y) test)))
  125. ((consp y) nil)
  126. ((funcall test x y) t)
  127. (t ())))
  128. (defun tree-equal-eql (x y)
  129. (labels ((recurse (x y)
  130. (if (eq x y)
  131. t
  132. (do ((x x (cdr x))
  133. (y y (cdr y)))
  134. ((or (atom x)
  135. (atom y))
  136. (or (eql x y)
  137. (return-from tree-equal-eql)))
  138. (cond ((consp (car x))
  139. (when (atom (car y))
  140. (return-from tree-equal-eql))
  141. (recurse (car x) (car y)))
  142. ((not (eql (car x) (car y)))
  143. (return-from tree-equal-eql)))))))
  144. (recurse x y)))
  145. (defun tree-equal (x y &key (test nil testp) (test-not nil notp))
  146. "Return T if X and Y are isomorphic trees with identical leaves."
  147. (declare (explicit-check))
  148. (declare (dynamic-extent test test-not))
  149. (cond (notp
  150. (when testp
  151. (error ":TEST and :TEST-NOT were both supplied."))
  152. (tree-equal-test-not x y (%coerce-callable-to-fun test-not)))
  153. ((or (not test)
  154. (eql test #'eql)
  155. (eql test 'eql))
  156. (tree-equal-eql x y))
  157. (t
  158. (tree-equal-test x y (%coerce-callable-to-fun test)))))
  159. (defun endp (object)
  160. "This is the recommended way to test for the end of a proper list. It
  161. returns true if OBJECT is NIL, false if OBJECT is a CONS, and an error
  162. for any other type of OBJECT."
  163. (endp object))
  164. (defun list-length (list)
  165. "Return the length of the given List, or Nil if the List is circular."
  166. (do ((n 0 (+ n 2))
  167. (y list (cddr y))
  168. (z list (cdr z)))
  169. (())
  170. (declare (type fixnum n)
  171. (type list y z))
  172. (when (endp y) (return n))
  173. (when (endp (cdr y)) (return (+ n 1)))
  174. (when (and (eq y z) (> n 0)) (return nil))))
  175. (defun nth (n list)
  176. "Return the nth object in a list where the car is the zero-th element."
  177. (declare (explicit-check)
  178. (optimize speed))
  179. (typecase n
  180. ((and fixnum unsigned-byte)
  181. (block nil
  182. (let ((i n)
  183. (result list))
  184. (tagbody
  185. loop
  186. (the list result)
  187. (if (plusp i)
  188. (psetq i (1- i)
  189. result (cdr result))
  190. (return (car result)))
  191. (go loop)))))
  192. (t
  193. (car (nthcdr n list)))))
  194. (defun first (list)
  195. "Return the 1st object in a list or NIL if the list is empty."
  196. (car list))
  197. (defun second (list)
  198. "Return the 2nd object in a list or NIL if there is no 2nd object."
  199. (cadr list))
  200. (defun third (list)
  201. "Return the 3rd object in a list or NIL if there is no 3rd object."
  202. (caddr list))
  203. (defun fourth (list)
  204. "Return the 4th object in a list or NIL if there is no 4th object."
  205. (cadddr list))
  206. (defun fifth (list)
  207. "Return the 5th object in a list or NIL if there is no 5th object."
  208. (car (cddddr list)))
  209. (defun sixth (list)
  210. "Return the 6th object in a list or NIL if there is no 6th object."
  211. (cadr (cddddr list)))
  212. (defun seventh (list)
  213. "Return the 7th object in a list or NIL if there is no 7th object."
  214. (caddr (cddddr list)))
  215. (defun eighth (list)
  216. "Return the 8th object in a list or NIL if there is no 8th object."
  217. (cadddr (cddddr list)))
  218. (defun ninth (list)
  219. "Return the 9th object in a list or NIL if there is no 9th object."
  220. (car (cddddr (cddddr list))))
  221. (defun tenth (list)
  222. "Return the 10th object in a list or NIL if there is no 10th object."
  223. (cadr (cddddr (cddddr list))))
  224. (defun rest (list)
  225. "Means the same as the cdr of a list."
  226. (cdr list))
  227. (defun nthcdr (n list)
  228. "Performs the cdr function n times on a list."
  229. (declare (explicit-check n)
  230. (optimize speed))
  231. (flet ((fast-nthcdr (n list)
  232. (do ((i n (1- i))
  233. (result list (cdr result)))
  234. ((not (plusp i)) result))))
  235. (typecase n
  236. ((and fixnum unsigned-byte)
  237. (fast-nthcdr n list))
  238. ;; Such a large list can only be circular
  239. (t
  240. (locally (declare (unsigned-byte n))
  241. (do ((i 0 (1+ i))
  242. (r-i list (cdr r-i))
  243. (r-2i list (cddr r-2i)))
  244. ((and (eq r-i r-2i) (not (zerop i)))
  245. (fast-nthcdr (mod n i) r-i))
  246. (declare (type fixnum i))))))))
  247. ;;; For [n]butlast
  248. (defun dotted-nthcdr (n list)
  249. (declare (fixnum n))
  250. (do ((i n (1- i))
  251. (result list (cdr result)))
  252. ((not (plusp i)) result)
  253. (declare (type fixnum i))
  254. (when (atom result)
  255. (return))))
  256. ;;; LAST
  257. ;;;
  258. ;;; Transforms in src/compiler/srctran.lisp pick the most specific
  259. ;;; version possible. %LAST/BIGNUM is admittedly somewhat academic...
  260. (macrolet ((last0-macro ()
  261. `(let ((rest list)
  262. (list list))
  263. (loop (unless (consp rest)
  264. (return rest))
  265. (shiftf list rest (cdr rest)))))
  266. (last1-macro ()
  267. `(let ((rest list)
  268. (list list))
  269. (loop (unless (consp rest)
  270. (return list))
  271. (shiftf list rest (cdr rest)))))
  272. (lastn-macro (type)
  273. `(let ((returned-list list)
  274. (checked-list list)
  275. (n (truly-the ,type n)))
  276. (declare (,type n))
  277. (tagbody
  278. :scan
  279. (pop checked-list)
  280. (when (atom checked-list)
  281. (go :done))
  282. (if (zerop (truly-the ,type (decf n)))
  283. (go :pop)
  284. (go :scan))
  285. :pop
  286. (pop returned-list)
  287. (pop checked-list)
  288. (if (atom checked-list)
  289. (go :done)
  290. (go :pop))
  291. :done)
  292. returned-list)))
  293. (defun %last0 (list)
  294. (declare (optimize speed (sb-c:verify-arg-count 0)))
  295. (last0-macro))
  296. (defun %last1 (list)
  297. (declare (optimize speed (sb-c:verify-arg-count 0)))
  298. (last1-macro))
  299. (defun %lastn/fixnum (list n)
  300. (declare (optimize speed (sb-c:verify-arg-count 0))
  301. (type (and unsigned-byte fixnum) n))
  302. (case n
  303. (1 (last1-macro))
  304. (0 (last0-macro))
  305. (t (lastn-macro fixnum))))
  306. (defun %lastn/bignum (list n)
  307. (declare (optimize speed (sb-c:verify-arg-count 0))
  308. (type (and unsigned-byte bignum) n))
  309. (lastn-macro unsigned-byte))
  310. (defun last (list &optional (n 1))
  311. "Return the last N conses (not the last element!) of a list."
  312. (case n
  313. (1 (last1-macro))
  314. (0 (last0-macro))
  315. (t
  316. (typecase n
  317. (fixnum
  318. (lastn-macro fixnum))
  319. (bignum
  320. (lastn-macro unsigned-byte)))))))
  321. (define-compiler-macro last (&whole form list &optional (n 1) &environment env)
  322. (if (sb-xc:constantp n env)
  323. (case (constant-form-value n env)
  324. (0 `(%last0 ,list))
  325. (1 `(%last1 ,list))
  326. (t form))
  327. form))
  328. (defun list (&rest args)
  329. "Return constructs and returns a list of its arguments."
  330. args)
  331. ;;; LIST* is done the same as LIST, except that the last cons is made
  332. ;;; a dotted pair.
  333. (defun list* (arg &rest others)
  334. "Return a list of the arguments with last cons a dotted pair."
  335. (let ((length (length others)))
  336. (cond ((= length 0) arg)
  337. ((= length 1)
  338. (cons arg (fast-&rest-nth 0 others)))
  339. (t
  340. (let* ((cons (list arg))
  341. (result cons)
  342. (index 0)
  343. (1-length (1- length)))
  344. (loop
  345. (cond
  346. ((< index 1-length)
  347. (setf cons
  348. (setf (cdr cons)
  349. (list (fast-&rest-nth index others))))
  350. (incf index))
  351. (t (return nil))))
  352. (setf (cdr cons) (fast-&rest-nth index others))
  353. result)))))
  354. (defun make-list (size &key initial-element)
  355. "Constructs a list with size elements each set to value"
  356. (declare (explicit-check))
  357. (%make-list size initial-element))
  358. ;;; This entry point is to be preferred, irrespective of
  359. ;;; whether or not the backend has vops for %MAKE-LIST.
  360. (defun %make-list (size initial-element)
  361. (declare (type index size))
  362. (do ((count size (1- count))
  363. (result '() (cons initial-element result)))
  364. ((<= count 0) result)
  365. (declare (type index count))))
  366. (defun append (&rest lists)
  367. "Construct a new list by concatenating the list arguments"
  368. (let* ((result (list nil))
  369. (tail result)
  370. (index 0)
  371. (length (length lists))
  372. (last (1- length)))
  373. (declare (truly-dynamic-extent result))
  374. (loop
  375. (cond
  376. ((< (truly-the index index) last)
  377. (let ((list (fast-&rest-nth (truly-the index index) lists)))
  378. (dolist (elt list)
  379. (setf (cdr (truly-the cons tail)) (list elt)
  380. tail (cdr tail))))
  381. (incf index))
  382. (t (return nil))))
  383. (cond
  384. ((zerop length) nil)
  385. ((null (cdr result))
  386. (fast-&rest-nth (truly-the index last) lists))
  387. (t
  388. (setf (cdr (truly-the cons tail))
  389. (fast-&rest-nth (truly-the index last) lists))
  390. (cdr result)))))
  391. (defun append2 (x y)
  392. (declare (optimize (sb-c:verify-arg-count 0)))
  393. (if (null x)
  394. y
  395. (let ((result (list (car x))))
  396. (do ((more (cdr x) (cdr more))
  397. (tail result (cdr tail)))
  398. ((null more)
  399. (rplacd (truly-the cons tail) y)
  400. result)
  401. (rplacd (truly-the cons tail) (list (car more)))))))
  402. ;;;; list copying functions
  403. (defun copy-list (list)
  404. "Return a new list which is EQUAL to LIST. LIST may be improper."
  405. (copy-list-macro list))
  406. (defun copy-alist (alist)
  407. "Return a new association list which is EQUAL to ALIST."
  408. (if (endp alist)
  409. alist
  410. (let ((result
  411. (cons (if (atom (car alist))
  412. (car alist)
  413. (cons (caar alist) (cdar alist)))
  414. nil)))
  415. (do ((x (cdr alist) (cdr x))
  416. (splice result
  417. (cdr (rplacd splice
  418. (cons
  419. (if (atom (car x))
  420. (car x)
  421. (cons (caar x) (cdar x)))
  422. nil)))))
  423. ((endp x)))
  424. result)))
  425. (defun copy-tree (object)
  426. "Recursively copy trees of conses."
  427. (if (consp object)
  428. (let ((result (list (if (consp (car object))
  429. (copy-tree (car object))
  430. (car object)))))
  431. (loop for last-cons = result then new-cons
  432. for cdr = (cdr object) then (cdr cdr)
  433. for car = (if (consp cdr)
  434. (car cdr)
  435. (return (setf (cdr last-cons) cdr)))
  436. for new-cons = (list (if (consp car)
  437. (copy-tree car)
  438. car))
  439. do (setf (cdr last-cons) new-cons))
  440. result)
  441. object))
  442. ;;;; more commonly-used list functions
  443. (defun revappend (x y)
  444. "Return (append (reverse x) y)."
  445. (do ((top x (cdr top))
  446. (result y (cons (car top) result)))
  447. ((endp top) result)))
  448. ;;; NCONC finds the first non-null list, so it can make splice point
  449. ;;; to a cons. After finding the first cons element, it holds it in a
  450. ;;; result variable while running down successive elements tacking
  451. ;;; them together. While tacking lists together, if we encounter a
  452. ;;; null list, we set the previous list's last cdr to nil just in case
  453. ;;; it wasn't already nil, and it could have been dotted while the
  454. ;;; null list was the last argument to NCONC. The manipulation of
  455. ;;; splice (that is starting it out on a first cons, setting LAST of
  456. ;;; splice, and setting splice to ele) inherently handles (nconc x x),
  457. ;;; and it avoids running down the last argument to NCONC which allows
  458. ;;; the last argument to be circular.
  459. (defun nconc (&rest lists)
  460. "Concatenates the lists given as arguments (by changing them)"
  461. (declare (optimize speed))
  462. (flet ((fail (object)
  463. (error 'type-error
  464. :datum object
  465. :expected-type 'list)))
  466. (do-rest-arg ((result index) lists)
  467. (typecase result
  468. (cons
  469. (let ((splice result))
  470. (do-rest-arg ((ele index) lists (1+ index))
  471. (typecase ele
  472. (cons (rplacd (last splice) ele)
  473. (setf splice ele))
  474. (null (rplacd (last splice) nil))
  475. (atom (if (< (1+ index) (length lists))
  476. (fail ele)
  477. (rplacd (last splice) ele)))))
  478. (return result)))
  479. (null)
  480. (atom
  481. (if (< (1+ index) (length lists))
  482. (fail result)
  483. (return result)))))))
  484. (defun nreconc (x y)
  485. "Return (NCONC (NREVERSE X) Y)."
  486. (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
  487. (2nd x 1st) ;2nd follows first down the list.
  488. (3rd y 2nd)) ;3rd follows 2nd down the list.
  489. ((atom 2nd) 3rd)
  490. (rplacd 2nd 3rd)))
  491. (defun butlast (list &optional (n 1))
  492. (declare (optimize speed)
  493. (explicit-check n))
  494. (cond ((not (typep n '(and fixnum unsigned-byte)))
  495. (the unsigned-byte n)
  496. nil)
  497. ((zerop n)
  498. (copy-list list))
  499. (t
  500. (let ((head (dotted-nthcdr (1- n) list)))
  501. (and (consp head) ; there are at least n
  502. (collect ((copy)) ; conses; copy!
  503. (do ((trail list (cdr trail))
  504. (head head (cdr head)))
  505. ;; HEAD is n-1 conses ahead of TRAIL;
  506. ;; when HEAD is at the last cons, return
  507. ;; the data copied so far.
  508. ((atom (cdr head))
  509. (copy))
  510. (copy (car trail)))))))))
  511. (defun nbutlast (list &optional (n 1))
  512. (declare (optimize speed)
  513. (explicit-check n))
  514. (cond ((not (typep n '(and fixnum unsigned-byte)))
  515. (the unsigned-byte n)
  516. nil)
  517. ((zerop n)
  518. list)
  519. (t
  520. (let ((head (dotted-nthcdr (1- n) list)))
  521. (and (consp head) ; there are more than n
  522. (consp (cdr head)) ; conses.
  523. ;; TRAIL trails by n cons to be able to
  524. ;; cut the list at the cons just before.
  525. (do ((trail list (cdr trail))
  526. (head (cdr head) (cdr head)))
  527. ((atom (cdr head))
  528. (setf (cdr trail) nil)
  529. list)))))))
  530. (defun ldiff (list object)
  531. "Return a new list, whose elements are those of LIST that appear before
  532. OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
  533. LIST must be a proper list or a dotted list."
  534. (do* ((list list (cdr list))
  535. (result (list ()))
  536. (splice result))
  537. ((atom list)
  538. (if (eql list object)
  539. (cdr result)
  540. (progn (rplacd splice list) (cdr result))))
  541. (if (eql list object)
  542. (return (cdr result))
  543. (setq splice (cdr (rplacd splice (list (car list))))))))
  544. ;;;; functions to alter list structure
  545. (defun rplaca (cons x)
  546. "Change the CAR of CONS to X and return the CONS."
  547. (rplaca cons x))
  548. (defun rplacd (cons x)
  549. "Change the CDR of CONS to X and return the CONS."
  550. (rplacd cons x))
  551. ;;; Set the Nth element of LIST to NEWVAL.
  552. (defun %setnth (n list newval)
  553. (typecase n
  554. (index
  555. (do ((count n (1- count))
  556. (list list (cdr list)))
  557. ((endp list)
  558. (error "~S is too large an index for SETF of NTH." n))
  559. (declare (type fixnum count))
  560. (when (<= count 0)
  561. (rplaca list newval)
  562. (return newval))))
  563. (t (let ((cons (nthcdr n list)))
  564. (when (endp cons)
  565. (error "~S is too large an index for SETF of NTH." n))
  566. (rplaca cons newval)
  567. newval))))
  568. ;;;; :KEY arg optimization to save funcall of IDENTITY
  569. ;;; APPLY-KEY saves us a function call sometimes.
  570. ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
  571. ;;; because it's used in seq.lisp and sort.lisp.
  572. (defmacro apply-key (key element)
  573. `(if ,key
  574. (funcall ,key ,element)
  575. ,element))
  576. (defmacro apply-key-function (key element)
  577. `(if ,key
  578. (funcall (truly-the function ,key) ,element)
  579. ,element))
  580. ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
  581. ;;; Use these with the following &KEY args:
  582. (defmacro with-set-keys (funcall)
  583. `(if notp
  584. ,(append funcall '(:key key :test-not test-not))
  585. ,(append funcall '(:key key :test test))))
  586. (defmacro satisfies-the-test (item elt)
  587. (let ((key-tmp (gensym)))
  588. `(let ((,key-tmp (apply-key key ,elt)))
  589. (cond (testp (funcall test ,item ,key-tmp))
  590. (notp (not (funcall test-not ,item ,key-tmp)))
  591. (t (funcall test ,item ,key-tmp))))))
  592. ;;;; substitution of expressions
  593. (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
  594. "Substitutes new for subtrees matching old."
  595. (declare (dynamic-extent key test test-not))
  596. (when (and testp notp)
  597. (error ":TEST and :TEST-NOT were both supplied."))
  598. (let ((key (and key (%coerce-callable-to-fun key)))
  599. (test (if testp (%coerce-callable-to-fun test) test))
  600. (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
  601. (declare (type function test test-not))
  602. (labels ((s (subtree)
  603. (cond ((satisfies-the-test old subtree) new)
  604. ((atom subtree) subtree)
  605. (t (let ((car (s (car subtree)))
  606. (cdr (s (cdr subtree))))
  607. (if (and (eq car (car subtree))
  608. (eq cdr (cdr subtree)))
  609. subtree
  610. (cons car cdr)))))))
  611. (s tree))))
  612. (defun subst-if (new test tree &key key)
  613. "Substitutes new for subtrees for which test is true."
  614. (declare (dynamic-extent test key))
  615. (let ((test (%coerce-callable-to-fun test))
  616. (key (and key (%coerce-callable-to-fun key))))
  617. (labels ((s (subtree)
  618. (cond ((funcall test (apply-key key subtree)) new)
  619. ((atom subtree) subtree)
  620. (t (let ((car (s (car subtree)))
  621. (cdr (s (cdr subtree))))
  622. (if (and (eq car (car subtree))
  623. (eq cdr (cdr subtree)))
  624. subtree
  625. (cons car cdr)))))))
  626. (s tree))))
  627. (defun subst-if-not (new test tree &key key)
  628. "Substitutes new for subtrees for which test is false."
  629. (declare (dynamic-extent test key))
  630. (let ((test (%coerce-callable-to-fun test))
  631. (key (and key (%coerce-callable-to-fun key))))
  632. (labels ((s (subtree)
  633. (cond ((not (funcall test (apply-key key subtree))) new)
  634. ((atom subtree) subtree)
  635. (t (let ((car (s (car subtree)))
  636. (cdr (s (cdr subtree))))
  637. (if (and (eq car (car subtree))
  638. (eq cdr (cdr subtree)))
  639. subtree
  640. (cons car cdr)))))))
  641. (s tree))))
  642. (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
  643. "Substitute NEW for subtrees matching OLD."
  644. (declare (dynamic-extent key test test-not))
  645. (when (and testp notp)
  646. (error ":TEST and :TEST-NOT were both supplied."))
  647. (let ((key (and key (%coerce-callable-to-fun key)))
  648. (test (if testp (%coerce-callable-to-fun test) test))
  649. (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
  650. (declare (type function test test-not))
  651. (labels ((s (subtree)
  652. (cond ((satisfies-the-test old subtree) new)
  653. ((atom subtree) subtree)
  654. (t (do* ((last nil subtree)
  655. (subtree subtree (cdr subtree)))
  656. ((atom subtree)
  657. (if (satisfies-the-test old subtree)
  658. (setf (cdr last) new)))
  659. (if (satisfies-the-test old subtree)
  660. (return (setf (cdr last) new))
  661. (setf (car subtree) (s (car subtree)))))
  662. subtree))))
  663. (s tree))))
  664. (defun nsubst-if (new test tree &key key)
  665. "Substitute NEW for subtrees of TREE for which TEST is true."
  666. (declare (dynamic-extent test key))
  667. (let ((test (%coerce-callable-to-fun test))
  668. (key (and key (%coerce-callable-to-fun key))))
  669. (labels ((s (subtree)
  670. (cond ((funcall test (apply-key key subtree)) new)
  671. ((atom subtree) subtree)
  672. (t (do* ((last nil subtree)
  673. (subtree subtree (cdr subtree)))
  674. ((atom subtree)
  675. (if (funcall test (apply-key key subtree))
  676. (setf (cdr last) new)))
  677. (if (funcall test (apply-key key subtree))
  678. (return (setf (cdr last) new))
  679. (setf (car subtree) (s (car subtree)))))
  680. subtree))))
  681. (s tree))))
  682. (defun nsubst-if-not (new test tree &key key)
  683. "Substitute NEW for subtrees of TREE for which TEST is false."
  684. (declare (dynamic-extent test key))
  685. (let ((test (%coerce-callable-to-fun test))
  686. (key (and key (%coerce-callable-to-fun key))))
  687. (labels ((s (subtree)
  688. (cond ((not (funcall test (apply-key key subtree))) new)
  689. ((atom subtree) subtree)
  690. (t (do* ((last nil subtree)
  691. (subtree subtree (cdr subtree)))
  692. ((atom subtree)
  693. (if (not (funcall test (apply-key key subtree)))
  694. (setf (cdr last) new)))
  695. (if (not (funcall test (apply-key key subtree)))
  696. (return (setf (cdr last) new))
  697. (setf (car subtree) (s (car subtree)))))
  698. subtree))))
  699. (s tree))))
  700. (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
  701. "Substitute from ALIST into TREE nondestructively."
  702. (declare (dynamic-extent key test test-not))
  703. (when (and testp notp)
  704. (error ":TEST and :TEST-NOT were both supplied."))
  705. (let ((key (and key (%coerce-callable-to-fun key)))
  706. (test (if testp (%coerce-callable-to-fun test) test))
  707. (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
  708. (declare (type function test test-not))
  709. (declare (inline assoc))
  710. (labels ((s (subtree)
  711. (let* ((key-val (apply-key key subtree))
  712. (assoc (if notp
  713. (assoc key-val alist :test-not test-not)
  714. (assoc key-val alist :test test))))
  715. (cond (assoc (cdr assoc))
  716. ((atom subtree) subtree)
  717. (t (let ((car (s (car subtree)))
  718. (cdr (s (cdr subtree))))
  719. (if (and (eq car (car subtree))
  720. (eq cdr (cdr subtree)))
  721. subtree
  722. (cons car cdr))))))))
  723. (s tree))))
  724. ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
  725. ;;; because it can be referenced in inline expansions.
  726. (defmacro nsublis-macro ()
  727. (let ((key-tmp (gensym)))
  728. `(let ((,key-tmp (apply-key key subtree)))
  729. (if notp
  730. (assoc ,key-tmp alist :test-not test-not)
  731. (assoc ,key-tmp alist :test test)))))
  732. (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
  733. "Substitute from ALIST into TREE destructively."
  734. (declare (dynamic-extent key test test-not))
  735. (when (and testp notp)
  736. (error ":TEST and :TEST-NOT were both supplied."))
  737. (let ((key (and key (%coerce-callable-to-fun key)))
  738. (test (if testp (%coerce-callable-to-fun test) test))
  739. (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
  740. (declare (type function test test-not)
  741. (inline assoc))
  742. (let (temp)
  743. (labels ((s (subtree)
  744. (cond ((setq temp (nsublis-macro))
  745. (cdr temp))
  746. ((atom subtree) subtree)
  747. (t (do* ((last nil subtree)
  748. (subtree subtree (cdr subtree)))
  749. ((atom subtree)
  750. (if (setq temp (nsublis-macro))
  751. (setf (cdr last) (cdr temp))))
  752. (if (setq temp (nsublis-macro))
  753. (return (setf (cdr last) (cdr temp)))
  754. (setf (car subtree) (s (car subtree)))))
  755. subtree))))
  756. (s tree)))))
  757. ;;;; functions for using lists as sets
  758. (defun member (item list &key key (test nil testp) (test-not nil notp))
  759. "Return the tail of LIST beginning with first element satisfying EQLity,
  760. :TEST, or :TEST-NOT with the given ITEM."
  761. (declare (explicit-check))
  762. (declare (dynamic-extent key test test-not))
  763. (when (and testp notp)
  764. (error ":TEST and :TEST-NOT were both supplied."))
  765. (let ((key (and key (%coerce-callable-to-fun key)))
  766. (test (and testp (%coerce-callable-to-fun test)))
  767. (test-not (and notp (%coerce-callable-to-fun test-not))))
  768. (cond (test
  769. (if key
  770. (%member-key-test item list key test)
  771. (%member-test item list test)))
  772. (test-not
  773. (if key
  774. (%member-key-test-not item list key test-not)
  775. (%member-test-not item list test-not)))
  776. (t
  777. (if key
  778. (%member-key item list key)
  779. (%member item list))))))
  780. (defun member-if (test list &key key)
  781. "Return tail of LIST beginning with first element satisfying TEST."
  782. (declare (explicit-check))
  783. (declare (dynamic-extent test key))
  784. (let ((test (%coerce-callable-to-fun test))
  785. (key (and key (%coerce-callable-to-fun key))))
  786. (if key
  787. (%member-if-key test list key)
  788. (%member-if test list))))
  789. (defun member-if-not (test list &key key)
  790. "Return tail of LIST beginning with first element not satisfying TEST."
  791. (declare (explicit-check))
  792. (declare (dynamic-extent test key))
  793. (let ((test (%coerce-callable-to-fun test))
  794. (key (and key (%coerce-callable-to-fun key))))
  795. (if key
  796. (%member-if-not-key test list key)
  797. (%member-if-not test list))))
  798. (defun tailp (object list)
  799. "Return true if OBJECT is the same as some tail of LIST, otherwise
  800. returns false. LIST must be a proper list or a dotted list."
  801. (do ((list list (cdr list)))
  802. ((atom list) (eql list object))
  803. (if (eql object list)
  804. (return t))))
  805. (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
  806. "Add ITEM to LIST unless it is already a member"
  807. (declare (explicit-check))
  808. (declare (dynamic-extent key test test-not))
  809. (when (and testp notp)
  810. (error ":TEST and :TEST-NOT were both supplied."))
  811. (let ((key (and key (%coerce-callable-to-fun key)))
  812. (test (and testp (%coerce-callable-to-fun test)))
  813. (test-not (and notp (%coerce-callable-to-fun test-not))))
  814. (cond (test
  815. (if key
  816. (%adjoin-key-test item list key test)
  817. (%adjoin-test item list test)))
  818. (test-not
  819. (if key
  820. (%adjoin-key-test-not item list key test-not)
  821. (%adjoin-test-not item list test-not)))
  822. (t
  823. (if key
  824. (%adjoin-key item list key)
  825. (%adjoin item list))))))
  826. ;;; For cases where MEMBER is called in a loop this allows to perform
  827. ;;; the dispatch that the MEMBER function does only once.
  828. (defmacro with-member-test ((test-var &optional first-clause) &body body)
  829. `(let* ((key (and key (%coerce-callable-to-fun key)))
  830. (,test-var (cond ,@(and first-clause ; used by LIST-REMOVE-DUPLICATES*
  831. `(,first-clause))
  832. (notp
  833. (if key
  834. (lambda (x list2 key test)
  835. (%member-key-test-not (funcall (truly-the function key) x)
  836. list2 key test))
  837. (lambda (x list2 key test)
  838. (declare (ignore key))
  839. (%member-test-not x list2 test))))
  840. (testp
  841. (if key
  842. (lambda (x list2 key test)
  843. (%member-key-test (funcall (truly-the function key) x)
  844. list2 key test))
  845. (lambda (x list2 key test)
  846. (declare (ignore key))
  847. (%member-test x list2 test))))
  848. (key
  849. (lambda (x list2 key test)
  850. (declare (ignore test))
  851. (%member-key (funcall (truly-the function key) x) list2 key)))
  852. (t
  853. (lambda (x list2 key test)
  854. (declare (ignore key test))
  855. (%member x list2)))))
  856. (test (cond (notp
  857. (%coerce-callable-to-fun test-not))
  858. (testp
  859. (%coerce-callable-to-fun test)))))
  860. ,@body))
  861. (defconstant +list-based-union-limit+ 80)
  862. (defun hash-table-test-p (fun)
  863. (or (eq fun #'eq)
  864. (eq fun #'eql)
  865. (eq fun #'equal)
  866. (eq fun #'equalp)
  867. (eq fun 'eq)
  868. (eq fun 'eql)
  869. (eq fun 'equal)
  870. (eq fun 'equalp)))
  871. (defun union (list1 list2 &key key (test nil testp) (test-not nil notp))
  872. "Return the union of LIST1 and LIST2."
  873. (declare (explicit-check))
  874. (declare (dynamic-extent key test test-not))
  875. (when (and testp notp)
  876. (error ":TEST and :TEST-NOT were both supplied."))
  877. ;; We have two possibilities here: for shortish lists we pick up the
  878. ;; shorter one as the result, and add the other one to it. For long
  879. ;; lists we use a hash-table when possible.
  880. (let ((n1 (length list1))
  881. (n2 (length list2)))
  882. (multiple-value-bind (short long n-short)
  883. (if (< n1 n2)
  884. (values list1 list2 n1)
  885. (values list2 list1 n2))
  886. (if (or (< n-short +list-based-union-limit+)
  887. notp
  888. (and testp
  889. (not (hash-table-test-p test))))
  890. (with-member-test (member-test)
  891. (let ((orig short))
  892. (dolist (elt long)
  893. (unless (funcall member-test elt orig key test)
  894. (push elt short)))
  895. short))
  896. (let ((table (make-hash-table :test (if testp
  897. test
  898. #'eql) :size (+ n1 n2)))
  899. (key (and key (%coerce-callable-to-fun key)))
  900. (union nil))
  901. (dolist (elt long)
  902. (setf (gethash (apply-key key elt) table) elt))
  903. (dolist (elt short)
  904. (setf (gethash (apply-key key elt) table) elt))
  905. (maphash (lambda (k v)
  906. (declare (ignore k))
  907. (push v union))
  908. table)
  909. union)))))
  910. (defun nunion (list1 list2 &key key (test nil testp) (test-not nil notp))
  911. "Destructively return the union of LIST1 and LIST2."
  912. (declare (explicit-check))
  913. (declare (dynamic-extent key test test-not))
  914. (when (and testp notp)
  915. (error ":TEST and :TEST-NOT were both supplied."))
  916. ;; We have two possibilities here: for shortish lists we pick up the
  917. ;; shorter one as the result, and add the other one to it. For long
  918. ;; lists we use a hash-table when possible.
  919. (let ((n1 (length list1))
  920. (n2 (length list2)))
  921. (multiple-value-bind (short long n-short)
  922. (if (< n1 n2)
  923. (values list1 list2 n1)
  924. (values list2 list1 n2))
  925. (if (or (< n-short +list-based-union-limit+)
  926. notp
  927. (and testp
  928. (not (hash-table-test-p test))))
  929. (with-member-test (member-test)
  930. (do ((orig short)
  931. (elt (car long) (car long)))
  932. ((endp long))
  933. (if (funcall member-test elt orig key test)
  934. (pop long)
  935. (shiftf long (cdr long) short long)))
  936. short)
  937. (let ((table (make-hash-table :test (if testp
  938. test
  939. #'eql) :size (+ n1 n2)))
  940. (key (and key (%coerce-callable-to-fun key))))
  941. (dolist (elt long)
  942. (setf (gethash (apply-key key elt) table) elt))
  943. (dolist (elt short)
  944. (setf (gethash (apply-key key elt) table) elt))
  945. (let ((union long)
  946. (head long))
  947. (maphash (lambda (k v)
  948. (declare (ignore k))
  949. (if head
  950. (setf (car head) v
  951. head (cdr head))
  952. (push v union)))
  953. table)
  954. union))))))
  955. (defun intersection (list1 list2
  956. &key key (test nil testp) (test-not nil notp))
  957. "Return the intersection of LIST1 and LIST2."
  958. (declare (explicit-check))
  959. (declare (dynamic-extent key test test-not))
  960. (when (and testp notp)
  961. (error ":TEST and :TEST-NOT were both supplied."))
  962. (when (and list1 list2)
  963. (with-member-test (member-test)
  964. (let ((res nil))
  965. (dolist (elt list1)
  966. (when (funcall member-test elt list2 key test)
  967. (push elt res)))
  968. res))))
  969. (defun nintersection (list1 list2
  970. &key key (test nil testp) (test-not nil notp))
  971. "Destructively return the intersection of LIST1 and LIST2."
  972. (declare (explicit-check))
  973. (declare (dynamic-extent key test test-not))
  974. (when (and testp notp)
  975. (error ":TEST and :TEST-NOT were both supplied."))
  976. (when (and list1 list2)
  977. (with-member-test (member-test)
  978. (let ((res nil)
  979. (list1 list1))
  980. (do () ((endp list1))
  981. (if (funcall member-test (car list1) list2 key test)
  982. (shiftf list1 (cdr list1) res list1)
  983. (setf list1 (cdr list1))))
  984. res))))
  985. (defun set-difference (list1 list2
  986. &key key (test nil testp) (test-not nil notp))
  987. "Return the elements of LIST1 which are not in LIST2."
  988. (declare (explicit-check))
  989. (declare (dynamic-extent key test test-not))
  990. (when (and testp notp)
  991. (error ":TEST and :TEST-NOT were both supplied."))
  992. (if list2
  993. (with-member-test (member-test)
  994. (let ((res nil))
  995. (dolist (elt list1)
  996. (unless (funcall member-test elt list2 key test)
  997. (push elt res)))
  998. res))
  999. list1))
  1000. (defun nset-difference (list1 list2
  1001. &key key (test nil testp) (test-not nil notp))
  1002. "Destructively return the elements of LIST1 which are not in LIST2."
  1003. (declare (explicit-check))
  1004. (declare (dynamic-extent key test test-not))
  1005. (when (and testp notp)
  1006. (error ":TEST and :TEST-NOT were both supplied."))
  1007. (if list2
  1008. (with-member-test (member-test)
  1009. (let ((res nil)
  1010. (list1 list1))
  1011. (do () ((endp list1))
  1012. (if (funcall member-test (car list1) list2 key test)
  1013. (setf list1 (cdr list1))
  1014. (shiftf list1 (cdr list1) res list1)))
  1015. res))
  1016. list1))
  1017. (defun set-exclusive-or (list1 list2
  1018. &key key (test nil testp) (test-not nil notp))
  1019. "Return new list of elements appearing exactly once in LIST1 and LIST2."
  1020. (declare (explicit-check))
  1021. (declare (dynamic-extent key test test-not))
  1022. (when (and testp notp)
  1023. (error ":TEST and :TEST-NOT were both supplied."))
  1024. (let ((result nil))
  1025. (with-member-test (member-test)
  1026. (dolist (elt list1)
  1027. (unless (funcall member-test elt list2 key test)
  1028. (push elt result)))
  1029. (dx-flet ((test (x y) (funcall (truly-the function test) y x)))
  1030. (dolist (elt list2)
  1031. (unless (funcall member-test elt list1 key #'test)
  1032. (push elt result)))))
  1033. result))
  1034. (defun nset-exclusive-or (list1 list2
  1035. &key key (test #'eql testp) (test-not #'eql notp))
  1036. "Destructively return a list with elements which appear but once in LIST1
  1037. and LIST2."
  1038. (declare (explicit-check))
  1039. (declare (dynamic-extent key test test-not))
  1040. (when (and testp notp)
  1041. (error ":TEST and :TEST-NOT were both supplied."))
  1042. (let ((key (and key (%coerce-callable-to-fun key)))
  1043. (test (if testp (%coerce-callable-to-fun test) test))
  1044. (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
  1045. (declare (type function test test-not))
  1046. ;; The outer loop examines LIST1 while the inner loop examines
  1047. ;; LIST2. If an element is found in LIST2 "equal" to the element
  1048. ;; in LIST1, both are spliced out. When the end of LIST1 is
  1049. ;; reached, what is left of LIST2 is tacked onto what is left of
  1050. ;; LIST1. The splicing operation ensures that the correct
  1051. ;; operation is performed depending on whether splice is at the
  1052. ;; top of the list or not.
  1053. (do ((list1 list1)
  1054. (list2 list2)
  1055. (x list1 (cdr x))
  1056. (splicex ())
  1057. (deleted-y ())
  1058. ;; elements of LIST2, which are "equal" to some processed
  1059. ;; earlier elements of LIST1
  1060. )
  1061. ((endp x)
  1062. (if (null splicex)
  1063. (setq list1 list2)
  1064. (rplacd splicex list2))
  1065. list1)
  1066. (let ((key-val-x (apply-key key (car x)))
  1067. (found-duplicate nil))
  1068. ;; Move all elements from LIST2, which are "equal" to (CAR X),
  1069. ;; to DELETED-Y.
  1070. (do* ((y list2 next-y)
  1071. (next-y (cdr y) (cdr y))
  1072. (splicey ()))
  1073. ((endp y))
  1074. (cond ((let ((key-val-y (apply-key key (car y))))
  1075. (if notp
  1076. (not (funcall test-not key-val-x key-val-y))
  1077. (funcall test key-val-x key-val-y)))
  1078. (if (null splicey)
  1079. (setq list2 (cdr y))
  1080. (rplacd splicey (cdr y)))
  1081. (setq deleted-y (rplacd y deleted-y))
  1082. (setq found-duplicate t))
  1083. (t (setq splicey y))))
  1084. (unless found-duplicate
  1085. (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
  1086. (if found-duplicate
  1087. (if (null splicex)
  1088. (setq list1 (cdr x))
  1089. (rplacd splicex (cdr x)))
  1090. (setq splicex x))))))
  1091. (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
  1092. "Return T if every element in LIST1 is also in LIST2."
  1093. (declare (explicit-check))
  1094. (declare (dynamic-extent key test test-not))
  1095. (when (and testp notp)
  1096. (error ":TEST and :TEST-NOT were both supplied."))
  1097. (with-member-test (member-test)
  1098. (dolist (elt list1)
  1099. (unless (funcall member-test elt list2 key test)
  1100. (return-from subsetp nil)))
  1101. t))
  1102. ;;;; functions that operate on association lists
  1103. (defun acons (key datum alist)
  1104. "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
  1105. (cons (cons key datum) alist))
  1106. (defun pairlis (keys data &optional (alist '()))
  1107. "Construct an association list from KEYS and DATA (adding to ALIST)."
  1108. (do ((x keys (cdr x))
  1109. (y data (cdr y)))
  1110. ((and (endp x) (endp y)) alist)
  1111. (if (or (endp x) (endp y))
  1112. (error "The lists of keys and data are of unequal length."))
  1113. (setq alist (acons (car x) (car y) alist))))
  1114. (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
  1115. "Return the cons in ALIST whose car is equal (by a given test or EQL) to
  1116. the ITEM."
  1117. (declare (explicit-check))
  1118. (declare (dynamic-extent key test test-not))
  1119. (when (and testp notp)
  1120. (error ":TEST and :TEST-NOT were both supplied."))
  1121. (let ((key (and key (%coerce-callable-to-fun key)))
  1122. (test (and testp (%coerce-callable-to-fun test)))
  1123. (test-not (and notp (%coerce-callable-to-fun test-not))))
  1124. (cond (test
  1125. (if key
  1126. (%assoc-key-test item alist key test)
  1127. (%assoc-test item alist test)))
  1128. (test-not
  1129. (if key
  1130. (%assoc-key-test-not item alist key test-not)
  1131. (%assoc-test-not item alist test-not)))
  1132. (t
  1133. (if key
  1134. (%assoc-key item alist key)
  1135. (%assoc item alist))))))
  1136. (defun assoc-if (predicate alist &key key)
  1137. "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
  1138. KEY is supplied, apply it to the CAR of each cons before testing."
  1139. (declare (explicit-check))
  1140. (declare (dynamic-extent predicate key))
  1141. (let ((predicate (%coerce-callable-to-fun predicate))
  1142. (key (and key (%coerce-callable-to-fun key))))
  1143. (if key
  1144. (%assoc-if-key predicate alist key)
  1145. (%assoc-if predicate alist))))
  1146. (defun assoc-if-not (predicate alist &key key)
  1147. "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
  1148. If KEY is supplied, apply it to the CAR of each cons before testing."
  1149. (declare (explicit-check))
  1150. (declare (dynamic-extent predicate key))
  1151. (let ((predicate (%coerce-callable-to-fun predicate))
  1152. (key (and key (%coerce-callable-to-fun key))))
  1153. (if key
  1154. (%assoc-if-not-key predicate alist key)
  1155. (%assoc-if-not predicate alist))))
  1156. (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
  1157. "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
  1158. the ITEM."
  1159. (declare (explicit-check))
  1160. (declare (dynamic-extent key test test-not))
  1161. (when (and testp notp)
  1162. (error ":TEST and :TEST-NOT were both supplied."))
  1163. (let ((key (and key (%coerce-callable-to-fun key)))
  1164. (test (and testp (%coerce-callable-to-fun test)))
  1165. (test-not (and notp (%coerce-callable-to-fun test-not))))
  1166. (cond (test
  1167. (if key
  1168. (%rassoc-key-test item alist key test)
  1169. (%rassoc-test item alist test)))
  1170. (test-not
  1171. (if key
  1172. (%rassoc-key-test-not item alist key test-not)
  1173. (%rassoc-test-not item alist test-not)))
  1174. (t
  1175. (if key
  1176. (%rassoc-key item alist key)
  1177. (%rassoc item alist))))))
  1178. (defun rassoc-if (predicate alist &key key)
  1179. "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
  1180. is supplied, apply it to the CDR of each cons before testing."
  1181. (declare (explicit-check))
  1182. (declare (dynamic-extent predicate key))
  1183. (let ((predicate (%coerce-callable-to-fun predicate))
  1184. (key (and key (%coerce-callable-to-fun key))))
  1185. (if key
  1186. (%rassoc-if-key predicate alist key)
  1187. (%rassoc-if predicate alist))))
  1188. (defun rassoc-if-not (predicate alist &key key)
  1189. "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
  1190. If KEY is supplied, apply it to the CDR of each cons before testing."
  1191. (declare (explicit-check))
  1192. (declare (dynamic-extent predicate key))
  1193. (let ((predicate (%coerce-callable-to-fun predicate))
  1194. (key (and key (%coerce-callable-to-fun key))))
  1195. (if key
  1196. (%rassoc-if-not-key predicate alist key)
  1197. (%rassoc-if-not predicate alist))))
  1198. ;;;; mapping functions
  1199. ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
  1200. ;;; MAPL, MAPLIST, and MAPCON
  1201. ;;;
  1202. ;;; Map the designated function over the arglists in the appropriate
  1203. ;;; way. It is done when any of the arglists runs out. Until then, it
  1204. ;;; CDRs down the arglists calling the function and accumulating
  1205. ;;; results as desired.
  1206. (defun map1 (fun-designator arglists accumulate take-car)
  1207. (declare (dynamic-extent fun-designator))
  1208. (do* ((fun (%coerce-callable-to-fun fun-designator))
  1209. (non-acc-result (car arglists))
  1210. (ret-list (list nil))
  1211. (temp ret-list)
  1212. (res nil)
  1213. (args (make-list (length arglists))))
  1214. ((dolist (x arglists) (or x (return t)))
  1215. (if accumulate
  1216. (cdr ret-list)
  1217. non-acc-result))
  1218. (do ((l arglists (cdr l))
  1219. (arg args (cdr arg)))
  1220. ((null l))
  1221. (setf (car arg) (if take-car (caar l) (car l)))
  1222. (setf (car l) (cdar l)))
  1223. (setq res (apply fun args))
  1224. (case accumulate
  1225. (:nconc
  1226. (when res
  1227. (setf (cdr temp) res)
  1228. ;; KLUDGE: it is said that MAPCON is equivalent to
  1229. ;; (apply #'nconc (maplist ...)) which means (nconc 1) would
  1230. ;; return 1, but (nconc 1 1) should signal an error.
  1231. ;; The transformed MAP code returns the last result, do that
  1232. ;; here as well for consistency and simplic

Large files files are truncated, but you can click here to view the full file