;;; WARNING: This document is currently somewhat out-of-date with respect
;;;  to recent discussions on the SRFI-1 mailing list. See
;;;     http://srfi.schemers.org/srfi-1/mail-archive/maillist.html
;;;  for the full archive. See
;;;     ftp://ftp.ai.mit.edu/pub/shivers/srfi/small-stuff.txt
;;;     ftp://ftp.ai.mit.edu/pub/shivers/srfi/issues.txt
;;;     ftp://ftp.ai.mit.edu/pub/shivers/srfi/closed-issues.txt
;;;  for a summary of the issues.
;;;     -Olin
;;;      99/6/26

;;; Scheme Underground list-processing library			-*- Scheme -*-
;;;
;;; Copyright (c) 1998 by Olin Shivers. You may do as you please with
;;; this code as long as you do not remove this copyright notice or
;;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;;;     -Olin

;;; SRFI DRAFT -- SRFI DRAFT -- SRFI DRAFT -- SRFI DRAFT -- SRFI DRAFT
;;; This is *draft* code for a SRFI proposal. If you see this notice in 
;;; production code, you've got obsolete, bad source -- go find the final 
;;; non-draft code on the Net.
;;; SRFI DRAFT -- SRFI DRAFT -- SRFI DRAFT -- SRFI DRAFT -- SRFI DRAFT

;;; This is a library of list- and pair-processing functions. I wrote it after
;;; carefully considering the functions provided by the libraries found in
;;; R4RS/R5RS Scheme, MIT Scheme, Gambit, RScheme, MzScheme, slib, Common
;;; Lisp, Bigloo, guile, T, APL and the SML standard basis. It is a pretty
;;; rich toolkit, providing a superset of the functionality found in any of
;;; the various Schemes I considered.

;;; This implementation is intended as a portable reference implementation
;;; of my list-lib package. See the porting notes below for more information.

;;; Exported:
;;; xcons tree-copy make-list list-tabulate cons* list-copy 
;;; proper-list? dotted-list? circular-list? not-pair? null-list? 
;;; circular-list length+
;;; iota
;;; first second third fourth fifth sixth seventh eighth ninth tenth
;;; car+cdr
;;; take       drop       
;;; take-right drop-right 
;;; take!      drop-right!
;;; last last-pair
;;; zip unzip1 unzip2 unzip3 unzip4 unzip5
;;; append! append-reverse append-reverse!
;;; unfold unfold/tail fold fold-right pair-fold pair-fold-right reduce reduce-right
;;; append-map append-map! map! pair-for-each filter-map map-in-order
;;; filter  partition  remove
;;; filter! partition! remove! 
;;; find find-tail any every list-index
;;; del  delq  delv  delete 
;;; del! delq! delv! delete!
;;; mem ass alist-cons alist-copy
;;; delq-duplicates  delv-duplicates  delete-duplicates  del-duplicates 
;;; delq-duplicates! delv-duplicates! delete-duplicates! del-duplicates!
;;; alist-delete  del-ass  del-assq  del-assv  del-assoc
;;; alist-delete! del-ass! del-assq! del-assv! del-assoc!
;;; reverse! 
;;; 
;;; In principle, the following R4RS list- and pair-processing procedures
;;; are also part of this package's exports, although they are not defined
;;; in this file:
;;;   cons pair? null? list? list length append reverse
;;;   car cdr ... cdddar cddddr set-car! set-cdr! list-ref
;;;   member memq memv assoc assq assv
;;;   map for-each
;;; The remaining R4RS list-processing procedure is not included: 
;;;   list-tail (use drop)

;;; A note on recursion and iteration/reversal:
;;; Many iterative list-processing algorithms naturally compute the elements
;;; of the answer list in the wrong order (left-to-right or head-to-tail) from
;;; the order needed to cons them into the proper answer (right-to-left, or
;;; tail-then-head). One style or idiom of programming these algorithms, then,
;;; loops, consing up the elements in reverse order, then destructively 
;;; reverses the list at the end of the loop. I do not do this. The natural
;;; and efficient way to code these algorithms is recursively. This trades off
;;; intermediate temporary list structure for intermediate temporary stack
;;; structure. In a stack-based system, this improves cache locality and
;;; lightens the load on the GC system. Don't stand on your head to iterate!
;;; Recurse, where natural. Multiple-value returns make this even more
;;; convenient, when the recursion/iteration has multiple state values.

