2014-02-01 08:45:59 -05:00
|
|
|
(define-library (srfi 1)
|
2014-02-11 02:50:08 -05:00
|
|
|
(import (scheme base)
|
2014-06-26 09:44:38 -04:00
|
|
|
(scheme cxr))
|
2014-02-01 08:45:59 -05:00
|
|
|
|
|
|
|
;; # Constructors
|
|
|
|
;; cons list
|
|
|
|
;; xcons cons* make-list list-tabulate
|
|
|
|
;; list-copy circular-list iota
|
2014-02-01 09:20:13 -05:00
|
|
|
(define (xcons a b)
|
|
|
|
(cons b a))
|
|
|
|
|
2014-02-11 02:50:08 -05:00
|
|
|
;; means for inter-referential definition
|
|
|
|
(define append-reverse #f)
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-08 09:11:17 -05:00
|
|
|
(define (cons* x . args)
|
2014-02-10 15:06:05 -05:00
|
|
|
(let rec ((acc '()) (x x) (lst args))
|
2014-02-08 09:11:17 -05:00
|
|
|
(if (null? lst)
|
2014-06-26 09:44:38 -04:00
|
|
|
(append-reverse acc x)
|
|
|
|
(rec (cons x acc) (car lst) (cdr lst)))))
|
2014-02-08 09:11:17 -05:00
|
|
|
|
|
|
|
(define (list-tabulate n init-proc)
|
2014-02-10 15:06:05 -05:00
|
|
|
(let rec ((acc '()) (n (- n 1)))
|
2014-02-08 09:11:17 -05:00
|
|
|
(if (zero? n)
|
2014-06-26 09:44:38 -04:00
|
|
|
(cons n acc)
|
|
|
|
(rec (cons n acc) (- n 1)))))
|
2014-02-08 09:11:17 -05:00
|
|
|
|
|
|
|
(define (circular-list elt . args)
|
|
|
|
(let ((lst (cons elt args)))
|
|
|
|
(let rec ((l lst))
|
2014-06-26 09:44:38 -04:00
|
|
|
(if (null? (cdr l))
|
|
|
|
(set-cdr! l lst)
|
|
|
|
(rec (cdr l))))
|
2014-02-08 09:11:17 -05:00
|
|
|
lst))
|
|
|
|
|
|
|
|
(define (iota count . lst)
|
|
|
|
(let ((start (if (pair? lst) (car lst) 0))
|
2014-06-26 09:44:38 -04:00
|
|
|
(step (if (and (pair? lst) (pair? (cdr lst)))
|
|
|
|
(cadr lst) 1)))
|
2014-02-10 15:06:05 -05:00
|
|
|
(let rec ((count (- count 1)) (acc '()))
|
2014-06-26 09:44:38 -04:00
|
|
|
(if (zero? count)
|
|
|
|
(cons start acc)
|
|
|
|
(rec (- count 1)
|
|
|
|
(cons (+ start (* count step)) acc))))))
|
2014-02-08 09:11:17 -05:00
|
|
|
|
|
|
|
(export cons list xcons make-list list-tabulate list-copy circular-list iota)
|
2014-02-01 08:45:59 -05:00
|
|
|
|
|
|
|
;; # Predicates
|
|
|
|
;; pair? null?
|
2014-02-08 09:11:17 -05:00
|
|
|
;; proper-list? circular-list? dotted-list?
|
2014-02-01 08:45:59 -05:00
|
|
|
;; not-pair? null-list?
|
|
|
|
;; list=
|
2014-02-01 09:20:13 -05:00
|
|
|
(define (not-pair? x)
|
|
|
|
(not (pair? x)))
|
2014-03-16 21:43:39 -04:00
|
|
|
;; detects circular list using Floyd's cycle-finding algorithm
|
2014-02-08 09:11:17 -05:00
|
|
|
(define (circular-list? x)
|
2014-03-16 21:43:39 -04:00
|
|
|
(let rec ((rapid x) (local x))
|
|
|
|
(if (and (pair? rapid) (pair? (cdr rapid)))
|
2014-06-26 09:44:38 -04:00
|
|
|
(if (eq? (cddr rapid) (cdr local))
|
|
|
|
#t
|
|
|
|
(rec (cddr rapid) (cdr local)))
|
|
|
|
#f)))
|
2014-02-08 09:11:17 -05:00
|
|
|
|
2014-03-16 21:43:39 -04:00
|
|
|
(define proper-list? list?)
|
2014-02-08 09:11:17 -05:00
|
|
|
|
|
|
|
(define (dotted-list? x)
|
|
|
|
(and (pair? x)
|
2014-06-26 09:44:38 -04:00
|
|
|
(not (proper-list? x))
|
|
|
|
(not (circular-list? x))))
|
2014-02-08 09:11:17 -05:00
|
|
|
|
|
|
|
(define (null-list? x)
|
|
|
|
(cond ((pair? x) #f)
|
2014-06-26 09:44:38 -04:00
|
|
|
((null? x) #t)
|
|
|
|
(else (error "null-list?: argument out of domain" x))))
|
2014-02-08 09:11:17 -05:00
|
|
|
|
|
|
|
(define (list= elt= . lists)
|
|
|
|
(or (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec1 ((list1 (car lists)) (others (cdr lists)))
|
|
|
|
(or (null? others)
|
|
|
|
(let ((list2 (car others))
|
|
|
|
(others (cdr others)))
|
|
|
|
(if (eq? list1 list2)
|
|
|
|
(rec1 list2 others)
|
|
|
|
(let rec2 ((l1 list1) (l2 list2))
|
|
|
|
(if (null-list? l1)
|
|
|
|
(and (null-list? l2)
|
|
|
|
(rec1 list2 others))
|
|
|
|
(and (not (null-list? l2))
|
|
|
|
(elt= (car l1) (car l2))
|
|
|
|
(rec2 (cdr l1) (cdr l2)))))))))))
|
2014-02-08 09:11:17 -05:00
|
|
|
|
|
|
|
(export pair? null? not-pair? proper-list? circular-list? null-list? list=)
|
2014-02-01 08:45:59 -05:00
|
|
|
|
|
|
|
;; # Selectors
|
|
|
|
;; car cdr ... cddadr cddddr list-ref
|
|
|
|
;; first second third fourth fifth sixth seventh eighth ninth tenth
|
|
|
|
;; car+cdr
|
|
|
|
;; take drop
|
|
|
|
;; take-right drop-right
|
|
|
|
;; take! drop-right!
|
|
|
|
;; split-at split-at!
|
|
|
|
;; last last-pair
|
2014-02-01 09:20:13 -05:00
|
|
|
(define (car+cdr pair)
|
|
|
|
(values (car pair) (cdr pair)))
|
|
|
|
|
|
|
|
(define (take x i)
|
|
|
|
(if (zero? i)
|
|
|
|
'()
|
|
|
|
(cons (car x)
|
|
|
|
(take (cdr x) (- i 1)))))
|
|
|
|
|
|
|
|
(define (drop x i)
|
|
|
|
(if (zero? i)
|
|
|
|
x
|
|
|
|
(drop (cdr x) (- i 1))))
|
|
|
|
|
2014-02-08 12:20:48 -05:00
|
|
|
(define (take-right flist i)
|
|
|
|
(let ((len (length flist)))
|
|
|
|
(drop flist (- len i))))
|
|
|
|
|
|
|
|
(define (drop-right flist i)
|
|
|
|
(let ((len (length flist)))
|
|
|
|
(take flist (- len i))))
|
|
|
|
|
|
|
|
(define (take! x i)
|
|
|
|
(let rec ((lis x) (n (- i 1)))
|
|
|
|
(if (zero? n)
|
2014-06-26 09:44:38 -04:00
|
|
|
(begin (set-cdr! lis '()) x)
|
|
|
|
(rec (cdr lis) (- n 1)))))
|
2014-02-10 09:01:17 -05:00
|
|
|
|
2014-02-08 12:20:48 -05:00
|
|
|
(define (drop-right! flist i)
|
|
|
|
(let ((lead (drop flist i)))
|
|
|
|
(if (not-pair? lead)
|
2014-06-26 09:44:38 -04:00
|
|
|
'()
|
|
|
|
(let rec ((lis1 flist) (lis2 (cdr lead)))
|
|
|
|
(if (pair? lis2)
|
|
|
|
(rec (cdr lis1) (cdr lis2))
|
|
|
|
(begin (set-cdr! lis1 '()) flist))))))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-01 09:20:13 -05:00
|
|
|
(define (split-at x i)
|
|
|
|
(values (take x i) (drop x i)))
|
|
|
|
|
2014-02-08 12:20:48 -05:00
|
|
|
(define (split-at! x i)
|
|
|
|
(values (take! x i) (drop x i)))
|
|
|
|
|
|
|
|
(define (last pair)
|
|
|
|
(car (take-right pair 1)))
|
|
|
|
|
|
|
|
(define (last-pair pair)
|
|
|
|
(take-right pair 1))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-08 12:20:48 -05:00
|
|
|
(define first car)
|
|
|
|
(define second cadr)
|
|
|
|
(define third caddr)
|
|
|
|
(define fourth cadddr)
|
|
|
|
(define (fifth pair)
|
2014-02-11 08:29:25 -05:00
|
|
|
(list-ref pair 4))
|
2014-02-08 12:20:48 -05:00
|
|
|
(define (sixth pair)
|
2014-02-11 08:29:25 -05:00
|
|
|
(list-ref pair 5))
|
2014-02-08 12:20:48 -05:00
|
|
|
(define (seventh pair)
|
2014-02-11 08:29:25 -05:00
|
|
|
(list-ref pair 6))
|
2014-02-08 12:20:48 -05:00
|
|
|
(define (eighth pair)
|
2014-02-11 08:29:25 -05:00
|
|
|
(list-ref pair 7))
|
2014-02-08 12:20:48 -05:00
|
|
|
(define (ninth pair)
|
2014-02-11 08:29:25 -05:00
|
|
|
(list-ref pair 8))
|
2014-02-08 12:20:48 -05:00
|
|
|
(define (tenth pair)
|
2014-02-11 08:29:25 -05:00
|
|
|
(list-ref pair 9))
|
|
|
|
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-08 12:20:48 -05:00
|
|
|
(export car cdr car+cdr list-ref
|
2014-06-26 09:44:38 -04:00
|
|
|
caar cadr cdar cddr caaar caadr cadar caddr cdaar cdadr cddar cdddr
|
|
|
|
caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr cdaaar cdaadr
|
|
|
|
cdadar cdaddr cddaar cddadr cdddar cddddr
|
|
|
|
first second third fourth fifth sixth seventh eighth ninth tenth
|
2014-02-08 12:20:48 -05:00
|
|
|
take drop take-right drop-right take! drop-right!
|
2014-06-26 09:44:38 -04:00
|
|
|
split-at split-at! last last-pair)
|
2014-02-01 08:45:59 -05:00
|
|
|
|
|
|
|
;; # Miscellaneous
|
|
|
|
;; length length+
|
|
|
|
;; append concatenate reverse
|
|
|
|
;; append! concatenate! reverse!
|
|
|
|
;; append-reverse append-reverse!
|
|
|
|
;; zip unzip1 unzip2 unzip3 unzip4 unzip5
|
|
|
|
;; count
|
2014-02-10 09:01:17 -05:00
|
|
|
(define (length+ lst)
|
|
|
|
(if (not (circular-list? lst))
|
2014-06-26 09:44:38 -04:00
|
|
|
(length lst)))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-01 09:20:13 -05:00
|
|
|
(define (concatenate lists)
|
|
|
|
(apply append lists))
|
|
|
|
|
2014-02-10 09:01:17 -05:00
|
|
|
(define (append! . lists)
|
|
|
|
(if (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
'()
|
|
|
|
(let rec ((lst lists))
|
|
|
|
(if (not-pair? (cdr lst))
|
|
|
|
(car lst)
|
|
|
|
(begin (set-cdr! (last-pair (car lst)) (cdr lst))
|
|
|
|
(rec (cdr lst)))))))
|
2014-02-01 09:20:13 -05:00
|
|
|
|
2014-02-10 09:01:17 -05:00
|
|
|
(define (concatenate! lists)
|
|
|
|
(apply append! lists))
|
2014-02-08 09:11:17 -05:00
|
|
|
|
2014-02-10 09:01:17 -05:00
|
|
|
(define (reverse! list)
|
2014-02-10 15:06:05 -05:00
|
|
|
(let rec ((lst list) (acc '()))
|
2014-02-10 09:01:17 -05:00
|
|
|
(if (null? lst)
|
2014-06-26 09:44:38 -04:00
|
|
|
acc
|
|
|
|
(let ((rst (cdr lst)))
|
|
|
|
(set-cdr! lst acc)
|
|
|
|
(rec rst lst)))))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-03-25 01:39:44 -04:00
|
|
|
(set! append-reverse
|
|
|
|
(lambda (rev-head tail)
|
|
|
|
(if (null? rev-head)
|
|
|
|
tail
|
|
|
|
(append-reverse (cdr rev-head) (cons (car rev-head) tail)))))
|
2014-02-10 09:01:17 -05:00
|
|
|
|
|
|
|
(define (append-reverse! rev-head tail)
|
|
|
|
(let ((rst (cdr rev-head)))
|
|
|
|
(if (null? rev-head)
|
2014-06-26 09:44:38 -04:00
|
|
|
tail
|
|
|
|
(begin (set-cdr! rev-head tail)
|
|
|
|
(append-reverse! rst rev-head)))))
|
2014-02-10 09:01:17 -05:00
|
|
|
|
|
|
|
(define (zip . lists)
|
|
|
|
(apply map list lists))
|
2014-02-08 12:20:48 -05:00
|
|
|
|
2014-02-10 09:01:17 -05:00
|
|
|
(define (unzip1 list)
|
|
|
|
(map first list))
|
|
|
|
|
|
|
|
(define (unzip2 list)
|
|
|
|
(values (map first list)
|
2014-06-26 09:44:38 -04:00
|
|
|
(map second list)))
|
2014-02-10 09:01:17 -05:00
|
|
|
|
|
|
|
(define (unzip3 list)
|
|
|
|
(values (map first list)
|
2014-06-26 09:44:38 -04:00
|
|
|
(map second list)
|
|
|
|
(map third list)))
|
2014-02-10 09:01:17 -05:00
|
|
|
|
|
|
|
(define (unzip4 list)
|
|
|
|
(values (map first list)
|
2014-06-26 09:44:38 -04:00
|
|
|
(map second list)
|
|
|
|
(map third list)
|
|
|
|
(map fourth list)))
|
2014-02-10 09:01:17 -05:00
|
|
|
|
2014-02-11 08:53:51 -05:00
|
|
|
(define (unzip5 list)
|
2014-02-10 09:01:17 -05:00
|
|
|
(values (map first list)
|
2014-06-26 09:44:38 -04:00
|
|
|
(map second list)
|
|
|
|
(map third list)
|
|
|
|
(map fourth list)
|
|
|
|
(map fifth list)))
|
2014-02-10 09:20:20 -05:00
|
|
|
|
|
|
|
(define (count pred . clists)
|
|
|
|
(let rec ((tflst (apply map pred clists)) (n 0))
|
|
|
|
(if (null? tflst)
|
2014-06-26 09:44:38 -04:00
|
|
|
n
|
|
|
|
(rec (cdr tflst) (if (car tflst) (+ n 1) n)))))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-10 09:01:17 -05:00
|
|
|
(export length length+
|
2014-06-26 09:44:38 -04:00
|
|
|
append append! concatenate concatenate!
|
|
|
|
reverse reverse! append-reverse append-reverse!
|
|
|
|
zip unzip1 unzip2 unzip3 unzip4 unzip5
|
|
|
|
count)
|
2014-02-01 08:45:59 -05:00
|
|
|
|
|
|
|
;; # Fold, unfold & map
|
|
|
|
;; map for-each
|
|
|
|
;; fold unfold pair-fold reduce
|
|
|
|
;; fold-right unfold-right pair-fold right reduce-right
|
|
|
|
;; append-map append-map!
|
|
|
|
;; map! pair-for-each filter-map map-in-order
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-10 15:06:05 -05:00
|
|
|
;; means for inter-referential definition
|
|
|
|
(define every #f)
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-10 15:06:05 -05:00
|
|
|
(define (fold kons knil clist . clists)
|
|
|
|
(if (null? clists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((acc knil) (clist clist))
|
|
|
|
(if (null? clist)
|
|
|
|
acc
|
|
|
|
(rec (kons (car clist) acc) (cdr clist))))
|
|
|
|
(let rec ((acc knil) (clists (cons clist clists)))
|
|
|
|
(if (every pair? clists)
|
|
|
|
(rec (apply kons (append (map car clists) (list acc)))
|
|
|
|
(map cdr clists))
|
|
|
|
acc))))
|
2014-02-10 15:06:05 -05:00
|
|
|
|
|
|
|
(define (fold-right kons knil clist . clists)
|
|
|
|
(if (null? clists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((clist clist) (cont values))
|
|
|
|
(if (null? clist)
|
|
|
|
(cont knil)
|
|
|
|
(rec (cdr clist) (lambda (x) (cont (kons (car clist) x))))))
|
|
|
|
(let rec ((clists (cons clist clists)) (cont values))
|
|
|
|
(if (every pair? clists)
|
|
|
|
(rec (map cdr clists)
|
|
|
|
(lambda (x)
|
|
|
|
(cont (apply kons (append (map car clists) (list x))))))
|
|
|
|
(cont knil)))))
|
2014-02-10 15:06:05 -05:00
|
|
|
|
|
|
|
(define (pair-fold kons knil clist . clists)
|
|
|
|
(if (null? clists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((acc knil) (clist clist))
|
|
|
|
(if (null? clist)
|
|
|
|
acc
|
|
|
|
(let ((tail (cdr clist)))
|
|
|
|
(rec (kons clist acc) tail))))
|
|
|
|
(let rec ((acc knil) (clists (cons clist clists)))
|
|
|
|
(if (every pair? clists)
|
|
|
|
(let ((tail (map cdr clists)))
|
|
|
|
(rec (apply kons (append clists (list acc)))
|
|
|
|
tail))
|
|
|
|
acc))))
|
2014-02-10 15:06:05 -05:00
|
|
|
|
|
|
|
(define (pair-fold-right kons knil clist . clists)
|
|
|
|
(if (null? clists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((clist clist) (cont values))
|
|
|
|
(if (null? clist)
|
|
|
|
(cont knil)
|
|
|
|
(let ((tail (map cdr clists)))
|
|
|
|
(rec tail (lambda (x) (cont (kons clist x)))))))
|
|
|
|
(let rec ((clists (cons clist clists)) (cont values))
|
|
|
|
(if (every pair? clists)
|
|
|
|
(let ((tail (map cdr clists)))
|
|
|
|
(rec tail
|
|
|
|
(lambda (x)
|
|
|
|
(cont (apply kons (append clists (list x)))))))
|
|
|
|
(cont knil)))))
|
2014-02-10 15:06:05 -05:00
|
|
|
|
|
|
|
(define (reduce f ridentity list)
|
|
|
|
(if (null? list)
|
2014-06-26 09:44:38 -04:00
|
|
|
ridentity
|
|
|
|
(fold f (car list) (cdr list))))
|
2014-02-10 15:06:05 -05:00
|
|
|
|
|
|
|
(define (reduce-right f ridentity list)
|
|
|
|
(fold-right f ridentity list))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-10 15:06:05 -05:00
|
|
|
(define (unfold p f g seed . tail-gen)
|
|
|
|
(let ((tail-gen (if (null? tail-gen)
|
2014-06-26 09:44:38 -04:00
|
|
|
(lambda (x) '())
|
|
|
|
(car tail-gen))))
|
2014-02-10 15:06:05 -05:00
|
|
|
(let rec ((seed seed) (cont values))
|
2014-06-26 09:44:38 -04:00
|
|
|
(if (p seed)
|
|
|
|
(cont (tail-gen seed))
|
|
|
|
(rec (g seed) (lambda (x) (cont (cons (f seed) x))))))))
|
2014-02-10 15:06:05 -05:00
|
|
|
|
|
|
|
(define (unfold-right p f g seed . tail)
|
|
|
|
(let rec ((seed seed) (lst tail))
|
|
|
|
(if (p seed)
|
2014-06-26 09:44:38 -04:00
|
|
|
lst
|
|
|
|
(rec (g seed) (cons (f seed) lst)))))
|
2014-02-10 15:06:05 -05:00
|
|
|
|
|
|
|
(define (append-map f . clists)
|
|
|
|
(apply append (apply map f clists)))
|
|
|
|
|
|
|
|
(define (append-map! f . clists)
|
|
|
|
(apply append! (apply map f clists)))
|
|
|
|
|
2014-03-25 01:39:44 -04:00
|
|
|
(define (pair-for-each f clist . clists)
|
|
|
|
(if (null? clist)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((clist clist))
|
|
|
|
(if (pair? clist)
|
|
|
|
(begin (f clist) (rec (cdr clist)))))
|
|
|
|
(let rec ((clists (cons clist clists)))
|
|
|
|
(if (every pair? clists)
|
|
|
|
(begin (apply f clists) (rec (map cdr clists)))))))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-10 15:06:05 -05:00
|
|
|
(define (map! f list . lists)
|
|
|
|
(if (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(pair-for-each (lambda (x) (set-car! x (f (car x)))) list)
|
|
|
|
(let rec ((list list) (lists lists))
|
|
|
|
(if (pair? list)
|
|
|
|
(let ((head (map car lists))
|
|
|
|
(rest (map cdr lists)))
|
|
|
|
(set-car! list (apply f (car list) head))
|
|
|
|
(rec (cdr list) rest)))))
|
2014-02-11 08:53:51 -05:00
|
|
|
list)
|
2014-02-10 15:06:05 -05:00
|
|
|
|
|
|
|
(define (map-in-order f clist . clists)
|
|
|
|
(if (null? clists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((clist clist) (acc '()))
|
|
|
|
(if (null? clist)
|
|
|
|
(reverse! acc)
|
|
|
|
(rec (cdr clist) (cons (f (car clist)) acc))))
|
|
|
|
(let rec ((clists (cons clist clists)) (acc '()))
|
|
|
|
(if (every pair? clists)
|
|
|
|
(rec (map cdr clists)
|
|
|
|
(cons* (apply f (map car clists)) acc))
|
|
|
|
(reverse! acc)))))
|
2014-02-10 15:06:05 -05:00
|
|
|
|
|
|
|
(define (filter-map f clist . clists)
|
2014-06-25 20:42:16 -04:00
|
|
|
(let recur ((l (apply map f clist clists)))
|
|
|
|
(cond ((null? l) '())
|
2014-06-26 09:44:38 -04:00
|
|
|
((car l) (cons (car l) (recur (cdr l))))
|
|
|
|
(else (recur (cdr l))))))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-10 15:06:05 -05:00
|
|
|
(export map for-each
|
2014-06-26 09:44:38 -04:00
|
|
|
fold unfold pair-fold reduce
|
|
|
|
fold-right unfold-right pair-fold-right reduce-right
|
|
|
|
append-map append-map!
|
|
|
|
map! pair-for-each filter-map map-in-order)
|
2014-02-01 08:45:59 -05:00
|
|
|
|
|
|
|
;; # Filtering & partitioning
|
|
|
|
;; filter partition remove
|
|
|
|
;; filter! partition! remove!
|
2014-02-01 09:20:13 -05:00
|
|
|
(define (filter pred list)
|
2014-06-26 05:40:40 -04:00
|
|
|
(let ((pcons (lambda (v acc) (if (pred v) (cons v acc) acc))))
|
|
|
|
(reverse (fold pcons '() list))))
|
2014-02-01 09:20:13 -05:00
|
|
|
|
2014-03-25 01:39:44 -04:00
|
|
|
(define (remove pred list)
|
|
|
|
(filter (lambda (x) (not (pred x))) list))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-01 09:20:13 -05:00
|
|
|
(define (partition pred list)
|
|
|
|
(values (filter pred list)
|
2014-02-10 15:34:32 -05:00
|
|
|
(remove pred list)))
|
2014-02-01 09:20:13 -05:00
|
|
|
|
2014-02-10 15:34:32 -05:00
|
|
|
(define (filter! pred list)
|
|
|
|
(let rec ((lst list))
|
|
|
|
(if (null? lst)
|
2014-06-26 09:44:38 -04:00
|
|
|
lst
|
|
|
|
(if (pred (car lst))
|
|
|
|
(begin (set-cdr! lst (rec (cdr lst)))
|
|
|
|
lst)
|
|
|
|
(rec (cdr lst))))))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-03-25 01:39:44 -04:00
|
|
|
(define (remove! pred list)
|
|
|
|
(filter! (lambda (x) (not (pred x))) list))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-10 15:34:32 -05:00
|
|
|
(define (partition! pred list)
|
|
|
|
(values (filter! pred list)
|
2014-06-26 09:44:38 -04:00
|
|
|
(remove! pred list)))
|
2014-02-10 15:34:32 -05:00
|
|
|
|
|
|
|
(export filter partition remove
|
2014-06-26 09:44:38 -04:00
|
|
|
filter! partition! remove!)
|
2014-02-01 08:45:59 -05:00
|
|
|
|
|
|
|
;; # Searching
|
|
|
|
;; member memq memv
|
|
|
|
;; find find-tail
|
|
|
|
;; any every
|
|
|
|
;; list-index
|
|
|
|
;; take-while drop-while take-while!
|
|
|
|
;; span break span! break!
|
2014-02-10 16:28:36 -05:00
|
|
|
|
2014-02-01 09:20:13 -05:00
|
|
|
(define (find-tail pred list)
|
|
|
|
(if (null? list)
|
|
|
|
#f
|
|
|
|
(if (pred (car list))
|
|
|
|
list
|
|
|
|
(find-tail pred (cdr list)))))
|
|
|
|
|
2014-03-25 01:39:44 -04:00
|
|
|
(define (find pred list)
|
|
|
|
(let ((tail (find-tail pred list)))
|
|
|
|
(if tail
|
|
|
|
(car tail)
|
|
|
|
#f)))
|
|
|
|
|
2014-02-10 16:28:36 -05:00
|
|
|
(define (take-while pred clist)
|
|
|
|
(let rec ((clist clist) (cont values))
|
|
|
|
(if (null? clist)
|
2014-06-26 09:44:38 -04:00
|
|
|
(cont '())
|
|
|
|
(if (pred (car clist))
|
|
|
|
(rec (cdr clist)
|
|
|
|
(lambda (x) (cont (cons (car clist) x))))
|
|
|
|
(cont '())))))
|
2014-02-10 16:28:36 -05:00
|
|
|
|
|
|
|
(define (take-while! pred clist)
|
|
|
|
(let rec ((clist clist))
|
|
|
|
(if (null? clist)
|
2014-06-26 09:44:38 -04:00
|
|
|
'()
|
|
|
|
(if (pred (car clist))
|
|
|
|
(begin (set-cdr! clist (rec (cdr clist)))
|
|
|
|
clist)
|
|
|
|
'()))))
|
2014-02-10 16:28:36 -05:00
|
|
|
|
|
|
|
(define (drop-while pred clist)
|
|
|
|
(let rec ((clist clist))
|
|
|
|
(if (null? clist)
|
2014-06-26 09:44:38 -04:00
|
|
|
'()
|
|
|
|
(if (pred (car clist))
|
|
|
|
(rec (cdr clist))
|
|
|
|
clist))))
|
2014-02-10 16:28:36 -05:00
|
|
|
|
|
|
|
(define (span pred clist)
|
|
|
|
(values (take-while pred clist)
|
2014-06-26 09:44:38 -04:00
|
|
|
(drop-while pred clist)))
|
2014-02-10 16:28:36 -05:00
|
|
|
|
|
|
|
(define (span! pred clist)
|
|
|
|
(values (take-while! pred clist)
|
2014-06-26 09:44:38 -04:00
|
|
|
(drop-while pred clist)))
|
2014-02-01 09:20:13 -05:00
|
|
|
|
2014-02-10 16:28:36 -05:00
|
|
|
(define (break pred clist)
|
|
|
|
(values (take-while (lambda (x) (not (pred x))) clist)
|
2014-06-26 09:44:38 -04:00
|
|
|
(drop-while (lambda (x) (not (pred x))) clist)))
|
2014-02-10 16:28:36 -05:00
|
|
|
|
|
|
|
(define (break! pred clist)
|
|
|
|
(values (take-while! (lambda (x) (not (pred x))) clist)
|
2014-06-26 09:44:38 -04:00
|
|
|
(drop-while (lambda (x) (not (pred x))) clist)))
|
2014-02-10 16:28:36 -05:00
|
|
|
|
|
|
|
(define (any pred clist . clists)
|
|
|
|
(if (null? clists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((clist clist))
|
|
|
|
(if (pair? clist)
|
|
|
|
(or (pred (car clist))
|
|
|
|
(rec (cdr clist)))))
|
|
|
|
(let rec ((clists (cons clist clists)))
|
|
|
|
(if (every pair? clists)
|
|
|
|
(or (apply pred (map car clists))
|
|
|
|
(rec (map cdr clists)))))))
|
2014-02-10 16:28:36 -05:00
|
|
|
|
2014-03-25 01:39:44 -04:00
|
|
|
(set! every
|
|
|
|
(lambda (pred clist . clists)
|
|
|
|
(if (null? clists)
|
|
|
|
(let rec ((clist clist))
|
|
|
|
(or (null? clist)
|
|
|
|
(if (pred (car clist))
|
|
|
|
(rec (cdr clist)))))
|
|
|
|
(let rec ((clists (cons clist clists)))
|
|
|
|
(or (any null? clists)
|
|
|
|
(if (apply pred (map car clists))
|
|
|
|
(rec (map cdr clists))))))))
|
2014-02-10 16:28:36 -05:00
|
|
|
|
|
|
|
(define (list-index pred clist . clists)
|
|
|
|
(if (null? clists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((clist clist) (n 0))
|
|
|
|
(if (pair? clist)
|
|
|
|
(if (pred (car clist))
|
|
|
|
n
|
|
|
|
(rec (cdr clist) (+ n 1)))))
|
|
|
|
(let rec ((clists (cons clist clists)) (n 0))
|
|
|
|
(if (every pair? clists)
|
|
|
|
(if (apply pred (map car clists))
|
|
|
|
n
|
|
|
|
(rec (map cdr clists) (+ n 1)))))))
|
2014-03-25 01:32:56 -04:00
|
|
|
|
2014-02-10 16:28:36 -05:00
|
|
|
(export member memq memv
|
2014-06-26 09:44:38 -04:00
|
|
|
find find-tail
|
|
|
|
any every
|
|
|
|
list-index
|
|
|
|
take-while drop-while take-while!
|
|
|
|
span break span! break!)
|
2014-02-01 08:45:59 -05:00
|
|
|
|
|
|
|
;; # Deleting
|
|
|
|
;; delete delete-duplicates
|
|
|
|
;; delete! delete-duplicates!
|
2014-02-11 02:40:59 -05:00
|
|
|
(define (delete x list . =)
|
|
|
|
(let ((= (if (null? =) equal? (car =))))
|
|
|
|
(remove (lambda (a) (= x a)) list)))
|
|
|
|
|
|
|
|
(define (delete! x list . =)
|
|
|
|
(let ((= (if (null? =) equal? (car =))))
|
|
|
|
(remove! (lambda (a) (= x a)) list)))
|
|
|
|
|
|
|
|
(define (delete-duplicates list . =)
|
|
|
|
(let ((= (if (null? =) equal? (car =))))
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((list list) (cont values))
|
|
|
|
(if (null? list)
|
|
|
|
(cont '())
|
|
|
|
(let* ((x (car list))
|
|
|
|
(rest (cdr list))
|
|
|
|
(deleted (delete x rest =)))
|
|
|
|
(rec deleted (lambda (y) (cont (cons x y)))))))))
|
2014-02-11 02:40:59 -05:00
|
|
|
|
|
|
|
(define (delete-duplicates! list . =)
|
|
|
|
(let ((= (if (null? =) equal? (car =))))
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((list list) (cont values))
|
|
|
|
(if (null? list)
|
|
|
|
(cont '())
|
|
|
|
(let* ((x (car list))
|
|
|
|
(rest (cdr list))
|
|
|
|
(deleted (delete! x list =)))
|
|
|
|
(rec deleted (lambda (y) (cont (cons x y)))))))))
|
2014-02-11 02:40:59 -05:00
|
|
|
|
|
|
|
(export delete delete-duplicates
|
2014-06-26 09:44:38 -04:00
|
|
|
delete! delete-duplicates!)
|
2014-02-01 08:45:59 -05:00
|
|
|
|
|
|
|
;; # Association lists
|
|
|
|
;; assoc assq assv
|
|
|
|
;; alist-cons alist-copy
|
|
|
|
;; alist-delete alist-delete!
|
2014-02-11 02:40:59 -05:00
|
|
|
(define (alist-cons key datum alist)
|
|
|
|
(cons (cons key datum) alist))
|
|
|
|
|
2014-02-11 02:50:08 -05:00
|
|
|
(define (alist-copy alist)
|
|
|
|
(map (lambda (elt) (cons (car elt) (cdr elt))) alist))
|
2014-02-11 02:40:59 -05:00
|
|
|
|
2014-02-11 02:50:08 -05:00
|
|
|
(define (alist-delete key alist . =)
|
|
|
|
(let ((= (if (null? =) equal? (car =))))
|
|
|
|
(remove (lambda (x) (= key (car x))) alist)))
|
|
|
|
|
|
|
|
(define (alist-delete! key alist . =)
|
|
|
|
(let ((= (if (null? =) equal? (car =))))
|
|
|
|
(remove! (lambda (x) (= key (car x))) alist)))
|
|
|
|
|
|
|
|
(export assoc assq assv
|
2014-06-26 09:44:38 -04:00
|
|
|
alist-cons alist-copy
|
|
|
|
alist-delete alist-delete!)
|
2014-02-01 08:45:59 -05:00
|
|
|
|
|
|
|
;; # Set operations on lists
|
|
|
|
;; lset<= lset= lset-adjoin
|
|
|
|
;; lset-union lset-union!
|
|
|
|
;; lset-intersection lset-intersection!
|
|
|
|
;; lset-difference lset-difference!
|
|
|
|
;; lset-xor lset-xor!
|
|
|
|
;; lset-diff+intersenction lset-diff+intersection!
|
2014-02-11 02:50:08 -05:00
|
|
|
(define (lset<= = . lists)
|
|
|
|
(or (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((head (car lists)) (rest (cdr lists)))
|
|
|
|
(or (null? rest)
|
|
|
|
(let ((next (car rest)) (rest (cdr rest)))
|
|
|
|
(and (or (eq? head next)
|
|
|
|
(every (lambda (x) (member x next =)) head))
|
|
|
|
(rec next rest)))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset= = . lists)
|
|
|
|
(or (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(let rec ((head (car lists)) (rest (cdr lists)))
|
|
|
|
(or (null? rest)
|
|
|
|
(let ((next (car rest)) (rest (cdr rest)))
|
|
|
|
(and (or (eq? head next)
|
|
|
|
(and (every (lambda (x) (member x next =)) head)
|
|
|
|
(every (lambda (x) (member x head =)) next))
|
|
|
|
(rec next rest))))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-adjoin = list . elts)
|
|
|
|
(let rec ((list list) (elts elts))
|
|
|
|
(if (null? elts)
|
2014-06-26 09:44:38 -04:00
|
|
|
list
|
|
|
|
(if (member (car elts) list)
|
|
|
|
(rec list (cdr elts))
|
|
|
|
(rec (cons (car elts) list) (cdr elts))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-union = . lists)
|
|
|
|
(if (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
lists
|
|
|
|
(let rec ((head (car lists)) (rest (cdr lists)))
|
|
|
|
(if (null? rest)
|
|
|
|
head
|
|
|
|
(let ((next (car rest)) (rest (cdr rest)))
|
|
|
|
(if (eq? head next)
|
|
|
|
(rec head rest)
|
|
|
|
(rec (apply lset-adjoin = head next) rest)))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-intersection = . lists)
|
|
|
|
(if (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
lists
|
|
|
|
(let rec ((head (car lists)) (rest (cdr lists)))
|
|
|
|
(if (null? rest)
|
|
|
|
head
|
|
|
|
(let ((next (car rest)) (rest (cdr rest)))
|
|
|
|
(if (eq? head next)
|
|
|
|
(rec head rest)
|
|
|
|
(rec (filter (lambda (x) (member x next =)) head)
|
|
|
|
rest)))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-difference = list . lists)
|
|
|
|
(let rec ((head list) (rest lists))
|
|
|
|
(if (null? rest)
|
2014-06-26 09:44:38 -04:00
|
|
|
head
|
|
|
|
(let ((next (car rest)) (rest (cdr rest)))
|
|
|
|
(if (eq? head next)
|
|
|
|
'()
|
|
|
|
(rec (remove (lambda (x) (member x next =)) head)
|
|
|
|
rest))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-xor = . lists)
|
|
|
|
(if (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
lists
|
|
|
|
(let rec ((head (car lists)) (rest (cdr lists)))
|
|
|
|
(if (null? rest)
|
|
|
|
head
|
|
|
|
(let ((next (car rest)) (rest (cdr rest)))
|
|
|
|
(if (eq? head next)
|
|
|
|
'()
|
|
|
|
(rec (append (remove (lambda (x) (member x next =)) head)
|
|
|
|
(remove (lambda (x) (member x head =)) next))
|
|
|
|
rest)))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-diff+intersection = list . lists)
|
|
|
|
(values (apply lset-difference = list lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(lset-intersection = list (apply lset-union lists))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-adjoin! = list . elts)
|
|
|
|
(let rec ((list list) (elts elts))
|
|
|
|
(if (null? elts)
|
2014-06-26 09:44:38 -04:00
|
|
|
list
|
|
|
|
(if (member (car elts) list)
|
|
|
|
(rec list (cdr elts))
|
|
|
|
(let ((tail (cdr elts)))
|
|
|
|
(set-cdr! elts list)
|
|
|
|
(rec elts tail))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-union! = . lists)
|
|
|
|
(letrec ((adjoin
|
2014-06-26 09:44:38 -04:00
|
|
|
(lambda (lst1 lst2)
|
|
|
|
(if (null? lst2)
|
|
|
|
lst1
|
|
|
|
(if (member (car lst2) lst1 =)
|
|
|
|
(adjoin lst1 (cdr lst2))
|
|
|
|
(let ((tail (cdr lst2)))
|
|
|
|
(set-cdr! lst2 lst1)
|
|
|
|
(adjoin lst2 tail)))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
(if (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
lists
|
|
|
|
(let rec ((head (car lists)) (rest (cdr lists)))
|
|
|
|
(if (null? rest)
|
|
|
|
head
|
|
|
|
(let ((next (car rest)) (rest (cdr rest)))
|
|
|
|
(if (eq? head next)
|
|
|
|
(rec head rest)
|
|
|
|
(rec (adjoin head next) rest))))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-intersection! = . lists)
|
|
|
|
(if (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
lists
|
|
|
|
(let rec ((head (car lists)) (rest (cdr lists)))
|
|
|
|
(if (null? rest)
|
|
|
|
head
|
|
|
|
(let ((next (car rest)) (rest (cdr rest)))
|
|
|
|
(if (eq? head next)
|
|
|
|
(rec head rest)
|
|
|
|
(rec (filter! (lambda (x) (member x next =)) head)
|
|
|
|
rest)))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-difference! = list . lists)
|
|
|
|
(let rec ((head list) (rest lists))
|
|
|
|
(if (null? rest)
|
2014-06-26 09:44:38 -04:00
|
|
|
head
|
|
|
|
(let ((next (car rest)) (rest (cdr rest)))
|
|
|
|
(if (eq? head next)
|
|
|
|
'()
|
|
|
|
(rec (remove! (lambda (x) (member x next =)) head)
|
|
|
|
rest))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-xor! = . lists)
|
|
|
|
(if (null? lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
lists
|
|
|
|
(let rec ((head (car lists)) (rest (cdr lists)))
|
|
|
|
(if (null? rest)
|
|
|
|
head
|
|
|
|
(let ((next (car rest)) (rest (cdr rest)))
|
|
|
|
(if (eq? head next)
|
|
|
|
'()
|
|
|
|
(rec (append! (remove! (lambda (x) (member x next =)) head)
|
|
|
|
(remove! (lambda (x) (member x head =)) next))
|
|
|
|
rest)))))))
|
2014-02-11 02:50:08 -05:00
|
|
|
|
|
|
|
(define (lset-diff+intersection! = list . lists)
|
|
|
|
(values (apply lset-difference! = list lists)
|
2014-06-26 09:44:38 -04:00
|
|
|
(lset-intersection! = list (apply lset-union! lists))))
|
2014-02-01 08:45:59 -05:00
|
|
|
|
2014-02-11 07:52:36 -05:00
|
|
|
(export lset<= lset= lset-adjoin
|
2014-06-26 09:44:38 -04:00
|
|
|
lset-union lset-union!
|
|
|
|
lset-intersection lset-intersection!
|
|
|
|
lset-difference lset-difference!
|
|
|
|
lset-xor lset-xor!
|
|
|
|
lset-diff+intersection lset-diff+intersection!)
|
2014-02-11 07:52:36 -05:00
|
|
|
|
2014-02-01 08:45:59 -05:00
|
|
|
;; # Primitive side-effects
|
|
|
|
;; set-car! set-cdr!
|
2014-02-01 09:20:13 -05:00
|
|
|
(export set-car! set-cdr!))
|