srfi-1.scm 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. ;;; SRFI-1 list-processing library -*- Scheme -*-
  2. ;;; Reference implementation
  3. ;;;
  4. ;;; Copyright (c) 1998, 1999 by Olin Shivers. You may do as you please with
  5. ;;; this code as long as you do not remove this copyright notice or
  6. ;;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
  7. ;;; -Olin
  8. ; Changes made for Scheme 48
  9. ;
  10. ; - CHECK-ARG is defined as a macro that does nothing.
  11. ; - replaced the one use of LET-OPTIONAL
  12. ; - added definition of :OPTIONAL
  13. (define-syntax check-arg
  14. (syntax-rules ()
  15. ((check-arg stuff ...) #f)))
  16. (define (:optional maybe-value default)
  17. (cond ((null? maybe-value)
  18. default)
  19. ((null? (cdr maybe-value))
  20. (car maybe-value))
  21. (else
  22. (assertion-violation ':optional
  23. "too many arguments passed to :optional" maybe-value))))
  24. ;;; This is a library of list- and pair-processing functions. I wrote it after
  25. ;;; carefully considering the functions provided by the libraries found in
  26. ;;; R4RS/R5RS Scheme, MIT Scheme, Gambit, RScheme, MzScheme, slib, Common
  27. ;;; Lisp, Bigloo, guile, T, APL and the SML standard basis. It is a pretty
  28. ;;; rich toolkit, providing a superset of the functionality found in any of
  29. ;;; the various Schemes I considered.
  30. ;;; This implementation is intended as a portable reference implementation
  31. ;;; for SRFI-1. See the porting notes below for more information.
  32. ;;; Exported:
  33. ;;; xcons tree-copy make-list list-tabulate cons* list-copy
  34. ;;; proper-list? circular-list? dotted-list? not-pair? null-list? list=
  35. ;;; circular-list length+
  36. ;;; iota
  37. ;;; first second third fourth fifth sixth seventh eighth ninth tenth
  38. ;;; car+cdr
  39. ;;; take drop
  40. ;;; take-right drop-right
  41. ;;; take! drop-right!
  42. ;;; split-at split-at!
  43. ;;; last last-pair
  44. ;;; zip unzip1 unzip2 unzip3 unzip4 unzip5
  45. ;;; count
  46. ;;; append! append-reverse append-reverse! concatenate concatenate!
  47. ;;; unfold fold pair-fold reduce
  48. ;;; unfold-right fold-right pair-fold-right reduce-right
  49. ;;; append-map append-map! map! pair-for-each filter-map map-in-order
  50. ;;; filter partition remove
  51. ;;; filter! partition! remove!
  52. ;;; find find-tail any every list-index
  53. ;;; take-while drop-while take-while!
  54. ;;; span break span! break!
  55. ;;; delete delete!
  56. ;;; alist-cons alist-copy
  57. ;;; delete-duplicates delete-duplicates!
  58. ;;; alist-delete alist-delete!
  59. ;;; reverse!
  60. ;;; lset<= lset= lset-adjoin
  61. ;;; lset-union lset-intersection lset-difference lset-xor lset-diff+intersection
  62. ;;; lset-union! lset-intersection! lset-difference! lset-xor! lset-diff+intersection!
  63. ;;;
  64. ;;; In principle, the following R4RS list- and pair-processing procedures
  65. ;;; are also part of this package's exports, although they are not defined
  66. ;;; in this file:
  67. ;;; Primitives: cons pair? null? car cdr set-car! set-cdr!
  68. ;;; Non-primitives: list length append reverse cadr ... cddddr list-ref
  69. ;;; memq memv assq assv
  70. ;;; (The non-primitives are defined in this file, but commented out.)
  71. ;;;
  72. ;;; These R4RS procedures have extended definitions in SRFI-1 and are defined
  73. ;;; in this file:
  74. ;;; map for-each member assoc
  75. ;;;
  76. ;;; The remaining two R4RS list-processing procedures are not included:
  77. ;;; list-tail (use drop)
  78. ;;; list? (use proper-list?)
  79. ;;; A note on recursion and iteration/reversal:
  80. ;;; Many iterative list-processing algorithms naturally compute the elements
  81. ;;; of the answer list in the wrong order (left-to-right or head-to-tail) from
  82. ;;; the order needed to cons them into the proper answer (right-to-left, or
  83. ;;; tail-then-head). One style or idiom of programming these algorithms, then,
  84. ;;; loops, consing up the elements in reverse order, then destructively
  85. ;;; reverses the list at the end of the loop. I do not do this. The natural
  86. ;;; and efficient way to code these algorithms is recursively. This trades off
  87. ;;; intermediate temporary list structure for intermediate temporary stack
  88. ;;; structure. In a stack-based system, this improves cache locality and
  89. ;;; lightens the load on the GC system. Don't stand on your head to iterate!
  90. ;;; Recurse, where natural. Multiple-value returns make this even more
  91. ;;; convenient, when the recursion/iteration has multiple state values.
  92. ;;; Porting:
  93. ;;; This is carefully tuned code; do not modify casually.
  94. ;;; - It is careful to share storage when possible;
  95. ;;; - Side-effecting code tries not to perform redundant writes.
  96. ;;;
  97. ;;; That said, a port of this library to a specific Scheme system might wish
  98. ;;; to tune this code to exploit particulars of the implementation.
  99. ;;; The single most important compiler-specific optimisation you could make
  100. ;;; to this library would be to add rewrite rules or transforms to:
  101. ;;; - transform applications of n-ary procedures (e.g. LIST=, CONS*, APPEND,
  102. ;;; LSET-UNION) into multiple applications of a primitive two-argument
  103. ;;; variant.
  104. ;;; - transform applications of the mapping functions (MAP, FOR-EACH, FOLD,
  105. ;;; ANY, EVERY) into open-coded loops. The killer here is that these
  106. ;;; functions are n-ary. Handling the general case is quite inefficient,
  107. ;;; requiring many intermediate data structures to be allocated and
  108. ;;; discarded.
  109. ;;; - transform applications of procedures that take optional arguments
  110. ;;; into calls to variants that do not take optional arguments. This
  111. ;;; eliminates unnecessary consing and parsing of the rest parameter.
  112. ;;;
  113. ;;; These transforms would provide BIG speedups. In particular, the n-ary
  114. ;;; mapping functions are particularly slow and cons-intensive, and are good
  115. ;;; candidates for tuning. I have coded fast paths for the single-list cases,
  116. ;;; but what you really want to do is exploit the fact that the compiler
  117. ;;; usually knows how many arguments are being passed to a particular
  118. ;;; application of these functions -- they are usually explicitly called, not
  119. ;;; passed around as higher-order values. If you can arrange to have your
  120. ;;; compiler produce custom code or custom linkages based on the number of
  121. ;;; arguments in the call, you can speed these functions up a *lot*. But this
  122. ;;; kind of compiler technology no longer exists in the Scheme world as far as
  123. ;;; I can see.
  124. ;;;
  125. ;;; Note that this code is, of course, dependent upon standard bindings for
  126. ;;; the R5RS procedures -- i.e., it assumes that the variable CAR is bound
  127. ;;; to the procedure that takes the car of a list. If your Scheme
  128. ;;; implementation allows user code to alter the bindings of these procedures
  129. ;;; in a manner that would be visible to these definitions, then there might
  130. ;;; be trouble. You could consider horrible kludgery along the lines of
  131. ;;; (define fact
  132. ;;; (let ((= =) (- -) (* *))
  133. ;;; (letrec ((real-fact (lambda (n)
  134. ;;; (if (= n 0) 1 (* n (real-fact (- n 1)))))))
  135. ;;; real-fact)))
  136. ;;; Or you could consider shifting to a reasonable Scheme system that, say,
  137. ;;; has a module system protecting code from this kind of lossage.
  138. ;;;
  139. ;;; This code does a fair amount of run-time argument checking. If your
  140. ;;; Scheme system has a sophisticated compiler that can eliminate redundant
  141. ;;; error checks, this is no problem. However, if not, these checks incur
  142. ;;; some performance overhead -- and, in a safe Scheme implementation, they
  143. ;;; are in some sense redundant: if we don't check to see that the PROC
  144. ;;; parameter is a procedure, we'll find out anyway three lines later when
  145. ;;; we try to call the value. It's pretty easy to rip all this argument
  146. ;;; checking code out if it's inappropriate for your implementation -- just
  147. ;;; nuke every call to CHECK-ARG.
  148. ;;;
  149. ;;; On the other hand, if you *do* have a sophisticated compiler that will
  150. ;;; actually perform soft-typing and eliminate redundant checks (Rice's systems
  151. ;;; being the only possible candidate of which I'm aware), leaving these checks
  152. ;;; in can *help*, since their presence can be elided in redundant cases,
  153. ;;; and in cases where they are needed, performing the checks early, at
  154. ;;; procedure entry, can "lift" a check out of a loop.
  155. ;;;
  156. ;;; Finally, I have only checked the properties that can portably be checked
  157. ;;; with R5RS Scheme -- and this is not complete. You may wish to alter
  158. ;;; the CHECK-ARG parameter checks to perform extra, implementation-specific
  159. ;;; checks, such as procedure arity for higher-order values.
  160. ;;;
  161. ;;; The code has only these non-R4RS dependencies:
  162. ;;; A few calls to an ASSERTION-VIOLATION procedure;
  163. ;;; Uses of the R5RS multiple-value procedure VALUES and the m-v binding
  164. ;;; RECEIVE macro (which isn't R5RS, but is a trivial macro).
  165. ;;; Many calls to a parameter-checking procedure check-arg:
  166. ;;; (define (check-arg pred val caller)
  167. ;;; (let lp ((val val))
  168. ;;; (if (pred val) val (lp (assertion-violation 'check-arg "Bad argument" val pred caller)))))
  169. ;;; A few uses of the LET-OPTIONAL and :OPTIONAL macros for parsing
  170. ;;; optional arguments.
  171. ;;;
  172. ;;; Most of these procedures use the NULL-LIST? test to trigger the
  173. ;;; base case in the inner loop or recursion. The NULL-LIST? function
  174. ;;; is defined to be a careful one -- it raises an error if passed a
  175. ;;; non-nil, non-pair value. The spec allows an implementation to use
  176. ;;; a less-careful implementation that simply defines NULL-LIST? to
  177. ;;; be NOT-PAIR?. This would speed up the inner loops of these procedures
  178. ;;; at the expense of having them silently accept dotted lists.
  179. ;;; A note on dotted lists:
  180. ;;; I, personally, take the view that the only consistent view of lists
  181. ;;; in Scheme is the view that *everything* is a list -- values such as
  182. ;;; 3 or "foo" or 'bar are simply empty dotted lists. This is due to the
  183. ;;; fact that Scheme actually has no true list type. It has a pair type,
  184. ;;; and there is an *interpretation* of the trees built using this type
  185. ;;; as lists.
  186. ;;;
  187. ;;; I lobbied to have these list-processing procedures hew to this
  188. ;;; view, and accept any value as a list argument. I was overwhelmingly
  189. ;;; overruled during the SRFI discussion phase. So I am inserting this
  190. ;;; text in the reference lib and the SRFI spec as a sort of "minority
  191. ;;; opinion" dissent.
  192. ;;;
  193. ;;; Many of the procedures in this library can be trivially redefined
  194. ;;; to handle dotted lists, just by changing the NULL-LIST? base-case
  195. ;;; check to NOT-PAIR?, meaning that any non-pair value is taken to be
  196. ;;; an empty list. For most of these procedures, that's all that is
  197. ;;; required.
  198. ;;;
  199. ;;; However, we have to do a little more work for some procedures that
  200. ;;; *produce* lists from other lists. Were we to extend these procedures to
  201. ;;; accept dotted lists, we would have to define how they terminate the lists
  202. ;;; produced as results when passed a dotted list. I designed a coherent set
  203. ;;; of termination rules for these cases; this was posted to the SRFI-1
  204. ;;; discussion list. I additionally wrote an earlier version of this library
  205. ;;; that implemented that spec. It has been discarded during later phases of
  206. ;;; the definition and implementation of this library.
  207. ;;;
  208. ;;; The argument *against* defining these procedures to work on dotted
  209. ;;; lists is that dotted lists are the rare, odd case, and that by
  210. ;;; arranging for the procedures to handle them, we lose error checking
  211. ;;; in the cases where a dotted list is passed by accident -- e.g., when
  212. ;;; the programmer swaps a two arguments to a list-processing function,
  213. ;;; one being a scalar and one being a list. For example,
  214. ;;; (member '(1 3 5 7 9) 7)
  215. ;;; This would quietly return #f if we extended MEMBER to accept dotted
  216. ;;; lists.
  217. ;;;
  218. ;;; The SRFI discussion record contains more discussion on this topic.
  219. ;;; Constructors
  220. ;;;;;;;;;;;;;;;;
  221. ;;; Occasionally useful as a value to be passed to a fold or other
  222. ;;; higher-order procedure.
  223. (define (xcons d a) (cons a d))
  224. ;;;; Recursively copy every cons.
  225. ;(define (tree-copy x)
  226. ; (let recur ((x x))
  227. ; (if (not (pair? x)) x
  228. ; (cons (recur (car x)) (recur (cdr x))))))
  229. ;;; Make a list of length LEN.
  230. (define (make-list len . maybe-elt)
  231. (check-arg (lambda (n) (and (integer? n) (>= n 0))) len make-list)
  232. (let ((elt (cond ((null? maybe-elt) #f) ; Default value
  233. ((null? (cdr maybe-elt)) (car maybe-elt))
  234. (else (apply assertion-violation
  235. 'make-list
  236. "Too many arguments to MAKE-LIST"
  237. len maybe-elt)))))
  238. (do ((i len (- i 1))
  239. (ans '() (cons elt ans)))
  240. ((<= i 0) ans))))
  241. ;(define (list . ans) ans) ; R4RS
  242. ;;; Make a list of length LEN. Elt i is (PROC i) for 0 <= i < LEN.
  243. (define (list-tabulate len proc)
  244. (check-arg (lambda (n) (and (integer? n) (>= n 0))) len list-tabulate)
  245. (check-arg procedure? proc list-tabulate)
  246. (do ((i (- len 1) (- i 1))
  247. (ans '() (cons (proc i) ans)))
  248. ((< i 0) ans)))
  249. ;;; (cons* a1 a2 ... an) = (cons a1 (cons a2 (cons ... an)))
  250. ;;; (cons* a1) = a1 (cons* a1 a2 ...) = (cons a1 (cons* a2 ...))
  251. ;;;
  252. ;;; (cons first (unfold not-pair? car cdr rest values))
  253. (define (cons* first . rest)
  254. (let recur ((x first) (rest rest))
  255. (if (pair? rest)
  256. (cons x (recur (car rest) (cdr rest)))
  257. x)))
  258. ;;; (unfold not-pair? car cdr lis values)
  259. (define (list-copy lis)
  260. (let recur ((lis lis))
  261. (if (pair? lis)
  262. (cons (car lis) (recur (cdr lis)))
  263. lis)))
  264. ;;; IOTA count [start step] (start start+step ... start+(count-1)*step)
  265. (define (iota count . maybe-start+step)
  266. (check-arg integer? count iota)
  267. (if (< count 0) (assertion-violation 'iota "Negative step count" count))
  268. ; (let-optionals maybe-start+step ((start 0) (step 1)) ...)
  269. (receive (start step)
  270. (case (length maybe-start+step)
  271. ((0) (values 0 1))
  272. ((2) (values (car maybe-start+step)
  273. (cadr maybe-start+step)))
  274. (else
  275. (apply assertion-violation 'iota "wrong number of arguments"
  276. count maybe-start+step)))
  277. (check-arg number? start iota)
  278. (check-arg number? step iota)
  279. (let loop ((n 0) (r '()))
  280. (if (= n count)
  281. (reverse r)
  282. (loop (+ 1 n)
  283. (cons (+ start (* n step)) r))))))
  284. ;;; I thought these were lovely, but the public at large did not share my
  285. ;;; enthusiasm...
  286. ;;; :IOTA to (0 ... to-1)
  287. ;;; :IOTA from to (from ... to-1)
  288. ;;; :IOTA from to step (from from+step ...)
  289. ;;; IOTA: to (1 ... to)
  290. ;;; IOTA: from to (from+1 ... to)
  291. ;;; IOTA: from to step (from+step from+2step ...)
  292. ;(define (%parse-iota-args arg1 rest-args proc)
  293. ; (let ((check (lambda (n) (check-arg integer? n proc))))
  294. ; (check arg1)
  295. ; (if (pair? rest-args)
  296. ; (let ((arg2 (check (car rest-args)))
  297. ; (rest (cdr rest-args)))
  298. ; (if (pair? rest)
  299. ; (let ((arg3 (check (car rest)))
  300. ; (rest (cdr rest)))
  301. ; (if (pair? rest) (assertion-violation '%parse-iota-args "Too many parameters" proc arg1 rest-args)
  302. ; (values arg1 arg2 arg3)))
  303. ; (values arg1 arg2 1)))
  304. ; (values 0 arg1 1))))
  305. ;
  306. ;(define (iota: arg1 . rest-args)
  307. ; (receive (from to step) (%parse-iota-args arg1 rest-args iota:)
  308. ; (let* ((numsteps (floor (/ (- to from) step)))
  309. ; (last-val (+ from (* step numsteps))))
  310. ; (if (< numsteps 0) (assertion-violation 'iota: "Negative step count" from to step))
  311. ; (do ((steps-left numsteps (- steps-left 1))
  312. ; (val last-val (- val step))
  313. ; (ans '() (cons val ans)))
  314. ; ((<= steps-left 0) ans)))))
  315. ;
  316. ;
  317. ;(define (:iota arg1 . rest-args)
  318. ; (receive (from to step) (%parse-iota-args arg1 rest-args :iota)
  319. ; (let* ((numsteps (ceiling (/ (- to from) step)))
  320. ; (last-val (+ from (* step (- numsteps 1)))))
  321. ; (if (< numsteps 0) (assertion-violation ':iota "Negative step count" from to step))
  322. ; (do ((steps-left numsteps (- steps-left 1))
  323. ; (val last-val (- val step))
  324. ; (ans '() (cons val ans)))
  325. ; ((<= steps-left 0) ans)))))
  326. (define (circular-list val1 . vals)
  327. (let ((ans (cons val1 vals)))
  328. (set-cdr! (last-pair ans) ans)
  329. ans))
  330. ;;; <proper-list> ::= () ; Empty proper list
  331. ;;; | (cons <x> <proper-list>) ; Proper-list pair
  332. ;;; Note that this definition rules out circular lists -- and this
  333. ;;; function is required to detect this case and return false.
  334. (define (proper-list? x)
  335. (let lp ((x x) (lag x))
  336. (if (pair? x)
  337. (let ((x (cdr x)))
  338. (if (pair? x)
  339. (let ((x (cdr x))
  340. (lag (cdr lag)))
  341. (and (not (eq? x lag)) (lp x lag)))
  342. (null? x)))
  343. (null? x))))
  344. ;;; A dotted list is a finite list (possibly of length 0) terminated
  345. ;;; by a non-nil value. Any non-cons, non-nil value (e.g., "foo" or 5)
  346. ;;; is a dotted list of length 0.
  347. ;;;
  348. ;;; <dotted-list> ::= <non-nil,non-pair> ; Empty dotted list
  349. ;;; | (cons <x> <dotted-list>) ; Proper-list pair
  350. (define (dotted-list? x)
  351. (let lp ((x x) (lag x))
  352. (if (pair? x)
  353. (let ((x (cdr x)))
  354. (if (pair? x)
  355. (let ((x (cdr x))
  356. (lag (cdr lag)))
  357. (and (not (eq? x lag)) (lp x lag)))
  358. (not (null? x))))
  359. (not (null? x)))))
  360. (define (circular-list? x)
  361. (let lp ((x x) (lag x))
  362. (and (pair? x)
  363. (let ((x (cdr x)))
  364. (and (pair? x)
  365. (let ((x (cdr x))
  366. (lag (cdr lag)))
  367. (or (eq? x lag) (lp x lag))))))))
  368. (define (not-pair? x) (not (pair? x))) ; Inline me.
  369. ;;; This is a legal definition which is fast and sloppy:
  370. ;;; (define null-list? not-pair?)
  371. ;;; but we'll provide a more careful one:
  372. (define (null-list? l)
  373. (cond ((pair? l) #f)
  374. ((null? l) #t)
  375. (else (assertion-violation 'null-list? "null-list?: argument out of domain" l))))
  376. (define (list= = . lists)
  377. (or (null? lists) ; special case
  378. (let lp1 ((list-a (car lists)) (others (cdr lists)))
  379. (or (null? others)
  380. (let ((list-b (car others))
  381. (others (cdr others)))
  382. (if (eq? list-a list-b) ; EQ? => LIST=
  383. (lp1 list-b others)
  384. (let lp2 ((list-a list-a) (list-b list-b))
  385. (if (null-list? list-a)
  386. (and (null-list? list-b)
  387. (lp1 list-b others))
  388. (and (not (null-list? list-b))
  389. (= (car list-a) (car list-b))
  390. (lp2 (cdr list-a) (cdr list-b)))))))))))
  391. ;;; R4RS, so commented out.
  392. ;(define (length x) ; LENGTH may diverge or
  393. ; (let lp ((x x) (len 0)) ; raise an error if X is
  394. ; (if (pair? x) ; a circular list. This version
  395. ; (lp (cdr x) (+ len 1)) ; diverges.
  396. ; len)))
  397. (define (length+ x) ; Returns #f if X is circular.
  398. (let lp ((x x) (lag x) (len 0))
  399. (if (pair? x)
  400. (let ((x (cdr x))
  401. (len (+ len 1)))
  402. (if (pair? x)
  403. (let ((x (cdr x))
  404. (lag (cdr lag))
  405. (len (+ len 1)))
  406. (and (not (eq? x lag)) (lp x lag len)))
  407. len))
  408. len)))
  409. (define (zip list1 . more-lists) (apply map list list1 more-lists))
  410. ;;; Selectors
  411. ;;;;;;;;;;;;;
  412. ;;; R4RS non-primitives:
  413. ;(define (caar x) (car (car x)))
  414. ;(define (cadr x) (car (cdr x)))
  415. ;(define (cdar x) (cdr (car x)))
  416. ;(define (cddr x) (cdr (cdr x)))
  417. ;
  418. ;(define (caaar x) (caar (car x)))
  419. ;(define (caadr x) (caar (cdr x)))
  420. ;(define (cadar x) (cadr (car x)))
  421. ;(define (caddr x) (cadr (cdr x)))
  422. ;(define (cdaar x) (cdar (car x)))
  423. ;(define (cdadr x) (cdar (cdr x)))
  424. ;(define (cddar x) (cddr (car x)))
  425. ;(define (cdddr x) (cddr (cdr x)))
  426. ;
  427. ;(define (caaaar x) (caaar (car x)))
  428. ;(define (caaadr x) (caaar (cdr x)))
  429. ;(define (caadar x) (caadr (car x)))
  430. ;(define (caaddr x) (caadr (cdr x)))
  431. ;(define (cadaar x) (cadar (car x)))
  432. ;(define (cadadr x) (cadar (cdr x)))
  433. ;(define (caddar x) (caddr (car x)))
  434. ;(define (cadddr x) (caddr (cdr x)))
  435. ;(define (cdaaar x) (cdaar (car x)))
  436. ;(define (cdaadr x) (cdaar (cdr x)))
  437. ;(define (cdadar x) (cdadr (car x)))
  438. ;(define (cdaddr x) (cdadr (cdr x)))
  439. ;(define (cddaar x) (cddar (car x)))
  440. ;(define (cddadr x) (cddar (cdr x)))
  441. ;(define (cdddar x) (cdddr (car x)))
  442. ;(define (cddddr x) (cdddr (cdr x)))
  443. (define first car)
  444. (define second cadr)
  445. (define third caddr)
  446. (define fourth cadddr)
  447. (define (fifth x) (car (cddddr x)))
  448. (define (sixth x) (cadr (cddddr x)))
  449. (define (seventh x) (caddr (cddddr x)))
  450. (define (eighth x) (cadddr (cddddr x)))
  451. (define (ninth x) (car (cddddr (cddddr x))))
  452. (define (tenth x) (cadr (cddddr (cddddr x))))
  453. (define (car+cdr pair) (values (car pair) (cdr pair)))
  454. ;;; take & drop
  455. (define (take lis k)
  456. (check-arg integer? k take)
  457. (let recur ((lis lis) (k k))
  458. (if (zero? k) '()
  459. (cons (car lis)
  460. (recur (cdr lis) (- k 1))))))
  461. (define (drop lis k)
  462. (check-arg integer? k drop)
  463. (let iter ((lis lis) (k k))
  464. (if (zero? k) lis (iter (cdr lis) (- k 1)))))
  465. (define (take! lis k)
  466. (check-arg integer? k take!)
  467. (if (zero? k) '()
  468. (begin (set-cdr! (drop lis (- k 1)) '())
  469. lis)))
  470. ;;; TAKE-RIGHT and DROP-RIGHT work by getting two pointers into the list,
  471. ;;; off by K, then chasing down the list until the lead pointer falls off
  472. ;;; the end.
  473. (define (take-right lis k)
  474. (check-arg integer? k take-right)
  475. (let lp ((lag lis) (lead (drop lis k)))
  476. (if (pair? lead)
  477. (lp (cdr lag) (cdr lead))
  478. lag)))
  479. (define (drop-right lis k)
  480. (check-arg integer? k drop-right)
  481. (let recur ((lag lis) (lead (drop lis k)))
  482. (if (pair? lead)
  483. (cons (car lag) (recur (cdr lag) (cdr lead)))
  484. '())))
  485. ;;; In this function, LEAD is actually K+1 ahead of LAG. This lets
  486. ;;; us stop LAG one step early, in time to smash its cdr to ().
  487. (define (drop-right! lis k)
  488. (check-arg integer? k drop-right!)
  489. (let ((lead (drop lis k)))
  490. (if (pair? lead)
  491. (let lp ((lag lis) (lead (cdr lead))) ; Standard case
  492. (if (pair? lead)
  493. (lp (cdr lag) (cdr lead))
  494. (begin (set-cdr! lag '())
  495. lis)))
  496. '()))) ; Special case dropping everything -- no cons to side-effect.
  497. ;(define (list-ref lis i) (car (drop lis i))) ; R4RS
  498. ;;; These use the APL convention, whereby negative indices mean
  499. ;;; "from the right." I liked them, but they didn't win over the
  500. ;;; SRFI reviewers.
  501. ;;; K >= 0: Take and drop K elts from the front of the list.
  502. ;;; K <= 0: Take and drop -K elts from the end of the list.
  503. ;(define (take lis k)
  504. ; (check-arg integer? k take)
  505. ; (if (negative? k)
  506. ; (list-tail lis (+ k (length lis)))
  507. ; (let recur ((lis lis) (k k))
  508. ; (if (zero? k) '()
  509. ; (cons (car lis)
  510. ; (recur (cdr lis) (- k 1)))))))
  511. ;
  512. ;(define (drop lis k)
  513. ; (check-arg integer? k drop)
  514. ; (if (negative? k)
  515. ; (let recur ((lis lis) (nelts (+ k (length lis))))
  516. ; (if (zero? nelts) '()
  517. ; (cons (car lis)
  518. ; (recur (cdr lis) (- nelts 1)))))
  519. ; (list-tail lis k)))
  520. ;
  521. ;
  522. ;(define (take! lis k)
  523. ; (check-arg integer? k take!)
  524. ; (cond ((zero? k) '())
  525. ; ((positive? k)
  526. ; (set-cdr! (list-tail lis (- k 1)) '())
  527. ; lis)
  528. ; (else (list-tail lis (+ k (length lis))))))
  529. ;
  530. ;(define (drop! lis k)
  531. ; (check-arg integer? k drop!)
  532. ; (if (negative? k)
  533. ; (let ((nelts (+ k (length lis))))
  534. ; (if (zero? nelts) '()
  535. ; (begin (set-cdr! (list-tail lis (- nelts 1)) '())
  536. ; lis)))
  537. ; (list-tail lis k)))
  538. (define (split-at x k)
  539. (check-arg integer? k split-at)
  540. (let recur ((lis x) (k k))
  541. (if (zero? k) (values '() lis)
  542. (receive (prefix suffix) (recur (cdr lis) (- k 1))
  543. (values (cons (car lis) prefix) suffix)))))
  544. (define (split-at! x k)
  545. (check-arg integer? k split-at!)
  546. (if (zero? k) (values '() x)
  547. (let* ((prev (drop x (- k 1)))
  548. (suffix (cdr prev)))
  549. (set-cdr! prev '())
  550. (values x suffix))))
  551. (define (last lis) (car (last-pair lis)))
  552. (define (last-pair lis)
  553. (check-arg pair? lis last-pair)
  554. (let lp ((lis lis))
  555. (let ((tail (cdr lis)))
  556. (if (pair? tail) (lp tail) lis))))
  557. ;;; Unzippers -- 1 through 5
  558. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  559. (define (unzip1 lis) (map car lis))
  560. (define (unzip2 lis)
  561. (let recur ((lis lis))
  562. (if (null-list? lis) (values lis lis) ; Use NOT-PAIR? to handle
  563. (let ((elt (car lis))) ; dotted lists.
  564. (receive (a b) (recur (cdr lis))
  565. (values (cons (car elt) a)
  566. (cons (cadr elt) b)))))))
  567. (define (unzip3 lis)
  568. (let recur ((lis lis))
  569. (if (null-list? lis) (values lis lis lis)
  570. (let ((elt (car lis)))
  571. (receive (a b c) (recur (cdr lis))
  572. (values (cons (car elt) a)
  573. (cons (cadr elt) b)
  574. (cons (caddr elt) c)))))))
  575. (define (unzip4 lis)
  576. (let recur ((lis lis))
  577. (if (null-list? lis) (values lis lis lis lis)
  578. (let ((elt (car lis)))
  579. (receive (a b c d) (recur (cdr lis))
  580. (values (cons (car elt) a)
  581. (cons (cadr elt) b)
  582. (cons (caddr elt) c)
  583. (cons (cadddr elt) d)))))))
  584. (define (unzip5 lis)
  585. (let recur ((lis lis))
  586. (if (null-list? lis) (values lis lis lis lis lis)
  587. (let ((elt (car lis)))
  588. (receive (a b c d e) (recur (cdr lis))
  589. (values (cons (car elt) a)
  590. (cons (cadr elt) b)
  591. (cons (caddr elt) c)
  592. (cons (cadddr elt) d)
  593. (cons (car (cddddr elt)) e)))))))
  594. ;;; append! append-reverse append-reverse! concatenate concatenate!
  595. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  596. (define (append! . lists)
  597. ;; First, scan through lists looking for a non-empty one.
  598. (let lp ((lists lists) (prev '()))
  599. (if (not (pair? lists)) prev
  600. (let ((first (car lists))
  601. (rest (cdr lists)))
  602. (if (not (pair? first)) (lp rest first)
  603. ;; Now, do the splicing.
  604. (let lp2 ((tail-cons (last-pair first))
  605. (rest rest))
  606. (if (pair? rest)
  607. (let ((next (car rest))
  608. (rest (cdr rest)))
  609. (set-cdr! tail-cons next)
  610. (lp2 (if (pair? next) (last-pair next) tail-cons)
  611. rest))
  612. first)))))))
  613. ;;; APPEND is R4RS.
  614. ;(define (append . lists)
  615. ; (if (pair? lists)
  616. ; (let recur ((list1 (car lists)) (lists (cdr lists)))
  617. ; (if (pair? lists)
  618. ; (let ((tail (recur (car lists) (cdr lists))))
  619. ; (fold-right cons tail list1)) ; Append LIST1 & TAIL.
  620. ; list1))
  621. ; '()))
  622. ;(define (append-reverse rev-head tail) (fold cons tail rev-head))
  623. ;(define (append-reverse! rev-head tail)
  624. ; (pair-fold (lambda (pair tail) (set-cdr! pair tail) pair)
  625. ; tail
  626. ; rev-head))
  627. ;;; Hand-inline the FOLD and PAIR-FOLD ops for speed.
  628. (define (append-reverse rev-head tail)
  629. (let lp ((rev-head rev-head) (tail tail))
  630. (if (null-list? rev-head) tail
  631. (lp (cdr rev-head) (cons (car rev-head) tail)))))
  632. (define (append-reverse! rev-head tail)
  633. (let lp ((rev-head rev-head) (tail tail))
  634. (if (null-list? rev-head) tail
  635. (let ((next-rev (cdr rev-head)))
  636. (set-cdr! rev-head tail)
  637. (lp next-rev rev-head)))))
  638. (define (concatenate lists) (reduce-right append '() lists))
  639. (define (concatenate! lists) (reduce-right append! '() lists))
  640. ;;; Fold/map internal utilities
  641. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  642. ;;; These little internal utilities are used by the general
  643. ;;; fold & mapper funs for the n-ary cases . It'd be nice if they got inlined.
  644. ;;; One the other hand, the n-ary cases are painfully inefficient as it is.
  645. ;;; An aggressive implementation should simply re-write these functions
  646. ;;; for raw efficiency; I have written them for as much clarity, portability,
  647. ;;; and simplicity as can be achieved.
  648. ;;;
  649. ;;; I use the dreaded call/cc to do local aborts. A good compiler could
  650. ;;; handle this with extreme efficiency. An implementation that provides
  651. ;;; a one-shot, non-persistent continuation grabber could help the compiler
  652. ;;; out by using that in place of the call/cc's in these routines.
  653. ;;;
  654. ;;; These functions have funky definitions that are precisely tuned to
  655. ;;; the needs of the fold/map procs -- for example, to minimize the number
  656. ;;; of times the argument lists need to be examined.
  657. ;;; Return (map cdr lists).
  658. ;;; However, if any element of LISTS is empty, just abort and return '().
  659. (define (%cdrs lists)
  660. (call-with-current-continuation
  661. (lambda (abort)
  662. (let recur ((lists lists))
  663. (if (pair? lists)
  664. (let ((lis (car lists)))
  665. (if (null-list? lis) (abort '())
  666. (cons (cdr lis) (recur (cdr lists)))))
  667. '())))))
  668. (define (%cars+ lists last-elt) ; (append! (map car lists) (list last-elt))
  669. (let recur ((lists lists))
  670. (if (pair? lists) (cons (caar lists) (recur (cdr lists))) (list last-elt))))
  671. ;;; LISTS is a (not very long) non-empty list of lists.
  672. ;;; Return two lists: the cars & the cdrs of the lists.
  673. ;;; However, if any of the lists is empty, just abort and return [() ()].
  674. (define (%cars+cdrs lists)
  675. (call-with-current-continuation
  676. (lambda (abort)
  677. (let recur ((lists lists))
  678. (if (pair? lists)
  679. (receive (list other-lists) (car+cdr lists)
  680. (if (null-list? list) (abort '() '()) ; LIST is empty -- bail out
  681. (receive (a d) (car+cdr list)
  682. (receive (cars cdrs) (recur other-lists)
  683. (values (cons a cars) (cons d cdrs))))))
  684. (values '() '()))))))
  685. ;;; Like %CARS+CDRS, but we pass in a final elt tacked onto the end of the
  686. ;;; cars list. What a hack.
  687. (define (%cars+cdrs+ lists cars-final)
  688. (call-with-current-continuation
  689. (lambda (abort)
  690. (let recur ((lists lists))
  691. (if (pair? lists)
  692. (receive (list other-lists) (car+cdr lists)
  693. (if (null-list? list) (abort '() '()) ; LIST is empty -- bail out
  694. (receive (a d) (car+cdr list)
  695. (receive (cars cdrs) (recur other-lists)
  696. (values (cons a cars) (cons d cdrs))))))
  697. (values (list cars-final) '()))))))
  698. ;;; Like %CARS+CDRS, but blow up if any list is empty.
  699. (define (%cars+cdrs/no-test lists)
  700. (let recur ((lists lists))
  701. (if (pair? lists)
  702. (receive (list other-lists) (car+cdr lists)
  703. (receive (a d) (car+cdr list)
  704. (receive (cars cdrs) (recur other-lists)
  705. (values (cons a cars) (cons d cdrs)))))
  706. (values '() '()))))
  707. ;;; count
  708. ;;;;;;;;;
  709. (define (count pred list1 . lists)
  710. (check-arg procedure? pred count)
  711. (if (pair? lists)
  712. ;; N-ary case
  713. (let lp ((list1 list1) (lists lists) (i 0))
  714. (if (null-list? list1) i
  715. (receive (as ds) (%cars+cdrs lists)
  716. (if (null? as) i
  717. (lp (cdr list1) ds
  718. (if (apply pred (car list1) as) (+ i 1) i))))))
  719. ;; Fast path
  720. (let lp ((lis list1) (i 0))
  721. (if (null-list? lis) i
  722. (lp (cdr lis) (if (pred (car lis)) (+ i 1) i))))))
  723. ;;; fold/unfold
  724. ;;;;;;;;;;;;;;;
  725. (define (unfold-right p f g seed . maybe-tail)
  726. (check-arg procedure? p unfold-right)
  727. (check-arg procedure? f unfold-right)
  728. (check-arg procedure? g unfold-right)
  729. (let lp ((seed seed) (ans (:optional maybe-tail '())))
  730. (if (p seed) ans
  731. (lp (g seed)
  732. (cons (f seed) ans)))))
  733. (define (unfold p f g seed . maybe-tail-gen)
  734. (check-arg procedure? p unfold)
  735. (check-arg procedure? f unfold)
  736. (check-arg procedure? g unfold)
  737. (if (pair? maybe-tail-gen)
  738. (let ((tail-gen (car maybe-tail-gen)))
  739. (if (pair? (cdr maybe-tail-gen))
  740. (apply assertion-violation 'unfold
  741. "Too many arguments"
  742. p f g seed maybe-tail-gen)
  743. (let recur ((seed seed))
  744. (if (p seed) (tail-gen seed)
  745. (cons (f seed) (recur (g seed)))))))
  746. (let recur ((seed seed))
  747. (if (p seed) '()
  748. (cons (f seed) (recur (g seed)))))))
  749. (define (fold kons knil lis1 . lists)
  750. (check-arg procedure? kons fold)
  751. (if (pair? lists)
  752. (let lp ((lists (cons lis1 lists)) (ans knil)) ; N-ary case
  753. (receive (cars+ans cdrs) (%cars+cdrs+ lists ans)
  754. (if (null? cars+ans) ans ; Done.
  755. (lp cdrs (apply kons cars+ans)))))
  756. (let lp ((lis lis1) (ans knil)) ; Fast path
  757. (if (null-list? lis) ans
  758. (lp (cdr lis) (kons (car lis) ans))))))
  759. (define (fold-right kons knil lis1 . lists)
  760. (check-arg procedure? kons fold-right)
  761. (if (pair? lists)
  762. (let recur ((lists (cons lis1 lists))) ; N-ary case
  763. (let ((cdrs (%cdrs lists)))
  764. (if (null? cdrs) knil
  765. (apply kons (%cars+ lists (recur cdrs))))))
  766. (let recur ((lis lis1)) ; Fast path
  767. (if (null-list? lis) knil
  768. (let ((head (car lis)))
  769. (kons head (recur (cdr lis))))))))
  770. (define (pair-fold-right f zero lis1 . lists)
  771. (check-arg procedure? f pair-fold-right)
  772. (if (pair? lists)
  773. (let recur ((lists (cons lis1 lists))) ; N-ary case
  774. (let ((cdrs (%cdrs lists)))
  775. (if (null? cdrs) zero
  776. (apply f (append! lists (list (recur cdrs)))))))
  777. (let recur ((lis lis1)) ; Fast path
  778. (if (null-list? lis) zero (f lis (recur (cdr lis)))))))
  779. (define (pair-fold f zero lis1 . lists)
  780. (check-arg procedure? f pair-fold)
  781. (if (pair? lists)
  782. (let lp ((lists (cons lis1 lists)) (ans zero)) ; N-ary case
  783. (let ((tails (%cdrs lists)))
  784. (if (null? tails) ans
  785. (lp tails (apply f (append! lists (list ans)))))))
  786. (let lp ((lis lis1) (ans zero))
  787. (if (null-list? lis) ans
  788. (let ((tail (cdr lis))) ; Grab the cdr now,
  789. (lp tail (f lis ans))))))) ; in case F SET-CDR!s LIS.
  790. ;;; REDUCE and REDUCE-RIGHT only use RIDENTITY in the empty-list case.
  791. ;;; These cannot meaningfully be n-ary.
  792. (define (reduce f ridentity lis)
  793. (check-arg procedure? f reduce)
  794. (if (null-list? lis) ridentity
  795. (fold f (car lis) (cdr lis))))
  796. (define (reduce-right f ridentity lis)
  797. (check-arg procedure? f reduce-right)
  798. (if (null-list? lis) ridentity
  799. (let recur ((head (car lis)) (lis (cdr lis)))
  800. (if (pair? lis)
  801. (f head (recur (car lis) (cdr lis)))
  802. head))))
  803. ;;; Mappers: append-map append-map! pair-for-each map! filter-map map-in-order
  804. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  805. (define (append-map f lis1 . lists)
  806. (really-append-map append-map append f lis1 lists))
  807. (define (append-map! f lis1 . lists)
  808. (really-append-map append-map! append! f lis1 lists))
  809. (define (really-append-map who appender f lis1 lists)
  810. (check-arg procedure? f who)
  811. (if (pair? lists)
  812. (receive (cars cdrs) (%cars+cdrs (cons lis1 lists))
  813. (if (null? cars) '()
  814. (let recur ((cars cars) (cdrs cdrs))
  815. (let ((vals (apply f cars)))
  816. (receive (cars2 cdrs2) (%cars+cdrs cdrs)
  817. (if (null? cars2) vals
  818. (appender vals (recur cars2 cdrs2))))))))
  819. ;; Fast path
  820. (if (null-list? lis1) '()
  821. (let recur ((elt (car lis1)) (rest (cdr lis1)))
  822. (let ((vals (f elt)))
  823. (if (null-list? rest) vals
  824. (appender vals (recur (car rest) (cdr rest)))))))))
  825. (define (pair-for-each proc lis1 . lists)
  826. (check-arg procedure? proc pair-for-each)
  827. (if (pair? lists)
  828. (let lp ((lists (cons lis1 lists)))
  829. (let ((tails (%cdrs lists)))
  830. (if (pair? tails)
  831. (begin (apply proc lists)
  832. (lp tails)))))
  833. ;; Fast path.
  834. (let lp ((lis lis1))
  835. (if (not (null-list? lis))
  836. (let ((tail (cdr lis))) ; Grab the cdr now,
  837. (proc lis) ; in case PROC SET-CDR!s LIS.
  838. (lp tail))))))
  839. ;;; We stop when LIS1 runs out, not when any list runs out.
  840. (define (map! f lis1 . lists)
  841. (check-arg procedure? f map!)
  842. (if (pair? lists)
  843. (let lp ((lis1 lis1) (lists lists))
  844. (if (not (null-list? lis1))
  845. (receive (heads tails) (%cars+cdrs/no-test lists)
  846. (set-car! lis1 (apply f (car lis1) heads))
  847. (lp (cdr lis1) tails))))
  848. ;; Fast path.
  849. (pair-for-each (lambda (pair) (set-car! pair (f (car pair)))) lis1))
  850. lis1)
  851. ;;; Map F across L, and save up all the non-false results.
  852. (define (filter-map f lis1 . lists)
  853. (check-arg procedure? f filter-map)
  854. (if (pair? lists)
  855. (let recur ((lists (cons lis1 lists)))
  856. (receive (cars cdrs) (%cars+cdrs lists)
  857. (if (pair? cars)
  858. (cond ((apply f cars) => (lambda (x) (cons x (recur cdrs))))
  859. (else (recur cdrs))) ; Tail call in this arm.
  860. '())))
  861. ;; Fast path.
  862. (let recur ((lis lis1))
  863. (if (null-list? lis) lis
  864. (let ((tail (recur (cdr lis))))
  865. (cond ((f (car lis)) => (lambda (x) (cons x tail)))
  866. (else tail)))))))
  867. ;;; Map F across lists, guaranteeing to go left-to-right.
  868. ;;; NOTE: Some implementations of R5RS MAP are compliant with this spec;
  869. ;;; in which case this procedure may simply be defined as a synonym for MAP.
  870. (define (map-in-order f lis1 . lists)
  871. (check-arg procedure? f map-in-order)
  872. (if (pair? lists)
  873. (let recur ((lists (cons lis1 lists)))
  874. (receive (cars cdrs) (%cars+cdrs lists)
  875. (if (pair? cars)
  876. (let ((x (apply f cars))) ; Do head first,
  877. (cons x (recur cdrs))) ; then tail.
  878. '())))
  879. ;; Fast path.
  880. (let recur ((lis lis1))
  881. (if (null-list? lis) lis
  882. (let ((tail (cdr lis))
  883. (x (f (car lis)))) ; Do head first,
  884. (cons x (recur tail))))))) ; then tail.
  885. ;;; We extend MAP to handle arguments of unequal length.
  886. (define map map-in-order)
  887. ;;; Apply F across lists, guaranteeing to go left-to-right.
  888. ;;; NOTE: Some implementations of R5RS MAP are compliant with this spec;
  889. ;;; in which case this procedure may simply be defined as a synonym for FOR-EACH.
  890. (define (for-each f lis1 . lists)
  891. (check-arg procedure? f for-each)
  892. (if (pair? lists)
  893. (let recur ((lists (cons lis1 lists)))
  894. (receive (cars cdrs) (%cars+cdrs lists)
  895. (if (pair? cars)
  896. (begin
  897. (apply f cars) ; Do head first,
  898. (recur cdrs))))) ; then tail.
  899. ;; Fast path.
  900. (let recur ((lis lis1))
  901. (if (not (null-list? lis))
  902. (begin
  903. (f (car lis)) ; Do head first,
  904. (recur (cdr lis))))))) ; then tail.
  905. ;;; filter, remove, partition
  906. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  907. ;;; FILTER, REMOVE, PARTITION and their destructive counterparts do not
  908. ;;; disorder the elements of their argument.
  909. ;; This FILTER shares the longest tail of L that has no deleted elements.
  910. ;; If Scheme had multi-continuation calls, they could be made more efficient.
  911. (define (filter pred lis) ; Sleazing with EQ? makes this
  912. (check-arg procedure? pred filter) ; one faster.
  913. (let recur ((lis lis))
  914. (if (null-list? lis) lis ; Use NOT-PAIR? to handle dotted lists.
  915. (let ((head (car lis))
  916. (tail (cdr lis)))
  917. (if (pred head)
  918. (let ((new-tail (recur tail))) ; Replicate the RECUR call so
  919. (if (eq? tail new-tail) lis
  920. (cons head new-tail)))
  921. (recur tail)))))) ; this one can be a tail call.
  922. ;;; Another version that shares longest tail.
  923. ;(define (filter pred lis)
  924. ; (receive (ans no-del?)
  925. ; ;; (recur l) returns L with (pred x) values filtered.
  926. ; ;; It also returns a flag NO-DEL? if the returned value
  927. ; ;; is EQ? to L, i.e. if it didn't have to delete anything.
  928. ; (let recur ((l l))
  929. ; (if (null-list? l) (values l #t)
  930. ; (let ((x (car l))
  931. ; (tl (cdr l)))
  932. ; (if (pred x)
  933. ; (receive (ans no-del?) (recur tl)
  934. ; (if no-del?
  935. ; (values l #t)
  936. ; (values (cons x ans) #f)))
  937. ; (receive (ans no-del?) (recur tl) ; Delete X.
  938. ; (values ans #f))))))
  939. ; ans))
  940. ;(define (filter! pred lis) ; Things are much simpler
  941. ; (let recur ((lis lis)) ; if you are willing to
  942. ; (if (pair? lis) ; push N stack frames & do N
  943. ; (cond ((pred (car lis)) ; SET-CDR! writes, where N is
  944. ; (set-cdr! lis (recur (cdr lis))); the length of the answer.
  945. ; lis)
  946. ; (else (recur (cdr lis))))
  947. ; lis)))
  948. ;;; This implementation of FILTER!
  949. ;;; - doesn't cons, and uses no stack;
  950. ;;; - is careful not to do redundant SET-CDR! writes, as writes to memory are
  951. ;;; usually expensive on modern machines, and can be extremely expensive on
  952. ;;; modern Schemes (e.g., ones that have generational GC's).
  953. ;;; It just zips down contiguous runs of in and out elts in LIS doing the
  954. ;;; minimal number of SET-CDR!s to splice the tail of one run of ins to the
  955. ;;; beginning of the next.
  956. (define (filter! pred lis)
  957. (check-arg procedure? pred filter!)
  958. (let lp ((ans lis))
  959. (cond ((null-list? ans) ans) ; Scan looking for
  960. ((not (pred (car ans))) (lp (cdr ans))) ; first cons of result.
  961. ;; ANS is the eventual answer.
  962. ;; SCAN-IN: (CDR PREV) = LIS and (CAR PREV) satisfies PRED.
  963. ;; Scan over a contiguous segment of the list that
  964. ;; satisfies PRED.
  965. ;; SCAN-OUT: (CAR PREV) satisfies PRED. Scan over a contiguous
  966. ;; segment of the list that *doesn't* satisfy PRED.
  967. ;; When the segment ends, patch in a link from PREV
  968. ;; to the start of the next good segment, and jump to
  969. ;; SCAN-IN.
  970. (else (letrec ((scan-in (lambda (prev lis)
  971. (if (pair? lis)
  972. (if (pred (car lis))
  973. (scan-in lis (cdr lis))
  974. (scan-out prev (cdr lis))))))
  975. (scan-out (lambda (prev lis)
  976. (let lp ((lis lis))
  977. (if (pair? lis)
  978. (if (pred (car lis))
  979. (begin (set-cdr! prev lis)
  980. (scan-in lis (cdr lis)))
  981. (lp (cdr lis)))
  982. (set-cdr! prev lis))))))
  983. (scan-in ans (cdr ans))
  984. ans)))))
  985. ;;; Answers share common tail with LIS where possible;
  986. ;;; the technique is slightly subtle.
  987. (define (partition pred lis)
  988. (check-arg procedure? pred partition)
  989. (let recur ((lis lis))
  990. (if (null-list? lis) (values lis lis) ; Use NOT-PAIR? to handle dotted lists.
  991. (let ((elt (car lis))
  992. (tail (cdr lis)))
  993. (receive (in out) (recur tail)
  994. (if (pred elt)
  995. (values (if (pair? out) (cons elt in) lis) out)
  996. (values in (if (pair? in) (cons elt out) lis))))))))
  997. ;(define (partition! pred lis) ; Things are much simpler
  998. ; (let recur ((lis lis)) ; if you are willing to
  999. ; (if (null-list? lis) (values lis lis) ; push N stack frames & do N
  1000. ; (let ((elt (car lis))) ; SET-CDR! writes, where N is
  1001. ; (receive (in out) (recur (cdr lis)) ; the length of LIS.
  1002. ; (cond ((pred elt)
  1003. ; (set-cdr! lis in)
  1004. ; (values lis out))
  1005. ; (else (set-cdr! lis out)
  1006. ; (values in lis))))))))
  1007. ;;; This implementation of PARTITION!
  1008. ;;; - doesn't cons, and uses no stack;
  1009. ;;; - is careful not to do redundant SET-CDR! writes, as writes to memory are
  1010. ;;; usually expensive on modern machines, and can be extremely expensive on
  1011. ;;; modern Schemes (e.g., ones that have generational GC's).
  1012. ;;; It just zips down contiguous runs of in and out elts in LIS doing the
  1013. ;;; minimal number of SET-CDR!s to splice these runs together into the result
  1014. ;;; lists.
  1015. (define (partition! pred lis)
  1016. (check-arg procedure? pred partition!)
  1017. (if (null-list? lis) (values lis lis)
  1018. ;; This pair of loops zips down contiguous in & out runs of the
  1019. ;; list, splicing the runs together. The invariants are
  1020. ;; SCAN-IN: (cdr in-prev) = LIS.
  1021. ;; SCAN-OUT: (cdr out-prev) = LIS.
  1022. (letrec ((scan-in (lambda (in-prev out-prev lis)
  1023. (let lp ((in-prev in-prev) (lis lis))
  1024. (if (pair? lis)
  1025. (if (pred (car lis))
  1026. (lp lis (cdr lis))
  1027. (begin (set-cdr! out-prev lis)
  1028. (scan-out in-prev lis (cdr lis))))
  1029. (set-cdr! out-prev lis))))) ; Done.
  1030. (scan-out (lambda (in-prev out-prev lis)
  1031. (let lp ((out-prev out-prev) (lis lis))
  1032. (if (pair? lis)
  1033. (if (pred (car lis))
  1034. (begin (set-cdr! in-prev lis)
  1035. (scan-in lis out-prev (cdr lis)))
  1036. (lp lis (cdr lis)))
  1037. (set-cdr! in-prev lis)))))) ; Done.
  1038. ;; Crank up the scan&splice loops.
  1039. (if (pred (car lis))
  1040. ;; LIS begins in-list. Search for out-list's first pair.
  1041. (let lp ((prev-l lis) (l (cdr lis)))
  1042. (cond ((not (pair? l)) (values lis l))
  1043. ((pred (car l)) (lp l (cdr l)))
  1044. (else (scan-out prev-l l (cdr l))
  1045. (values lis l)))) ; Done.
  1046. ;; LIS begins out-list. Search for in-list's first pair.
  1047. (let lp ((prev-l lis) (l (cdr lis)))
  1048. (cond ((not (pair? l)) (values l lis))
  1049. ((pred (car l))
  1050. (scan-in l prev-l (cdr l))
  1051. (values l lis)) ; Done.
  1052. (else (lp l (cdr l)))))))))
  1053. ;;; Inline us, please.
  1054. (define (remove pred l) (filter (lambda (x) (not (pred x))) l))
  1055. (define (remove! pred l) (filter! (lambda (x) (not (pred x))) l))
  1056. ;;; Here's the taxonomy for the DELETE/ASSOC/MEMBER functions.
  1057. ;;; (I don't actually think these are the world's most important
  1058. ;;; functions -- the procedural FILTER/REMOVE/FIND/FIND-TAIL variants
  1059. ;;; are far more general.)
  1060. ;;;
  1061. ;;; Function Action
  1062. ;;; ---------------------------------------------------------------------------
  1063. ;;; remove pred lis Delete by general predicate
  1064. ;;; delete x lis [=] Delete by element comparison
  1065. ;;;
  1066. ;;; find pred lis Search by general predicate
  1067. ;;; find-tail pred lis Search by general predicate
  1068. ;;; member x lis [=] Search by element comparison
  1069. ;;;
  1070. ;;; assoc key lis [=] Search alist by key comparison
  1071. ;;; alist-delete key alist [=] Alist-delete by key comparison
  1072. (define (delete x lis . maybe-=)
  1073. (let ((= (:optional maybe-= equal?)))
  1074. (filter (lambda (y) (not (= x y))) lis)))
  1075. (define (delete! x lis . maybe-=)
  1076. (let ((= (:optional maybe-= equal?)))
  1077. (filter! (lambda (y) (not (= x y))) lis)))
  1078. ;;; Extended from R4RS to take an optional comparison argument.
  1079. (define (member x lis . maybe-=)
  1080. (let ((= (:optional maybe-= equal?)))
  1081. (find-tail (lambda (y) (= x y)) lis)))
  1082. ;;; R4RS, hence we don't bother to define.
  1083. ;;; The MEMBER and then FIND-TAIL call should definitely
  1084. ;;; be inlined for MEMQ & MEMV.
  1085. ;(define (memq x lis) (member x lis eq?))
  1086. ;(define (memv x lis) (member x lis eqv?))
  1087. ;;; right-duplicate deletion
  1088. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1089. ;;; delete-duplicates delete-duplicates!
  1090. ;;;
  1091. ;;; Beware -- these are N^2 algorithms. To efficiently remove duplicates
  1092. ;;; in long lists, sort the list to bring duplicates together, then use a
  1093. ;;; linear-time algorithm to kill the dups. Or use an algorithm based on
  1094. ;;; element-marking. The former gives you O(n lg n), the latter is linear.
  1095. (define (delete-duplicates lis . maybe-=)
  1096. (let ((elt= (:optional maybe-= equal?)))
  1097. (check-arg procedure? elt= delete-duplicates)
  1098. (let recur ((lis lis))
  1099. (if (null-list? lis) lis
  1100. (let* ((x (car lis))
  1101. (tail (cdr lis))
  1102. (new-tail (recur (delete x tail elt=))))
  1103. (if (eq? tail new-tail) lis (cons x new-tail)))))))
  1104. (define (delete-duplicates! lis . maybe-=)
  1105. (let ((elt= (:optional maybe-= equal?)))
  1106. (check-arg procedure? elt= delete-duplicates!)
  1107. (let recur ((lis lis))
  1108. (if (null-list? lis) lis
  1109. (let* ((x (car lis))
  1110. (tail (cdr lis))
  1111. (new-tail (recur (delete! x tail elt=))))
  1112. (if (eq? tail new-tail) lis (cons x new-tail)))))))
  1113. ;;; alist stuff
  1114. ;;;;;;;;;;;;;;;
  1115. ;;; Extended from R4RS to take an optional comparison argument.
  1116. (define (assoc x lis . maybe-=)
  1117. (let ((= (:optional maybe-= equal?)))
  1118. (find (lambda (entry) (= x (car entry))) lis)))
  1119. (define (alist-cons key datum alist) (cons (cons key datum) alist))
  1120. (define (alist-copy alist)
  1121. (map (lambda (elt) (cons (car elt) (cdr elt)))
  1122. alist))
  1123. (define (alist-delete key alist . maybe-=)
  1124. (let ((= (:optional maybe-= equal?)))
  1125. (filter (lambda (elt) (not (= key (car elt)))) alist)))
  1126. (define (alist-delete! key alist . maybe-=)
  1127. (let ((= (:optional maybe-= equal?)))
  1128. (filter! (lambda (elt) (not (= key (car elt)))) alist)))
  1129. ;;; find find-tail take-while drop-while span break any every list-index
  1130. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1131. (define (find pred list)
  1132. (cond ((find-tail pred list) => car)
  1133. (else #f)))
  1134. (define (find-tail pred list)
  1135. (check-arg procedure? pred find-tail)
  1136. (let lp ((list list))
  1137. (and (not (null-list? list))
  1138. (if (pred (car list)) list
  1139. (lp (cdr list))))))
  1140. (define (take-while pred lis)
  1141. (check-arg procedure? pred take-while)
  1142. (let recur ((lis lis))
  1143. (if (null-list? lis) '()
  1144. (let ((x (car lis)))
  1145. (if (pred x)
  1146. (cons x (recur (cdr lis)))
  1147. '())))))
  1148. (define (drop-while pred lis)
  1149. (check-arg procedure? pred drop-while)
  1150. (let lp ((lis lis))
  1151. (if (null-list? lis) '()
  1152. (if (pred (car lis))
  1153. (lp (cdr lis))
  1154. lis))))
  1155. (define (take-while! pred lis)
  1156. (check-arg procedure? pred take-while!)
  1157. (if (or (null-list? lis) (not (pred (car lis)))) '()
  1158. (begin (let lp ((prev lis) (rest (cdr lis)))
  1159. (if (pair? rest)
  1160. (let ((x (car rest)))
  1161. (if (pred x) (lp rest (cdr rest))
  1162. (set-cdr! prev '())))))
  1163. lis)))
  1164. (define (span pred lis)
  1165. (check-arg procedure? pred span)
  1166. (let recur ((lis lis))
  1167. (if (null-list? lis) (values '() '())
  1168. (let ((x (car lis)))
  1169. (if (pred x)
  1170. (receive (prefix suffix) (recur (cdr lis))
  1171. (values (cons x prefix) suffix))
  1172. (values '() lis))))))
  1173. (define (span! pred lis)
  1174. (check-arg procedure? pred span!)
  1175. (if (or (null-list? lis) (not (pred (car lis)))) (values '() lis)
  1176. (let ((suffix (let lp ((prev lis) (rest (cdr lis)))
  1177. (if (null-list? rest) rest
  1178. (let ((x (car rest)))
  1179. (if (pred x) (lp rest (cdr rest))
  1180. (begin (set-cdr! prev '())
  1181. rest)))))))
  1182. (values lis suffix))))
  1183. (define (break pred lis) (span (lambda (x) (not (pred x))) lis))
  1184. (define (break! pred lis) (span! (lambda (x) (not (pred x))) lis))
  1185. (define (any pred lis1 . lists)
  1186. (check-arg procedure? pred any)
  1187. (if (pair? lists)
  1188. ;; N-ary case
  1189. (receive (heads tails) (%cars+cdrs (cons lis1 lists))
  1190. (and (pair? heads)
  1191. (let lp ((heads heads) (tails tails))
  1192. (receive (next-heads next-tails) (%cars+cdrs tails)
  1193. (if (pair? next-heads)
  1194. (or (apply pred heads) (lp next-heads next-tails))
  1195. (apply pred heads)))))) ; Last PRED app is tail call.
  1196. ;; Fast path
  1197. (and (not (null-list? lis1))
  1198. (let lp ((head (car lis1)) (tail (cdr lis1)))
  1199. (if (null-list? tail)
  1200. (pred head) ; Last PRED app is tail call.
  1201. (or (pred head) (lp (car tail) (cdr tail))))))))
  1202. ;(define (every pred list) ; Simple definition.
  1203. ; (let lp ((list list)) ; Doesn't return the last PRED value.
  1204. ; (or (not (pair? list))
  1205. ; (and (pred (car list))
  1206. ; (lp (cdr list))))))
  1207. (define (every pred lis1 . lists)
  1208. (check-arg procedure? pred every)
  1209. (if (pair? lists)
  1210. ;; N-ary case
  1211. (receive (heads tails) (%cars+cdrs (cons lis1 lists))
  1212. (or (not (pair? heads))
  1213. (let lp ((heads heads) (tails tails))
  1214. (receive (next-heads next-tails) (%cars+cdrs tails)
  1215. (if (pair? next-heads)
  1216. (and (apply pred heads) (lp next-heads next-tails))
  1217. (apply pred heads)))))) ; Last PRED app is tail call.
  1218. ;; Fast path
  1219. (or (null-list? lis1)
  1220. (let lp ((head (car lis1)) (tail (cdr lis1)))
  1221. (if (null-list? tail)
  1222. (pred head) ; Last PRED app is tail call.
  1223. (and (pred head) (lp (car tail) (cdr tail))))))))
  1224. (define (list-index pred lis1 . lists)
  1225. (check-arg procedure? pred list-index)
  1226. (if (pair? lists)
  1227. ;; N-ary case
  1228. (let lp ((lists (cons lis1 lists)) (n 0))
  1229. (receive (heads tails) (%cars+cdrs lists)
  1230. (and (pair? heads)
  1231. (if (apply pred heads) n
  1232. (lp tails (+ n 1))))))
  1233. ;; Fast path
  1234. (let lp ((lis lis1) (n 0))
  1235. (and (not (null-list? lis))
  1236. (if (pred (car lis)) n (lp (cdr lis) (+ n 1)))))))
  1237. ;;; Reverse
  1238. ;;;;;;;;;;;
  1239. ;R4RS, so not defined here.
  1240. ;(define (reverse lis) (fold cons '() lis))
  1241. ;(define (reverse! lis)
  1242. ; (pair-fold (lambda (pair tail) (set-cdr! pair tail) pair) '() lis))
  1243. (define (reverse! lis)
  1244. (let lp ((lis lis) (ans '()))
  1245. (if (null-list? lis) ans
  1246. (let ((tail (cdr lis)))
  1247. (set-cdr! lis ans)
  1248. (lp tail lis)))))
  1249. ;;; Lists-as-sets
  1250. ;;;;;;;;;;;;;;;;;
  1251. ;;; This is carefully tuned code; do not modify casually.
  1252. ;;; - It is careful to share storage when possible;
  1253. ;;; - Side-effecting code tries not to perform redundant writes.
  1254. ;;; - It tries to avoid linear-time scans in special cases where constant-time
  1255. ;;; computations can be performed.
  1256. ;;; - It relies on similar properties from the other list-lib procs it calls.
  1257. ;;; For example, it uses the fact that the implementations of MEMBER and
  1258. ;;; FILTER in this source code share longest common tails between args
  1259. ;;; and results to get structure sharing in the lset procedures.
  1260. (define (%lset2<= = lis1 lis2) (every (lambda (x) (member x lis2 =)) lis1))
  1261. (define (lset<= = . lists)
  1262. (check-arg procedure? = lset<=)
  1263. (or (not (pair? lists)) ; 0-ary case
  1264. (let lp ((s1 (car lists)) (rest (cdr lists)))
  1265. (or (not (pair? rest))
  1266. (let ((s2 (car rest)) (rest (cdr rest)))
  1267. (and (or (eq? s2 s1) ; Fast path
  1268. (%lset2<= = s1 s2)) ; Real test
  1269. (lp s2 rest)))))))
  1270. (define (lset= = . lists)
  1271. (check-arg procedure? = lset=)
  1272. (or (not (pair? lists)) ; 0-ary case
  1273. (let lp ((s1 (car lists)) (rest (cdr lists)))
  1274. (or (not (pair? rest))
  1275. (let ((s2 (car rest))
  1276. (rest (cdr rest)))
  1277. (and (or (eq? s1 s2) ; Fast path
  1278. (and (%lset2<= = s1 s2) (%lset2<= = s2 s1))) ; Real test
  1279. (lp s2 rest)))))))
  1280. (define (lset-adjoin = lis . elts)
  1281. (check-arg procedure? = lset-adjoin)
  1282. (fold (lambda (elt ans) (if (member elt ans =) ans (cons elt ans)))
  1283. lis elts))
  1284. (define (lset-union = . lists)
  1285. (check-arg procedure? = lset-union)
  1286. (reduce (lambda (lis ans) ; Compute ANS + LIS.
  1287. (cond ((null? lis) ans) ; Don't copy any lists
  1288. ((null? ans) lis) ; if we don't have to.
  1289. ((eq? lis ans) ans)
  1290. (else
  1291. (fold (lambda (elt ans) (if (any (lambda (x) (= x elt)) ans)
  1292. ans
  1293. (cons elt ans)))
  1294. ans lis))))
  1295. '() lists))
  1296. (define (lset-union! = . lists)
  1297. (check-arg procedure? = lset-union!)
  1298. (reduce (lambda (lis ans) ; Splice new elts of LIS onto the front of ANS.
  1299. (cond ((null? lis) ans) ; Don't copy any lists
  1300. ((null? ans) lis) ; if we don't have to.
  1301. ((eq? lis ans) ans)
  1302. (else
  1303. (pair-fold (lambda (pair ans)
  1304. (let ((elt (car pair)))
  1305. (if (any (lambda (x) (= x elt)) ans)
  1306. ans
  1307. (begin (set-cdr! pair ans) pair))))
  1308. ans lis))))
  1309. '() lists))
  1310. (define (lset-intersection = lis1 . lists)
  1311. (check-arg procedure? = lset-intersection)
  1312. (let ((lists (delete lis1 lists eq?))) ; Throw out any LIS1 vals.
  1313. (cond ((any null-list? lists) '()) ; Short cut
  1314. ((null? lists) lis1) ; Short cut
  1315. (else (filter (lambda (x)
  1316. (every (lambda (lis) (member x lis =)) lists))
  1317. lis1)))))
  1318. (define (lset-intersection! = lis1 . lists)
  1319. (check-arg procedure? = lset-intersection!)
  1320. (let ((lists (delete lis1 lists eq?))) ; Throw out any LIS1 vals.
  1321. (cond ((any null-list? lists) '()) ; Short cut
  1322. ((null? lists) lis1) ; Short cut
  1323. (else (filter! (lambda (x)
  1324. (every (lambda (lis) (member x lis =)) lists))
  1325. lis1)))))
  1326. (define (lset-difference = lis1 . lists)
  1327. (check-arg procedure? = lset-difference)
  1328. (let ((lists (filter pair? lists))) ; Throw out empty lists.
  1329. (cond ((null? lists) lis1) ; Short cut
  1330. ((memq lis1 lists) '()) ; Short cut
  1331. (else (filter (lambda (x)
  1332. (every (lambda (lis) (not (member x lis =)))
  1333. lists))
  1334. lis1)))))
  1335. (define (lset-difference! = lis1 . lists)
  1336. (check-arg procedure? = lset-difference!)
  1337. (let ((lists (filter pair? lists))) ; Throw out empty lists.
  1338. (cond ((null? lists) lis1) ; Short cut
  1339. ((memq lis1 lists) '()) ; Short cut
  1340. (else (filter! (lambda (x)
  1341. (every (lambda (lis) (not (member x lis =)))
  1342. lists))
  1343. lis1)))))
  1344. (define (lset-xor = . lists)
  1345. (check-arg procedure? = lset-xor)
  1346. (reduce (lambda (b a) ; Compute A xor B:
  1347. ;; Note that this code relies on the constant-time
  1348. ;; short-cuts provided by LSET-DIFF+INTERSECTION,
  1349. ;; LSET-DIFFERENCE & APPEND to provide constant-time short
  1350. ;; cuts for the cases A = (), B = (), and A eq? B. It takes
  1351. ;; a careful case analysis to see it, but it's carefully
  1352. ;; built in.
  1353. ;; Compute a-b and a^b, then compute b-(a^b) and
  1354. ;; cons it onto the front of a-b.
  1355. (receive (a-b a-int-b) (lset-diff+intersection = a b)
  1356. (cond ((null? a-b) (lset-difference = b a))
  1357. ((null? a-int-b) (append b a))
  1358. (else (fold (lambda (xb ans)
  1359. (if (member xb a-int-b =) ans (cons xb ans)))
  1360. a-b
  1361. b)))))
  1362. '() lists))
  1363. (define (lset-xor! = . lists)
  1364. (check-arg procedure? = lset-xor!)
  1365. (reduce (lambda (b a) ; Compute A xor B:
  1366. ;; Note that this code relies on the constant-time
  1367. ;; short-cuts provided by LSET-DIFF+INTERSECTION,
  1368. ;; LSET-DIFFERENCE & APPEND to provide constant-time short
  1369. ;; cuts for the cases A = (), B = (), and A eq? B. It takes
  1370. ;; a careful case analysis to see it, but it's carefully
  1371. ;; built in.
  1372. ;; Compute a-b and a^b, then compute b-(a^b) and
  1373. ;; cons it onto the front of a-b.
  1374. (receive (a-b a-int-b) (lset-diff+intersection! = a b)
  1375. (cond ((null? a-b) (lset-difference! = b a))
  1376. ((null? a-int-b) (append! b a))
  1377. (else (pair-fold (lambda (b-pair ans)
  1378. (if (member (car b-pair) a-int-b =) ans
  1379. (begin (set-cdr! b-pair ans) b-pair)))
  1380. a-b
  1381. b)))))
  1382. '() lists))
  1383. (define (lset-diff+intersection = lis1 . lists)
  1384. (check-arg procedure? = lset-diff+intersection)
  1385. (cond ((every null-list? lists) (values lis1 '())) ; Short cut
  1386. ((memq lis1 lists) (values '() lis1)) ; Short cut
  1387. (else (partition (lambda (elt)
  1388. (not (any (lambda (lis) (member elt lis =))
  1389. lists)))
  1390. lis1))))
  1391. (define (lset-diff+intersection! = lis1 . lists)
  1392. (check-arg procedure? = lset-diff+intersection!)
  1393. (cond ((every null-list? lists) (values lis1 '())) ; Short cut
  1394. ((memq lis1 lists) (values '() lis1)) ; Short cut
  1395. (else (partition! (lambda (elt)
  1396. (not (any (lambda (lis) (member elt lis =))
  1397. lists)))
  1398. lis1))))