;;; Porting:
;;; This is carefully tuned code; do not modify casually.
;;;   - It is careful to share storage when possible;
;;;   - Side-effecting code tries not to perform redundant writes.
;;; That said, a port of this library to a specific Scheme system might wish
;;; to tune this code to exploit particulars of the implementation. In
;;; particular, the n-ary mapping functions are particularly slow and
;;; cons-intensive, and are good candidates for tuning. I have coded fast
;;; paths for the single-list cases, but what you really want to do is exploit
;;; the fact that the compiler usually knows how many arguments are being 
;;; passed to a particular application of these functions -- they are usually
;;; explicitly called, not passed around as higher-order values. If you can 
;;; arrange to have your compiler produce custom code or custom linkages based
;;; on the number of arguments in the call, you can speed these functions up 
;;; a lot. But this kind of compiler technology no longer exists in the Scheme
;;; world as far as I can see.
;;;
;;; Note that this code is, of course, dependent upon standard bindings for
;;; the R5RS procedures -- i.e., it assumes that the variable CAR is bound
;;; to the procedure that takes the car of a list. If your Scheme 
;;; implementation allows user code to alter the bindings of these procedures
;;; in a manner that would be visible to these definitions, then there might
;;; be trouble. You could consider horrible kludgery along the lines of
;;;    (define fact 
;;;      (let ((= =) (- -) (* *))
;;;        (letrec ((real-fact (lambda (n) 
;;;                              (if (= n 0) 1 (* n (real-fact (- n 1)))))))
;;;          real-fact)))
;;; Or you could consider shifting to a reasonable Scheme system that, say,
;;; has a module system protecting code from this kind of lossage.
;;;
;;; This code does a fair amount of run-time argument checking. If your
;;; Scheme system has a sophisticated compiler that can eliminate redundant
;;; error checks, this is no problem. However, if not, these checks incur
;;; some performance overhead -- and, in a safe Scheme implementation, they
;;; are in some sense redundant: if we don't check to see that the PROC 
;;; parameter is a procedure, we'll find out anyway three lines later when
;;; we try to call the value. It's pretty easy to rip all this argument 
;;; checking code out if it's inappropriate for your implementation -- just
;;; nuke every call to CHECK-ARG.
;;;
;;; On the other hand, if you *do* have a sophisticated compiler that will
;;; actually perform soft-typing and eliminate redundant checks (Rice being
;;; the only possible candidate of which I'm aware), leaving these checks 
;;; in can *help*, since their presence can be elided in redundant cases,
;;; and in cases where they are needed, performing the checks early, at
;;; procedure entry, can "lift" a check out of a loop. 
;;;
;;; Finally, I have only checked the properties that can portably be checked
;;; with R5RS Scheme -- and this is not complete. You may wish to alter
;;; the CHECK-ARG parameter checks to perform extra, implementation-specific
;;; checks, such as procedure arity for higher-order values.
;;;
;;; The code has only three non-R4RS dependencies:
;;;   A few calls to an ERROR procedure;
;;;   Uses of the R5RS multiple-value procedure VALUES and the m-v binding
;;;     RECEIVE macro (which isn't R5RS, but is a trivial macro).
;;;   Many calls to a parameter-checking procedure check-arg:
;;;    (define (check-arg pred val caller)
;;;      (let lp ((val val))
;;;        (if (pred val) val (lp (error "Bad argument" val pred caller)))))

;;; Constructors
;;;;;;;;;;;;;;;;

;;; Occasionally useful as a value to be passed to a fold or other
;;; higher-order procedure.
(define (xcons d a) (cons a d))

;;; Recursively copy every cons.
(define (tree-copy x)
  (let recur ((x x))
    (if (not (pair? x)) x
	(cons (recur (car x)) (recur (cdr x))))))

;;; Make a list of length LEN.

(define (make-list len . maybe-elt)
  (check-arg (lambda (n) (and (integer? n) (>= n 0))) len make-list)
  (let ((elt (cond ((null? maybe-elt) #f) ; Default value
		   ((null? (cdr maybe-elt)) (car maybe-elt))
		   (else (error "Too many arguments to MAKE-LIST"
				(cons len maybe-elt))))))
    (do ((i len (- i 1))
	 (ans '() (cons elt ans)))
	((<= i 0) ans))))

;;; Make a list of length LEN. Elt i is (PROC i) for 0 <= i < LEN.

(define (list-tabulate len proc)
  (check-arg (lambda (n) (and (integer? n) (>= n 0))) len list-tabulate)
  (check-arg procedure? proc list-tabulate)
  (do ((i (- len 1) (- i 1))
       (ans '() (cons (proc i) ans)))
      ((< i 0) ans)))

;;; (cons* a1 a2 ... an) = (cons a1 (cons a2 (cons ... an)))
;;; (cons* a1) = a1	(cons* a1 a2 ...) = (cons a1 (cons* a2 ...))
;;;
;;; (cons first (unfold/tail not-pair? car cdr values rest))

(define (cons* first . rest)
  (let recur ((x first) (rest rest))
    (if (pair? rest)
	(cons x (recur (car rest) (cdr rest)))
	x)))

;;; (unfold/tail not-pair? car cdr values lis)

(define (list-copy lis)				
  (let recur ((lis lis))			
    (if (pair? lis)				
	(cons (car lis) (recur (cdr lis)))	
	lis)))					

;;; IOTA count [start step]	(start start+step ... start+(count-1)*step)

(define (iota count . maybe-start+step)
  (check-arg integer? count iota)
  (if (< count 0) (error "Negative step count" iota count))
  (let-optionals maybe-start+step ((start 0) (step 1))
    (check-arg number? start iota)
    (check-arg number? step iota)
    (let ((last-val (+ start (* (- count 1) step))))
      (do ((count count (- count 1))
	   (val last-val (- val step))
	   (ans '() (cons val ans)))
	  ((<= count 0)  ans)))))
	  
;;; I thought these were lovely, but the public at large did not share my
;;; enthusiasm...
;;; :IOTA to		(0 ... to-1)
;;; :IOTA from to	(from ... to-1)
;;; :IOTA from to step  (from from+step ...)

;;; IOTA: to		(1 ... to)
;;; IOTA: from to	(from+1 ... to)
;;; IOTA: from to step	(from+step from+2step ...)

;(define (%parse-iota-args arg1 rest-args proc)
;  (let ((check (lambda (n) (check-arg integer? n proc))))
;    (check arg1)
;    (if (pair? rest-args)
;	(let ((arg2 (check (car rest-args)))
;	      (rest (cdr rest-args)))
;	  (if (pair? rest)
;	      (let ((arg3 (check (car rest)))
;		    (rest (cdr rest)))
;		(if (pair? rest) (error "Too many parameters" proc arg1 rest-args)
;		    (values arg1 arg2 arg3)))
;	      (values arg1 arg2 1)))
;	(values 0 arg1 1))))
;
;(define (iota: arg1 . rest-args)
;  (receive (from to step) (%parse-iota-args arg1 rest-args iota:)
;    (let* ((numsteps (floor (/ (- to from) step)))
;	   (last-val (+ from (* step numsteps))))
;      (if (< numsteps 0) (error "Negative step count" iota: from to step))
;      (do ((steps-left numsteps (- steps-left 1))
;	   (val last-val (- val step))
;	   (ans '() (cons val ans)))
;	  ((<= steps-left 0) ans)))))
;
;
;(define (:iota arg1 . rest-args)
;  (receive (from to step) (%parse-iota-args arg1 rest-args :iota)
;    (let* ((numsteps (ceiling (/ (- to from) step)))
;	   (last-val (+ from (* step (- numsteps 1)))))
;      (if (< numsteps 0) (error "Negative step count" :iota from to step))
;      (do ((steps-left numsteps (- steps-left 1))
;	   (val last-val (- val step))
;	   (ans '() (cons val ans)))
;	  ((<= steps-left 0) ans)))))



(define (circular-list val1 . vals)
  (let ((ans (cons val1 vals)))
    (set-cdr! (last-pair ans) ans)
    ans))

;;; <proper-list> ::= ()			; Empty proper list
;;;		  |   (cons <x> <proper-list>)	; Proper-list pair
;;; Note that this definition rules out circular lists -- and this
;;; function is required to detect this case and return false.

(define (proper-list? x)
  (let lp ((x x) (lag x))
    (if (pair? x)
	(let ((x (cdr x)))
	  (if (pair? x)
	      (let ((x   (cdr x))
		    (lag (cdr lag)))
		(and (not (eq? x lag)) (lp x lag)))
	      (null? x)))
	(null? x))))


;;; A dotted list is a finite list (possibly of length 0) terminated
;;; by a non-nil value. Any non-cons, non-nil value (e.g., "foo" or 5)
;;; is a dotted list of length 0.
;;;
;;; <dotted-list> ::= <non-nil,non-pair>	; Empty dotted list
;;;               |   (cons <x> <dotted-list>)	; Proper-list pair

(define (dotted-list? x)
  (let lp ((x x) (lag x))
    (if (pair? x)
	(let ((x (cdr x)))
	  (if (pair? x)
	      (let ((x   (cdr x))
		    (lag (cdr lag)))
		(and (not (eq? x lag)) (lp x lag)))
	      (not (null? x))))
	(not (null? x)))))

(define (circular-list? x)
  (let lp ((x x) (lag x))
    (and (pair? x)
	 (let ((x (cdr x)))
	   (and (pair? x)
		(let ((x   (cdr x))
		      (lag (cdr lag)))
		  (or (eq? x lag) (lp x lag))))))))

(define (not-pair? x) (not (pair? x)))	; Inline me.

;;; R4RS, so commented out.
;(define (length x)			; LENGTH may diverge or
;  (let lp ((x x) (len 0))		; raise an error if X is
;    (if (pair? x)			; a circular list. This version
;        (lp (cdr x) (+ len 1))		; diverges.
;        len)))

(define (length+ x)			; Returns #f if X is circular.
  (let lp ((x x) (lag x) (len 0))
    (if (pair? x)
	(let ((x (cdr x))
	      (len (+ len 1)))
	  (if (pair? x)
	      (let ((x   (cdr x))
		    (lag (cdr lag))
		    (len (+ len 1)))
		(and (not (eq? x lag)) (lp x lag len)))
	      len))
	len)))

(define (zip list1 . more-lists) (apply map list list1 more-lists))


;;; Selectors
;;;;;;;;;;;;;

(define first  car)
(define second cadr)
(define third  caddr)
(define fourth cadddr)
(define (fifth   x) (car    (cddddr x)))
(define (sixth   x) (cadr   (cddddr x)))
(define (seventh x) (caddr  (cddddr x)))
(define (eighth  x) (cadddr (cddddr x)))
(define (ninth   x) (car  (cddddr (cddddr x))))
(define (tenth   x) (cadr (cddddr (cddddr x))))

(define (car+cdr pair) (values (car pair) (cdr pair)))

;;; take & drop

(define (take lis k)
  (check-arg integer? k take)
  (let recur ((lis lis) (k k))
    (if (zero? k) '()
	(cons (car lis)
	      (recur (cdr lis) (- k 1))))))

(define (drop lis k)
  (check-arg integer? k drop)
  (let iter ((lis lis) (k k))
    (if (zero? k) lis (iter (cdr lis) (- k 1)))))

(define (take! lis k)
  (check-arg integer? k take!)
  (if (zero? k) '()
      (begin (set-cdr! (drop lis (- k 1)) '())
	     lis)))

(define (take-right lis k)  (drop  lis (- (length lis) k)))
(define (drop-right lis k)  (take  lis (- (length lis) k)))
(define (drop-right! lis k) (take! lis (- (length lis) k)))


;;; These use the APL convention, whereby negative indices mean 
;;; "from the right." I liked them, but they didn't win over the
;;; SRFI reviewers.
;;; K >= 0: Take and drop  K elts from the front of the list.
;;; K <= 0: Take and drop -K elts from the end   of the list.

;(define (take lis k)
;  (check-arg integer? k take)
;  (if (negative? k)
;      (list-tail lis (+ k (length lis)))
;      (let recur ((lis lis) (k k))
;	(if (zero? k) '()
;	    (cons (car lis)
;		  (recur (cdr lis) (- k 1)))))))
;
;(define (drop lis k)
;  (check-arg integer? k drop)
;  (if (negative? k)
;      (let recur ((lis lis) (nelts (+ k (length lis))))
;	(if (zero? nelts) '()
;	    (cons (car lis)
;		  (recur (cdr lis) (- nelts 1)))))
;      (list-tail lis k)))
;
;
;(define (take! lis k)
;  (check-arg integer? k take!)
;  (cond ((zero? k) '())
;	((positive? k)
;	 (set-cdr! (list-tail lis (- k 1)) '())
;	 lis)
;	(else (list-tail lis (+ k (length lis))))))
;
;(define (drop! lis k)
;  (check-arg integer? k drop!)
;  (if (negative? k)
;      (let ((nelts (+ k (length lis))))
;	(if (zero? nelts) '()
;	    (begin (set-cdr! (list-tail lis (- nelts 1)) '())
;		   lis)))
;      (list-tail lis k)))


(define (last lis) (car (last-pair lis)))

(define (last-pair lis)
  (check-arg pair? lis last-pair)
  (let lp ((lis lis))
    (let ((tail (cdr lis)))
      (if (pair? tail) (lp tail) lis))))


;;; Unzippers -- 1 through 5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (unzip1 lis) (map car lis))

(define (unzip2 lis)
  (let recur ((lis lis))
    (if (pair? lis)
	(let ((elt (car lis)))
	  (receive (a b) (recur (cdr lis))
	    (values (cons (car  elt) a)
		    (cons (cadr elt) b))))
	(values lis lis))))

(define (unzip3 lis)
  (let recur ((lis lis))
    (if (pair? lis)
	(let ((elt (car lis)))
	  (receive (a b c) (recur (cdr lis))
	    (values (cons (car   elt) a)
		    (cons (cadr  elt) b)
		    (cons (caddr elt) c))))
	(values lis lis lis))))

(define (unzip4 lis)
  (let recur ((lis lis))
    (if (pair? lis)
	(let ((elt (car lis)))
	  (receive (a b c d) (recur (cdr lis))
	    (values (cons (car    elt) a)
		    (cons (cadr   elt) b)
		    (cons (caddr  elt) c)
		    (cons (cadddr elt) d))))
	(values lis lis lis lis))))

(define (unzip5 lis)
  (let recur ((lis lis))
    (if (pair? lis)
	(let ((elt (car lis)))
	  (receive (a b c d e) (recur (cdr lis))
	    (values (cons (car     elt) a)
		    (cons (cadr    elt) b)
		    (cons (caddr   elt) c)
		    (cons (cadddr  elt) d)
		    (cons (car (cddddr  elt)) e))))
	(values lis lis lis lis lis))))



;;; append! append append-reverse append-reverse!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (append! . lists)
  ;; First, scan through lists looking for a non-empty one.
  (let lp ((lists lists) (prev '()))
    (if (not (pair? lists)) prev
	(let ((first (car lists))
	      (rest (cdr lists)))
	  (if (not (pair? first)) (lp rest first)

	      ;; Now, do the splicing.
	      (let lp2 ((tail-cons (last-pair first))
			(rest rest))
		(if (pair? rest)
		    (let ((next (car rest))
			  (rest (cdr rest)))
		      (set-cdr! tail-cons next)
		      (lp2 (if (pair? next) (last-pair next) tail-cons)
			   rest))
		    first)))))))

;;; APPEND is R4RS, but this one works with improper lists.
(define (append . lists)
  (if (pair? lists)
      (let recur ((list1 (car lists)) (lists (cdr lists)))
	(if (pair? lists)
	    (let ((tail (recur (car lists) (cdr lists))))
	      (fold-right cons tail list1)) ; Append LIST1 & TAIL.
	    list1))
      '()))

(define (append-reverse rev-head tail) (fold cons tail rev-head))

(define (append-reverse! rev-head tail)
  (pair-fold (lambda (pair tail) (set-cdr! pair tail) pair)
	     tail
	     rev-head))


;;; Fold/map internal utilities
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; These little internal utilities are used by the general
;;; fold & mapper funs. It'd be nice if they got inlined.

(define (%cars lists)	; (map car lists)
  (let recur ((lists lists))
    (if (pair? lists) (cons (caar lists) (recur (cdr lists))) '())))

(define (%cdrs lists)	; (map cdr lists)
  (let recur ((lists lists))
    (if (pair? lists) (cons (cdar lists) (recur (cdr lists))) '())))

(define (%cars+ lists last-elt)	; (append! (map car lists) (list last-elt))
  (let recur ((lists lists))
    (if (pair? lists) (cons (caar lists) (recur (cdr lists))) (list last-elt))))

;;; Can't be defined as (EVERY PAIR? LISTS), because EVERY uses it!
(define (%all-pairs? lists)	
  (let lp ((lists lists))
    (or (not (pair? lists)) (and (pair? (car lists)) (lp (cdr lists))))))

;;; LISTS is a (not very long) non-empty list. Return three values:
;;; - a list of the cars of LISTS' elts, or '() if LIST contains a non-pair.
;;; - a list of the cdrs of LISTS' elts, or '() if LIST contains a non-pair.
;;; - if any element of LISTS is not a pair, the leftmost non-pair. Otw,
;;;   undefined.
;;; I could do a faster version of this if I could assume throwing to 
;;; a local CALL/CC compiled into a simple jump.

(define (%cars+cdrs+terminator lists)
  ;; Scan LISTS looking for a non-pair.
  (let lp ((ls lists))
    (if (pair? ls)
	(receive (list1 rest) (car+cdr ls)
	  (if (pair? list1)
	      (lp rest)
	      (values '() '() list1))) ; Found a non-pair; quit.

	;; LISTS is all pairs. Pick out the cars & cdrs.
	(receive (cars cdrs) (let recur ((lists lists))
			       (if (pair? lists)
				   (receive (a d) (car+cdr (car lists))
				     (receive (cars cdrs) (recur (cdr lists))
				       (values (cons a cars) (cons d cdrs))))
				   (values '() '())))
	  (values cars cdrs #f)))))


;;; fold/unfold
;;;;;;;;;;;;;;;

(define (unfold/tail p f g e seed)
  (check-arg procedure? p unfold/tail)
  (check-arg procedure? f unfold/tail)
  (check-arg procedure? g unfold/tail)
  (check-arg procedure? e unfold/tail)
  (let recur ((seed seed))
    (if (p seed) (e seed)
	(cons (f seed) (recur (g seed))))))

(define (unfold p f g seed)
  (check-arg procedure? p unfold)
  (check-arg procedure? f unfold)
  (check-arg procedure? g unfold)
  (let recur ((seed seed))
    (if (p seed) '()
	(cons (f seed) (recur (g seed))))))

(define (fold kons knil lis1 . lists)
  (check-arg procedure? kons fold)
  (if (pair? lists)
      (let lp ((lists (cons lis1 lists)) (ans knil))	; N-ary case
	(if (%all-pairs? lists)
	    (lp (%cdrs lists)
		(apply kons (%cars+ lists ans)))
	    ans))
	    
      (let lp ((lis lis1) (ans knil))			; Fast path
	(if (pair? lis)
	    (lp (cdr lis) (kons (car lis) ans))
	    ans))))


(define (fold-right kons knil lis1 . lists)
  (check-arg procedure? kons fold-right)
  (if (pair? lists)
      (let recur ((lists (cons lis1 lists)))		; N-ary case
	(if (%all-pairs? lists)
	    (apply kons (%cars+ lists (recur (%cdrs lists))))
	    knil))

      (let recur ((lis lis1))				; Fast path
	(if (pair? lis)
	    (let ((head (car lis)))
	      (kons head (recur (cdr lis))))
	    knil))))


(define (pair-fold-right f zero lis1 . lists)
  (check-arg procedure? f pair-fold-right)
  (if (pair? lists)
      (let recur ((lists (cons lis1 lists)))		; N-ary case
	(if (%all-pairs? lists)
	    (apply f (append! lists (list (recur (%cdrs lists)))))
	    zero))

      (let recur ((lis lis1))				; Fast path
	(if (pair? lis) (f lis (recur (cdr lis))) zero))))

(define (pair-fold f zero lis1 . lists)
  (check-arg procedure? f pair-fold)
  (if (pair? lists)
      (let lp ((lists (cons lis1 lists)) (ans zero))	; N-ary case
	(if (%all-pairs? lists)
	    (let ((tails (%cdrs lists)))
	      (lp tails (apply f (append! lists (list ans)))))
	    ans))

      (let lp ((lis lis1) (ans zero))
	(if (pair? lis)
	    (let ((tail (cdr lis)))	; Grab the cdr now,
	      (lp tail (f lis ans)))	; in case F SET-CDR!s LIS.
	    ans))))
      

;;; REDUCE and REDUCE-RIGHT only use RIDENTITY in the empty-list case.
;;; These cannot meaningfully be n-ary.

(define (reduce f ridentity lis)
  (check-arg procedure? f reduce)
  (if (pair? lis)
      (fold f (car lis) (cdr lis))
      ridentity))

(define (reduce-right f ridentity lis)
  (check-arg procedure? f reduce-right)
  (if (pair? lis)
      (let recur ((head (car lis)) (lis (cdr lis)))
	(if (pair? lis)
	    (f head (recur (car lis) (cdr lis)))
	    head))
      ridentity))



;;; Mappers: append-map append-map! pair-for-each map! filter-map map-in-order
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (append-map f lis1 lists)
  (really-append-map append-map  append  f lis1 lists))
(define (append-map! f lis1 lists) 
  (really-append-map append-map! append! f lis1 lists))

(define (really-append-map who appender f lis1 lists)
  (check-arg procedure? f who)
  (if (pair? lists)
      (let ((lists (cons lis1 lists)))
	(if (%all-pairs? lists)
	    (let recur ((first-args (%cars lists))
			(rest-args (%cdrs lists)))
	      (let ((vals (apply f first-args)))
		(if (%all-pairs? rest-args)
		    (appender (apply f (%cars lists)) (recur (%cdrs lists)))
		    vals)))
	    '()))

      ;; Fast path
      (if (pair? lis1)
	  (let recur ((elt (car lis1)) (rest (cdr lis1)))
	    (if (pair? rest)
		(appender (f elt) (recur (car rest) (cdr rest)))
		(f elt)))
	  '())))


(define (pair-for-each f lis1 . lists)
  (check-arg procedure? f pair-for-each)
  (if (pair? lists)

      (let lp ((lists (cons lis1 lists)))
	(if (%all-pairs? lists)
	    (let ((tails (%cdrs lists)))	; Grab the cdrs now,
	      (apply f lists)			; in case F SET-CDR!s its args.
	      (lp tails))))

      ;; Fast path.
      (let lp ((lis lis1))
	(if (pair? lis) (let ((tail (cdr lis)))	; Grab the cdr now,
			  (f lis)		; in case F SET-CDR!s LIS.
			  (lp tail))))))

;;; We stop when LIS1 runs out, not when any list runs out.
(define (map! f lis1 . lists)
  (check-arg procedure? f map!)
  (if (pair? lists)
      (let lp ((lis1 lis1) (lists lists))
	(if (pair? lis1)
	    (let ((tail1 (cdr lis1))
		  (tails (%cdrs lists)))
	      (set-car! lis1 (apply f (car lis1) (%cars lists)))
	      (lp tail1 tails))))

      ;; Fast path.
      (pair-for-each (lambda (pair) (set-car! pair (f (car pair)))) lis1))
  lis1)


;;; Map F across L, and save up all the non-false results.
(define (filter-map f lis1 . lists)
  (check-arg procedure? f filter-map)
  (if (pair? lists)
      (let recur ((lists (cons lis1 lists)))
	(receive (cars cdrs terminator) (%cars+cdrs+terminator lists)
	  (if (pair? cars)
	      (cond ((apply f cars) => (lambda (x) (cons x (recur cdrs))))
		    (else (recur cdrs)))
	      terminator)))
	    
      ;; Fast path.
      (let recur ((lis lis))
	(if (pair? lis)
	    (let ((tail (recur (cdr lis))))
	      (cond ((f (car lis)) => (lambda (x) (cons x tail)))
		    (else tail)))
	    lis))


;;; Map F across lists, guaranteeing to go left-to-right.
;;; NOTE: Some implementations of R5RS MAP are compliant with this spec;
;;; in which case this procedure may simply be defined as a synonym for MAP.

(define (map-in-order f lis1 . lists)
  (check-arg procedure? f map-in-order)
  (if (pair? lists)
      (let recur ((lists (cons lis1 lists)))
	(receive (cars cdrs terminator) (%cars+cdrs+terminator lists)
	  (if (pair? cars)
	      (let ((x (apply f cars)))	; Do head first,
		(cons x (recur cdrs)))	; then tail.
	      terminator)))
	    
      ;; Fast path.
      (let recur ((lis lis1))
	(if (pair? lis)
	    (let* ((tail (cdr lis))
		   (x (f (car lis))))	; Do head first,
	      (cons x (recur tail)))	; then tail.
	    lis))))

;;; R4RS, but this definition is guaranteed to obey list-lib's spec on
;;; improper lists.
(define map map-in-order)	


;;; filter, remove, partition
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; FILTER, REMOVE, PARTITION and their destructive counterparts do not
;;; disorder the elements of their argument.

;; This FILTER shares the longest tail of L that has no deleted elements.
;; If Scheme had multi-continuation calls, they could be made more efficient.

(define (filter pred lis)			; Sleazing with EQ? makes this
  (check-arg procedure? pred filter)		; one faster.
  (let recur ((lis lis))		
    (if (pair? lis)
	(let ((head (car lis))
	      (tail (cdr lis)))
	  (if (pred head)
	      (let ((new-tail (recur tail)))	; Replicate the RECUR call so
		(if (eq? tail new-tail) lis
		    (cons head new-tail)))
	      (recur tail)))			; this one can be a tail call.
	lis)))

;;; Another version that shares longest tail.
;(define (filter pred lis)
;  (receive (ans no-del?)
;      ;; (recur l) returns L with (pred x) values filtered.
;      ;; It also returns a flag NO-DEL? if the returned value
;      ;; is EQ? to L, i.e. if it didn't have to delete anything.
;      (let recur ((l l))
;	(if (not (pair? l)) (values l #t)
;	    (let ((x  (car l))
;		  (tl (cdr l)))
;	      (if (pred x)
;		  (receive (ans no-del?) (recur tl)
;		    (if no-del?
;			(values l #t)
;			(values (cons x ans) #f)))
;		  (receive (ans no-del?) (recur tl) ; Delete X.
;		    (values ans #f))))))
;    ans))



;(define (filter! pred lis)			; Things are much simpler
;  (let recur ((lis lis))			; if you are willing to
;    (if (pair? lis)				; push N stack frames & do N
;        (cond ((pred (car lis))		; SET-CDR! writes, where N is
;               (set-cdr! lis (recur (cdr lis))); the length of the answer.
;               lis)				
;              (else (recur (cdr lis))))
;        lis)))


;;; This implementation of FILTER!
;;; - doesn't cons, and uses no stack;
;;; - is careful not to do redundant SET-CDR! writes, as writes to memory are 
;;;   usually expensive on modern machines, and can be extremely expensive on 
;;;   modern Schemes (e.g., ones that have generational GC's).
;;; It just zips down contiguous runs of in and out elts in LIS doing the 
;;; minimal number of SET-CDR!s to splice the tail of one run of ins to the 
;;; beginning of the next.

(define (filter! pred lis)
  (check-arg procedure? pred filter!)
  (let lp ((ans lis))
    (cond ((not (pair? ans))      ans)			; Scan looking for
	  ((not (pred (car ans))) (lp (cdr ans)))	; first cons of result.

	  ;; ANS is the eventual answer.
	  ;; SCAN-IN: (CDR PREV) = LIS and (CAR PREV) satisfies PRED.
	  ;;          Scan over a contiguous segment of the list that
	  ;;          satisfies PRED.
	  ;; SCAN-OUT: (CAR PREV) satisfies PRED. Scan over a contiguous
	  ;;           segment of the list that *doesn't* satisfy PRED.
	  ;;           When the segment ends, patch in a link from PREV
	  ;;           to the start of the next good segment, and jump to
	  ;;           SCAN-IN.
	  (else (letrec ((scan-in (lambda (prev lis)
				    (if (pair? lis)
					(if (pred (car lis))
					    (scan-in lis (cdr lis))
					    (scan-out prev (cdr lis))))))
			 (scan-out (lambda (prev lis)
				     (let lp ((lis lis))
				       (if (pair? lis)
					   (if (pred (car lis))
					       (begin (set-cdr! prev lis)
						      (scan-in lis (cdr lis)))
					       (lp (cdr lis)))
					   (set-cdr! prev lis))))))
		  (scan-in ans (cdr ans))
		  ans)))))



;;; Answers share common tail with LIS where possible; 
;;; the technique is slightly subtle.

(define (partition pred lis)
  (check-arg procedure? pred partition)
  (let recur ((lis lis))
    (if (not (pair? lis)) (values lis lis)
	(let ((elt (car lis))
	      (tail (cdr lis)))
	  (receive (in out) (recur tail)
	    (if (pred elt)
		(values (if (pair? out) (cons elt in) lis) out)
		(values in (if (pair? in) (cons elt out) lis))))))))



;(define (partition! pred lis)			; Things are much simpler
;  (let recur ((lis lis))			; if you are willing to
;    (if (not (pair? lis)) (values lis lis)	; push N stack frames & do N
;        (let ((elt (car lis)))			; SET-CDR! writes, where N is
;          (receive (in out) (recur (cdr lis))	; the length of LIS.
;            (cond ((pred elt)
;                   (set-cdr! lis in)
;                   (values lis out))
;                  (else (set-cdr! lis out)
;                        (values in lis))))))))


;;; This implementation of PARTITION!
;;; - doesn't cons, and uses no stack;
;;; - is careful not to do redundant SET-CDR! writes, as writes to memory are
;;;   usually expensive on modern machines, and can be extremely expensive on 
;;;   modern Schemes (e.g., ones that have generational GC's).
;;; It just zips down contiguous runs of in and out elts in LIS doing the
;;; minimal number of SET-CDR!s to splice these runs together into the result 
;;; lists.

(define (partition! pred lis)
  (check-arg procedure? pred partition!)
  (if (not (pair? lis)) (values lis lis)

      ;; This pair of loops zips down contiguous in & out runs of the
      ;; list, splicing the runs together. The invariants are
      ;;   SCAN-IN:  (cdr in-prev)  = LIS.
      ;;   SCAN-OUT: (cdr out-prev) = LIS.
      (letrec ((scan-in (lambda (in-prev out-prev lis)
			  (let lp ((in-prev in-prev) (lis lis))
			    (if (pair? lis)
				(if (pred (car lis))
				    (lp lis (cdr lis))
				    (begin (set-cdr! out-prev lis)
					   (scan-out in-prev lis (cdr lis))))
				(set-cdr! out-prev lis))))) ; Done.

	       (scan-out (lambda (in-prev out-prev lis)
			   (let lp ((out-prev out-prev) (lis lis))
			     (if (pair? lis)
				 (if (pred (car lis))
				     (begin (set-cdr! in-prev lis)
					    (scan-in lis out-prev (cdr lis)))
				     (lp lis (cdr lis)))
				 (set-cdr! in-prev lis)))))) ; Done.

	;; Crank up the scan&splice loops.
	(if (pred (car lis))
	    ;; LIS begins in-list. Search for out-list's first pair.
	    (let lp ((prev-l lis) (l (cdr lis)))
	      (cond ((not (pair? l)) (values lis l))
		    ((pred (car l)) (lp l (cdr l)))
		    (else (scan-out prev-l l (cdr l))
			  (values lis l))))	; Done.

	    ;; LIS begins out-list. Search for in-list's first pair.
	    (let lp ((prev-l lis) (l (cdr lis)))
	      (cond ((not (pair? l)) (values l lis))
		    ((pred (car l))
		     (scan-in l prev-l (cdr l))
		     (values l lis))		; Done.
		    (else (lp l (cdr l)))))))))


;;; Inline us, please.
(define (remove  pred l) (filter  (lambda (x) (not (pred x))) l))
(define (remove! pred l) (filter! (lambda (x) (not (pred x))) l))



;;; Here's the taxonomy for the DELETE/ASSOC/MEMBER functions.
;;; (I don't actually think these are the world's most important
;;; functions -- the procedural FILTER/REMOVE/FIND/FIND-TAIL variants
;;; are far more general.)
;;;
;;; Pure		linear-update	     Action
;;; ---------------------------------------------------------------------------
;;; remove pred lis	remove! pred lis     Delete by general predicate
;;; del  = x lis	del!  = x lis	     Delete by general comparison
;;; delq   x lis	delq!   x lis	     Delete by EQ?    comparison
;;; delv   x lis	delv!   x lis	     Delete by EQV?   comparison
;;; delete x lis	delete! x lis	     Delete by EQUAL? comparison
;;;					     
;;; find-tail pred lis			     Search by general predicate
;;; mem  = x lis			     Search by general comparison
;;; memq   x lis			     Search by EQ?    comparison
;;; memv   x lis			     Search by EQV?   comparison
;;; member x lis			     Search by EQUAL? comparison
;;;			   		     
;;; find pred lis			     Search alist by general predicate
;;; ass = x lis				     Search alist by general comparison
;;; assq  x lis				     Search alist by EQ?    comparison
;;; assv  x lis				     Search alist by EQV?   comparison
;;; assoc x lis				     Search alist by EQUAL? comparison
;;;					     
;;; remove pred alist	remove! pred alist   Alist-delete by general predicate
;;; del-ass = x alist	del-ass! = x alist   Alist-delete by general comparison
;;; del-assq  x alist	del-assq!  x alist   Alist-delete by EQ?    comparison
;;; del-assv  x alist	del-assv!  x alist   Alist-delete by EQV?   comparison
;;; del-assoc x alist	del-assoc! x alist   Alist-delete by EQUAL? comparison

(define (del  = x lis) (filter  (lambda (y) (not (= x y))) lis))
(define (del! = x lis) (filter! (lambda (y) (not (= x y))) lis))

;;; The DEL and then FILTER call should definitely be inlined for DELQ & DELV.
(define (delq  x lis) (del  eq? x lis))
(define (delq! x lis) (del! eq? x lis))

(define (delv  x lis) (del  eqv? x lis))
(define (delv! x lis) (del! eqv? x lis))

(define (delete  x lis) (del  equal? x lis))
(define (delete! x lis) (del! equal? x lis))


(define (mem = x lis) (find-tail (lambda (y) (= x y)) lis))

;;; R4RS, hence we don't bother to define.
;;; The MEM and then FIND-TAIL call should definitely
;;; be inlined for MEMQ & MEMV.
;(define (memq    x lis) (mem eq?    x lis))
;(define (memv    x lis) (mem eqv?   x lis))
;(define (member  x lis) (mem equal? x lis))


;;; right-duplicate deletion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; delq-duplicates  delv-duplicates  delete-duplicates  del-duplicates 
;;; delq-duplicates! delv-duplicates! delete-duplicates! del-duplicates!
;;;
;;; Beware -- these are N^2 algorithms. To efficiently remove duplicates
;;; in long lists, sort the list to bring duplicates together, then use a 
;;; linear-time algorithm to kill the dups. Or use an algorithm based on
;;; element-marking. The former gives you O(n lg n), the latter is linear.

(define (del-duplicates elt= lis)
  (check-arg procedure? elt= del-duplicates)
  (let recur ((lis lis))
    (if (pair? lis)
	(let* ((x (car lis))
	       (tail (cdr lis))
	       (new-tail (recur (del elt= x tail))))
	  (if (eq? tail new-tail) lis (cons x new-tail)))
	lis)))

(define (del-duplicates! elt= lis)
  (check-arg procedure? elt= del-duplicates!)
  (let recur ((lis lis))
    (if (pair? lis)
	(let* ((x (car lis))
	       (tail (cdr lis))
	       (new-tail (recur (del! elt= x tail))))
	  (if (eq? tail new-tail) lis (cons x new-tail)))
	lis)))

(define (delq-duplicates   l)  (del-duplicates eq?    l))
(define (delv-duplicates   l)  (del-duplicates eqv?   l))
(define (delete-duplicates l)  (del-duplicates equal? l))

(define (delq-duplicates!   l)  (del-duplicates! eq?    l))
(define (delv-duplicates!   l)  (del-duplicates! eqv?   l))
(define (delete-duplicates! l)  (del-duplicates! equal? l))



;;; alist stuff
;;;;;;;;;;;;;;;

(define (ass = x lis) (find (lambda (entry) (= x (car entry))) lis))

;;; R4RS, hence we don't bother to define. 
;;; The ASS and then FIND call should definitely be inlined for ASSQ & ASSV.
;(define (assq  x lis) (ass eq?    x lis))
;(define (assv  x lis) (ass eqv?   x lis))
;(define (assoc x lis) (ass equal? x lis))

(define (alist-cons key datum alist) (cons (cons key datum) alist))

(define (alist-copy alist)
  (map (lambda (elt) (cons (car elt) (cdr elt)))
       alist))

(define (alist-delete = key alist)
  (filter (lambda (elt) (not (= key (car elt)))) alist))
(define (alist-delete! = key alist)
  (filter! (lambda (elt) (not (= key (car elt)))) alist))

(define del-ass  alist-delete)
(define del-ass! alist-delete!)

(define (del-assq  key alist) (alist-delete  eq?    key alist))
(define (del-assq! key alist) (alist-delete! eq?    key alist))

(define (del-assv  key alist) (alist-delete  eqv?   key alist))
(define (del-assv! key alist) (alist-delete! eqv?   key alist))

(define (del-assoc  key alist) (alist-delete  equal? key alist))
(define (del-assoc! key alist) (alist-delete! equal? key alist))



;;; find find-tail any every list-index
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; ANY returns the first true value produced by PRED.
;;; FIND returns the first list elt passed by PRED.

(define (find pred list)
  (cond ((find-tail pred list) => car)
	(else #f)))

(define (find-tail pred list)
  (check-arg procedure? pred find-tail)
  (let lp ((list list))
    (and (pair? list)
	 (if (pred (car list)) list
	     (lp (cdr list))))))

(define (any pred lis1 . lists)
  (check-arg procedure? pred any)
  (if (pair? lists)

      ;; N-ary case
      (and (%all-pairs? lists) (pair? lis1)
	   (let lp ((heads (cons (car lis1) (%cars lists)))
		    (tails (cons (cdr lis1) (%cdrs lists))))
	     (if (%all-pairs? tails)
		 (or (apply pred heads) (lp (%cars tails) (%cdrs tails)))
		 (apply pred heads))))	; Tail-call the last PRED call.      


      ;; Fast path
      (and (pair? list)
	   (let lp ((list list))	; LIST is a pair. 
	     (let ((head (car list))
		   (tail (cdr list)))
	       (if (pair? tail)
		   (or (pred head) (lp tail))
		   (pred head)))))))	; Tail-call the last PRED call.


;(define (every pred list)		; Simple definition.
;  (let lp ((list list))		; Doesn't return the last PRED value.
;    (or (not (pair? list))
;	(and (pred (car list))
;	     (lp (cdr list))))))

(define (every pred lis1 . lists)
  (check-arg procedure? pred every)
  (if (pair? lists)

      ;; N-ary case
      (or (not (and (%all-pairs? lists) (pair? lis1)))
	  (let lp ((heads (cons (car lis1) (%cars lists)))
		   (tails (cons (cdr lis1) (%cdrs lists))))
	    (if (%all-pairs? tails)
		(and (apply pred heads) (lp (%cars tails) (%cdrs tails)))
		(apply pred heads))))	; Tail-call the last PRED call.

      ;; Fast path
      (or (not (pair? lis1))	
	  (let lp ((head (car lis1))  (tail (cdr lis1)))
	    (if (pair? tail)
		(and (pred head) (lp (car tail) (cdr tail)))
		(pred head))))))	; Tail-call the last PRED call.


(define (list-index pred lis1 . lists)
  (check-arg procedure? pred list-index)
  (if (pair? lists)

      ;; N-ary case
      (let lp ((lists (cons lis1 lists)) (n 0))
	(and (%all-pairs? lists)
	     (if (apply pred (%cars lists)) n
		 (lp (%cdrs lists) (+ n 1)))))

      ;; Fast path
      (let lp ((lis lis1) (n 0))
	(and (pair? lis)
	     (if (pred (car lis)) n (lp (cdr lis) (+ n 1)))))))

;;; Reverse
;;;;;;;;;;;

;R4RS, so not defined here.
;(define (reverse lis) (fold cons '() lis))
				      
(define (reverse! lis)
  (pair-fold (lambda (pair tail) (set-cdr! pair tail) pair) '() lis))
