srfi-1.scm 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  1. ;;; srfi-1.scm --- List Library
  2. ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009, 2010, 2011, 2014, 2020, 2021 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Some parts from the reference implementation, which is
  18. ;;; Copyright (c) 1998, 1999 by Olin Shivers. You may do as you please with
  19. ;;; this code as long as you do not remove this copyright notice or
  20. ;;; hold me liable for its use.
  21. ;;; Author: Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
  22. ;;; Date: 2001-06-06
  23. ;;; Commentary:
  24. ;; This is an implementation of SRFI-1 (List Library).
  25. ;;
  26. ;; All procedures defined in SRFI-1, which are not already defined in
  27. ;; the Guile core library, are exported. The procedures in this
  28. ;; implementation work, but they have not been tuned for speed or
  29. ;; memory usage.
  30. ;;
  31. ;; This module is fully documented in the Guile Reference Manual.
  32. ;;; Code:
  33. (define-module (srfi srfi-1)
  34. :export (
  35. ;;; Constructors
  36. ;; cons <= in the core
  37. ;; list <= in the core
  38. xcons
  39. ;; cons* <= in the core
  40. ;; make-list <= in the core
  41. list-tabulate
  42. list-copy
  43. circular-list
  44. ;; iota <= in the core
  45. ;;; Predicates
  46. proper-list?
  47. circular-list?
  48. dotted-list?
  49. ;; pair? <= in the core
  50. ;; null? <= in the core
  51. null-list?
  52. not-pair?
  53. list=
  54. ;;; Selectors
  55. ;; car <= in the core
  56. ;; cdr <= in the core
  57. ;; caar <= in the core
  58. ;; cadr <= in the core
  59. ;; cdar <= in the core
  60. ;; cddr <= in the core
  61. ;; caaar <= in the core
  62. ;; caadr <= in the core
  63. ;; cadar <= in the core
  64. ;; caddr <= in the core
  65. ;; cdaar <= in the core
  66. ;; cdadr <= in the core
  67. ;; cddar <= in the core
  68. ;; cdddr <= in the core
  69. ;; caaaar <= in the core
  70. ;; caaadr <= in the core
  71. ;; caadar <= in the core
  72. ;; caaddr <= in the core
  73. ;; cadaar <= in the core
  74. ;; cadadr <= in the core
  75. ;; caddar <= in the core
  76. ;; cadddr <= in the core
  77. ;; cdaaar <= in the core
  78. ;; cdaadr <= in the core
  79. ;; cdadar <= in the core
  80. ;; cdaddr <= in the core
  81. ;; cddaar <= in the core
  82. ;; cddadr <= in the core
  83. ;; cdddar <= in the core
  84. ;; cddddr <= in the core
  85. ;; list-ref <= in the core
  86. first
  87. second
  88. third
  89. fourth
  90. fifth
  91. sixth
  92. seventh
  93. eighth
  94. ninth
  95. tenth
  96. car+cdr
  97. take
  98. drop
  99. take-right
  100. drop-right
  101. take!
  102. drop-right!
  103. split-at
  104. split-at!
  105. last
  106. ;; last-pair <= in the core
  107. ;;; Miscelleneous: length, append, concatenate, reverse, zip & count
  108. ;; length <= in the core
  109. length+
  110. ;; append <= in the core
  111. ;; append! <= in the core
  112. concatenate
  113. concatenate!
  114. ;; reverse <= in the core
  115. ;; reverse! <= in the core
  116. append-reverse
  117. append-reverse!
  118. zip
  119. unzip1
  120. unzip2
  121. unzip3
  122. unzip4
  123. unzip5
  124. count
  125. ;;; Fold, unfold & map
  126. fold
  127. fold-right
  128. pair-fold
  129. pair-fold-right
  130. reduce
  131. reduce-right
  132. unfold
  133. unfold-right
  134. ;; map ; Extended.
  135. ;; for-each ; Extended.
  136. append-map
  137. append-map!
  138. map!
  139. ;; map-in-order ; Extended.
  140. pair-for-each
  141. filter-map
  142. ;;; Filtering & partitioning
  143. ;; filter <= in the core
  144. partition
  145. remove
  146. ;; filter! <= in the core
  147. partition!
  148. remove!
  149. ;;; Searching
  150. find
  151. find-tail
  152. take-while
  153. take-while!
  154. drop-while
  155. span
  156. span!
  157. break
  158. break!
  159. any
  160. every
  161. ;; list-index ; Extended.
  162. ;; member ; Extended.
  163. ;; memq <= in the core
  164. ;; memv <= in the core
  165. ;;; Deletion
  166. ;; delete ; Extended.
  167. ;; delete! ; Extended.
  168. delete-duplicates
  169. delete-duplicates!
  170. ;;; Association lists
  171. ;; assoc ; Extended.
  172. ;; assq <= in the core
  173. ;; assv <= in the core
  174. alist-cons
  175. alist-copy
  176. alist-delete
  177. alist-delete!
  178. ;;; Set operations on lists
  179. lset<=
  180. lset=
  181. lset-adjoin
  182. lset-union
  183. lset-intersection
  184. lset-difference
  185. lset-xor
  186. lset-diff+intersection
  187. lset-union!
  188. lset-intersection!
  189. lset-difference!
  190. lset-xor!
  191. lset-diff+intersection!
  192. ;;; Primitive side-effects
  193. ;; set-car! <= in the core
  194. ;; set-cdr! <= in the core
  195. )
  196. :re-export (cons list cons* make-list pair? null?
  197. car cdr caar cadr cdar cddr
  198. caaar caadr cadar caddr cdaar cdadr cddar cdddr
  199. caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr
  200. cdaaar cdaadr cdadar cdaddr cddaar cddadr cdddar cddddr
  201. list-ref last-pair length append append! reverse reverse!
  202. filter filter! memq memv assq assv set-car! set-cdr!
  203. iota)
  204. :replace (map for-each map-in-order list-copy list-index member
  205. delete delete! assoc)
  206. )
  207. (cond-expand-provide (current-module) '(srfi-1))
  208. ;;; Constructors
  209. (define (xcons d a)
  210. "Like `cons', but with interchanged arguments. Useful mostly when passed to
  211. higher-order procedures."
  212. (cons a d))
  213. (define (wrong-type-arg caller arg)
  214. (scm-error 'wrong-type-arg (symbol->string caller)
  215. "Wrong type argument: ~S" (list arg) '()))
  216. (define-syntax-rule (check-arg pred arg caller)
  217. (if (not (pred arg))
  218. (wrong-type-arg 'caller arg)))
  219. (define (out-of-range proc arg)
  220. (scm-error 'out-of-range proc
  221. "Value out of range: ~A" (list arg) (list arg)))
  222. ;; the srfi spec doesn't seem to forbid inexact integers.
  223. (define (non-negative-integer? x) (and (integer? x) (>= x 0)))
  224. (define (list-tabulate n init-proc)
  225. "Return an N-element list, where each list element is produced by applying the
  226. procedure INIT-PROC to the corresponding list index. The order in which
  227. INIT-PROC is applied to the indices is not specified."
  228. (check-arg non-negative-integer? n list-tabulate)
  229. (let lp ((n n) (acc '()))
  230. (if (<= n 0)
  231. acc
  232. (lp (- n 1) (cons (init-proc (- n 1)) acc)))))
  233. (define (list-copy lst)
  234. "Return a copy of the given list @var{lst}.
  235. @var{lst} can be a proper or improper list. And if @var{lst} is not a
  236. pair then it's treated as the final tail of an improper list and simply
  237. returned."
  238. ;; This routine differs from the core list-copy in allowing improper
  239. ;; lists. Maybe the core could allow them too.
  240. (if (not (pair? lst))
  241. lst
  242. (let ((result (cons (car lst) (cdr lst))))
  243. (let lp ((tail result))
  244. (let ((next (cdr tail)))
  245. (if (pair? next)
  246. (begin
  247. (set-cdr! tail (cons (car next) (cdr next)))
  248. (lp next))
  249. result))))))
  250. (define (circular-list elt1 . elts)
  251. (set! elts (cons elt1 elts))
  252. (set-cdr! (last-pair elts) elts)
  253. elts)
  254. ;;; Predicates
  255. (define (proper-list? x)
  256. (list? x))
  257. (define (circular-list? x)
  258. (if (not-pair? x)
  259. #f
  260. (let lp ((hare (cdr x)) (tortoise x))
  261. (if (not-pair? hare)
  262. #f
  263. (let ((hare (cdr hare)))
  264. (if (not-pair? hare)
  265. #f
  266. (if (eq? hare tortoise)
  267. #t
  268. (lp (cdr hare) (cdr tortoise)))))))))
  269. (define (dotted-list? x)
  270. (cond
  271. ((null? x) #f)
  272. ((not-pair? x) #t)
  273. (else
  274. (let lp ((hare (cdr x)) (tortoise x))
  275. (cond
  276. ((null? hare) #f)
  277. ((not-pair? hare) #t)
  278. (else
  279. (let ((hare (cdr hare)))
  280. (cond
  281. ((null? hare) #f)
  282. ((not-pair? hare) #t)
  283. ((eq? hare tortoise) #f)
  284. (else
  285. (lp (cdr hare) (cdr tortoise)))))))))))
  286. (define (null-list? x)
  287. (cond
  288. ((proper-list? x)
  289. (null? x))
  290. ((circular-list? x)
  291. #f)
  292. (else
  293. (error "not a proper list in null-list?"))))
  294. (define (not-pair? x)
  295. "Return #t if X is not a pair, #f otherwise.
  296. This is shorthand notation `(not (pair? X))' and is supposed to be used for
  297. end-of-list checking in contexts where dotted lists are allowed."
  298. (not (pair? x)))
  299. (define (list= elt= . rest)
  300. (define (lists-equal a b)
  301. (let lp ((a a) (b b))
  302. (cond ((null? a)
  303. (null? b))
  304. ((null? b)
  305. #f)
  306. (else
  307. (and (elt= (car a) (car b))
  308. (lp (cdr a) (cdr b)))))))
  309. (check-arg procedure? elt= list=)
  310. (or (null? rest)
  311. (let lp ((lists rest))
  312. (or (null? (cdr lists))
  313. (and (lists-equal (car lists) (cadr lists))
  314. (lp (cdr lists)))))))
  315. ;;; Selectors
  316. (define first car)
  317. (define second cadr)
  318. (define third caddr)
  319. (define fourth cadddr)
  320. (define (fifth x) (car (cddddr x)))
  321. (define (sixth x) (cadr (cddddr x)))
  322. (define (seventh x) (caddr (cddddr x)))
  323. (define (eighth x) (cadddr (cddddr x)))
  324. (define (ninth x) (car (cddddr (cddddr x))))
  325. (define (tenth x) (cadr (cddddr (cddddr x))))
  326. (define (car+cdr x)
  327. "Return two values, the `car' and the `cdr' of PAIR."
  328. (values (car x) (cdr x)))
  329. (define take list-head)
  330. (define drop list-tail)
  331. ;;; TAKE-RIGHT and DROP-RIGHT work by getting two pointers into the list,
  332. ;;; off by K, then chasing down the list until the lead pointer falls off
  333. ;;; the end. Note that they diverge for circular lists.
  334. (define (take-right lis k)
  335. (let lp ((lag lis) (lead (drop lis k)))
  336. (if (pair? lead)
  337. (lp (cdr lag) (cdr lead))
  338. lag)))
  339. (define (drop-right lis k)
  340. (let recur ((lag lis) (lead (drop lis k)))
  341. (if (pair? lead)
  342. (cons (car lag) (recur (cdr lag) (cdr lead)))
  343. '())))
  344. (define (take! lst i)
  345. "Linear-update variant of `take'."
  346. (if (= i 0)
  347. '()
  348. (let ((tail (drop lst (- i 1))))
  349. (set-cdr! tail '())
  350. lst)))
  351. (define (drop-right! lst i)
  352. "Linear-update variant of `drop-right'."
  353. (let ((tail (drop lst i)))
  354. (if (null? tail)
  355. '()
  356. (let loop ((prev lst)
  357. (tail (cdr tail)))
  358. (if (null? tail)
  359. (if (pair? prev)
  360. (begin
  361. (set-cdr! prev '())
  362. lst)
  363. lst)
  364. (loop (cdr prev)
  365. (cdr tail)))))))
  366. (define (split-at lst i)
  367. "Return two values, a list of the elements before index I in LST, and
  368. a list of those after."
  369. (if (< i 0)
  370. (out-of-range 'split-at i)
  371. (let lp ((l lst) (n i) (acc '()))
  372. (if (<= n 0)
  373. (values (reverse! acc) l)
  374. (lp (cdr l) (- n 1) (cons (car l) acc))))))
  375. (define (split-at! lst i)
  376. "Linear-update variant of `split-at'."
  377. (cond ((< i 0)
  378. (out-of-range 'split-at! i))
  379. ((= i 0)
  380. (values '() lst))
  381. (else
  382. (let lp ((l lst) (n (- i 1)))
  383. (if (<= n 0)
  384. (let ((tmp (cdr l)))
  385. (set-cdr! l '())
  386. (values lst tmp))
  387. (lp (cdr l) (- n 1)))))))
  388. (define (last pair)
  389. "Return the last element of the non-empty, finite list PAIR."
  390. (car (last-pair pair)))
  391. ;;; Miscelleneous: length, append, concatenate, reverse, zip & count
  392. (define (length+ lst)
  393. "Return the length of @var{lst}, or @code{#f} if @var{lst} is circular."
  394. (let lp ((tortoise lst)
  395. (hare lst)
  396. (i 0))
  397. (if (not-pair? hare)
  398. (if (null? hare)
  399. i
  400. (scm-error 'wrong-type-arg "length+"
  401. "Argument not a proper or circular list: ~s"
  402. (list lst) (list lst)))
  403. (let ((hare (cdr hare)))
  404. (if (not-pair? hare)
  405. (if (null? hare)
  406. (1+ i)
  407. (scm-error 'wrong-type-arg "length+"
  408. "Argument not a proper or circular list: ~s"
  409. (list lst) (list lst)))
  410. (let ((tortoise (cdr tortoise))
  411. (hare (cdr hare)))
  412. (if (eq? hare tortoise)
  413. #f
  414. (lp tortoise hare (+ i 2)))))))))
  415. (define (concatenate lists)
  416. "Construct a list by appending all lists in @var{lists}.
  417. @code{concatenate} is the same as @code{(apply append @var{lists})}.
  418. It exists because some Scheme implementations have a limit on the number
  419. of arguments a function takes, which the @code{apply} might exceed. In
  420. Guile there is no such limit."
  421. (apply append lists))
  422. (define (concatenate! lists)
  423. "Construct a list by appending all lists in @var{lists}. Those
  424. lists may be modified to produce the result.
  425. @code{concatenate!} is the same as @code{(apply append! @var{lists})}.
  426. It exists because some Scheme implementations have a limit on the number
  427. of arguments a function takes, which the @code{apply} might exceed. In
  428. Guile there is no such limit."
  429. (apply append! lists))
  430. (define (append-reverse rev-head tail)
  431. "Reverse @var{rev-head}, append @var{tail} to it, and return the
  432. result. This is equivalent to @code{(append (reverse @var{rev-head})
  433. @var{tail})}, but its implementation is more efficient.
  434. @example
  435. (append-reverse '(1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)
  436. @end example"
  437. (let lp ((rh rev-head)
  438. (result tail))
  439. (if (pair? rh)
  440. (lp (cdr rh) (cons (car rh) result))
  441. (begin
  442. (unless (null? rh)
  443. (wrong-type-arg 'append-reverse rev-head))
  444. result))))
  445. (define (append-reverse! rev-head tail)
  446. "Reverse @var{rev-head}, append @var{tail} to it, and return the
  447. result. This is equivalent to @code{(append! (reverse! @var{rev-head})
  448. @var{tail})}, but its implementation is more efficient.
  449. @example
  450. (append-reverse! (list 1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)
  451. @end example
  452. @var{rev-head} may be modified in order to produce the result."
  453. (let lp ((rh rev-head)
  454. (result tail))
  455. (if (pair? rh)
  456. (let ((next rh)
  457. (rh (cdr rh)))
  458. (set-cdr! next result)
  459. (lp rh next))
  460. (begin
  461. (unless (null? rh)
  462. (wrong-type-arg 'append-reverse! rev-head))
  463. result))))
  464. (define (zip clist1 . rest)
  465. (let lp ((l (cons clist1 rest)) (acc '()))
  466. (if (any null? l)
  467. (reverse! acc)
  468. (lp (map cdr l) (cons (map car l) acc)))))
  469. (define (unzip1 l)
  470. (map first l))
  471. (define (unzip2 l)
  472. (values (map first l) (map second l)))
  473. (define (unzip3 l)
  474. (values (map first l) (map second l) (map third l)))
  475. (define (unzip4 l)
  476. (values (map first l) (map second l) (map third l) (map fourth l)))
  477. (define (unzip5 l)
  478. (values (map first l) (map second l) (map third l) (map fourth l)
  479. (map fifth l)))
  480. (define count
  481. (case-lambda
  482. ((pred lst)
  483. (let lp ((lst lst) (c 0))
  484. (if (null? lst)
  485. c
  486. (lp (cdr lst) (if (pred (car lst)) (1+ c) c)))))
  487. ((pred l1 l2)
  488. (let lp ((l1 l1) (l2 l2) (c 0))
  489. (if (or (null? l1) (null? l2))
  490. c
  491. (lp (cdr l1) (cdr l2)
  492. (if (pred (car l1) (car l2)) (1+ c) c)))))
  493. ((pred lst . lists)
  494. (let lp ((lst lst) (lists lists) (c 0))
  495. (if (or (null? lst) (any null? lists))
  496. c
  497. (lp (cdr lst)
  498. (map cdr lists)
  499. (if (apply pred (car lst) (map car lists)) (1+ c) c)))))))
  500. ;;; Fold, unfold & map
  501. (define fold
  502. (case-lambda
  503. "Apply PROC to the elements of LIST1 ... LISTN to build a result, and return
  504. that result. See the manual for details."
  505. ((kons knil list1)
  506. (check-arg procedure? kons fold)
  507. (check-arg list? list1 fold)
  508. (let fold1 ((knil knil) (list1 list1))
  509. (if (pair? list1)
  510. (fold1 (kons (car list1) knil) (cdr list1))
  511. knil)))
  512. ((kons knil list1 list2)
  513. (check-arg procedure? kons fold)
  514. (let* ((len1 (length+ list1))
  515. (len2 (length+ list2))
  516. (len (if (and len1 len2)
  517. (min len1 len2)
  518. (or len1 len2))))
  519. (unless len
  520. (scm-error 'wrong-type-arg "fold"
  521. "Args do not contain a proper (finite) list: ~S"
  522. (list (list list1 list2)) #f))
  523. (let fold2 ((knil knil) (list1 list1) (list2 list2) (len len))
  524. (if (zero? len)
  525. knil
  526. (fold2 (kons (car list1) (car list2) knil)
  527. (cdr list1) (cdr list2) (1- len))))))
  528. ((kons knil list1 . rest)
  529. (check-arg procedure? kons fold)
  530. (let foldn ((knil knil) (lists (cons list1 rest)))
  531. (if (any null? lists)
  532. knil
  533. (let ((cars (map car lists))
  534. (cdrs (map cdr lists)))
  535. (foldn (apply kons (append! cars (list knil))) cdrs)))))))
  536. (define (fold-right kons knil clist1 . rest)
  537. (check-arg procedure? kons fold-right)
  538. (if (null? rest)
  539. (let loop ((lst (reverse clist1))
  540. (result knil))
  541. (if (null? lst)
  542. result
  543. (loop (cdr lst)
  544. (kons (car lst) result))))
  545. (let loop ((lists (map reverse (cons clist1 rest)))
  546. (result knil))
  547. (if (any1 null? lists)
  548. result
  549. (loop (map cdr lists)
  550. (apply kons (append! (map car lists) (list result))))))))
  551. (define (pair-fold kons knil clist1 . rest)
  552. (check-arg procedure? kons pair-fold)
  553. (if (null? rest)
  554. (let f ((knil knil) (list1 clist1))
  555. (if (null? list1)
  556. knil
  557. (let ((tail (cdr list1)))
  558. (f (kons list1 knil) tail))))
  559. (let f ((knil knil) (lists (cons clist1 rest)))
  560. (if (any null? lists)
  561. knil
  562. (let ((tails (map cdr lists)))
  563. (f (apply kons (append! lists (list knil))) tails))))))
  564. (define (pair-fold-right kons knil clist1 . rest)
  565. (check-arg procedure? kons pair-fold-right)
  566. (if (null? rest)
  567. (let f ((list1 clist1))
  568. (if (null? list1)
  569. knil
  570. (kons list1 (f (cdr list1)))))
  571. (let f ((lists (cons clist1 rest)))
  572. (if (any null? lists)
  573. knil
  574. (apply kons (append! lists (list (f (map cdr lists)))))))))
  575. (define* (unfold p f g seed #:optional (tail-gen (lambda (x) '())))
  576. (define (reverse+tail lst seed)
  577. (let loop ((lst lst)
  578. (result (tail-gen seed)))
  579. (if (null? lst)
  580. result
  581. (loop (cdr lst)
  582. (cons (car lst) result)))))
  583. (check-arg procedure? p unfold)
  584. (check-arg procedure? f unfold)
  585. (check-arg procedure? g unfold)
  586. (check-arg procedure? tail-gen unfold)
  587. (let loop ((seed seed)
  588. (result '()))
  589. (if (p seed)
  590. (reverse+tail result seed)
  591. (loop (g seed)
  592. (cons (f seed) result)))))
  593. (define* (unfold-right p f g seed #:optional (tail '()))
  594. (check-arg procedure? p unfold-right)
  595. (check-arg procedure? f unfold-right)
  596. (check-arg procedure? g unfold-right)
  597. (let uf ((seed seed) (lis tail))
  598. (if (p seed)
  599. lis
  600. (uf (g seed) (cons (f seed) lis)))))
  601. (define (reduce f ridentity lst)
  602. "`reduce' is a variant of `fold', where the first call to F is on two
  603. elements from LST, rather than one element and a given initial value.
  604. If LST is empty, RIDENTITY is returned. If LST has just one element
  605. then that's the return value."
  606. (check-arg procedure? f reduce)
  607. (if (null? lst)
  608. ridentity
  609. (fold f (car lst) (cdr lst))))
  610. (define (reduce-right f ridentity lst)
  611. "`reduce-right' is a variant of `fold-right', where the first call to
  612. F is on two elements from LST, rather than one element and a given
  613. initial value. If LST is empty, RIDENTITY is returned. If LST
  614. has just one element then that's the return value."
  615. (check-arg procedure? f reduce)
  616. (if (null? lst)
  617. ridentity
  618. (fold-right f (last lst) (drop-right lst 1))))
  619. (define map
  620. (case-lambda
  621. ((f l)
  622. (check-arg procedure? f map)
  623. (check-arg list? l map)
  624. (let map1 ((l l))
  625. (if (pair? l)
  626. (cons (f (car l)) (map1 (cdr l)))
  627. '())))
  628. ((f l1 l2)
  629. (check-arg procedure? f map)
  630. (let* ((len1 (length+ l1))
  631. (len2 (length+ l2))
  632. (len (if (and len1 len2)
  633. (min len1 len2)
  634. (or len1 len2))))
  635. (unless len
  636. (scm-error 'wrong-type-arg "map"
  637. "Args do not contain a proper (finite) list: ~S"
  638. (list (list l1 l2)) #f))
  639. (let map2 ((l1 l1) (l2 l2) (len len))
  640. (if (zero? len)
  641. '()
  642. (cons (f (car l1) (car l2))
  643. (map2 (cdr l1) (cdr l2) (1- len)))))))
  644. ((f l1 . rest)
  645. (check-arg procedure? f map)
  646. (let ((len (fold (lambda (ls len)
  647. (let ((ls-len (length+ ls)))
  648. (if len
  649. (if ls-len (min ls-len len) len)
  650. ls-len)))
  651. (length+ l1)
  652. rest)))
  653. (if (not len)
  654. (scm-error 'wrong-type-arg "map"
  655. "Args do not contain a proper (finite) list: ~S"
  656. (list (cons l1 rest)) #f))
  657. (let mapn ((l1 l1) (rest rest) (len len))
  658. (if (zero? len)
  659. '()
  660. (cons (apply f (car l1) (map car rest))
  661. (mapn (cdr l1) (map cdr rest) (1- len)))))))))
  662. (define map-in-order map)
  663. (define for-each
  664. (case-lambda
  665. ((f l)
  666. (check-arg procedure? f for-each)
  667. (check-arg list? l for-each)
  668. (let for-each1 ((l l))
  669. (unless (null? l)
  670. (f (car l))
  671. (for-each1 (cdr l)))))
  672. ((f l1 l2)
  673. (check-arg procedure? f for-each)
  674. (let* ((len1 (length+ l1))
  675. (len2 (length+ l2))
  676. (len (if (and len1 len2)
  677. (min len1 len2)
  678. (or len1 len2))))
  679. (unless len
  680. (scm-error 'wrong-type-arg "for-each"
  681. "Args do not contain a proper (finite) list: ~S"
  682. (list (list l1 l2)) #f))
  683. (let for-each2 ((l1 l1) (l2 l2) (len len))
  684. (unless (zero? len)
  685. (f (car l1) (car l2))
  686. (for-each2 (cdr l1) (cdr l2) (1- len))))))
  687. ((f l1 . rest)
  688. (check-arg procedure? f for-each)
  689. (let ((len (fold (lambda (ls len)
  690. (let ((ls-len (length+ ls)))
  691. (if len
  692. (if ls-len (min ls-len len) len)
  693. ls-len)))
  694. (length+ l1)
  695. rest)))
  696. (if (not len)
  697. (scm-error 'wrong-type-arg "for-each"
  698. "Args do not contain a proper (finite) list: ~S"
  699. (list (cons l1 rest)) #f))
  700. (let for-eachn ((l1 l1) (rest rest) (len len))
  701. (if (> len 0)
  702. (begin
  703. (apply f (car l1) (map car rest))
  704. (for-eachn (cdr l1) (map cdr rest) (1- len)))))))))
  705. (define (append-map f clist1 . rest)
  706. (concatenate (apply map f clist1 rest)))
  707. (define (append-map! f clist1 . rest)
  708. (concatenate! (apply map f clist1 rest)))
  709. ;; OPTIMIZE-ME: Re-use cons cells of list1
  710. (define map! map)
  711. (define (filter-map proc list1 . rest)
  712. "Apply PROC to the elements of LIST1... and return a list of the
  713. results as per SRFI-1 `map', except that any #f results are omitted from
  714. the list returned."
  715. (check-arg procedure? proc filter-map)
  716. (if (null? rest)
  717. (let lp ((l list1)
  718. (rl '()))
  719. (if (null? l)
  720. (reverse! rl)
  721. (let ((res (proc (car l))))
  722. (if res
  723. (lp (cdr l) (cons res rl))
  724. (lp (cdr l) rl)))))
  725. (let lp ((l (cons list1 rest))
  726. (rl '()))
  727. (if (any1 null? l)
  728. (reverse! rl)
  729. (let ((res (apply proc (map car l))))
  730. (if res
  731. (lp (map cdr l) (cons res rl))
  732. (lp (map cdr l) rl)))))))
  733. (define (pair-for-each f clist1 . rest)
  734. (check-arg procedure? f pair-for-each)
  735. (if (null? rest)
  736. (let lp ((l clist1))
  737. (if (null? l)
  738. (if #f #f)
  739. (begin
  740. (f l)
  741. (lp (cdr l)))))
  742. (let lp ((l (cons clist1 rest)))
  743. (if (any1 null? l)
  744. (if #f #f)
  745. (begin
  746. (apply f l)
  747. (lp (map cdr l)))))))
  748. ;;; Filtering & partitioning
  749. (define (partition pred lst)
  750. "Partition the elements of @var{list} with predicate @var{pred}.
  751. Return two values: the list of elements satisfying @var{pred} and the
  752. list of elements @emph{not} satisfying @var{pred}. The order of the
  753. output lists follows the order of @var{list}. @var{list} is not
  754. mutated. One of the output lists may share memory with @var{list}."
  755. (let ((matches (list #f))
  756. (mismatches (list #f)))
  757. (let lp ((lst lst)
  758. (matches-end matches)
  759. (mismatches-end mismatches))
  760. (if (null? lst)
  761. (values (cdr matches) (cdr mismatches))
  762. (let ((x (car lst)))
  763. (if (pred x)
  764. (begin
  765. (set-cdr! matches-end (list x))
  766. (lp (cdr lst) (cdr matches-end) mismatches-end))
  767. (begin
  768. (set-cdr! mismatches-end (list x))
  769. (lp (cdr lst) matches-end (cdr mismatches-end)))))))))
  770. (define (list-prefix-and-tail lst stop)
  771. (when (eq? lst stop)
  772. (error "Prefix cannot be empty"))
  773. (let ((rl (list (car lst))))
  774. (let lp ((lst (cdr lst)) (tail rl))
  775. (if (eq? lst stop)
  776. (values rl tail)
  777. (let ((new-tail (list (car lst))))
  778. (set-cdr! tail new-tail)
  779. (lp (cdr lst) new-tail))))))
  780. (define (remove pred lst)
  781. "Return a list containing all elements from @var{list} which do not
  782. satisfy the predicate @var{pred}. The elements in the result list have
  783. the same order as in @var{list}. The order in which @var{pred} is
  784. applied to the list elements is not specified, and the result may share
  785. a common tail with @{list}."
  786. ;; Traverse the lst, keeping the tail of it, in which we have yet to
  787. ;; find a duplicate, in last-kept. Share that tail with the result
  788. ;; (possibly the entire original lst). Build the result by
  789. ;; destructively appending unique values to its tail, and henever we
  790. ;; find a duplicate, copy the pending last-kept prefix into the result
  791. ;; and move last-kept forward to the current position in lst.
  792. (if (null? lst)
  793. lst
  794. (let ((result (list #f)))
  795. (let lp ((lst lst)
  796. (last-kept lst)
  797. (tail result))
  798. (if (null? lst)
  799. (begin
  800. (set-cdr! tail last-kept)
  801. (cdr result))
  802. (let ((item (car lst)))
  803. (if (pred item)
  804. (if (eq? last-kept lst)
  805. (lp (cdr lst) (cdr lst) tail)
  806. (call-with-values
  807. (lambda () (list-prefix-and-tail last-kept lst))
  808. (lambda (prefix new-tail)
  809. (set-cdr! tail prefix)
  810. (lp (cdr lst) (cdr lst) new-tail))))
  811. (lp (cdr lst) last-kept tail))))))))
  812. (define (partition! pred lst)
  813. "Partition the elements of @var{list} with predicate @var{pred}.
  814. Return two values: the list of elements satisfying @var{pred} and the
  815. list of elements @emph{not} satisfying @var{pred}. The order of the
  816. output lists follows the order of @var{list}. @var{list} is not
  817. mutated. @var{lst} may be modified to construct the return lists."
  818. (let ((matches (cons #f lst))
  819. (mismatches (list #f)))
  820. (let lp ((matches-next matches)
  821. (mismatches-end mismatches))
  822. (let ((next (cdr matches-next)))
  823. (if (null? next)
  824. (values (cdr matches) (cdr mismatches))
  825. (let ((x (car next)))
  826. (if (pred x)
  827. (lp (cdr matches-next) mismatches-end)
  828. (begin
  829. (set-cdr! matches-next (cdr next))
  830. (set-cdr! mismatches-end (list x))
  831. (lp matches-next (cdr mismatches-end))))))))))
  832. (define (remove! pred lst)
  833. "Return a list containing all elements from @var{list} which do not
  834. satisfy the predicate @var{pred}. The elements in the result list have
  835. the same order as in @var{list}. The order in which @var{pred} is
  836. applied to the list elements is not specified. @var{list} may be
  837. modified to build the return list."
  838. (cond
  839. ((null? lst) lst)
  840. ((pred (car lst)) (remove! pred (cdr lst)))
  841. (else
  842. (let lp ((prev lst))
  843. (let ((next (cdr prev)))
  844. (if (null? next)
  845. lst
  846. (let ((x (car next)))
  847. (if (pred x)
  848. (begin
  849. (set-cdr! prev (cdr next))
  850. (lp prev))
  851. (lp next)))))))))
  852. ;;; Searching
  853. (define (find pred lst)
  854. "Return the first element of @var{lst} that satisfies the predicate
  855. @var{pred}, or return @code{#f} if no such element is found."
  856. (check-arg procedure? pred find)
  857. (let loop ((lst lst))
  858. (and (not (null? lst))
  859. (let ((head (car lst)))
  860. (if (pred head)
  861. head
  862. (loop (cdr lst)))))))
  863. (define (find-tail pred lst)
  864. "Return the first pair of @var{lst} whose @sc{car} satisfies the
  865. predicate @var{pred}, or return @code{#f} if no such element is found."
  866. (check-arg procedure? pred find-tail)
  867. (let loop ((lst lst))
  868. (and (not (null? lst))
  869. (let ((head (car lst)))
  870. (if (pred head)
  871. lst
  872. (loop (cdr lst)))))))
  873. (define (take-while pred ls)
  874. "Return a new list which is the longest initial prefix of LS whose
  875. elements all satisfy the predicate PRED."
  876. (check-arg procedure? pred take-while)
  877. (cond ((null? ls) '())
  878. ((not (pred (car ls))) '())
  879. (else
  880. (let ((result (list (car ls))))
  881. (let lp ((ls (cdr ls)) (p result))
  882. (cond ((null? ls) result)
  883. ((not (pred (car ls))) result)
  884. (else
  885. (set-cdr! p (list (car ls)))
  886. (lp (cdr ls) (cdr p)))))))))
  887. (define (take-while! pred lst)
  888. "Linear-update variant of `take-while'."
  889. (check-arg procedure? pred take-while!)
  890. (let loop ((prev #f)
  891. (rest lst))
  892. (cond ((null? rest)
  893. lst)
  894. ((pred (car rest))
  895. (loop rest (cdr rest)))
  896. (else
  897. (if (pair? prev)
  898. (begin
  899. (set-cdr! prev '())
  900. lst)
  901. '())))))
  902. (define (drop-while pred lst)
  903. "Drop the longest initial prefix of LST whose elements all satisfy the
  904. predicate PRED."
  905. (check-arg procedure? pred drop-while)
  906. (let loop ((lst lst))
  907. (cond ((null? lst)
  908. '())
  909. ((pred (car lst))
  910. (loop (cdr lst)))
  911. (else lst))))
  912. (define (span pred lst)
  913. "Return two values, the longest initial prefix of LST whose elements
  914. all satisfy the predicate PRED, and the remainder of LST."
  915. (check-arg procedure? pred span)
  916. (let lp ((lst lst) (rl '()))
  917. (if (and (not (null? lst))
  918. (pred (car lst)))
  919. (lp (cdr lst) (cons (car lst) rl))
  920. (values (reverse! rl) lst))))
  921. (define (span! pred list)
  922. "Linear-update variant of `span'."
  923. (check-arg procedure? pred span!)
  924. (let loop ((prev #f)
  925. (rest list))
  926. (cond ((null? rest)
  927. (values list '()))
  928. ((pred (car rest))
  929. (loop rest (cdr rest)))
  930. (else
  931. (if (pair? prev)
  932. (begin
  933. (set-cdr! prev '())
  934. (values list rest))
  935. (values '() list))))))
  936. (define (break pred clist)
  937. "Return two values, the longest initial prefix of LST whose elements
  938. all fail the predicate PRED, and the remainder of LST."
  939. (check-arg procedure? pred break)
  940. (let lp ((clist clist) (rl '()))
  941. (if (or (null? clist)
  942. (pred (car clist)))
  943. (values (reverse! rl) clist)
  944. (lp (cdr clist) (cons (car clist) rl)))))
  945. (define (break! pred list)
  946. "Linear-update variant of `break'."
  947. (check-arg procedure? pred break!)
  948. (let loop ((l list)
  949. (prev #f))
  950. (cond ((null? l)
  951. (values list '()))
  952. ((pred (car l))
  953. (if (pair? prev)
  954. (begin
  955. (set-cdr! prev '())
  956. (values list l))
  957. (values '() list)))
  958. (else
  959. (loop (cdr l) l)))))
  960. (define (any pred ls . lists)
  961. (check-arg procedure? pred any)
  962. (if (null? lists)
  963. (any1 pred ls)
  964. (let lp ((lists (cons ls lists)))
  965. (cond ((any1 null? lists)
  966. #f)
  967. ((any1 null? (map cdr lists))
  968. (apply pred (map car lists)))
  969. (else
  970. (or (apply pred (map car lists)) (lp (map cdr lists))))))))
  971. (define (any1 pred ls)
  972. (let lp ((ls ls))
  973. (cond ((null? ls)
  974. #f)
  975. ((null? (cdr ls))
  976. (pred (car ls)))
  977. (else
  978. (or (pred (car ls)) (lp (cdr ls)))))))
  979. (define (every pred ls . lists)
  980. (check-arg procedure? pred every)
  981. (if (null? lists)
  982. (every1 pred ls)
  983. (let lp ((lists (cons ls lists)))
  984. (cond ((any1 null? lists)
  985. #t)
  986. ((any1 null? (map cdr lists))
  987. (apply pred (map car lists)))
  988. (else
  989. (and (apply pred (map car lists)) (lp (map cdr lists))))))))
  990. (define (every1 pred ls)
  991. (let lp ((ls ls))
  992. (cond ((null? ls)
  993. #t)
  994. ((null? (cdr ls))
  995. (pred (car ls)))
  996. (else
  997. (and (pred (car ls)) (lp (cdr ls)))))))
  998. (define (list-index pred clist1 . rest)
  999. "Return the index of the first set of elements, one from each of
  1000. CLIST1 ... CLISTN, that satisfies PRED."
  1001. (check-arg procedure? pred list-index)
  1002. (if (null? rest)
  1003. (let lp ((l clist1) (i 0))
  1004. (if (null? l)
  1005. #f
  1006. (if (pred (car l))
  1007. i
  1008. (lp (cdr l) (+ i 1)))))
  1009. (let lp ((lists (cons clist1 rest)) (i 0))
  1010. (cond ((any1 null? lists)
  1011. #f)
  1012. ((apply pred (map car lists)) i)
  1013. (else
  1014. (lp (map cdr lists) (+ i 1)))))))
  1015. ;;; Deletion
  1016. (define* (delete x lst #:optional (pred equal?))
  1017. "Return a list containing the elements of @var{lst} but with
  1018. those equal to @var{x} deleted. The returned elements will be in the
  1019. same order as they were in @var{lst}.
  1020. Equality is determined by @var{pred}, or @code{equal?} if not given. An
  1021. equality call is made just once for each element, but the order in which
  1022. the calls are made on the elements is unspecified.
  1023. The equality calls are always @code{(pred x elem)}, ie.@: the given
  1024. @var{x} is first. This means for instance elements greater than 5 can
  1025. be deleted with @code{(delete 5 lst <)}.
  1026. @var{lst} is not modified, but the returned list might share a common
  1027. tail with @var{lst}."
  1028. (remove (lambda (elem) (pred x elem)) lst))
  1029. (define (member-before x lst stop =)
  1030. (cond
  1031. ((null? lst) #f)
  1032. ((eq? lst stop) #f)
  1033. ((= (car lst) x) #t)
  1034. (else (member-before x (cdr lst) stop =))))
  1035. (define* (delete! x lst #:optional (pred equal?))
  1036. "Return a list containing the elements of @var{lst} but with
  1037. those equal to @var{x} deleted. The returned elements will be in the
  1038. same order as they were in @var{lst}.
  1039. Equality is determined by @var{pred}, or @code{equal?} if not given. An
  1040. equality call is made just once for each element, but the order in which
  1041. the calls are made on the elements is unspecified.
  1042. The equality calls are always @code{(pred x elem)}, ie.@: the given
  1043. @var{x} is first. This means for instance elements greater than 5 can
  1044. be deleted with @code{(delete 5 lst <)}.
  1045. @var{lst} may be modified to construct the returned list."
  1046. (remove! (lambda (elem) (pred x elem)) lst))
  1047. (define* (delete-duplicates lst #:optional (= equal?))
  1048. "Return a list containing the elements of @var{lst} but without
  1049. duplicates.
  1050. When elements are equal, only the first in @var{lst} is retained. Equal
  1051. elements can be anywhere in @var{lst}, they don't have to be adjacent.
  1052. The returned list will have the retained elements in the same order as
  1053. they were in @var{lst}.
  1054. Equality is determined by @var{pred}, or @code{equal?} if not given.
  1055. Calls @code{(pred x y)} are made with element @var{x} being before
  1056. @var{y} in @var{lst}. A call is made at most once for each combination,
  1057. but the sequence of the calls across the elements is unspecified.
  1058. @var{lst} is not modified, but the return might share a common tail with
  1059. @var{lst}.
  1060. In the worst case, this is an @math{O(N^2)} algorithm because it must
  1061. check each element against all those preceding it. For long lists it is
  1062. more efficient to sort and then compare only adjacent elements."
  1063. ;; Same implementation as remove (see comments there), except that the
  1064. ;; predicate checks for duplicates in both last-seen and the pending
  1065. ;; result.
  1066. (if (null? lst)
  1067. lst
  1068. (let ((result (list #f)))
  1069. (let lp ((lst lst)
  1070. (last-kept lst)
  1071. (tail result))
  1072. (if (null? lst)
  1073. (begin
  1074. (set-cdr! tail last-kept)
  1075. (cdr result))
  1076. (let ((item (car lst)))
  1077. (if (or (member item (cdr result) (lambda (x y) (= y x)))
  1078. (member-before item last-kept lst =))
  1079. (if (eq? last-kept lst)
  1080. (lp (cdr lst) (cdr lst) tail)
  1081. (call-with-values
  1082. (lambda () (list-prefix-and-tail last-kept lst))
  1083. (lambda (prefix new-tail)
  1084. (set-cdr! tail prefix)
  1085. (lp (cdr lst) (cdr lst) new-tail))))
  1086. ;; unique, keep
  1087. (lp (cdr lst) last-kept tail))))))))
  1088. (define* (delete-duplicates! lst #:optional (= equal?))
  1089. "Return a list containing the elements of @var{lst} but without
  1090. duplicates.
  1091. When elements are equal, only the first in @var{lst} is retained. Equal
  1092. elements can be anywhere in @var{lst}, they don't have to be adjacent.
  1093. The returned list will have the retained elements in the same order as
  1094. they were in @var{lst}.
  1095. Equality is determined by @var{=}, or @code{equal?} if not given.
  1096. Calls @code{(= x y)} are made with element @var{x} being before
  1097. @var{y} in @var{lst}. A call is made at most once for each combination,
  1098. but the sequence of the calls across the elements is unspecified.
  1099. @var{lst} is not modified, but the return might share a common tail with
  1100. @var{lst}.
  1101. In the worst case, this is an @math{O(N^2)} algorithm because it must
  1102. check each element against all those preceding it. For long lists it is
  1103. more efficient to sort and then compare only adjacent elements."
  1104. (if (null? lst)
  1105. lst
  1106. (let lp ((tail lst))
  1107. (let ((next (cdr tail)))
  1108. (if (null? next)
  1109. lst
  1110. (if (member-before (car next) lst next =)
  1111. (begin
  1112. (set-cdr! tail (cdr next))
  1113. (lp tail))
  1114. (lp next)))))))
  1115. ;;; Association lists
  1116. (define alist-cons acons)
  1117. (define (alist-copy alist)
  1118. "Return a copy of ALIST, copying both the pairs comprising the list
  1119. and those making the associations."
  1120. (let lp ((a alist)
  1121. (rl '()))
  1122. (if (null? a)
  1123. (reverse! rl)
  1124. (lp (cdr a) (alist-cons (caar a) (cdar a) rl)))))
  1125. (define* (alist-delete key alist #:optional (k= equal?))
  1126. (check-arg procedure? k= alist-delete)
  1127. (let lp ((a alist) (rl '()))
  1128. (if (null? a)
  1129. (reverse! rl)
  1130. (if (k= key (caar a))
  1131. (lp (cdr a) rl)
  1132. (lp (cdr a) (cons (car a) rl))))))
  1133. (define* (alist-delete! key alist #:optional (k= equal?))
  1134. (alist-delete key alist k=)) ; XXX:optimize
  1135. ;;; Delete / assoc / member
  1136. (define* (assoc key alist #:optional (= equal?))
  1137. "Behaves like @code{assq} but uses third argument @var{pred} for key
  1138. comparison. If @var{pred} is not supplied, @code{equal?} is
  1139. used. (Extended from R5RS.)"
  1140. (cond
  1141. ((eq? = eq?) (assq key alist))
  1142. ((eq? = eqv?) (assv key alist))
  1143. (else
  1144. (check-arg procedure? = assoc)
  1145. (let loop ((alist alist))
  1146. (and (pair? alist)
  1147. (let ((item (car alist)))
  1148. (check-arg pair? item assoc)
  1149. (if (= key (car item))
  1150. item
  1151. (loop (cdr alist)))))))))
  1152. (define* (member x ls #:optional (= equal?))
  1153. (cond
  1154. ;; This might be performance-sensitive, so punt on the check here,
  1155. ;; relying on memq/memv to check that = is a procedure.
  1156. ((eq? = eq?) (memq x ls))
  1157. ((eq? = eqv?) (memv x ls))
  1158. (else
  1159. (check-arg procedure? = member)
  1160. (find-tail (lambda (y) (= x y)) ls))))
  1161. ;;; Set operations on lists
  1162. (define (lset<= = . rest)
  1163. (check-arg procedure? = lset<=)
  1164. (if (null? rest)
  1165. #t
  1166. (let lp ((f (car rest)) (r (cdr rest)))
  1167. (or (null? r)
  1168. (and (every (lambda (el) (member el (car r) =)) f)
  1169. (lp (car r) (cdr r)))))))
  1170. (define (lset= = . rest)
  1171. (check-arg procedure? = lset<=)
  1172. (if (null? rest)
  1173. #t
  1174. (let lp ((f (car rest)) (r (cdr rest)))
  1175. (or (null? r)
  1176. (and (every (lambda (el) (member el (car r) =)) f)
  1177. (every (lambda (el) (member el f (lambda (x y) (= y x)))) (car r))
  1178. (lp (car r) (cdr r)))))))
  1179. ;; It's not quite clear if duplicates among the `rest' elements are meant to
  1180. ;; be cast out. The spec says `=' is called as (= lstelem restelem),
  1181. ;; suggesting perhaps not, but the reference implementation shows the "list"
  1182. ;; at each stage as including those elements already added. The latter
  1183. ;; corresponds to what's described for lset-union, so that's what's done.
  1184. ;;
  1185. (define (lset-adjoin = list . rest)
  1186. "Add to LIST any of the elements of REST not already in the list.
  1187. These elements are `cons'ed onto the start of LIST (so the return shares
  1188. a common tail with LIST), but the order they're added is unspecified.
  1189. The given `=' procedure is used for comparing elements, called
  1190. as `(@var{=} listelem elem)', i.e., the second argument is one of the
  1191. given REST parameters."
  1192. ;; If `=' is `eq?' or `eqv?', users won't be able to tell which arg is
  1193. ;; first, so we can pass the raw procedure through to `member',
  1194. ;; allowing `memq' / `memv' to be selected.
  1195. (define pred
  1196. (if (or (eq? = eq?) (eq? = eqv?))
  1197. =
  1198. (begin
  1199. (check-arg procedure? = lset-adjoin)
  1200. (lambda (x y) (= y x)))))
  1201. (let lp ((ans list) (rest rest))
  1202. (if (null? rest)
  1203. ans
  1204. (lp (if (member (car rest) ans pred)
  1205. ans
  1206. (cons (car rest) ans))
  1207. (cdr rest)))))
  1208. (define (lset-union = . rest)
  1209. ;; Likewise, allow memq / memv to be used if possible.
  1210. (define pred
  1211. (if (or (eq? = eq?) (eq? = eqv?))
  1212. =
  1213. (begin
  1214. (check-arg procedure? = lset-union)
  1215. (lambda (x y) (= y x)))))
  1216. (fold (lambda (lis ans) ; Compute ANS + LIS.
  1217. (cond ((null? lis) ans) ; Don't copy any lists
  1218. ((null? ans) lis) ; if we don't have to.
  1219. ((eq? lis ans) ans)
  1220. (else
  1221. (fold (lambda (elt ans)
  1222. (if (member elt ans pred)
  1223. ans
  1224. (cons elt ans)))
  1225. ans lis))))
  1226. '()
  1227. rest))
  1228. (define (lset-intersection = list1 . rest)
  1229. (check-arg procedure? = lset-intersection)
  1230. (let lp ((l list1) (acc '()))
  1231. (if (null? l)
  1232. (reverse! acc)
  1233. (if (every (lambda (ll) (member (car l) ll =)) rest)
  1234. (lp (cdr l) (cons (car l) acc))
  1235. (lp (cdr l) acc)))))
  1236. (define (lset-difference = lset . removals)
  1237. "Return @var{lst} with any elements in the lists in @var{removals}
  1238. removed (ie.@: subtracted). For only one @var{lst} argument, just that
  1239. list is returned.
  1240. The given @var{equal} procedure is used for comparing elements, called
  1241. as @code{(@var{equal} elem1 elemN)}. The first argument is from
  1242. @var{lst} and the second from one of the subsequent lists. But exactly
  1243. which calls are made and in what order is unspecified.
  1244. @example
  1245. (lset-difference eqv? (list 'x 'y)) @result{} (x y)
  1246. (lset-difference eqv? (list 1 2 3) '(3 1)) @result{} (2)
  1247. (lset-difference eqv? (list 1 2 3) '(3) '(2)) @result{} (1)
  1248. @end example
  1249. The result may share a common tail with @var{lset}."
  1250. ;; REVIEW: if we think they're actually going to be sets, i.e. no
  1251. ;; duplicates, then might it be better to just reduce via per-set
  1252. ;; delete -- more transient allocation but maybe a lot less work?
  1253. (check-arg procedure? = lset-difference)
  1254. (cond
  1255. ((null? lset) lset)
  1256. ((null? removals) lset)
  1257. (else (remove (lambda (x) (any (lambda (s) (member x s =)) removals))
  1258. lset))))
  1259. (define (lset-xor = . rest)
  1260. (check-arg procedure? = lset-xor)
  1261. (fold (lambda (lst res)
  1262. (let lp ((l lst) (acc '()))
  1263. (if (null? l)
  1264. (let lp0 ((r res) (acc acc))
  1265. (if (null? r)
  1266. (reverse! acc)
  1267. (if (member (car r) lst =)
  1268. (lp0 (cdr r) acc)
  1269. (lp0 (cdr r) (cons (car r) acc)))))
  1270. (if (member (car l) res =)
  1271. (lp (cdr l) acc)
  1272. (lp (cdr l) (cons (car l) acc))))))
  1273. '()
  1274. rest))
  1275. (define (lset-diff+intersection = list1 . rest)
  1276. (check-arg procedure? = lset-diff+intersection)
  1277. (let lp ((l list1) (accd '()) (acci '()))
  1278. (if (null? l)
  1279. (values (reverse! accd) (reverse! acci))
  1280. (let ((appears (every (lambda (ll) (member (car l) ll =)) rest)))
  1281. (if appears
  1282. (lp (cdr l) accd (cons (car l) acci))
  1283. (lp (cdr l) (cons (car l) accd) acci))))))
  1284. (define (lset-union! = . rest)
  1285. (check-arg procedure? = lset-union!)
  1286. (apply lset-union = rest)) ; XXX:optimize
  1287. (define (lset-intersection! = list1 . rest)
  1288. (check-arg procedure? = lset-intersection!)
  1289. (apply lset-intersection = list1 rest)) ; XXX:optimize
  1290. (define (lset-difference! = lset . removals)
  1291. "Return @var{lst} with any elements in the lists in @var{removals}
  1292. removed (ie.@: subtracted). For only one @var{lst} argument, just that
  1293. list is returned.
  1294. The given @var{equal} procedure is used for comparing elements, called
  1295. as @code{(@var{equal} elem1 elemN)}. The first argument is from
  1296. @var{lst} and the second from one of the subsequent lists. But exactly
  1297. which calls are made and in what order is unspecified.
  1298. @example
  1299. (lset-difference! eqv? (list 'x 'y)) @result{} (x y)
  1300. (lset-difference! eqv? (list 1 2 3) '(3 1)) @result{} (2)
  1301. (lset-difference! eqv? (list 1 2 3) '(3) '(2)) @result{} (1)
  1302. @end example
  1303. @code{lset-difference!} may modify @var{lst} to form its result."
  1304. (check-arg procedure? = lset-intersection!)
  1305. (cond
  1306. ((null? lset) lset)
  1307. ((null? removals) lset)
  1308. (else (remove! (lambda (x) (any (lambda (s) (member x s =)) removals))
  1309. lset))))
  1310. (define (lset-xor! = . rest)
  1311. (check-arg procedure? = lset-xor!)
  1312. (apply lset-xor = rest)) ; XXX:optimize
  1313. (define (lset-diff+intersection! = list1 . rest)
  1314. (check-arg procedure? = lset-diff+intersection!)
  1315. (apply lset-diff+intersection = list1 rest)) ; XXX:optimize
  1316. ;;; srfi-1.scm ends here