1.upstream.scm 54 KB

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