Debian Codegraph @

%3a116/ilists.sls

raw
1 (library (srfi :116 ilists)
2
3 (export iq
4 ipair ilist xipair ipair* make-ilist ilist-copy ilist-tabulate iiota
5 ipair?
6 proper-ilist? ilist? dotted-ilist? not-ipair? null-ilist? ilist=
7 icar icdr ilist-ref
8 ifirst isecond ithird ifourth ififth isixth iseventh ieighth ininth itenth
9 icaar icadr icdar icddr
10 icaaar icaadr icadar icaddr icdaar icdadr icddar icdddr
11 icaaaar icaaadr icaadar icaaddr icadaar icadadr icaddar icadddr
12 icdaaar icdaadr icdadar icdaddr icddaar icddadr icdddar icddddr
13 icar+icdr itake idrop ilist-tail
14 itake-right idrop-right isplit-at ilast last-ipair
15 ilength iappend iconcatenate ireverse iappend-reverse
16 izip iunzip1 iunzip2 iunzip3 iunzip4 iunzip5
17 icount imap ifor-each ifold iunfold ipair-fold ireduce
18 ifold-right iunfold-right ipair-fold-right ireduce-right
19 iappend-map ipair-for-each ifilter-map imap-in-order
20 ifilter ipartition iremove imember imemq imemv
21 ifind ifind-tail iany ievery
22 ilist-index itake-while idrop-while ispan ibreak
23 idelete idelete-duplicates
24 iassoc iassq iassv ialist-cons ialist-delete
25 replace-icar replace-icdr
26 pair->ipair ipair->pair list->ilist ilist->list
27 tree->itree itree->tree gtree->itree gtree->tree
28 iapply)
29
30 (import (except (rnrs) define-record-type)
31 (only (srfi :1) set-cdr! set-car!)
32 (srfi :9))
33
34 (define (error* msg . args)
35 (error 'ilist msg args))
36
37 ;;;; Enhancements and hooks in Olin's SRFI-1 code to make it work for ilists
38
39 ;;; The basic ilist cell
40
41 (define-record-type <ilist> (ipair icar icdr) ipair? (icar icar) (icdr icdr))
42
43 ;;; SRFI 8 syntax for receiving multiple values
44
45 (define-syntax receive
46 (syntax-rules ()
47 ((receive formals expression body ...)
48 (call-with-values (lambda () expression)
49 (lambda formals body ...)))))
50
51 ;;; Syntax for quoting ilists
52
53 (define-syntax iq
54 (syntax-rules ()
55 ((iq . tree) (gtree->itree 'tree))))
56
57 ;;; Replacers
58
59 (define (replace-icar old new)
60 (ipair new (icdr old)))
61
62 (define (replace-icdr old new)
63 (ipair (icar old) new))
64
65 ;;; Conversion between lists and ilists
66
67 (define (pair->ipair pair)
68 (ipair (car pair) (cdr pair)))
69
70 (define (ipair->pair ipair)
71 (cons (icar ipair) (icdr ipair)))
72
73 (define (list->ilist list)
74 (let lp ((list list))
75 (if (pair? list)
76 (ipair (car list) (lp (cdr list)))
77 list)))
78
79 (define (ilist . objs)
80 (list->ilist objs))
81
82 (define (ilist->list ilist)
83 (let lp ((ilist ilist))
84 (if (ipair? ilist)
85 (cons (icar ilist) (lp (icdr ilist)))
86 ilist)))
87
88 (define (tree->itree obj)
89 (if (pair? obj)
90 (ipair (tree->itree (car obj)) (tree->itree (cdr obj)))
91 obj))
92
93 (define (itree->tree obj)
94 (if (ipair? obj)
95 (cons (itree->tree (icar obj)) (itree->tree (icdr obj)))
96 obj))
97
98 (define (gtree->itree obj)
99 (cond
100 ((pair? obj)
101 (ipair (gtree->itree (car obj)) (gtree->itree (cdr obj))))
102 ((ipair? obj)
103 (ipair (gtree->itree (icar obj)) (gtree->itree (icdr obj))))
104 (else
105 obj)))
106
107 (define (gtree->tree obj)
108 (cond
109 ((pair? obj)
110 (cons (gtree->tree (car obj)) (gtree->tree (cdr obj))))
111 ((ipair? obj)
112 (cons (gtree->tree (icar obj)) (gtree->tree (icdr obj))))
113 (else
114 obj)))
115
116 ;; Apply a function to (arguments and) an ilist
117 ;; If ilists are built in, optimize this!
118 ;; Need a few SRFI-1 routines
119
120 (define (take! ls i)
121 (if (<= i 0)
122 '()
123 (let ((tail (list-tail ls (- i 1))))
124 (set-cdr! tail '())
125 ls)))
126
127 (define (drop-right! ls i)
128 (take! ls (- (length ls) i)))
129
130 (define (last ls) (if (null? (cdr ls)) (car ls) (last (cdr ls))))
131
132 (define (iapply proc . ilists)
133 (cond
134 ((null? ilists)
135 (apply proc '()))
136 ((null? (cdr ilists))
137 (apply proc (ilist->list (car ilists))))
138 (else
139 (let ((final (ilist->list (last ilists))))
140 (apply proc (append (drop-right! ilists 1) final))))))
141
142 ;;; Printer for debugging
143
144 (define (write-ipair ipair port)
145 (write (gtree->tree ipair) port))
146
147 ;;; Stuff needed by Olin's code
148
149 (define-syntax :optional
150 (syntax-rules ()
151 ((:optional rest default)
152 (cond
153 ((null? rest) default)
154 ((null? (cdr rest)) (car rest))
155 (else (error* "Too many arguments"))))))
156
157 ;;; Analogues of R5RS list routines (not defined by Olin)
158
159 (define (iassq x lis)
160 (ifind (lambda (entry) (eq? x (icar entry))) lis))
161
162 (define (iassv x lis)
163 (ifind (lambda (entry) (eqv? x (icar entry))) lis))
164
165 (define (ifor-each proc lis1 . lists)
166 (check-arg procedure? proc ipair-for-each)
167 (if (pair? lists)
168
169 (let lp ((lists (cons lis1 lists)))
170 (let ((tails (%cdrs lists)))
171 (if (pair? tails)
172 (begin (apply proc (map icar lists))
173 (lp tails)))))
174
175 ;; Fast path.
176 (let lp ((lis lis1))
177 (if (not (null-ilist? lis))
178 (let ((tail (icdr lis))) ; Grab the icdr now,
179 (proc (icar lis)) ; even though it's unnecessary
180 (lp tail))))))
181
182 ;;; SRFI-116 ilist-processing library -*- Scheme -*-
183 ;;; Sample implementation
184 ;;;
185 ;;; Copyright (c) 1998, 1999 by Olin Shivers.
186 ;;; Modifications Copyright (c) 2014 by John Cowan.
187 ;;; You may do as you please with
188 ;;; this code as long as you do not remove either copyright notice
189 ;;; or hold us liable for its use. Please send bug reports to
190 ;;; <[email protected]>.
191
192 ;;; This is a library of ilist- and ipair-processing functions. I wrote it after
193 ;;; carefully considering the functions provided by the libraries found in
194 ;;; R4RS/R5RS Scheme, MIT Scheme, Gambit, RScheme, MzScheme, slib, Common
195 ;;; Lisp, Bigloo, guile, T, APL and the SML standard basis. It is a pretty
196 ;;; rich toolkit, providing a superset of the functionality found in any of
197 ;;; the various Schemes I considered.
198
199 ;;; This implementation is intended as a portable reference implementation
200 ;;; for SRFI-116. See the porting notes below for more information.
201
202 ;;; Exported:
203 ;;; xipair tree-copy make-ilist ilist-tabulate ipair* ilist-copy
204 ;;; proper-ilist? circular-ilist? dotted-ilist? not-ipair? null-ilist? ilist=
205 ;;; ilength+
206 ;;; iiota
207 ;;; ifirst isecond ithird ifourth ififth isixth iseventh ieighth ininth itenth
208 ;;; icar+icdr
209 ;;; itake idrop
210 ;;; itake-right idrop-right
211 ;;; isplit-at
212 ;;; ilast last-ipair
213 ;;; izip iunzip1 iunzip2 iunzip3 iunzip4 iunzip5
214 ;;; icount
215 ;;; iappend-reverse iconcatenate
216 ;;; iunfold ifold ipair-fold ireduce
217 ;;; iunfold-right ifold-right ipair-fold-right ireduce-right
218 ;;; iappend-map ipair-for-each ifilter-map imap-in-order
219 ;;; ifilter ipartition iremove
220 ;;; ifind ifind-tail iany ievery ilist-index
221 ;;; itake-while idrop-while
222 ;;; ispan ibreak
223 ;;; idelete
224 ;;; ialist-cons alist-copy
225 ;;; idelete-duplicates
226 ;;; ialist-delete
227 ;;; ipair ipair? null? icar icdr
228 ;;; ilist ilength iappend ireverse icadr ... icddddr ilist-ref
229 ;;; first second third fourth fifth sixth seventh eighth ninth tenth
230 ;;; imemq imemv iassq iassv
231 ;;;
232 ;;; ilist-tail (same as idrop)
233 ;;; ilist? (same as proper-ilist?)
234
235
236 ;;; A note on recursion and iteration/reversal:
237 ;;; Many iterative ilist-processing algorithms naturally compute the elements
238 ;;; of the answer ilist in the wrong order (left-to-right or head-to-tail) from
239 ;;; the order needed to pair them into the proper answer (right-to-left, or
240 ;;; tail-then-head). One style or idiom of programming these algorithms, then,
241 ;;; loops, consing up the elements in reverse order, then destructively
242 ;;; reverses the ilist at the end of the loop. I do not do this. The natural
243 ;;; and efficient way to code these algorithms is recursively. This trades off
244 ;;; intermediate temporary ilist structure for intermediate temporary stack
245 ;;; structure. In a stack-based system, this improves cache locality and
246 ;;; lightens the load on the GC system. Don't stand on your head to iterate!
247 ;;; Recurse, where natural. Multiple-value returns make this even more
248 ;;; convenient, when the recursion/iteration has multiple state values.
249
250 ;;; Porting:
251 ;;; This is carefully tuned code; do not modify casually.
252 ;;; - It is careful to share storage when possible;
253 ;;;
254 ;;; That said, a port of this library to a specific Scheme system might wish
255 ;;; to tune this code to exploit particulars of the implementation.
256 ;;; The single most important compiler-specific optimisation you could make
257 ;;; to this library would be to add rewrite rules or transforms to:
258 ;;; - transform applications of n-ary procedures (e.g. ILIST=, IPAIR*, IIAPPEND,
259 ;;; ILSET-UNION) into multiple applications of a primitive two-argument
260 ;;; variant.
261 ;;; - transform applications of the mapping functions (IMAP, IFOR-EACH, IFOLD,
262 ;;; IANY, IEVERY) into open-coded loops. The killer here is that these
263 ;;; functions are n-ary. Handling the general case is quite inefficient,
264 ;;; requiring many intermediate data structures to be allocated and
265 ;;; discarded.
266 ;;; - transform applications of procedures that take optional arguments
267 ;;; into calls to variants that do not take optional arguments. This
268 ;;; eliminates unnecessary consing and parsing of the rest parameter.
269 ;;;
270 ;;; These transforms would provide BIG speedups. In particular, the n-ary
271 ;;; mapping functions are particularly slow and pair-intensive, and are good
272 ;;; candidates for tuning. I have coded fast paths for the single-ilist cases,
273 ;;; but what you really want to do is exploit the fact that the compiler
274 ;;; usually knows how many arguments are being passed to a particular
275 ;;; application of these functions -- they are usually explicitly called, not
276 ;;; passed around as higher-order values. If you can arrange to have your
277 ;;; compiler produce custom code or custom linkages based on the number of
278 ;;; arguments in the call, you can speed these functions up a *lot*. But this
279 ;;; kind of compiler technology no longer exists in the Scheme world as far as
280 ;;; I can see.
281 ;;;
282 ;;; This code does a fair amount of run-time argument checking. If your
283 ;;; Scheme system has a sophisticated compiler that can eliminate redundant
284 ;;; error checks, this is no problem. However, if not, these checks incur
285 ;;; some performance overhead -- and, in a safe Scheme implementation, they
286 ;;; are in some sense redundant: if we don't check to see that the PROC
287 ;;; parameter is a procedure, we'll find out anyway three lines later when
288 ;;; we try to call the value. It's pretty easy to rip all this argument
289 ;;; checking code out if it's inappropriate for your implementation -- just
290 ;;; nuke every call to CHECK-ARG.
291 ;;;
292 ;;; On the other hand, if you *do* have a sophisticated compiler that will
293 ;;; actually perform soft-typing and eliminate redundant checks (Rice's systems
294 ;;; being the only possible candidate of which I'm aware), leaving these checks
295 ;;; in can *help*, since their presence can be elided in redundant cases,
296 ;;; and in cases where they are needed, performing the checks early, at
297 ;;; procedure entry, can "lift" a check out of a loop.
298 ;;;
299 ;;; Finally, I have only checked the properties that can portably be checked
300 ;;; with R5RS Scheme -- and this is not complete. You may wish to alter
301 ;;; the CHECK-ARG parameter checks to perform extra, implementation-specific
302 ;;; checks, such as procedure arity for higher-order values.
303 ;;;
304 ;;; The code has only these non-R4RS dependencies:
305 ;;; A few calls to an ERROR procedure;
306 ;;; Uses of the R5RS multiple-value procedure VALUES and the m-v binding
307 ;;; RECEIVE macro (which isn't R5RS, but is a trivial macro).
308 ;;; Many calls to a parameter-checking procedure check-arg:
309
310 (define (check-arg pred val caller)
311 (let lp ((val val))
312 (if (pred val) val (lp (error* "Bad argument" val pred caller)))))
313
314 ;;; A few uses of the LET-OPTIONAL and :OPTIONAL macros for parsing
315 ;;; optional arguments.
316 ;;;
317 ;;; Most of these procedures use the NULL-ILIST? test to trigger the
318 ;;; base case in the inner loop or recursion. The NULL-ILIST? function
319 ;;; is defined to be a careful one -- it raises an error if passed a
320 ;;; non-nil, non-ipair value. The spec allows an implementation to use
321 ;;; a less-careful implementation that simply defines NULL-ILIST? to
322 ;;; be NOT-IPAIR?. This would speed up the inner loops of these procedures
323 ;;; at the expense of having them silently accept dotted lists.
324
325 ;;; A note on dotted lists:
326 ;;; Many of the procedures in this library can be trivially redefined
327 ;;; to handle dotted lists, just by changing the NULL-ILIST? base-case
328 ;;; check to NOT-IPAIR?, meaning that any non-ipair value is taken to be
329 ;;; an empty ilist. For most of these procedures, that's all that is
330 ;;; required.
331 ;;;
332 ;;; However, we have to do a little more work for some procedures that
333 ;;; *produce* ilists from other ilists. Were we to extend these procedures to
334 ;;; accept dotted ilists, we would have to define how they terminate the ilists
335 ;;; produced as results when passed a dotted ilist.
336 ;;;
337 ;;; The argument *against* defining these procedures to work on dotted
338 ;;; ilists is that dotted ilists are the rare, odd case, and that by
339 ;;; arranging for the procedures to handle them, we lose error checking
340 ;;; in the cases where a dotted ilist is passed by accident -- e.g., when
341 ;;; the programmer swaps a two arguments to an ilist-processing function,
342 ;;; one being a scalar and one being an ilist. For example,
343 ;;; (imember '(1 3 5 7 9) 7)
344 ;;; This would quietly return #f if we extended IMEMBER to accept dotted
345 ;;; lists.
346
347
348 ;;; Constructors
349 ;;;;;;;;;;;;;;;;
350
351 ;;; Occasionally useful as a value to be passed to a ifold or other
352 ;;; higher-order procedure.
353 (define (xipair d a) (ipair a d))
354
355 ;;;; Recursively copy every ipair.
356
357 ;;; Make an ilist of length LEN.
358
359 (define (make-ilist len . maybe-elt)
360 (check-arg (lambda (n) (and (integer? n) (>= n 0))) len make-ilist)
361 (let ((elt (cond ((null? maybe-elt) #f) ; Default value
362 ((null? (cdr maybe-elt)) (car maybe-elt))
363 (else (error* "Too many arguments to MAKE-ILIST"
364 (ipair len maybe-elt))))))
365 (do ((i len (- i 1))
366 (ans '() (ipair elt ans)))
367 ((<= i 0) ans))))
368
369
370 ;; The ilist procedure is defined in ilists-base.scm.
371
372
373 ;;; Make an ilist of ilength LEN. Elt i is (PROC i) for 0 <= i < LEN.
374
375 (define (ilist-tabulate len proc)
376 (check-arg (lambda (n) (and (integer? n) (>= n 0))) len ilist-tabulate)
377 (check-arg procedure? proc ilist-tabulate)
378 (do ((i (- len 1) (- i 1))
379 (ans '() (ipair (proc i) ans)))
380 ((< i 0) ans)))
381
382 ;;; (ipair* a1 a2 ... an) = (ipair a1 (ipair a2 (ipair ... an)))
383 ;;; (ipair* a1) = a1 (ipair* a1 a2 ...) = (ipair a1 (ipair* a2 ...))
384 ;;;
385 ;;; (ipair ifirst (iunfold not-ipair? icar icdr rest values))
386
387 (define (ipair* ifirst . rest)
388 (let recur ((x ifirst) (rest rest))
389 (if (pair? rest)
390 (ipair x (recur (car rest) (cdr rest)))
391 x)))
392
393 ;;; (iunfold not-ipair? icar icdr lis values)
394
395 (define (ilist-copy lis)
396 (let recur ((lis lis))
397 (if (ipair? lis)
398 (ipair (icar lis) (recur (icdr lis)))
399 lis)))
400
401 ;;; IIOTA count [start step] (start start+step ... start+(count-1)*step)
402
403 (define (iiota count . maybe-start+step)
404 (check-arg integer? count iiota)
405 (if (< count 0) (error* "Negative step count" iiota count))
406 (let ((start (if (pair? maybe-start+step) (car maybe-start+step) 0))
407 (step (if (and (pair? maybe-start+step)
408 (pair? (cdr maybe-start+step)))
409 (cadr maybe-start+step)
410 1)))
411 (check-arg number? start iiota)
412 (check-arg number? step iiota)
413 (let loop ((n 0) (r '()))
414 (if (= n count)
415 (ireverse r)
416 (loop (+ 1 n)
417 (ipair (+ start (* n step)) r))))))
418
419
420 ;;; <proper-ilist> ::= () ; Empty proper ilist
421 ;;; | (ipair <x> <proper-ilist>) ; Proper-ilist ipair
422 ;;; Note that this definition rules out circular lists -- and this
423 ;;; function is required to detect this case and return false.
424
425 (define (ilist? x) (proper-ilist? x))
426 (define (proper-ilist? x)
427 (let lp ((x x) (lag x))
428 (if (ipair? x)
429 (let ((x (icdr x)))
430 (if (ipair? x)
431 (let ((x (icdr x))
432 (lag (icdr lag)))
433 (and (not (eq? x lag)) (lp x lag)))
434 (null? x)))
435 (null? x))))
436
437
438 ;;; A dotted ilist is a finite ilist (possibly of ilength 0) terminated
439 ;;; by a non-nil value. Any non-ipair, non-nil value (e.g., "foo" or 5)
440 ;;; is a dotted ilist of ilength 0.
441 ;;;
442 ;;; <dotted-ilist> ::= <non-nil,non-ipair> ; Empty dotted ilist
443 ;;; | (ipair <x> <dotted-ilist>) ; Proper-ilist ipair
444
445 (define (dotted-ilist? x)
446 (let lp ((x x) (lag x))
447 (if (ipair? x)
448 (let ((x (icdr x)))
449 (if (ipair? x)
450 (let ((x (icdr x))
451 (lag (icdr lag)))
452 (and (not (eq? x lag)) (lp x lag)))
453 (not (null? x))))
454 (not (null? x)))))
455
456 (define (not-ipair? x) (not (ipair? x))) ; Inline me.
457
458 ;;; This is a legal definition which is fast and sloppy:
459 ;;; (define null-ilist? not-ipair?)
460 ;;; but we'll provide a more careful one:
461 (define (null-ilist? l)
462 (cond ((ipair? l) #f)
463 ((null? l) #t)
464 (else (error* "null-ilist?: argument out of domain" l))))
465
466
467 (define (ilist= = . ilists)
468 (or (null? ilists) ; special case
469
470 (let lp1 ((ilist-a (car ilists)) (others (cdr ilists)))
471 (or (null? others)
472 (let ((ilist-b (car others))
473 (others (cdr others)))
474 (if (eq? ilist-a ilist-b) ; EQ? => LIST=
475 (lp1 ilist-b others)
476 (let lp2 ((pair-a ilist-a) (pair-b ilist-b))
477 (if (null-ilist? pair-a)
478 (and (null-ilist? pair-b)
479 (lp1 ilist-b others))
480 (and (not (null-ilist? pair-b))
481 (= (icar pair-a) (icar pair-b))
482 (lp2 (icdr pair-a) (icdr pair-b)))))))))))
483
484
485
486 (define (ilength x) ; ILENGTH may diverge or
487 (let lp ((x x) (len 0)) ; raise an error if X is
488 (if (ipair? x) ; a circular ilist. This version
489 (lp (icdr x) (+ len 1)) ; diverges.
490 len)))
491
492 (define (izip ilist1 . more-lists) (apply imap ilist ilist1 more-lists))
493
494
495 ;;; Selectors
496 ;;;;;;;;;;;;;
497
498 (define (icaar x) (icar (icar x)))
499 (define (icadr x) (icar (icdr x)))
500 (define (icdar x) (icdr (icar x)))
501 (define (icddr x) (icdr (icdr x)))
502
503 (define (icaaar x) (icaar (icar x)))
504 (define (icaadr x) (icaar (icdr x)))
505 (define (icadar x) (icadr (icar x)))
506 (define (icaddr x) (icadr (icdr x)))
507 (define (icdaar x) (icdar (icar x)))
508 (define (icdadr x) (icdar (icdr x)))
509 (define (icddar x) (icddr (icar x)))
510 (define (icdddr x) (icddr (icdr x)))
511
512 (define (icaaaar x) (icaaar (icar x)))
513 (define (icaaadr x) (icaaar (icdr x)))
514 (define (icaadar x) (icaadr (icar x)))
515 (define (icaaddr x) (icaadr (icdr x)))
516 (define (icadaar x) (icadar (icar x)))
517 (define (icadadr x) (icadar (icdr x)))
518 (define (icaddar x) (icaddr (icar x)))
519 (define (icadddr x) (icaddr (icdr x)))
520 (define (icdaaar x) (icdaar (icar x)))
521 (define (icdaadr x) (icdaar (icdr x)))
522 (define (icdadar x) (icdadr (icar x)))
523 (define (icdaddr x) (icdadr (icdr x)))
524 (define (icddaar x) (icddar (icar x)))
525 (define (icddadr x) (icddar (icdr x)))
526 (define (icdddar x) (icdddr (icar x)))
527 (define (icddddr x) (icdddr (icdr x)))
528
529
530 (define ifirst icar)
531 (define isecond icadr)
532 (define ithird icaddr)
533 (define ifourth icadddr)
534 (define (ififth x) (icar (icddddr x)))
535 (define (isixth x) (icadr (icddddr x)))
536 (define (iseventh x) (icaddr (icddddr x)))
537 (define (ieighth x) (icadddr (icddddr x)))
538 (define (ininth x) (icar (icddddr (icddddr x))))
539 (define (itenth x) (icadr (icddddr (icddddr x))))
540
541 (define (icar+icdr ipair) (values (icar ipair) (icdr ipair)))
542
543 ;;; itake & idrop
544
545 (define (itake lis k)
546 (check-arg integer? k itake)
547 (let recur ((lis lis) (k k))
548 (if (zero? k) '()
549 (ipair (icar lis)
550 (recur (icdr lis) (- k 1))))))
551
552 (define (ilist-tail lis k) (idrop lis k))
553 (define (idrop lis k)
554 (check-arg integer? k idrop)
555 (let iter ((lis lis) (k k))
556 (if (zero? k) lis (iter (icdr lis) (- k 1)))))
557
558 ;;; ITAKE-RIGHT and IDROP-RIGHT work by getting two pointers into the ilist,
559 ;;; off by K, then chasing down the ilist until the lead pointer falls off
560 ;;; the end.
561
562 (define (itake-right lis k)
563 (check-arg integer? k itake-right)
564 (let lp ((lag lis) (lead (idrop lis k)))
565 (if (ipair? lead)
566 (lp (icdr lag) (icdr lead))
567 lag)))
568
569 (define (idrop-right lis k)
570 (check-arg integer? k idrop-right)
571 (let recur ((lag lis) (lead (idrop lis k)))
572 (if (ipair? lead)
573 (ipair (icar lag) (recur (icdr lag) (icdr lead)))
574 '())))
575
576 ;;; In this function, LEAD is actually K+1 ahead of LAG. This lets
577 ;;; us stop LAG one step early, in time to smash its icdr to ().
578 (define (ilist-ref lis i) (icar (idrop lis i))) ; R4RS
579
580 (define (isplit-at x k)
581 (check-arg integer? k isplit-at)
582 (let recur ((lis x) (k k))
583 (if (zero? k) (values '() lis)
584 (receive (prefix suffix) (recur (icdr lis) (- k 1))
585 (values (ipair (icar lis) prefix) suffix)))))
586
587 (define (ilast lis) (icar (last-ipair lis)))
588
589 (define (last-ipair lis)
590 (check-arg ipair? lis last-ipair)
591 (let lp ((lis lis))
592 (let ((tail (icdr lis)))
593 (if (ipair? tail) (lp tail) lis))))
594
595
596 ;;; Unzippers -- 1 through 5
597 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
598
599 (define (iunzip1 lis) (imap icar lis))
600
601 (define (iunzip2 lis)
602 (let recur ((lis lis))
603 (if (null-ilist? lis) (values lis lis) ; Use NOT-IPAIR? to handle
604 (let ((elt (icar lis))) ; dotted lists.
605 (receive (a b) (recur (icdr lis))
606 (values (ipair (icar elt) a)
607 (ipair (icadr elt) b)))))))
608
609 (define (iunzip3 lis)
610 (let recur ((lis lis))
611 (if (null-ilist? lis) (values lis lis lis)
612 (let ((elt (icar lis)))
613 (receive (a b c) (recur (icdr lis))
614 (values (ipair (icar elt) a)
615 (ipair (icadr elt) b)
616 (ipair (icaddr elt) c)))))))
617
618 (define (iunzip4 lis)
619 (let recur ((lis lis))
620 (if (null-ilist? lis) (values lis lis lis lis)
621 (let ((elt (icar lis)))
622 (receive (a b c d) (recur (icdr lis))
623 (values (ipair (icar elt) a)
624 (ipair (icadr elt) b)
625 (ipair (icaddr elt) c)
626 (ipair (icadddr elt) d)))))))
627
628 (define (iunzip5 lis)
629 (let recur ((lis lis))
630 (if (null-ilist? lis) (values lis lis lis lis lis)
631 (let ((elt (icar lis)))
632 (receive (a b c d e) (recur (icdr lis))
633 (values (ipair (icar elt) a)
634 (ipair (icadr elt) b)
635 (ipair (icaddr elt) c)
636 (ipair (icadddr elt) d)
637 (ipair (icar (icddddr elt)) e)))))))
638
639
640 ;;; iappend-reverse iconcatenate
641 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
642
643 (define (iappend . lists)
644 (if (pair? lists)
645 (let recur ((list1 (car lists)) (lists (cdr lists)))
646 (if (pair? lists)
647 (let ((tail (recur (car lists) (cdr lists))))
648 (ifold-right ipair tail list1)) ; Append LIST1 & TAIL.
649 list1))
650 '()))
651
652 ;; (define (iappend-reverse rev-head tail) (ifold ipair tail rev-head))
653
654
655 ;;; Hand-inline the IFOLD and PAIR-IFOLD ops for speed.
656
657 (define (iappend-reverse rev-head tail)
658 (let lp ((rev-head rev-head) (tail tail))
659 (if (null-ilist? rev-head) tail
660 (lp (icdr rev-head) (ipair (icar rev-head) tail)))))
661
662 (define (iconcatenate lists) (ireduce-right iappend '() lists))
663
664 ;;; Fold/imap internal utilities
665 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
666 ;;; These little internal utilities are used by the general
667 ;;; ifold & mapper funs for the n-ary cases . It'd be nice if they got inlined.
668 ;;; One the other hand, the n-ary cases are painfully inefficient as it is.
669 ;;; An aggressive implementation should simply re-write these functions
670 ;;; for raw efficiency; I have written them for as much clarity, portability,
671 ;;; and simplicity as can be achieved.
672 ;;;
673 ;;; I use the dreaded call/cc to do local aborts. A good compiler could
674 ;;; handle this with extreme efficiency. An implementation that provides
675 ;;; a one-shot, non-persistent continuation grabber could help the compiler
676 ;;; out by using that in place of the call/cc's in these routines.
677 ;;;
678 ;;; These functions have funky definitions that are precisely tuned to
679 ;;; the needs of the ifold/imap procs -- for example, to minimize the number
680 ;;; of times the argument lists need to be examined.
681
682 ;;; Return (map icdr ilists).
683 ;;; However, if any element of ILISTS is empty, just abort and return '().
684 (define (%cdrs lists)
685 (call-with-current-continuation
686 (lambda (abort)
687 (let recur ((lists lists))
688 (if (pair? lists)
689 (let ((lis (car lists)))
690 (if (null? lis) (abort '())
691 (cons (icdr lis) (recur (cdr lists)))))
692 '())))))
693
694 (define (%cars+ lists last-elt) ; (append (map icar lists) (list last-elt))
695 (let recur ((lists lists))
696 (if (pair? lists)
697 (cons (icar (car lists)) (recur (cdr lists)))
698 (list last-elt))))
699
700 ;;; LISTS is a (not very long) non-empty list of ilists.
701 ;;; Return two lists: the icars & the icdrs of the ilists.
702 ;;; However, if any of the ilists is empty, just abort and return [() ()].
703
704 (define (%cars+cdrs ilists)
705 (call-with-current-continuation
706 (lambda (abort)
707 (let recur ((ilists ilists))
708 (if (pair? ilists)
709 (let ((ilist (car ilists))
710 (other-ilists (cdr ilists)))
711 (if (null? ilist) (abort '() '()) ; LIST is empty -- bail out
712 (let ((a (icar ilist))
713 (d (icdr ilist)))
714 (receive (icars icdrs) (recur other-ilists)
715 (values (cons a icars) (cons d icdrs))))))
716 (values '() '()))))))
717
718 ;;; Like %CARS+CDRS, but we pass in a final elt tacked onto the end of the
719 ;;; cars ilist. What a hack.
720 (define (%cars+cdrs+ ilists cars-final)
721 (call-with-current-continuation
722 (lambda (abort)
723 (let recur ((ilists ilists))
724 (if (pair? ilists)
725 (let ((ilist (car ilists))
726 (other-ilists (cdr ilists)))
727 (if (null? ilist) (abort '() '()) ; LIST is empty -- bail out
728 (receive (a d) (icar+icdr ilist)
729 (receive (cars cdrs) (recur other-ilists)
730 (values (cons a cars) (cons d cdrs))))))
731 (values (list cars-final) '()))))))
732
733 ;;; Like %CARS+CDRS, but blow up if any ilist is empty.
734 (define (%cars+cdrs/no-test ilists)
735 (let recur ((ilists ilists))
736 (if (pair? ilists)
737 (let ((ilist (car ilists))
738 (other-ilists (cdr ilists)))
739 (let ((a (icar ilist))
740 (d (icdr ilist)))
741 (receive (cars cdrs) (recur other-ilists)
742 (values (cons a cars) (cons d cdrs)))))
743 (values '() '()))))
744
745
746 ;;; icount
747 ;;;;;;;;;
748 (define (icount pred ilist1 . ilists)
749 (check-arg procedure? pred icount)
750 (if (pair? ilists)
751
752 ;; N-ary case
753 (let lp ((ilist1 ilist1) (ilists ilists) (i 0))
754 (if (null-ilist? ilist1) i
755 (receive (as ds) (%cars+cdrs ilists)
756 (if (null? as) i
757 (lp (icdr ilist1) ds
758 (if (apply pred (icar ilist1) as) (+ i 1) i))))))
759
760 ;; Fast path
761 (let lp ((lis ilist1) (i 0))
762 (if (null-ilist? lis) i
763 (lp (icdr lis) (if (pred (icar lis)) (+ i 1) i))))))
764
765
766 ;;; ifold/iunfold
767 ;;;;;;;;;;;;;;;
768
769 (define (iunfold-right p f g seed . maybe-tail)
770 (check-arg procedure? p iunfold-right)
771 (check-arg procedure? f iunfold-right)
772 (check-arg procedure? g iunfold-right)
773 (let lp ((seed seed) (ans (:optional maybe-tail '())))
774 (if (p seed) ans
775 (lp (g seed)
776 (ipair (f seed) ans)))))
777
778
779 (define (iunfold p f g seed . maybe-tail-gen)
780 (check-arg procedure? p iunfold)
781 (check-arg procedure? f iunfold)
782 (check-arg procedure? g iunfold)
783 (if (pair? maybe-tail-gen)
784
785 (let ((tail-gen (car maybe-tail-gen)))
786 (if (pair? (cdr maybe-tail-gen))
787 (apply error* "Too many arguments" iunfold p f g seed maybe-tail-gen)
788
789 (let recur ((seed seed))
790 (if (p seed) (tail-gen seed)
791 (ipair (f seed) (recur (g seed)))))))
792
793 (let recur ((seed seed))
794 (if (p seed) '()
795 (ipair (f seed) (recur (g seed)))))))
796
797
798 (define (ifold kons knil ilis1 . ilists)
799 (check-arg procedure? kons ifold)
800 (if (pair? ilists)
801 (let lp ((ilists (cons ilis1 ilists)) (ans knil)) ; N-ary case
802 (receive (cars+ans cdrs) (%cars+cdrs+ ilists ans)
803 (if (null? cars+ans) ans ; Done.
804 (lp cdrs (apply kons cars+ans)))))
805
806 (let lp ((ilis ilis1) (ans knil)) ; Fast path
807 (if (null-ilist? ilis) ans
808 (lp (icdr ilis) (kons (icar ilis) ans))))))
809
810
811 (define (ifold-right kons knil ilis1 . ilists)
812 (check-arg procedure? kons ifold-right)
813 (if (pair? ilists)
814 (let recur ((ilists (cons ilis1 ilists))) ; N-ary case
815 (let ((cdrs (%cdrs ilists)))
816 (if (null? cdrs) knil
817 (apply kons (%cars+ ilists (recur cdrs))))))
818
819 (let recur ((ilis ilis1)) ; Fast path
820 (if (null? ilis) knil
821 (let ((head (icar ilis)))
822 (kons head (recur (icdr ilis))))))))
823
824
825 (define (ipair-fold-right f zero ilis1 . ilists)
826 (check-arg procedure? f ipair-fold-right)
827 (if (pair? ilists)
828 (let recur ((ilists (cons ilis1 ilists))) ; N-ary case
829 (let ((cdrs (%cdrs ilists)))
830 (if (null? cdrs) zero
831 (apply f (append ilists (list (recur cdrs)))))))
832
833 (let recur ((ilis ilis1)) ; Fast path
834 (if (null-ilist? ilis) zero (f ilis (recur (icdr ilis)))))))
835
836 (define (ipair-fold f zero ilis1 . ilists)
837 (check-arg procedure? f ipair-fold)
838 (if (pair? ilists)
839 (let lp ((ilists (cons ilis1 ilists)) (ans zero)) ; N-ary case
840 (let ((tails (%cdrs ilists)))
841 (if (null? tails) ans
842 (lp tails (apply f (append ilists (list ans)))))))
843
844 (let lp ((ilis ilis1) (ans zero))
845 (if (null-ilist? ilis) ans
846 (let ((tail (icdr ilis))) ; Grab the icdr now,
847 (lp tail (f ilis ans))))))) ; in case F SET-CDR!s LIS.
848
849
850 ;;; IREDUCE and IREDUCE-RIGHT only use RIDENTITY in the empty-ilist case.
851 ;;; These cannot meaningfully be n-ary.
852
853 (define (ireduce f ridentity ilis)
854 (check-arg procedure? f ireduce)
855 (if (null-ilist? ilis) ridentity
856 (ifold f (icar ilis) (icdr ilis))))
857
858 (define (ireduce-right f ridentity ilis)
859 (check-arg procedure? f ireduce-right)
860 (if (null-ilist? ilis) ridentity
861 (let recur ((head (icar ilis)) (ilis (icdr ilis)))
862 (if (ipair? ilis)
863 (f head (recur (icar ilis) (icdr ilis)))
864 head))))
865
866
867
868 ;;; Mappers: iappend-map ipair-for-each ifilter-map imap-in-order
869 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
870
871 (define (iappend-map f ilis1 . ilists)
872 (really-iappend-map iappend-map iappend f ilis1 ilists))
873
874 (define (really-iappend-map who appender f ilis1 ilists)
875 (check-arg procedure? f who)
876 (if (pair? ilists)
877 (receive (cars cdrs) (%cars+cdrs (cons ilis1 ilists))
878 (if (null? cars) '()
879 (let recur ((cars cars) (cdrs cdrs))
880 (let ((vals (apply f cars)))
881 (receive (cars2 cdrs2) (%cars+cdrs cdrs)
882 (if (null? cars2) vals
883 (appender vals (recur cars2 cdrs2))))))))
884
885 ;; Fast path
886 (if (null-ilist? ilis1) '()
887 (let recur ((elt (icar ilis1)) (rest (icdr ilis1)))
888 (let ((vals (f elt)))
889 (if (null-ilist? rest) vals
890 (appender vals (recur (icar rest) (icdr rest)))))))))
891
892
893 (define (ipair-for-each proc ilis1 . ilists)
894 (check-arg procedure? proc ipair-for-each)
895 (if (pair? ilists)
896
897 (let lp ((ilists (cons ilis1 ilists)))
898 (let ((itails (%cdrs ilists)))
899 (if (pair? itails)
900 (begin (apply proc ilists)
901 (lp itails)))))
902
903 ;; Fast path.
904 (let lp ((ilis ilis1))
905 (if (not (null-ilist? ilis))
906 (let ((tail (icdr ilis))) ; Grab the icdr now,
907 (proc ilis) ; even though nothing can happen
908 (lp tail))))))
909
910 ;;; We stop when LIS1 runs out, not when any ilist runs out.
911 ;;; Map F across L, and save up all the non-false results.
912 (define (ifilter-map f ilis1 . ilists)
913 (check-arg procedure? f ifilter-map)
914 (if (pair? ilists)
915 (let recur ((ilists (cons ilis1 ilists)))
916 (receive (cars cdrs) (%cars+cdrs ilists)
917 (if (pair? cars)
918 (cond ((apply f cars) => (lambda (x) (ipair x (recur cdrs))))
919 (else (recur cdrs))) ; Tail call in this arm.
920 '())))
921
922 ;; Fast path.
923 (let recur ((ilis ilis1))
924 (if (null-ilist? ilis) ilis
925 (let ((tail (recur (icdr ilis))))
926 (cond ((f (icar ilis)) => (lambda (x) (ipair x tail)))
927 (else tail)))))))
928
929
930 ;;; Map F across lists, guaranteeing to go left-to-right.
931
932 (define (imap-in-order f lis1 . lists)
933 (check-arg procedure? f imap-in-order)
934 (if (pair? lists)
935 (let recur ((lists (cons lis1 lists)))
936 (receive (cars cdrs) (%cars+cdrs lists)
937 (if (pair? cars)
938 (let ((x (apply f cars))) ; Do head first,
939 (ipair x (recur cdrs))) ; then tail.
940 '())))
941
942 ;; Fast path.
943 (let recur ((lis lis1))
944 (if (null-ilist? lis) lis
945 (let ((tail (icdr lis))
946 (x (f (icar lis)))) ; Do head ifirst,
947 (ipair x (recur tail))))))) ; then tail.
948
949
950 ;;; We extend IMAP to handle arguments of unequal ilength.
951 (define imap imap-in-order)
952
953
954 ;;; ifilter, iremove, ipartition
955 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
956 ;;; IFILTER, IREMOVE, IPARTITION do not
957 ;;; disorder the elements of their argument.
958
959 ;; This IFILTER shares the longest tail of L that has no deleted elements.
960 ;; If Scheme had multi-continuation calls, they could be made more efficient.
961
962 (define (ifilter pred lis) ; Sleazing with EQ? makes this
963 (check-arg procedure? pred ifilter) ; one faster.
964 (let recur ((lis lis))
965 (if (null-ilist? lis) lis ; Use NOT-IPAIR? to handle dotted lists.
966 (let ((head (icar lis))
967 (tail (icdr lis)))
968 (if (pred head)
969 (let ((new-tail (recur tail))) ; Replicate the RECUR call so
970 (if (eq? tail new-tail) lis
971 (ipair head new-tail)))
972 (recur tail)))))) ; this one can be a tail call.
973
974
975 ;;; Another version that shares longest tail.
976 ;(define (ifilter pred lis)
977 ; (receive (ans no-del?)
978 ; ;; (recur l) returns L with (pred x) values filtered.
979 ; ;; It also returns a flag NO-DEL? if the returned value
980 ; ;; is EQ? to L, i.e. if it didn't have to delete anything.
981 ; (let recur ((l l))
982 ; (if (null-ilist? l) (values l #t)
983 ; (let ((x (icar l))
984 ; (tl (icdr l)))
985 ; (if (pred x)
986 ; (receive (ans no-del?) (recur tl)
987 ; (if no-del?
988 ; (values l #t)
989 ; (values (ipair x ans) #f)))
990 ; (receive (ans no-del?) (recur tl) ; Delete X.
991 ; (values ans #f))))))
992 ; ans))
993
994
995
996 ;;; Answers share common tail with LIS where possible;
997 ;;; the technique is slightly subtle.
998
999 (define (ipartition pred lis)
1000 (check-arg procedure? pred ipartition)
1001 (let recur ((lis lis))
1002 (if (null-ilist? lis) (values lis lis) ; Use NOT-IPAIR? to handle dotted lists.
1003 (let ((elt (icar lis))
1004 (tail (icdr lis)))
1005 (receive (in out) (recur tail)
1006 (if (pred elt)
1007 (values (if (ipair? out) (ipair elt in) lis) out)
1008 (values in (if (ipair? in) (ipair elt out) lis))))))))
1009
1010
1011
1012
1013 ;;; Inline us, please.
1014 (define (iremove pred l) (ifilter (lambda (x) (not (pred x))) l))
1015
1016
1017
1018 ;;; Here's the taxonomy for the IDELETE/IASSOC/IMEMBER functions.
1019 ;;; (I don't actually think these are the world's most important
1020 ;;; functions -- the procedural IFILTER/IREMOVE/IFIND/IFIND-TAIL variants
1021 ;;; are far more general.)
1022 ;;;
1023 ;;; Function Action
1024 ;;; ---------------------------------------------------------------------------
1025 ;;; iremove pred lis Delete by general predicate
1026 ;;; idelete x lis [=] Delete by element comparison
1027 ;;;
1028 ;;; ifind pred lis Search by general predicate
1029 ;;; ifind-tail pred lis Search by general predicate
1030 ;;; imember x lis [=] Search by element comparison
1031 ;;;
1032 ;;; iassoc key lis [=] Search alist by key comparison
1033 ;;; ialist-delete key alist [=] Alist-idelete by key comparison
1034
1035 (define (idelete x lis . maybe-=)
1036 (let ((= (:optional maybe-= equal?)))
1037 (ifilter (lambda (y) (not (= x y))) lis)))
1038
1039 ;;; Extended from R4RS to take an optional comparison argument.
1040 (define (imember x lis . maybe-=)
1041 (let ((= (:optional maybe-= equal?)))
1042 (ifind-tail (lambda (y) (= x y)) lis)))
1043
1044 ;;; The IMEMBER and then IFIND-TAIL call should definitely
1045 ;;; be inlined for IMEMQ & IMEMV.
1046 (define (imemq x lis) (imember x lis eq?))
1047 (define (imemv x lis) (imember x lis eqv?))
1048
1049
1050 ;;; right-duplicate deletion
1051 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1052 ;;; idelete-duplicates
1053 ;;;
1054 ;;; Beware -- these are N^2 algorithms. To efficiently iremove duplicates
1055 ;;; in long lists, sort the ilist to bring duplicates together, then use a
1056 ;;; linear-time algorithm to kill the dups. Or use an algorithm based on
1057 ;;; element-marking. The former gives you O(n lg n), the latter is linear.
1058
1059 (define (idelete-duplicates lis . maybe-=)
1060 (let ((elt= (:optional maybe-= equal?)))
1061 (check-arg procedure? elt= idelete-duplicates)
1062 (let recur ((lis lis))
1063 (if (null-ilist? lis) lis
1064 (let* ((x (icar lis))
1065 (tail (icdr lis))
1066 (new-tail (recur (idelete x tail elt=))))
1067 (if (eq? tail new-tail) lis (ipair x new-tail)))))))
1068
1069 ;;; alist stuff
1070 ;;;;;;;;;;;;;;;
1071
1072 ;;; Extended from R4RS to itake an optional comparison argument.
1073 (define (iassoc x lis . maybe-=)
1074 (let ((= (:optional maybe-= equal?)))
1075 (ifind (lambda (entry) (= x (icar entry))) lis)))
1076
1077 (define (ialist-cons key datum alist) (ipair (ipair key datum) alist))
1078
1079 (define (alist-copy alist)
1080 (imap (lambda (elt) (ipair (icar elt) (icdr elt)))
1081 alist))
1082
1083 (define (ialist-delete key alist . maybe-=)
1084 (let ((= (:optional maybe-= equal?)))
1085 (ifilter (lambda (elt) (not (= key (icar elt)))) alist)))
1086
1087
1088 ;;; ifind ifind-tail itake-while idrop-while ispan ibreak iany ievery ilist-index
1089 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1090
1091 (define (ifind pred ilist)
1092 (cond ((ifind-tail pred ilist) => icar)
1093 (else #f)))
1094
1095 (define (ifind-tail pred ilist)
1096 (check-arg procedure? pred ifind-tail)
1097 (let lp ((ilist ilist))
1098 (and (not (null-ilist? ilist))
1099 (if (pred (icar ilist)) ilist
1100 (lp (icdr ilist))))))
1101
1102 (define (itake-while pred lis)
1103 (check-arg procedure? pred itake-while)
1104 (let recur ((lis lis))
1105 (if (null-ilist? lis) '()
1106 (let ((x (icar lis)))
1107 (if (pred x)
1108 (ipair x (recur (icdr lis)))
1109 '())))))
1110
1111 (define (idrop-while pred lis)
1112 (check-arg procedure? pred idrop-while)
1113 (let lp ((lis lis))
1114 (if (null-ilist? lis) '()
1115 (if (pred (icar lis))
1116 (lp (icdr lis))
1117 lis))))
1118
1119 (define (ispan pred lis)
1120 (check-arg procedure? pred ispan)
1121 (let recur ((lis lis))
1122 (if (null-ilist? lis) (values '() '())
1123 (let ((x (icar lis)))
1124 (if (pred x)
1125 (receive (prefix suffix) (recur (icdr lis))
1126 (values (ipair x prefix) suffix))
1127 (values '() lis))))))
1128
1129 (define (ibreak pred lis) (ispan (lambda (x) (not (pred x))) lis))
1130 (define (ievery pred lis1 . lists)
1131 (check-arg procedure? pred ievery)
1132 (if (pair? lists)
1133
1134 ;; N-ary case
1135 (receive (heads tails) (%cars+cdrs (ipair lis1 lists))
1136 (or (not (ipair? heads))
1137 (let lp ((heads heads) (tails tails))
1138 (receive (next-heads next-tails) (%cars+cdrs tails)
1139 (if (ipair? next-heads)
1140 (and (apply pred heads) (lp next-heads next-tails))
1141 (apply pred heads)))))) ; Last PRED app is tail call.
1142
1143 ;; Fast path
1144 (or (null-ilist? lis1)
1145 (let lp ((head (icar lis1)) (tail (icdr lis1)))
1146 (if (null-ilist? tail)
1147 (pred head) ; Last PRED app is tail call.
1148 (and (pred head) (lp (icar tail) (icdr tail))))))))
1149
1150 (define (iany pred ilis1 . ilists)
1151 (check-arg procedure? pred iany)
1152 (if (pair? ilists)
1153
1154 ;; N-ary case
1155 (receive (heads tails) (%cars+cdrs (cons ilis1 ilists))
1156 (and (pair? heads)
1157 (let lp ((heads heads) (tails tails))
1158 (receive (next-heads next-tails) (%cars+cdrs tails)
1159 (if (pair? next-heads)
1160 (or (apply pred heads) (lp next-heads next-tails))
1161 (apply pred heads)))))) ; Last PRED app is tail call.
1162
1163 ;; Fast path
1164 (and (not (null-ilist? ilis1))
1165 (let lp ((head (icar ilis1)) (tail (icdr ilis1)))
1166 (if (null-ilist? tail)
1167 (pred head) ; Last PRED app is tail call.
1168 (or (pred head) (lp (icar tail) (icdr tail))))))))
1169
1170 (define (ilist-index pred lis1 . lists)
1171 (check-arg procedure? pred ilist-index)
1172 (if (pair? lists)
1173
1174 ;; N-ary case
1175 (let lp ((lists (cons lis1 lists)) (n 0))
1176 (receive (heads tails) (%cars+cdrs lists)
1177 (and (pair? heads)
1178 (if (apply pred heads) n
1179 (lp tails (+ n 1))))))
1180
1181 ;; Fast path
1182 (let lp ((lis lis1) (n 0))
1183 (and (not (null-ilist? lis))
1184 (if (pred (icar lis)) n (lp (icdr lis) (+ n 1)))))))
1185
1186 ;;; Reverse
1187 ;;;;;;;;;;;
1188
1189 (define (ireverse lis) (ifold ipair '() lis)))