fix rotting indentation, switch to #lang

svn: r8987
This commit is contained in:
Eli Barzilay 2008-03-16 15:17:50 +00:00
parent 696f8a24ba
commit 38ba4f29e8
12 changed files with 1292 additions and 1409 deletions

View File

@ -2,7 +2,7 @@
;;; <alist.ss> ---- Association list functions ;;; <alist.ss> ---- Association list functions
;;; Time-stamp: <02/03/01 13:56:33 noel> ;;; Time-stamp: <02/03/01 13:56:33 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -32,45 +32,41 @@
;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;; -Olin ;; -Olin
(module alist #lang mzscheme
mzscheme
(require mzlib/etc (require mzlib/etc
srfi/optional srfi/optional
(only "search.ss" find) (only "search.ss" find)
"filter.ss" "filter.ss"
(rename "fold.ss" s:map map)) (rename "fold.ss" s:map map))
(provide (rename my-assoc assoc) (provide (rename my-assoc assoc)
alist-cons alist-cons
alist-copy alist-copy
alist-delete alist-delete
#;alist-delete!) #;alist-delete!)
;; Extended from R4RS to take an optional comparison argument.
;; Extended from R4RS to take an optional comparison argument. (define my-assoc
(define my-assoc (opt-lambda (x lis (maybe-= equal?))
(opt-lambda (x lis (maybe-= equal?)) (let ((= maybe-=))
(let ((= maybe-=)) (find (lambda (entry) (= x (car entry))) lis))))
(find (lambda (entry) (= x (car entry))) lis))))
(define (alist-cons key datum alist) (cons (cons key datum) alist)) (define (alist-cons key datum alist) (cons (cons key datum) alist))
(define (alist-copy alist) (define (alist-copy alist)
(s:map (lambda (elt) (cons (car elt) (cdr elt))) (s:map (lambda (elt) (cons (car elt) (cdr elt)))
alist)) alist))
(define alist-delete (define alist-delete
(opt-lambda (key alist (maybe-= equal?)) (opt-lambda (key alist (maybe-= equal?))
(let ((= maybe-=)) (let ((= maybe-=))
(filter (lambda (elt) (not (= key (car elt)))) alist)))) (filter (lambda (elt) (not (= key (car elt)))) alist))))
#; #;
(define alist-delete! (define alist-delete!
(opt-lambda (key alist (maybe-= equal?)) (opt-lambda (key alist (maybe-= equal?))
(let ((= maybe-=)) (let ((= maybe-=))
(filter! (lambda (elt) (not (= key (car elt)))) alist)))) (filter! (lambda (elt) (not (= key (car elt)))) alist))))
)
;;; alist.ss ends here ;;; alist.ss ends here

View File

@ -2,7 +2,7 @@
;;; <cons.ss> ---- List constructors ;;; <cons.ss> ---- List constructors
;;; Time-stamp: <02/02/27 12:19:59 noel> ;;; Time-stamp: <02/02/27 12:19:59 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -32,92 +32,79 @@
;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;; -Olin ;; -Olin
(module cons #lang mzscheme
mzscheme
(require mzlib/etc (require mzlib/etc
srfi/optional srfi/optional
"selector.ss") "selector.ss")
(provide xcons (provide xcons
make-list make-list
list-tabulate list-tabulate
cons* cons*
list-copy list-copy
circular-list circular-list
iota) iota)
;; Occasionally useful as a value to be passed to a fold or other ;; Occasionally useful as a value to be passed to a fold or other
;; higher-order procedure. ;; higher-order procedure.
(define (xcons d a) (cons a d)) (define (xcons d a) (cons a d))
;; Make a list of length LEN.
;; Make a list of length LEN. (define make-list
(opt-lambda (len [elt #f])
(define make-list (check-arg (lambda (n) (and (integer? n) (>= n 0))) len 'make-list)
(opt-lambda (len [elt #f]) (do ((i len (- i 1))
(check-arg (lambda (n) (and (integer? n) (>= n 0))) len 'make-list) (ans '() (cons elt ans)))
(do ((i len (- i 1)) ((<= i 0) ans))))
(ans '() (cons elt ans)))
((<= i 0) ans))))
;; Make a list of length LEN. Elt i is (PROC i) for 0 <= i < LEN. ;; 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 not-pair? car cdr rest values))
(define (cons* first . rest) (define (list-tabulate len proc)
(let recur ((x first) (rest rest)) (check-arg (lambda (n) (and (integer? n) (>= n 0))) len 'list-tabulate)
(if (pair? rest) (check-arg procedure? proc 'list-tabulate)
(cons x (recur (car rest) (cdr rest))) (do ((i (- len 1) (- i 1))
x))) (ans '() (cons (proc i) ans)))
((< i 0) ans)))
(define (list-copy lis) ;; (cons* a1 a2 ... an) = (cons a1 (cons a2 (cons ... an)))
(let recur ((lis lis)) ;; (cons* a1) = a1; (cons* a1 a2 ...) = (cons a1 (cons* a2 ...))
(if (pair? lis) ;;
(cons (car lis) (recur (cdr lis))) ;; (cons first (unfold not-pair? car cdr rest values))
lis)))
(define (circular-list val1 . vals)
(let ([ph (make-placeholder #f)])
(placeholder-set! ph
(cons val1
(let loop ([vals vals])
(if (null? vals)
ph
(cons (car vals)
(loop (cdr vals)))))))
(make-reader-graph ph)))
(define (cons* first . rest)
(let recur ((x first) (rest rest))
(if (pair? rest)
(cons x (recur (car rest) (cdr rest)))
x)))
;; IOTA count [start step] (start start+step ... start+(count-1)*step) (define (list-copy lis)
(let recur ((lis lis))
(define iota (if (pair? lis)
(opt-lambda (count [start 0] [step 1]) (cons (car lis) (recur (cdr lis)))
(check-arg integer? count 'iota) lis)))
(check-arg number? start 'iota)
(check-arg number? step 'iota)
(unless (or (zero? count) (positive? count))
(error 'iota "count expected to be non-negative, got: ~a" count))
(let loop ([n 0])
(cond
[(= n count) '()]
[else (cons (+ start (* n step))
(loop (add1 n)))]))))
) (define (circular-list val1 . vals)
(let ([ph (make-placeholder #f)])
(placeholder-set! ph
(cons val1 (let loop ([vals vals])
(if (null? vals)
ph
(cons (car vals) (loop (cdr vals)))))))
(make-reader-graph ph)))
;; IOTA count [start step] (start start+step ... start+(count-1)*step)
(define iota
(opt-lambda (count [start 0] [step 1])
(check-arg integer? count 'iota)
(check-arg number? start 'iota)
(check-arg number? step 'iota)
(unless (or (zero? count) (positive? count))
(error 'iota "count expected to be non-negative, got: ~a" count))
(let loop ([n 0])
(if (= n count) '()
(cons (+ start (* n step)) (loop (add1 n)))))))
;;; cons.ss ends here ;;; cons.ss ends here

View File

@ -2,7 +2,7 @@
;;; <delete.ss> ---- List deletion functions ;;; <delete.ss> ---- List deletion functions
;;; Time-stamp: <02/03/01 07:26:12 noel> ;;; Time-stamp: <02/03/01 07:26:12 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -33,63 +33,59 @@
;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;; -Olin ;; -Olin
(module delete #lang mzscheme
mzscheme
(require mzlib/etc (require mzlib/etc
srfi/optional srfi/optional
"predicate.ss" "predicate.ss"
"filter.ss") "filter.ss")
(provide delete (provide delete
(rename delete delete!) (rename delete delete!)
delete-duplicates delete-duplicates
(rename delete-duplicates delete-duplicates!)) (rename delete-duplicates delete-duplicates!))
(define delete (define delete
(opt-lambda (x lis (maybe-= equal?)) (opt-lambda (x lis (maybe-= equal?))
(let ((= maybe-=)) (let ((= maybe-=))
(filter (lambda (y) (not (= x y))) lis)))) (filter (lambda (y) (not (= x y))) lis))))
#; #;
(define delete! (define delete!
(opt-lambda (x lis (maybe-= equal?)) (opt-lambda (x lis (maybe-= equal?))
(let ((= maybe-=)) (let ((= maybe-=))
(filter! (lambda (y) (not (= x y))) lis)))) (filter! (lambda (y) (not (= x y))) lis))))
;; right-duplicate deletion ;; right-duplicate deletion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; delete-duplicates delete-duplicates! ;; delete-duplicates delete-duplicates!
;; ;;
;; Beware -- these are N^2 algorithms. To efficiently remove duplicates ;; Beware -- these are N^2 algorithms. To efficiently remove duplicates
;; in long lists, sort the list to bring duplicates together, then use a ;; 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 ;; 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. ;; element-marking. The former gives you O(n lg n), the latter is linear.
(define delete-duplicates (define delete-duplicates
(opt-lambda (lis (maybe-= equal?)) (opt-lambda (lis (maybe-= equal?))
(let ((elt= maybe-=)) (let ((elt= maybe-=))
(check-arg procedure? elt= 'delete-duplicates) (check-arg procedure? elt= 'delete-duplicates)
(let recur ((lis lis)) (let recur ((lis lis))
(if (null-list? lis) lis (if (null-list? lis) lis
(let* ((x (car lis)) (let* ((x (car lis))
(tail (cdr lis)) (tail (cdr lis))
(new-tail (recur (delete x tail elt=)))) (new-tail (recur (delete x tail elt=))))
(if (eq? tail new-tail) lis (cons x new-tail)))))))) (if (eq? tail new-tail) lis (cons x new-tail))))))))
#;
(define delete-duplicates!
(opt-lambda (lis (maybe-= equal?))
(let ((elt= maybe-=))
(check-arg procedure? elt= 'delete-duplicates!)
(let recur ((lis lis))
(if (null-list? lis) lis
(let* ((x (car lis))
(tail (cdr lis))
(new-tail (recur (delete! x tail elt=))))
(if (eq? tail new-tail) lis (cons x new-tail))))))))
)
#;
(define delete-duplicates!
(opt-lambda (lis (maybe-= equal?))
(let ((elt= maybe-=))
(check-arg procedure? elt= 'delete-duplicates!)
(let recur ((lis lis))
(if (null-list? lis) lis
(let* ((x (car lis))
(tail (cdr lis))
(new-tail (recur (delete! x tail elt=))))
(if (eq? tail new-tail) lis (cons x new-tail))))))))
;;; delete.ss ends here ;;; delete.ss ends here

View File

@ -2,7 +2,7 @@
;;; <filter.ss> ---- List filtering and partitioning functions ;;; <filter.ss> ---- List filtering and partitioning functions
;;; Time-stamp: <02/03/01 07:26:43 noel> ;;; Time-stamp: <02/03/01 07:26:43 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -32,162 +32,145 @@
;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;; -Olin ;; -Olin
(module filter #lang mzscheme
mzscheme
(require mzlib/etc (require mzlib/etc
srfi/optional srfi/optional
"predicate.ss") "predicate.ss"
(require srfi/8/receive) srfi/8/receive)
(provide filter (provide filter
partition partition
remove remove
(rename filter filter!) (rename filter filter!)
(rename partition partition!) (rename partition partition!)
(rename remove remove!)) (rename remove remove!))
;; filter, remove, partition
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FILTER, REMOVE, PARTITION and their destructive counterparts do not
;; disorder the elements of their argument.
;; filter, remove, partition ;; This FILTER shares the longest tail of L that has no deleted
;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; elements. If Scheme had multi-continuation calls, they could be
;; FILTER, REMOVE, PARTITION and their destructive counterparts do not ;; made more efficient.
;; disorder the elements of their argument.
;; This FILTER shares the longest tail of L that has no deleted (define (filter pred lis) ; Sleazing with EQ? makes this
;; elements. If Scheme had multi-continuation calls, they could be (check-arg procedure? pred 'filter) ; one faster.
;; made more efficient. (let recur ((lis lis))
(if (null-list? lis) lis ; Use NOT-PAIR? to handle dotted lists.
(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.
(define (filter pred lis) ; Sleazing with EQ? makes this ;; This implementation of FILTER!
(check-arg procedure? pred 'filter) ; one faster. ;; - doesn't cons, and uses no stack;
(let recur ((lis lis)) ;; - is careful not to do redundant SET-CDR! writes, as writes to memory are
(if (null-list? lis) lis ; Use NOT-PAIR? to handle dotted lists. ;; usually expensive on modern machines, and can be extremely expensive on
(let ((head (car lis)) ;; modern Schemes (e.g., ones that have generational GC's).
(tail (cdr lis))) ;; It just zips down contiguous runs of in and out elts in LIS doing the
(if (pred head) ;; minimal number of SET-CDR!s to splice the tail of one run of ins to the
(let ((new-tail (recur tail))) ; Replicate the RECUR call so ;; beginning of the next.
(if (eq? tail new-tail) lis #;
(cons head new-tail))) (define (filter! pred lis)
(recur tail)))))) ; this one can be a tail call. (check-arg procedure? pred 'filter!)
(let lp ((ans lis))
(cond ((null-list? 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 (null-list? lis) (values lis lis) ; Use NOT-PAIR? to handle dotted lists.
(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))))))))
;; 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 (null-list? 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))
;; 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 ((null-list? 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 (null-list? lis) (values lis lis) ; Use NOT-PAIR? to handle dotted lists.
(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))))))))
;; 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 (null-list? 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))
)
;;; filter.ss ends here ;;; filter.ss ends here

View File

@ -2,7 +2,7 @@
;;; <fold.ss> ---- List folds ;;; <fold.ss> ---- List folds
;;; Time-stamp: <02/02/28 12:02:38 noel> ;;; Time-stamp: <02/02/28 12:02:38 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -32,260 +32,234 @@
;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;; -Olin ;; -Olin
(module fold #lang mzscheme
mzscheme
(require srfi/optional (require srfi/optional
"predicate.ss" "predicate.ss"
"selector.ss" "selector.ss"
"util.ss") "util.ss"
(require srfi/8/receive) srfi/8/receive)
(provide (rename my-map map) (provide (rename my-map map)
(rename my-for-each for-each) (rename my-for-each for-each)
fold fold
unfold unfold
pair-fold pair-fold
reduce reduce
fold-right fold-right
unfold-right unfold-right
pair-fold-right pair-fold-right
reduce-right reduce-right
append-map append-map
(rename append-map append-map!) (rename append-map append-map!)
(rename my-map map!) (rename my-map map!)
pair-for-each pair-for-each
filter-map filter-map
map-in-order) map-in-order)
;; fold/unfold
;;;;;;;;;;;;;;
(define (unfold-right p f g seed . maybe-tail)
(check-arg procedure? p 'unfold-right)
(check-arg procedure? f 'unfold-right)
(check-arg procedure? g 'unfold-right)
(let lp ((seed seed) (ans maybe-tail))
(if (p seed) ans
(lp (g seed)
(cons (f seed) ans)))))
(define (unfold p f g seed . maybe-tail-gen)
(check-arg procedure? p 'unfold)
(check-arg procedure? f 'unfold)
(check-arg procedure? g 'unfold)
(if (pair? maybe-tail-gen)
(let ((tail-gen (car maybe-tail-gen)))
(if (pair? (cdr maybe-tail-gen))
(apply error "Too many arguments" unfold p f g seed maybe-tail-gen)
(let recur ((seed seed))
(if (p seed) (tail-gen seed)
(cons (f seed) (recur (g seed)))))))
(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
(receive (cars+ans cdrs) (%cars+cdrs+ lists ans)
(if (null? cars+ans) ans ; Done.
(lp cdrs (apply kons cars+ans)))))
(let lp ((lis lis1) (ans knil)) ; Fast path
(if (null-list? lis) ans
(lp (cdr lis) (kons (car lis) 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
(let ((cdrs (%cdrs lists)))
(if (null? cdrs) knil
(apply kons (%cars+ lists (recur cdrs))))))
(let recur ((lis lis1)) ; Fast path
(if (null-list? lis) knil
(let ((head (car lis)))
(kons head (recur (cdr lis))))))))
(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
(let ((cdrs (%cdrs lists)))
(if (null? cdrs) zero
(apply f (append lists (list (recur cdrs)))))))
(let recur ((lis lis1)) ; Fast path
(if (null-list? lis) zero (f lis (recur (cdr lis)))))))
(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
(let ((tails (%cdrs lists)))
(if (null? tails) ans
(lp tails (apply f (append lists (list ans)))))))
(let lp ((lis lis1) (ans zero))
(if (null-list? lis) ans
(let ((tail (cdr lis))) ; Grab the cdr now,
(lp tail (f lis ans))))))) ; in case F SET-CDR!s LIS.
;; 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 (null-list? lis) ridentity
(fold f (car lis) (cdr lis))))
(define (reduce-right f ridentity lis)
(check-arg procedure? f 'reduce-right)
(if (null-list? lis) ridentity
(let recur ((head (car lis)) (lis (cdr lis)))
(if (pair? lis)
(f head (recur (car lis) (cdr lis)))
head))))
;; 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)
(receive (cars cdrs) (%cars+cdrs (cons lis1 lists))
(if (null? cars) '()
(let recur ((cars cars) (cdrs cdrs))
(let ((vals (apply f cars)))
(receive (cars2 cdrs2) (%cars+cdrs cdrs)
(if (null? cars2) vals
(appender vals (recur cars2 cdrs2))))))))
;; Fast path
(if (null-list? lis1) '()
(let recur ((elt (car lis1)) (rest (cdr lis1)))
(let ((vals (f elt)))
(if (null-list? rest) vals
(appender vals (recur (car rest) (cdr rest)))))))))
(define (pair-for-each proc lis1 . lists)
(check-arg procedure? proc 'pair-for-each)
(if (pair? lists)
(let lp ((lists (cons lis1 lists)))
(let ((tails (%cdrs lists)))
(if (pair? tails)
(begin (apply proc lists)
(lp tails)))))
;; Fast path.
(let lp ((lis lis1))
(if (not (null-list? lis))
(let ((tail (cdr lis))) ; Grab the cdr now,
(proc lis) ; in case PROC 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 (not (null-list? lis1))
(receive (heads tails) (%cars+cdrs/no-test lists)
(set-car! lis1 (apply f (car lis1) heads))
(lp (cdr lis1) 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) (%cars+cdrs lists)
(if (pair? cars)
(cond ((apply f cars) => (lambda (x) (cons x (recur cdrs))))
(else (recur cdrs))) ; Tail call in this arm.
'())))
;; Fast path.
(let recur ((lis lis1))
(if (null-list? lis) lis
(let ((tail (recur (cdr lis))))
(cond ((f (car lis)) => (lambda (x) (cons x tail)))
(else tail)))))))
;; 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) (%cars+cdrs lists)
(if (pair? cars)
(let ((x (apply f cars))) ; Do head first,
(cons x (recur cdrs))) ; then tail.
'())))
;; Fast path.
(let recur ((lis lis1))
(if (null-list? lis) lis
(let ((tail (cdr lis))
(x (f (car lis)))) ; Do head first,
(cons x (recur tail))))))) ; then tail.
;; We extend MAP to handle arguments of unequal length.
(define my-map map-in-order)
;; fold/unfold
;;;;;;;;;;;;;;
(define (unfold-right p f g seed . maybe-tail)
(check-arg procedure? p 'unfold-right)
(check-arg procedure? f 'unfold-right)
(check-arg procedure? g 'unfold-right)
(let lp ((seed seed) (ans maybe-tail))
(if (p seed) ans
(lp (g seed)
(cons (f seed) ans)))))
(define (unfold p f g seed . maybe-tail-gen)
(check-arg procedure? p 'unfold)
(check-arg procedure? f 'unfold)
(check-arg procedure? g 'unfold)
(if (pair? maybe-tail-gen)
(let ((tail-gen (car maybe-tail-gen)))
(if (pair? (cdr maybe-tail-gen))
(apply error "Too many arguments" unfold p f g seed maybe-tail-gen)
(let recur ((seed seed))
(if (p seed) (tail-gen seed)
(cons (f seed) (recur (g seed)))))))
(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
(receive (cars+ans cdrs) (%cars+cdrs+ lists ans)
(if (null? cars+ans) ans ; Done.
(lp cdrs (apply kons cars+ans)))))
(let lp ((lis lis1) (ans knil)) ; Fast path
(if (null-list? lis) ans
(lp (cdr lis) (kons (car lis) 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
(let ((cdrs (%cdrs lists)))
(if (null? cdrs) knil
(apply kons (%cars+ lists (recur cdrs))))))
(let recur ((lis lis1)) ; Fast path
(if (null-list? lis) knil
(let ((head (car lis)))
(kons head (recur (cdr lis))))))))
(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
(let ((cdrs (%cdrs lists)))
(if (null? cdrs) zero
(apply f (append lists (list (recur cdrs)))))))
(let recur ((lis lis1)) ; Fast path
(if (null-list? lis) zero (f lis (recur (cdr lis)))))))
(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
(let ((tails (%cdrs lists)))
(if (null? tails) ans
(lp tails (apply f (append lists (list ans)))))))
(let lp ((lis lis1) (ans zero))
(if (null-list? lis) ans
(let ((tail (cdr lis))) ; Grab the cdr now,
(lp tail (f lis ans))))))) ; in case F SET-CDR!s LIS.
;; 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 (null-list? lis) ridentity
(fold f (car lis) (cdr lis))))
(define (reduce-right f ridentity lis)
(check-arg procedure? f 'reduce-right)
(if (null-list? lis) ridentity
(let recur ((head (car lis)) (lis (cdr lis)))
(if (pair? lis)
(f head (recur (car lis) (cdr lis)))
head))))
;; 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)
(receive (cars cdrs) (%cars+cdrs (cons lis1 lists))
(if (null? cars) '()
(let recur ((cars cars) (cdrs cdrs))
(let ((vals (apply f cars)))
(receive (cars2 cdrs2) (%cars+cdrs cdrs)
(if (null? cars2) vals
(appender vals (recur cars2 cdrs2))))))))
;; Fast path
(if (null-list? lis1) '()
(let recur ((elt (car lis1)) (rest (cdr lis1)))
(let ((vals (f elt)))
(if (null-list? rest) vals
(appender vals (recur (car rest) (cdr rest)))))))))
(define (pair-for-each proc lis1 . lists)
(check-arg procedure? proc 'pair-for-each)
(if (pair? lists)
(let lp ((lists (cons lis1 lists)))
(let ((tails (%cdrs lists)))
(if (pair? tails)
(begin (apply proc lists)
(lp tails)))))
;; Fast path.
(let lp ((lis lis1))
(if (not (null-list? lis))
(let ((tail (cdr lis))) ; Grab the cdr now,
(proc lis) ; in case PROC 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 (not (null-list? lis1))
(receive (heads tails) (%cars+cdrs/no-test lists)
(set-car! lis1 (apply f (car lis1) heads))
(lp (cdr lis1) 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) (%cars+cdrs lists)
(if (pair? cars)
(cond ((apply f cars) => (lambda (x) (cons x (recur cdrs))))
(else (recur cdrs))) ; Tail call in this arm.
'())))
;; Fast path.
(let recur ((lis lis1))
(if (null-list? lis) lis
(let ((tail (recur (cdr lis))))
(cond ((f (car lis)) => (lambda (x) (cons x tail)))
(else tail)))))))
;; 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) (%cars+cdrs lists)
(if (pair? cars)
(let ((x (apply f cars))) ; Do head first,
(cons x (recur cdrs))) ; then tail.
'())))
;; Fast path.
(let recur ((lis lis1))
(if (null-list? lis) lis
(let ((tail (cdr lis))
(x (f (car lis)))) ; Do head first,
(cons x (recur tail))))))) ; then tail.
;; We extend MAP to handle arguments of unequal length.
(define my-map map-in-order)
;;; Apply F across lists, guaranteeing to go left-to-right. ;;; Apply F across lists, guaranteeing to go left-to-right.
;;; NOTE: Some implementations of R5RS MAP are compliant with this spec; ;;; NOTE: Some implementations of R5RS MAP are compliant with this spec;
;;; in which case this procedure may simply be defined as a synonym for FOR-EACH. ;;; in which case this procedure may simply be defined as a synonym for FOR-EACH.
(define (my-for-each f lis1 . lists) (define (my-for-each f lis1 . lists)
(check-arg procedure? f for-each) (check-arg procedure? f for-each)
(if (pair? lists) (if (pair? lists)
(let recur ((lists (cons lis1 lists))) (let recur ((lists (cons lis1 lists)))
(receive (cars cdrs) (%cars+cdrs lists) (receive (cars cdrs) (%cars+cdrs lists)
(if (pair? cars) (if (pair? cars)
(begin (begin
(apply f cars) ; Do head first, (apply f cars) ; Do head first,
(recur cdrs))))) ; then tail. (recur cdrs))))) ; then tail.
;; Fast path.
;; Fast path. (let recur ((lis lis1))
(let recur ((lis lis1)) (if (not (null-list? lis))
(if (not (null-list? lis)) (begin
(begin (f (car lis)) ; Do head first,
(f (car lis)) ; Do head first, (recur (cdr lis)))))))
(recur (cdr lis)))))))
)
;;; fold.ss ends here ;;; fold.ss ends here

View File

@ -1,4 +1,4 @@
;;; SRFI-1 list-processing library -*- Scheme -*- ;;; SRFI-1 list-processing library -*- Scheme -*-
;;; Reference implementation ;;; Reference implementation
;;; ;;;
;;; Copyright (c) 1998, 1999 by Olin Shivers. You may do as you please with ;;; Copyright (c) 1998, 1999 by Olin Shivers. You may do as you please with
@ -17,25 +17,25 @@
;;; for SRFI-1. See the porting notes below for more information. ;;; for SRFI-1. See the porting notes below for more information.
;;; Exported: ;;; Exported:
;;; xcons tree-copy make-list list-tabulate cons* list-copy ;;; xcons tree-copy make-list list-tabulate cons* list-copy
;;; proper-list? circular-list? dotted-list? not-pair? null-list? list= ;;; proper-list? circular-list? dotted-list? not-pair? null-list? list=
;;; circular-list length+ ;;; circular-list length+
;;; iota ;;; iota
;;; first second third fourth fifth sixth seventh eighth ninth tenth ;;; first second third fourth fifth sixth seventh eighth ninth tenth
;;; car+cdr ;;; car+cdr
;;; take drop ;;; take drop
;;; take-right drop-right ;;; take-right drop-right
;;; take! drop-right! ;;; take! drop-right!
;;; split-at split-at! ;;; split-at split-at!
;;; last last-pair ;;; last last-pair
;;; zip unzip1 unzip2 unzip3 unzip4 unzip5 ;;; zip unzip1 unzip2 unzip3 unzip4 unzip5
;;; count ;;; count
;;; append! append-reverse append-reverse! concatenate concatenate! ;;; append! append-reverse append-reverse! concatenate concatenate!
;;; unfold fold pair-fold reduce ;;; unfold fold pair-fold reduce
;;; unfold-right fold-right pair-fold-right reduce-right ;;; unfold-right fold-right pair-fold-right reduce-right
;;; append-map append-map! map! pair-for-each filter-map map-in-order ;;; append-map append-map! map! pair-for-each filter-map map-in-order
;;; filter partition remove ;;; filter partition remove
;;; filter! partition! remove! ;;; filter! partition! remove!
;;; find find-tail any every list-index ;;; find find-tail any every list-index
;;; take-while drop-while take-while! ;;; take-while drop-while take-while!
;;; span break span! break! ;;; span break span! break!
@ -43,11 +43,11 @@
;;; alist-cons alist-copy ;;; alist-cons alist-copy
;;; delete-duplicates delete-duplicates! ;;; delete-duplicates delete-duplicates!
;;; alist-delete alist-delete! ;;; alist-delete alist-delete!
;;; reverse! ;;; reverse!
;;; lset<= lset= lset-adjoin ;;; lset<= lset= lset-adjoin
;;; lset-union lset-intersection lset-difference lset-xor lset-diff+intersection ;;; lset-union lset-intersection lset-difference lset-xor lset-diff+intersection
;;; lset-union! lset-intersection! lset-difference! lset-xor! lset-diff+intersection! ;;; lset-union! lset-intersection! lset-difference! lset-xor! lset-diff+intersection!
;;; ;;;
;;; In principle, the following R4RS list- and pair-processing procedures ;;; In principle, the following R4RS list- and pair-processing procedures
;;; are also part of this package's exports, although they are not defined ;;; are also part of this package's exports, although they are not defined
;;; in this file: ;;; in this file:
@ -60,7 +60,7 @@
;;; in this file: ;;; in this file:
;;; map for-each member assoc ;;; map for-each member assoc
;;; ;;;
;;; The remaining two R4RS list-processing procedures are not included: ;;; The remaining two R4RS list-processing procedures are not included:
;;; list-tail (use drop) ;;; list-tail (use drop)
;;; list? (use proper-list?) ;;; list? (use proper-list?)
@ -70,7 +70,7 @@
;;; of the answer list in the wrong order (left-to-right or head-to-tail) from ;;; 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 ;;; 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, ;;; tail-then-head). One style or idiom of programming these algorithms, then,
;;; loops, consing up the elements in reverse order, then destructively ;;; 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 ;;; 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 ;;; and efficient way to code these algorithms is recursively. This trades off
;;; intermediate temporary list structure for intermediate temporary stack ;;; intermediate temporary list structure for intermediate temporary stack
@ -83,16 +83,16 @@
;;; This is carefully tuned code; do not modify casually. ;;; This is carefully tuned code; do not modify casually.
;;; - It is careful to share storage when possible; ;;; - It is careful to share storage when possible;
;;; - Side-effecting code tries not to perform redundant writes. ;;; - Side-effecting code tries not to perform redundant writes.
;;; ;;;
;;; That said, a port of this library to a specific Scheme system might wish ;;; That said, a port of this library to a specific Scheme system might wish
;;; to tune this code to exploit particulars of the implementation. ;;; to tune this code to exploit particulars of the implementation.
;;; The single most important compiler-specific optimisation you could make ;;; The single most important compiler-specific optimisation you could make
;;; to this library would be to add rewrite rules or transforms to: ;;; to this library would be to add rewrite rules or transforms to:
;;; - transform applications of n-ary procedures (e.g. LIST=, CONS*, APPEND, ;;; - transform applications of n-ary procedures (e.g. LIST=, CONS*, APPEND,
;;; LSET-UNION) into multiple applications of a primitive two-argument ;;; LSET-UNION) into multiple applications of a primitive two-argument
;;; variant. ;;; variant.
;;; - transform applications of the mapping functions (MAP, FOR-EACH, FOLD, ;;; - transform applications of the mapping functions (MAP, FOR-EACH, FOLD,
;;; ANY, EVERY) into open-coded loops. The killer here is that these ;;; ANY, EVERY) into open-coded loops. The killer here is that these
;;; functions are n-ary. Handling the general case is quite inefficient, ;;; functions are n-ary. Handling the general case is quite inefficient,
;;; requiring many intermediate data structures to be allocated and ;;; requiring many intermediate data structures to be allocated and
;;; discarded. ;;; discarded.
@ -114,13 +114,13 @@
;;; ;;;
;;; Note that this code is, of course, dependent upon standard bindings for ;;; 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 ;;; 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 ;;; to the procedure that takes the car of a list. If your Scheme
;;; implementation allows user code to alter the bindings of these procedures ;;; implementation allows user code to alter the bindings of these procedures
;;; in a manner that would be visible to these definitions, then there might ;;; in a manner that would be visible to these definitions, then there might
;;; be trouble. You could consider horrible kludgery along the lines of ;;; be trouble. You could consider horrible kludgery along the lines of
;;; (define fact ;;; (define fact
;;; (let ((= =) (- -) (* *)) ;;; (let ((= =) (- -) (* *))
;;; (letrec ((real-fact (lambda (n) ;;; (letrec ((real-fact (lambda (n)
;;; (if (= n 0) 1 (* n (real-fact (- n 1))))))) ;;; (if (= n 0) 1 (* n (real-fact (- n 1)))))))
;;; real-fact))) ;;; real-fact)))
;;; Or you could consider shifting to a reasonable Scheme system that, say, ;;; Or you could consider shifting to a reasonable Scheme system that, say,
@ -130,18 +130,18 @@
;;; Scheme system has a sophisticated compiler that can eliminate redundant ;;; Scheme system has a sophisticated compiler that can eliminate redundant
;;; error checks, this is no problem. However, if not, these checks incur ;;; error checks, this is no problem. However, if not, these checks incur
;;; some performance overhead -- and, in a safe Scheme implementation, they ;;; 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 ;;; 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 ;;; 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 ;;; 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 ;;; checking code out if it's inappropriate for your implementation -- just
;;; nuke every call to CHECK-ARG. ;;; nuke every call to CHECK-ARG.
;;; ;;;
;;; On the other hand, if you *do* have a sophisticated compiler that will ;;; On the other hand, if you *do* have a sophisticated compiler that will
;;; actually perform soft-typing and eliminate redundant checks (Rice's systems ;;; actually perform soft-typing and eliminate redundant checks (Rice's systems
;;; being the only possible candidate of which I'm aware), leaving these checks ;;; 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, ;;; in can *help*, since their presence can be elided in redundant cases,
;;; and in cases where they are needed, performing the checks early, at ;;; and in cases where they are needed, performing the checks early, at
;;; procedure entry, can "lift" a check out of a loop. ;;; procedure entry, can "lift" a check out of a loop.
;;; ;;;
;;; Finally, I have only checked the properties that can portably be checked ;;; 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 ;;; with R5RS Scheme -- and this is not complete. You may wish to alter
@ -197,7 +197,7 @@
;;; the definition and implementation of this library. ;;; the definition and implementation of this library.
;;; ;;;
;;; The argument *against* defining these procedures to work on dotted ;;; The argument *against* defining these procedures to work on dotted
;;; lists is that dotted lists are the rare, odd case, and that by ;;; lists is that dotted lists are the rare, odd case, and that by
;;; arranging for the procedures to handle them, we lose error checking ;;; arranging for the procedures to handle them, we lose error checking
;;; in the cases where a dotted list is passed by accident -- e.g., when ;;; in the cases where a dotted list is passed by accident -- e.g., when
;;; the programmer swaps a two arguments to a list-processing function, ;;; the programmer swaps a two arguments to a list-processing function,
@ -209,42 +209,35 @@
;;; The SRFI discussion record contains more discussion on this topic. ;;; The SRFI discussion record contains more discussion on this topic.
;; JBC, 2003-10-20: some of the names provided by list.ss are prefixed ;; JBC, 2003-10-20: some of the names provided by list.ss are prefixed
;; with an s: to avoid colliding with mzscheme. The wrapper 1.ss ;; with an s: to avoid colliding with mzscheme. The wrapper 1.ss
;; changes their names back to the non-prefixed form. ;; changes their names back to the non-prefixed form.
(module list mzscheme #lang mzscheme
(require srfi/optional) (require srfi/optional)
(require "cons.ss" (require "cons.ss"
"selector.ss" "selector.ss"
"predicate.ss" "predicate.ss"
"misc.ss" "misc.ss"
(all-except "fold.ss" map for-each) (all-except "fold.ss" map for-each)
(rename "fold.ss" s:map map) (rename "fold.ss" s:map map)
(rename "fold.ss" s:for-each for-each) (rename "fold.ss" s:for-each for-each)
(all-except "search.ss" member) (all-except "search.ss" member)
(rename "search.ss" s:member member) (rename "search.ss" s:member member)
"filter.ss" "filter.ss"
"delete.ss" "delete.ss"
(all-except "alist.ss" assoc) (all-except "alist.ss" assoc)
(rename "alist.ss" s:assoc assoc) (rename "alist.ss" s:assoc assoc)
"lset.ss") "lset.ss")
(provide (all-from "cons.ss")
(provide (all-from "selector.ss")
(all-from "cons.ss") (all-from "predicate.ss")
(all-from "selector.ss") (all-from "misc.ss")
(all-from "predicate.ss") (all-from "fold.ss")
(all-from "misc.ss") (all-from "search.ss")
(all-from "fold.ss") (all-from "filter.ss")
(all-from "search.ss") (all-from "delete.ss")
(all-from "filter.ss") (all-from "alist.ss")
(all-from "delete.ss") (all-from "lset.ss"))
(all-from "alist.ss")
(all-from "lset.ss"))
;;end of the unit
)

View File

@ -2,7 +2,7 @@
;;; <lset.ss> ---- Lists as Sets ;;; <lset.ss> ---- Lists as Sets
;;; Time-stamp: <03/03/13 16:20:56 noel> ;;; Time-stamp: <03/03/13 16:20:56 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -32,211 +32,201 @@
;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;; -Olin ;; -Olin
(module lset #lang mzscheme
mzscheme
(require srfi/optional (require srfi/optional
(all-except "search.ss" member) (all-except "search.ss" member)
(all-except "fold.ss" map for-each) (all-except "fold.ss" map for-each)
(rename "search.ss" s:member member) (rename "search.ss" s:member member)
"delete.ss" "delete.ss"
"predicate.ss" "predicate.ss"
"filter.ss") "filter.ss"
(require srfi/8/receive) srfi/8/receive)
(provide lset<= (provide lset<=
lset= lset=
lset-adjoin lset-adjoin
lset-union lset-union
(rename lset-union lset-union!) (rename lset-union lset-union!)
lset-intersection lset-intersection
lset-difference lset-difference
(rename lset-difference lset-difference!) (rename lset-difference lset-difference!)
lset-xor lset-xor
(rename lset-xor lset-xor!) (rename lset-xor lset-xor!)
lset-diff+intersection lset-diff+intersection
(rename lset-diff+intersection lset-diff+intersection!)) (rename lset-diff+intersection lset-diff+intersection!))
;; Lists-as-sets ;; Lists-as-sets
;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;
;; This is carefully tuned code; do not modify casually. ;; This is carefully tuned code; do not modify casually.
;; - It is careful to share storage when possible; ;; - It is careful to share storage when possible;
;; - Side-effecting code tries not to perform redundant writes. ;; - Side-effecting code tries not to perform redundant writes.
;; - It tries to avoid linear-time scans in special cases where constant-time ;; - It tries to avoid linear-time scans in special cases where constant-time
;; computations can be performed. ;; computations can be performed.
;; - It relies on similar properties from the other list-lib procs it calls. ;; - It relies on similar properties from the other list-lib procs it calls.
;; For example, it uses the fact that the implementations of MEMBER and ;; For example, it uses the fact that the implementations of MEMBER and
;; FILTER in this source code share longest common tails between args ;; FILTER in this source code share longest common tails between args
;; and results to get structure sharing in the lset procedures. ;; and results to get structure sharing in the lset procedures.
(define (%lset2<= = lis1 lis2) (every (lambda (x) (s:member x lis2 =)) lis1)) (define (%lset2<= = lis1 lis2) (every (lambda (x) (s:member x lis2 =)) lis1))
(define (lset<= = . lists) (define (lset<= = . lists)
(check-arg procedure? = 'lset<=) (check-arg procedure? = 'lset<=)
(or (not (pair? lists)) ; 0-ary case (or (not (pair? lists)) ; 0-ary case
(let lp ((s1 (car lists)) (rest (cdr lists))) (let lp ((s1 (car lists)) (rest (cdr lists)))
(or (not (pair? rest)) (or (not (pair? rest))
(let ((s2 (car rest)) (rest (cdr rest))) (let ((s2 (car rest)) (rest (cdr rest)))
(and (or (eq? s2 s1) ; Fast path (and (or (eq? s2 s1) ; Fast path
(%lset2<= = s1 s2)) ; Real test (%lset2<= = s1 s2)) ; Real test
(lp s2 rest))))))) (lp s2 rest)))))))
(define (lset= = . lists) (define (lset= = . lists)
(check-arg procedure? = 'lset=) (check-arg procedure? = 'lset=)
(or (not (pair? lists)) ; 0-ary case (or (not (pair? lists)) ; 0-ary case
(let lp ((s1 (car lists)) (rest (cdr lists))) (let lp ((s1 (car lists)) (rest (cdr lists)))
(or (not (pair? rest)) (or (not (pair? rest))
(let ((s2 (car rest)) (let ((s2 (car rest))
(rest (cdr rest))) (rest (cdr rest)))
(and (or (eq? s1 s2) ; Fast path (and (or (eq? s1 s2) ; Fast path
(and (%lset2<= = s1 s2) (%lset2<= = s2 s1))) ; Real test (and (%lset2<= = s1 s2) (%lset2<= = s2 s1))) ; Real test
(lp s2 rest))))))) (lp s2 rest)))))))
(define (lset-adjoin = lis . elts)
(define (lset-adjoin = lis . elts) (check-arg procedure? = 'lset-adjoin)
(check-arg procedure? = 'lset-adjoin) (fold (lambda (elt ans) (if (s:member elt ans =) ans (cons elt ans)))
(fold (lambda (elt ans) (if (s:member elt ans =) ans (cons elt ans))) lis elts))
lis elts))
(define (lset-union = . lists)
(check-arg procedure? = 'lset-union)
(define (lset-union = . lists) (reduce (lambda (lis ans) ; Compute ANS + LIS.
(check-arg procedure? = 'lset-union) (cond ((null? lis) ans) ; Don't copy any lists
(reduce (lambda (lis ans) ; Compute ANS + LIS. ((null? ans) lis) ; if we don't have to.
(cond ((null? lis) ans) ; Don't copy any lists ((eq? lis ans) ans)
((null? ans) lis) ; if we don't have to. (else
((eq? lis ans) ans) (fold (lambda (elt ans)
(else (if (any (lambda (x) (= x elt)) ans)
(fold (lambda (elt ans) (if (any (lambda (x) (= x elt)) ans) ans
ans (cons elt ans)))
(cons elt ans))) ans lis))))
ans lis)))) '() lists))
'() lists))
#;
#; (define (lset-union! = . lists)
(define (lset-union! = . lists) (check-arg procedure? = 'lset-union!)
(check-arg procedure? = 'lset-union!) (reduce (lambda (lis ans) ; Splice new elts of LIS onto the front of ANS.
(reduce (lambda (lis ans) ; Splice new elts of LIS onto the front of ANS. (cond ((null? lis) ans) ; Don't copy any lists
(cond ((null? lis) ans) ; Don't copy any lists ((null? ans) lis) ; if we don't have to.
((null? ans) lis) ; if we don't have to. ((eq? lis ans) ans)
((eq? lis ans) ans) (else
(else (pair-fold (lambda (pair ans)
(pair-fold (lambda (pair ans) (let ((elt (car pair)))
(let ((elt (car pair))) (if (any (lambda (x) (= x elt)) ans)
(if (any (lambda (x) (= x elt)) ans) ans
ans (begin (set-cdr! pair ans) pair))))
(begin (set-cdr! pair ans) pair)))) ans lis))))
ans lis)))) '() lists))
'() lists))
(define (lset-intersection = lis1 . lists)
(check-arg procedure? = 'lset-intersection)
(define (lset-intersection = lis1 . lists) (let ((lists (delete lis1 lists eq?))) ; Throw out any LIS1 vals.
(check-arg procedure? = 'lset-intersection) (cond ((any null-list? lists) '()) ; Short cut
(let ((lists (delete lis1 lists eq?))) ; Throw out any LIS1 vals. ((null? lists) lis1) ; Short cut
(cond ((any null-list? lists) '()) ; Short cut (else (filter (lambda (x)
((null? lists) lis1) ; Short cut (every (lambda (lis) (s:member x lis =)) lists))
(else (filter (lambda (x) lis1)))))
(every (lambda (lis) (s:member x lis =)) lists))
lis1))))) #;
(define (lset-intersection! = lis1 . lists)
#; (check-arg procedure? = 'lset-intersection!)
(define (lset-intersection! = lis1 . lists) (let ((lists (delete lis1 lists eq?))) ; Throw out any LIS1 vals.
(check-arg procedure? = 'lset-intersection!) (cond ((any null-list? lists) '()) ; Short cut
(let ((lists (delete lis1 lists eq?))) ; Throw out any LIS1 vals. ((null? lists) lis1) ; Short cut
(cond ((any null-list? lists) '()) ; Short cut (else (filter! (lambda (x)
((null? lists) lis1) ; Short cut (every (lambda (lis) (s:member x lis =)) lists))
(else (filter! (lambda (x) lis1)))))
(every (lambda (lis) (s:member x lis =)) lists))
lis1))))) (define (lset-difference = lis1 . lists)
(check-arg procedure? = 'lset-difference)
(let ((lists (filter pair? lists))) ; Throw out empty lists.
(define (lset-difference = lis1 . lists) (cond ((null? lists) lis1) ; Short cut
(check-arg procedure? = 'lset-difference) ((memq lis1 lists) '()) ; Short cut
(let ((lists (filter pair? lists))) ; Throw out empty lists. (else (filter (lambda (x)
(cond ((null? lists) lis1) ; Short cut (every (lambda (lis) (not (s:member x lis =)))
((memq lis1 lists) '()) ; Short cut lists))
(else (filter (lambda (x) lis1)))))
(every (lambda (lis) (not (s:member x lis =)))
lists)) #;
lis1))))) (define (lset-difference! = lis1 . lists)
(check-arg procedure? = 'lset-difference!)
#; (let ((lists (filter pair? lists))) ; Throw out empty lists.
(define (lset-difference! = lis1 . lists) (cond ((null? lists) lis1) ; Short cut
(check-arg procedure? = 'lset-difference!) ((memq lis1 lists) '()) ; Short cut
(let ((lists (filter pair? lists))) ; Throw out empty lists. (else (filter! (lambda (x)
(cond ((null? lists) lis1) ; Short cut (every (lambda (lis) (not (s:member x lis =)))
((memq lis1 lists) '()) ; Short cut lists))
(else (filter! (lambda (x) lis1)))))
(every (lambda (lis) (not (s:member x lis =)))
lists)) (define (lset-xor = . lists)
lis1))))) (check-arg procedure? = 'lset-xor)
(reduce (lambda (b a) ; Compute A xor B:
;; Note that this code relies on the constant-time
(define (lset-xor = . lists) ;; short-cuts provided by LSET-DIFF+INTERSECTION,
(check-arg procedure? = 'lset-xor) ;; LSET-DIFFERENCE & APPEND to provide constant-time short
(reduce (lambda (b a) ; Compute A xor B: ;; cuts for the cases A = (), B = (), and A eq? B. It takes
;; Note that this code relies on the constant-time ;; a careful case analysis to see it, but it's carefully
;; short-cuts provided by LSET-DIFF+INTERSECTION, ;; built in.
;; LSET-DIFFERENCE & APPEND to provide constant-time short ;; Compute a-b and a^b, then compute b-(a^b) and
;; cuts for the cases A = (), B = (), and A eq? B. It takes ;; cons it onto the front of a-b.
;; a careful case analysis to see it, but it's carefully (receive (a-b a-int-b) (lset-diff+intersection = a b)
;; built in. (cond ((null? a-b) (lset-difference = b a))
((null? a-int-b) (append b a))
;; Compute a-b and a^b, then compute b-(a^b) and (else (fold (lambda (xb ans)
;; cons it onto the front of a-b. (if (s:member xb a-int-b =) ans (cons xb ans)))
(receive (a-b a-int-b) (lset-diff+intersection = a b) a-b
(cond ((null? a-b) (lset-difference = b a)) b)))))
((null? a-int-b) (append b a)) '() lists))
(else (fold (lambda (xb ans)
(if (s:member xb a-int-b =) ans (cons xb ans))) #;
a-b (define (lset-xor! = . lists)
b))))) (check-arg procedure? = 'lset-xor!)
'() lists)) (reduce (lambda (b a) ; Compute A xor B:
;; Note that this code relies on the constant-time
#; ;; short-cuts provided by LSET-DIFF+INTERSECTION,
(define (lset-xor! = . lists) ;; LSET-DIFFERENCE & APPEND to provide constant-time short
(check-arg procedure? = 'lset-xor!) ;; cuts for the cases A = (), B = (), and A eq? B. It takes
(reduce (lambda (b a) ; Compute A xor B: ;; a careful case analysis to see it, but it's carefully
;; Note that this code relies on the constant-time ;; built in.
;; short-cuts provided by LSET-DIFF+INTERSECTION, ;; Compute a-b and a^b, then compute b-(a^b) and
;; LSET-DIFFERENCE & APPEND to provide constant-time short ;; cons it onto the front of a-b.
;; cuts for the cases A = (), B = (), and A eq? B. It takes (receive (a-b a-int-b) (lset-diff+intersection! = a b)
;; a careful case analysis to see it, but it's carefully (cond ((null? a-b) (lset-difference! = b a))
;; built in. ((null? a-int-b) (append! b a))
(else (pair-fold
;; Compute a-b and a^b, then compute b-(a^b) and (lambda (b-pair ans)
;; cons it onto the front of a-b. (if (s:member (car b-pair) a-int-b =) ans
(receive (a-b a-int-b) (lset-diff+intersection! = a b) (begin (set-cdr! b-pair ans) b-pair)))
(cond ((null? a-b) (lset-difference! = b a)) a-b
((null? a-int-b) (append! b a)) b)))))
(else (pair-fold (lambda (b-pair ans) '() lists))
(if (s:member (car b-pair) a-int-b =) ans
(begin (set-cdr! b-pair ans) b-pair))) (define (lset-diff+intersection = lis1 . lists)
a-b (check-arg procedure? = 'lset-diff+intersection)
b))))) (cond ((every null-list? lists) (values lis1 '())) ; Short cut
'() lists)) ((memq lis1 lists) (values '() lis1)) ; Short cut
(else (partition (lambda (elt)
(not (any (lambda (lis) (s:member elt lis =))
(define (lset-diff+intersection = lis1 . lists) lists)))
(check-arg procedure? = 'lset-diff+intersection) lis1))))
(cond ((every null-list? lists) (values lis1 '())) ; Short cut
((memq lis1 lists) (values '() lis1)) ; Short cut #;
(else (partition (lambda (elt) (define (lset-diff+intersection! = lis1 . lists)
(not (any (lambda (lis) (s:member elt lis =)) (check-arg procedure? = 'lset-diff+intersection!)
lists))) (cond ((every null-list? lists) (values lis1 '())) ; Short cut
lis1)))) ((memq lis1 lists) (values '() lis1)) ; Short cut
(else (partition! (lambda (elt)
#; (not (any (lambda (lis) (s:member elt lis =))
(define (lset-diff+intersection! = lis1 . lists) lists)))
(check-arg procedure? = 'lset-diff+intersection!) lis1))))
(cond ((every null-list? lists) (values lis1 '())) ; Short cut
((memq lis1 lists) (values '() lis1)) ; Short cut
(else (partition! (lambda (elt)
(not (any (lambda (lis) (s:member elt lis =))
lists)))
lis1))))
)
;;; lset.ss ends here ;;; lset.ss ends here

View File

@ -2,7 +2,7 @@
;;; <misc.ss> ---- Miscellaneous list procedures ;;; <misc.ss> ---- Miscellaneous list procedures
;;; Time-stamp: <02/03/01 13:52:22 noel> ;;; Time-stamp: <02/03/01 13:52:22 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -32,171 +32,161 @@
;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;; -Olin ;; -Olin
(module misc #lang mzscheme
mzscheme
(require srfi/optional (require srfi/optional
"predicate.ss" "predicate.ss"
"selector.ss" "selector.ss"
"util.ss" "util.ss"
(only "fold.ss" reduce-right) (only "fold.ss" reduce-right)
(rename "fold.ss" srfi-1:map map)) (rename "fold.ss" srfi-1:map map)
(require srfi/8/receive) srfi/8/receive)
(provide length+ (provide length+
concatenate concatenate
(rename append append!) (rename append append!)
(rename concatenate concatenate!) (rename concatenate concatenate!)
(rename reverse reverse!) (rename reverse reverse!)
append-reverse append-reverse
(rename append-reverse append-reverse!) (rename append-reverse append-reverse!)
zip zip
unzip1 unzip1
unzip2 unzip2
unzip3 unzip3
unzip4 unzip4
unzip5 unzip5
count) count)
;; count
;;;;;;;;
(define (count pred list1 . lists)
(check-arg procedure? pred 'count)
(if (pair? lists)
;; N-ary case
(let lp ((list1 list1) (lists lists) (i 0))
(if (null-list? list1) i
(receive (as ds) (%cars+cdrs lists)
(if (null? as) i
(lp (cdr list1) ds
(if (apply pred (car list1) as) (+ i 1) i))))))
;; Fast path
(let lp ((lis list1) (i 0))
(if (null-list? lis) i
(lp (cdr lis) (if (pred (car lis)) (+ i 1) i))))))
(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 srfi-1:map list list1 more-lists))
;; Unzippers -- 1 through 5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (unzip1 lis) (map car lis))
(define (unzip2 lis)
(let recur ((lis lis))
(if (null-list? lis) (values lis lis) ; Use NOT-PAIR? to handle
(let ((elt (car lis))) ; dotted lists.
(receive (a b) (recur (cdr lis))
(values (cons (car elt) a)
(cons (cadr elt) b)))))))
(define (unzip3 lis)
(let recur ((lis lis))
(if (null-list? lis) (values lis lis 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)))))))
(define (unzip4 lis)
(let recur ((lis lis))
(if (null-list? lis) (values lis lis lis 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)))))))
(define (unzip5 lis)
(let recur ((lis lis))
(if (null-list? lis) (values lis lis lis lis 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)))))))
;; append! append-reverse append-reverse! concatenate concatenate!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#;
(define (my-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)))))))
;; count ;;(define (append-reverse rev-head tail) (fold cons tail rev-head))
;;;;;;;;
(define (count pred list1 . lists)
(check-arg procedure? pred 'count)
(if (pair? lists)
;; N-ary case ;;(define (append-reverse! rev-head tail)
(let lp ((list1 list1) (lists lists) (i 0)) ;; (pair-fold (lambda (pair tail) (set-cdr! pair tail) pair)
(if (null-list? list1) i ;; tail
(receive (as ds) (%cars+cdrs lists) ;; rev-head))
(if (null? as) i
(lp (cdr list1) ds
(if (apply pred (car list1) as) (+ i 1) i))))))
;; Fast path ;; Hand-inline the FOLD and PAIR-FOLD ops for speed.
(let lp ((lis list1) (i 0))
(if (null-list? lis) i
(lp (cdr lis) (if (pred (car lis)) (+ i 1) i))))))
(define (append-reverse rev-head tail)
(let lp ((rev-head rev-head) (tail tail))
(if (null-list? rev-head) tail
(lp (cdr rev-head) (cons (car rev-head) tail)))))
(define (length+ x) ; Returns #f if X is circular. #;
(let lp ((x x) (lag x) (len 0)) (define (append-reverse! rev-head tail)
(if (pair? x) (let lp ((rev-head rev-head) (tail tail))
(let ((x (cdr x)) (if (null-list? rev-head) tail
(len (+ len 1))) (let ((next-rev (cdr rev-head)))
(if (pair? x) (set-cdr! rev-head tail)
(let ((x (cdr x)) (lp next-rev rev-head)))))
(lag (cdr lag))
(len (+ len 1)))
(and (not (eq? x lag)) (lp x lag len)))
len))
len)))
(define (concatenate lists) (reduce-right append '() lists))
#;
(define (concatenate! lists) (reduce-right my-append! '() lists))
#;
(define (my-reverse! lis)
(let lp ((lis lis) (ans '()))
(if (null-list? lis) ans
(let ((tail (cdr lis)))
(set-cdr! lis ans)
(lp tail lis)))))
(define (zip list1 . more-lists) (apply srfi-1:map list list1 more-lists))
;; Unzippers -- 1 through 5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (unzip1 lis) (map car lis))
(define (unzip2 lis)
(let recur ((lis lis))
(if (null-list? lis) (values lis lis) ; Use NOT-PAIR? to handle
(let ((elt (car lis))) ; dotted lists.
(receive (a b) (recur (cdr lis))
(values (cons (car elt) a)
(cons (cadr elt) b)))))))
(define (unzip3 lis)
(let recur ((lis lis))
(if (null-list? lis) (values lis lis 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)))))))
(define (unzip4 lis)
(let recur ((lis lis))
(if (null-list? lis) (values lis lis lis 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)))))))
(define (unzip5 lis)
(let recur ((lis lis))
(if (null-list? lis) (values lis lis lis lis 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)))))))
;; append! append-reverse append-reverse! concatenate concatenate!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#;
(define (my-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)))))))
;;(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))
;; Hand-inline the FOLD and PAIR-FOLD ops for speed.
(define (append-reverse rev-head tail)
(let lp ((rev-head rev-head) (tail tail))
(if (null-list? rev-head) tail
(lp (cdr rev-head) (cons (car rev-head) tail)))))
#;
(define (append-reverse! rev-head tail)
(let lp ((rev-head rev-head) (tail tail))
(if (null-list? rev-head) tail
(let ((next-rev (cdr rev-head)))
(set-cdr! rev-head tail)
(lp next-rev rev-head)))))
(define (concatenate lists) (reduce-right append '() lists))
#;
(define (concatenate! lists) (reduce-right my-append! '() lists))
#;
(define (my-reverse! lis)
(let lp ((lis lis) (ans '()))
(if (null-list? lis) ans
(let ((tail (cdr lis)))
(set-cdr! lis ans)
(lp tail lis)))))
)
;;; misc.ss ends here ;;; misc.ss ends here

View File

@ -2,7 +2,7 @@
;;; <predicate.ss> ---- List Predicates ;;; <predicate.ss> ---- List Predicates
;;; Time-stamp: <02/02/27 12:57:15 noel> ;;; Time-stamp: <02/02/27 12:57:15 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -33,91 +33,86 @@
;; -Olin ;; -Olin
(module predicate #lang mzscheme
mzscheme
(require srfi/optional) (require srfi/optional)
(provide pair? (provide pair?
null? null?
proper-list? proper-list?
circular-list? circular-list?
dotted-list? dotted-list?
not-pair? not-pair?
null-list? null-list?
list=) list=)
;; <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.
;; This is a legal definition which is fast and sloppy:
;; (define null-list? not-pair?)
;; but we'll provide a more careful one:
(define (null-list? l)
(cond ((pair? l) #f)
((null? l) #t)
(else (error "null-list?: argument out of domain" l))))
(define (list= = . lists)
(or (null? lists) ; special case
(let lp1 ((list-a (car lists)) (others (cdr lists)))
(or (null? others)
(let ((list-b (car others))
(others (cdr others)))
(if (eq? list-a list-b) ; EQ? => LIST=
(lp1 list-b others)
(let lp2 ((la list-a) (lb list-b))
(if (null-list? la)
(and (null-list? lb)
(lp1 list-b others))
(and (not (null-list? lb))
(= (car la) (car lb))
(lp2 (cdr la) (cdr lb)))))))))))
;; <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.
;; This is a legal definition which is fast and sloppy:
;; (define null-list? not-pair?)
;; but we'll provide a more careful one:
(define (null-list? l)
(cond ((pair? l) #f)
((null? l) #t)
(else (error "null-list?: argument out of domain" l))))
(define (list= = . lists)
(or (null? lists) ; special case
(let lp1 ((list-a (car lists)) (others (cdr lists)))
(or (null? others)
(let ((list-b (car others))
(others (cdr others)))
(if (eq? list-a list-b) ; EQ? => LIST=
(lp1 list-b others)
(let lp2 ((la list-a) (lb list-b))
(if (null-list? la)
(and (null-list? lb)
(lp1 list-b others))
(and (not (null-list? lb))
(= (car la) (car lb))
(lp2 (cdr la) (cdr lb)))))))))))
)
;;; predicate.ss ends here ;;; predicate.ss ends here

View File

@ -2,7 +2,7 @@
;;; <search.ss> ---- List searching functions ;;; <search.ss> ---- List searching functions
;;; Time-stamp: <02/02/28 12:11:01 noel> ;;; Time-stamp: <02/02/28 12:11:01 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -32,124 +32,118 @@
;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;; -Olin ;; -Olin
(module search #lang mzscheme
mzscheme
(require mzlib/etc (require mzlib/etc
srfi/optional srfi/optional
"predicate.ss" "predicate.ss"
"util.ss") "util.ss"
(require srfi/8/receive) srfi/8/receive)
(provide (rename my-member member) (provide (rename my-member member)
find find
find-tail find-tail
any any
every every
list-index list-index
take-while take-while
drop-while drop-while
(rename take-while take-while!) (rename take-while take-while!)
span span
break break
(rename span span!) (rename span span!)
(rename break break!)) (rename break break!))
;; Extended from R4RS to take an optional comparison argument. ;; Extended from R4RS to take an optional comparison argument.
(define my-member (define my-member
(opt-lambda (x lis (maybe-= equal?)) (opt-lambda (x lis (maybe-= equal?))
(let ((= maybe-=)) (let ((= maybe-=))
(find-tail (lambda (y) (= x y)) lis)))) (find-tail (lambda (y) (= x y)) lis))))
;; find find-tail take-while drop-while span break any every list-index ;; find find-tail take-while drop-while span break any every list-index
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (find pred list) (define (find pred list)
(cond ((find-tail pred list) => car) (cond ((find-tail pred list) => car)
(else #f))) (else #f)))
(define (find-tail pred list) (define (find-tail pred list)
(check-arg procedure? pred 'find-tail) (check-arg procedure? pred 'find-tail)
(let lp ((list list)) (let lp ((list list))
(and (not (null-list? list)) (and (not (null-list? list))
(if (pred (car list)) list (if (pred (car list)) list
(lp (cdr list)))))) (lp (cdr list))))))
(define (take-while pred lis) (define (take-while pred lis)
(check-arg procedure? pred 'take-while) (check-arg procedure? pred 'take-while)
(let recur ((lis lis)) (let recur ((lis lis))
(if (null-list? lis) '() (if (null-list? lis) '()
(let ((x (car lis))) (let ((x (car lis)))
(if (pred x) (if (pred x)
(cons x (recur (cdr lis))) (cons x (recur (cdr lis)))
'()))))) '())))))
(define (drop-while pred lis) (define (drop-while pred lis)
(check-arg procedure? pred 'drop-while) (check-arg procedure? pred 'drop-while)
(let lp ((lis lis)) (let lp ((lis lis))
(if (null-list? lis) '() (cond ((null-list? lis) '())
(if (pred (car lis)) ((pred (car lis)) (lp (cdr lis)))
(lp (cdr lis)) (else lis))))
lis))))
#;
(define (take-while! pred lis)
(check-arg procedure? pred 'take-while!)
(if (or (null-list? lis) (not (pred (car lis)))) '()
(begin (let lp ((prev lis) (rest (cdr lis)))
(if (pair? rest)
(let ((x (car rest)))
(if (pred x) (lp rest (cdr rest))
(set-cdr! prev '())))))
lis)))
(define (span pred lis)
(check-arg procedure? pred 'span)
(let recur ((lis lis))
(if (null-list? lis) (values '() '())
(let ((x (car lis)))
(if (pred x)
(receive (prefix suffix) (recur (cdr lis))
(values (cons x prefix) suffix))
(values '() lis))))))
#; #;
(define (span! pred lis) (define (take-while! pred lis)
(check-arg procedure? pred 'span!) (check-arg procedure? pred 'take-while!)
(if (or (null-list? lis) (not (pred (car lis)))) (values '() lis) (if (or (null-list? lis) (not (pred (car lis)))) '()
(let ((suffix (let lp ((prev lis) (rest (cdr lis))) (begin (let lp ((prev lis) (rest (cdr lis)))
(if (null-list? rest) rest (if (pair? rest)
(let ((x (car rest))) (let ((x (car rest)))
(if (pred x) (lp rest (cdr rest)) (if (pred x) (lp rest (cdr rest))
(begin (set-cdr! prev '()) (set-cdr! prev '())))))
rest))))))) lis)))
(values lis suffix))))
(define (break pred lis) (span (lambda (x) (not (pred x))) lis))
#;
(define (break! pred lis) (span! (lambda (x) (not (pred x))) lis))
(define (any pred lis1 . lists)
(check-arg procedure? pred 'any)
(if (pair? lists)
;; N-ary case
(receive (heads tails) (%cars+cdrs (cons lis1 lists))
(and (pair? heads)
(let lp ((heads heads) (tails tails))
(receive (next-heads next-tails) (%cars+cdrs tails)
(if (pair? next-heads)
(or (apply pred heads) (lp next-heads next-tails))
(apply pred heads)))))) ; Last PRED app is tail call.
;; Fast path
(and (not (null-list? lis1))
(let lp ((head (car lis1)) (tail (cdr lis1)))
(if (null-list? tail)
(pred head) ; Last PRED app is tail call.
(or (pred head) (lp (car tail) (cdr tail))))))))
(define (span pred lis)
(check-arg procedure? pred 'span)
(let recur ((lis lis))
(if (null-list? lis) (values '() '())
(let ((x (car lis)))
(if (pred x)
(receive (prefix suffix) (recur (cdr lis))
(values (cons x prefix) suffix))
(values '() lis))))))
#;
(define (span! pred lis)
(check-arg procedure? pred 'span!)
(if (or (null-list? lis) (not (pred (car lis)))) (values '() lis)
(let ((suffix (let lp ((prev lis) (rest (cdr lis)))
(if (null-list? rest) rest
(let ((x (car rest)))
(if (pred x) (lp rest (cdr rest))
(begin (set-cdr! prev '())
rest)))))))
(values lis suffix))))
(define (break pred lis) (span (lambda (x) (not (pred x))) lis))
#;
(define (break! pred lis) (span! (lambda (x) (not (pred x))) lis))
(define (any pred lis1 . lists)
(check-arg procedure? pred 'any)
(if (pair? lists)
;; N-ary case
(receive (heads tails) (%cars+cdrs (cons lis1 lists))
(and (pair? heads)
(let lp ((heads heads) (tails tails))
(receive (next-heads next-tails) (%cars+cdrs tails)
(if (pair? next-heads)
(or (apply pred heads) (lp next-heads next-tails))
(apply pred heads)))))) ; Last PRED app is tail call.
;; Fast path
(and (not (null-list? lis1))
(let lp ((head (car lis1)) (tail (cdr lis1)))
(if (null-list? tail)
(pred head) ; Last PRED app is tail call.
(or (pred head) (lp (car tail) (cdr tail))))))))
;(define (every pred list) ; Simple definition. ;(define (every pred list) ; Simple definition.
; (let lp ((list list)) ; Doesn't return the last PRED value. ; (let lp ((list list)) ; Doesn't return the last PRED value.
@ -157,41 +151,36 @@
; (and (pred (car list)) ; (and (pred (car list))
; (lp (cdr list)))))) ; (lp (cdr list))))))
(define (every pred lis1 . lists) (define (every pred lis1 . lists)
(check-arg procedure? pred 'every) (check-arg procedure? pred 'every)
(if (pair? lists) (if (pair? lists)
;; N-ary case
;; N-ary case (receive (heads tails) (%cars+cdrs (cons lis1 lists))
(receive (heads tails) (%cars+cdrs (cons lis1 lists)) (or (not (pair? heads))
(or (not (pair? heads)) (let lp ((heads heads) (tails tails))
(let lp ((heads heads) (tails tails)) (receive (next-heads next-tails) (%cars+cdrs tails)
(receive (next-heads next-tails) (%cars+cdrs tails) (if (pair? next-heads)
(if (pair? next-heads) (and (apply pred heads) (lp next-heads next-tails))
(and (apply pred heads) (lp next-heads next-tails)) (apply pred heads)))))) ; Last PRED app is tail call.
(apply pred heads)))))) ; Last PRED app is tail call. ;; Fast path
(or (null-list? lis1)
;; Fast path (let lp ((head (car lis1)) (tail (cdr lis1)))
(or (null-list? lis1) (if (null-list? tail)
(let lp ((head (car lis1)) (tail (cdr lis1))) (pred head) ; Last PRED app is tail call.
(if (null-list? tail) (and (pred head) (lp (car tail) (cdr tail))))))))
(pred head) ; Last PRED app is tail call.
(and (pred head) (lp (car tail) (cdr tail))))))))
(define (list-index pred lis1 . lists) (define (list-index pred lis1 . lists)
(check-arg procedure? pred 'list-index) (check-arg procedure? pred 'list-index)
(if (pair? lists) (if (pair? lists)
;; N-ary case
;; N-ary case (let lp ((lists (cons lis1 lists)) (n 0))
(let lp ((lists (cons lis1 lists)) (n 0)) (receive (heads tails) (%cars+cdrs lists)
(receive (heads tails) (%cars+cdrs lists) (and (pair? heads)
(and (pair? heads) (if (apply pred heads) n
(if (apply pred heads) n (lp tails (+ n 1))))))
(lp tails (+ n 1)))))) ;; Fast path
(let lp ((lis lis1) (n 0))
(and (not (null-list? lis))
(if (pred (car lis)) n (lp (cdr lis) (+ n 1)))))))
;; Fast path
(let lp ((lis lis1) (n 0))
(and (not (null-list? lis))
(if (pred (car lis)) n (lp (cdr lis) (+ n 1)))))))
)
;;; search.ss ends here ;;; search.ss ends here

View File

@ -2,7 +2,7 @@
;;; <selector.ss> ---- List selectors ;;; <selector.ss> ---- List selectors
;;; Time-stamp: <02/02/27 12:49:44 noel> ;;; Time-stamp: <02/02/27 12:49:44 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -32,119 +32,112 @@
;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;; -Olin ;; -Olin
(module selector #lang mzscheme
mzscheme
(require srfi/optional) (require srfi/optional
(require srfi/8/receive) srfi/8/receive)
(provide (provide first second
first second third fourth
third fourth fifth sixth
fifth sixth seventh eighth
seventh eighth ninth tenth
ninth tenth car+cdr
car+cdr take drop
take drop take-right drop-right
take-right drop-right (rename take take!) (rename drop-right drop-right!)
(rename take take!) (rename drop-right drop-right!) split-at (rename split-at split-at!)
split-at (rename split-at split-at!) last
last last-pair)
last-pair)
(define first car) (define first car)
(define second cadr) (define second cadr)
(define third caddr) (define third caddr)
(define fourth cadddr) (define fourth cadddr)
(define (fifth x) (car (cddddr x))) (define (fifth x) (car (cddddr x)))
(define (sixth x) (cadr (cddddr x))) (define (sixth x) (cadr (cddddr x)))
(define (seventh x) (caddr (cddddr x))) (define (seventh x) (caddr (cddddr x)))
(define (eighth x) (cadddr (cddddr x))) (define (eighth x) (cadddr (cddddr x)))
(define (ninth x) (car (cddddr (cddddr x)))) (define (ninth x) (car (cddddr (cddddr x))))
(define (tenth x) (cadr (cddddr (cddddr x)))) (define (tenth x) (cadr (cddddr (cddddr x))))
(define (car+cdr pair) (values (car pair) (cdr pair))) (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)))
;; TAKE-RIGHT and DROP-RIGHT work by getting two pointers into the list,
;; off by K, then chasing down the list until the lead pointer falls off
;; the end.
(define (take-right lis k)
(check-arg integer? k 'take-right)
(let lp ((lag lis) (lead (drop lis k)))
(if (pair? lead)
(lp (cdr lag) (cdr lead))
lag)))
(define (drop-right lis k)
(check-arg integer? k 'drop-right)
(let recur ((lag lis) (lead (drop lis k)))
(if (pair? lead)
(cons (car lag) (recur (cdr lag) (cdr lead)))
'())))
;; In this function, LEAD is actually K+1 ahead of LAG. This lets
;; us stop LAG one step early, in time to smash its cdr to ().
#;
(define (drop-right! lis k)
(check-arg integer? k 'drop-right!)
(let ((lead (drop lis k)))
(if (pair? lead)
(let lp ((lag lis) (lead (cdr lead))) ; Standard case
(if (pair? lead)
(lp (cdr lag) (cdr lead))
(begin (set-cdr! lag '())
lis)))
'()))) ; Special case dropping everything -- no cons to side-effect.
(define (split-at x k)
(check-arg integer? k 'split-at)
(let recur ((lis x) (k k))
(if (zero? k) (values '() lis)
(receive (prefix suffix) (recur (cdr lis) (- k 1))
(values (cons (car lis) prefix) suffix)))))
#;
(define (split-at! x k)
(check-arg integer? k 'split-at!)
(if (zero? k) (values '() x)
(let* ((prev (drop x (- k 1)))
(suffix (cdr prev)))
(set-cdr! prev '())
(values x suffix))))
(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))))
;; 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)))
;; TAKE-RIGHT and DROP-RIGHT work by getting two pointers into the list,
;; off by K, then chasing down the list until the lead pointer falls off
;; the end.
(define (take-right lis k)
(check-arg integer? k 'take-right)
(let lp ((lag lis) (lead (drop lis k)))
(if (pair? lead)
(lp (cdr lag) (cdr lead))
lag)))
(define (drop-right lis k)
(check-arg integer? k 'drop-right)
(let recur ((lag lis) (lead (drop lis k)))
(if (pair? lead)
(cons (car lag) (recur (cdr lag) (cdr lead)))
'())))
;; In this function, LEAD is actually K+1 ahead of LAG. This lets
;; us stop LAG one step early, in time to smash its cdr to ().
#;
(define (drop-right! lis k)
(check-arg integer? k 'drop-right!)
(let ((lead (drop lis k)))
(if (pair? lead)
(let lp ((lag lis) (lead (cdr lead))) ; Standard case
(if (pair? lead)
(lp (cdr lag) (cdr lead))
(begin (set-cdr! lag '())
lis)))
'()))) ; Special case dropping everything -- no cons to side-effect.
(define (split-at x k)
(check-arg integer? k 'split-at)
(let recur ((lis x) (k k))
(if (zero? k) (values '() lis)
(receive (prefix suffix) (recur (cdr lis) (- k 1))
(values (cons (car lis) prefix) suffix)))))
#;
(define (split-at! x k)
(check-arg integer? k 'split-at!)
(if (zero? k) (values '() x)
(let* ((prev (drop x (- k 1)))
(suffix (cdr prev)))
(set-cdr! prev '())
(values x suffix))))
(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))))
)
;;; selector.ss ends here ;;; selector.ss ends here

View File

@ -2,7 +2,7 @@
;;; <util.ss> ---- Utility functions ;;; <util.ss> ---- Utility functions
;;; Time-stamp: <02/02/28 12:05:00 noel> ;;; Time-stamp: <02/02/28 12:05:00 noel>
;;; ;;;
;;; Copyright (C) 2002 by Noel Welsh. ;;; Copyright (C) 2002 by Noel Welsh.
;;; ;;;
;;; This file is part of SRFI-1. ;;; This file is part of SRFI-1.
@ -32,94 +32,91 @@
;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
;; -Olin ;; -Olin
(module util #lang mzscheme
mzscheme
(require srfi/optional (require srfi/optional
"predicate.ss" "predicate.ss"
"selector.ss") "selector.ss"
(require srfi/8/receive) srfi/8/receive)
(provide %cdrs (provide %cdrs
%cars+ %cars+
%cars+cdrs %cars+cdrs
%cars+cdrs+ %cars+cdrs+
%cars+cdrs/no-test) %cars+cdrs/no-test)
;; Fold/map internal utilities ;; Fold/map internal utilities
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; These little internal utilities are used by the general ;; These little internal utilities are used by the general
;; fold & mapper funs for the n-ary cases . It'd be nice if they got inlined. ;; fold & mapper funs for the n-ary cases . It'd be nice if they got inlined.
;; One the other hand, the n-ary cases are painfully inefficient as it is. ;; One the other hand, the n-ary cases are painfully inefficient as it is.
;; An aggressive implementation should simply re-write these functions ;; An aggressive implementation should simply re-write these functions
;; for raw efficiency; I have written them for as much clarity, portability, ;; for raw efficiency; I have written them for as much clarity, portability,
;; and simplicity as can be achieved. ;; and simplicity as can be achieved.
;; ;;
;; I use the dreaded call/cc to do local aborts. A good compiler could ;; I use the dreaded call/cc to do local aborts. A good compiler could
;; handle this with extreme efficiency. An implementation that provides ;; handle this with extreme efficiency. An implementation that provides
;; a one-shot, non-persistent continuation grabber could help the compiler ;; a one-shot, non-persistent continuation grabber could help the compiler
;; out by using that in place of the call/cc's in these routines. ;; out by using that in place of the call/cc's in these routines.
;; ;;
;; These functions have funky definitions that are precisely tuned to ;; These functions have funky definitions that are precisely tuned to
;; the needs of the fold/map procs -- for example, to minimize the number ;; the needs of the fold/map procs -- for example, to minimize the number
;; of times the argument lists need to be examined. ;; of times the argument lists need to be examined.
;; Return (map cdr lists). ;; Return (map cdr lists).
;; However, if any element of LISTS is empty, just abort and return '(). ;; However, if any element of LISTS is empty, just abort and return '().
(define (%cdrs lists) (define (%cdrs lists)
(call-with-escape-continuation (call-with-escape-continuation
(lambda (abort) (lambda (abort)
(let recur ((lists lists)) (let recur ((lists lists))
(if (pair? lists) (if (pair? lists)
(let ((lis (car lists))) (let ((lis (car lists)))
(if (null-list? lis) (abort '()) (if (null-list? lis) (abort '())
(cons (cdr lis) (recur (cdr lists))))) (cons (cdr lis) (recur (cdr lists)))))
'()))))) '())))))
(define (%cars+ lists last-elt) ; (append! (map car lists) (list last-elt)) (define (%cars+ lists last-elt) ; (append! (map car lists) (list last-elt))
(let recur ((lists lists)) (let recur ((lists lists))
(if (pair? lists) (cons (caar lists) (recur (cdr lists))) (list last-elt)))) (if (pair? lists) (cons (caar lists) (recur (cdr lists))) (list last-elt))))
;; LISTS is a (not very long) non-empty list of lists. ;; LISTS is a (not very long) non-empty list of lists.
;; Return two lists: the cars & the cdrs of the lists. ;; Return two lists: the cars & the cdrs of the lists.
;; However, if any of the lists is empty, just abort and return [() ()]. ;; However, if any of the lists is empty, just abort and return [() ()].
(define (%cars+cdrs lists) (define (%cars+cdrs lists)
(call-with-escape-continuation (call-with-escape-continuation
(lambda (abort) (lambda (abort)
(let recur ((lists lists)) (let recur ((lists lists))
(if (pair? lists) (if (pair? lists)
(receive (list other-lists) (car+cdr lists) (receive (list other-lists) (car+cdr lists)
(if (null-list? list) (abort '() '()) ; LIST is empty -- bail out (if (null-list? list) (abort '() '()) ; LIST is empty -- bail out
(receive (a d) (car+cdr list) (receive (a d) (car+cdr list)
(receive (cars cdrs) (recur other-lists) (receive (cars cdrs) (recur other-lists)
(values (cons a cars) (cons d cdrs)))))) (values (cons a cars) (cons d cdrs))))))
(values '() '())))))) (values '() '()))))))
;; Like %CARS+CDRS, but we pass in a final elt tacked onto the end of the ;; Like %CARS+CDRS, but we pass in a final elt tacked onto the end of the
;; cars list. What a hack. ;; cars list. What a hack.
(define (%cars+cdrs+ lists cars-final) (define (%cars+cdrs+ lists cars-final)
(call-with-escape-continuation (call-with-escape-continuation
(lambda (abort) (lambda (abort)
(let recur ((lists lists)) (let recur ((lists lists))
(if (pair? lists) (if (pair? lists)
(receive (list other-lists) (car+cdr lists) (receive (list other-lists) (car+cdr lists)
(if (null-list? list) (abort '() '()) ; LIST is empty -- bail out (if (null-list? list) (abort '() '()) ; LIST is empty -- bail out
(receive (a d) (car+cdr list) (receive (a d) (car+cdr list)
(receive (cars cdrs) (recur other-lists) (receive (cars cdrs) (recur other-lists)
(values (cons a cars) (cons d cdrs)))))) (values (cons a cars) (cons d cdrs))))))
(values (list cars-final) '())))))) (values (list cars-final) '()))))))
;; Like %CARS+CDRS, but blow up if any list is empty. ;; Like %CARS+CDRS, but blow up if any list is empty.
(define (%cars+cdrs/no-test lists) (define (%cars+cdrs/no-test lists)
(let recur ((lists lists)) (let recur ((lists lists))
(if (pair? lists) (if (pair? lists)
(receive (list other-lists) (car+cdr lists) (receive (list other-lists) (car+cdr lists)
(receive (a d) (car+cdr list) (receive (a d) (car+cdr list)
(receive (cars cdrs) (recur other-lists) (receive (cars cdrs) (recur other-lists)
(values (cons a cars) (cons d cdrs))))) (values (cons a cars) (cons d cdrs)))))
(values '() '())))) (values '() '()))))
)
;;; util.ss ends here ;;; util.ss ends here