1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
; Copyright (C) 1991, 1993, 1995, 2001, 2003 Aubrey Jaffer.
; Copyright (C) 2000 Colin Walters
;
;Permission to copy this software, to modify it, to redistribute it,
;to distribute modified versions, and to use it for any purpose is
;granted, subject to the following restrictions and understandings.
;
;1. Any copy made of this software must include this copyright notice
;in full.
;
;2. I have made no warranty or representation that the operation of
;this software will be error-free, and I am under no obligation to
;provide any services, by way of maintenance, update, or otherwise.
;
;3. In conjunction with products arising from the use of this
;material, there shall be no use of my name in any advertising,
;promotional, or sales literature without prior written consent in
;each case.
;;; Some of these functions may be already defined in your Scheme.
;;; Comment out those definitions for functions which are already defined.
(require 'multiarg-apply)
;;;; LIST FUNCTIONS FROM COMMON LISP
;;; Some tail-recursive optimizations made by
;;; Colin Walters <walters@cis.ohio-state.edu>
;;; AGJ restored order July 2001.
;;;@ From: hugh@ear.mit.edu (Hugh Secker-Walker)
(define (make-list k . init)
(set! init (if (pair? init) (car init)))
(do ((k (+ -1 k) (+ -1 k))
(result '() (cons init result)))
((negative? k) result)))
;@
(define (copy-list lst) (append lst '()))
;@
(define (adjoin obj lst) (if (memv obj lst) lst (cons obj lst)))
;@
(define union
(letrec ((onion
(lambda (lst1 lst2)
(if (null? lst1)
lst2
(onion (cdr lst1) (comlist:adjoin (car lst1) lst2))))))
(lambda (lst1 lst2)
(cond ((null? lst1) lst2)
((null? lst2) lst1)
((null? (cdr lst1)) (comlist:adjoin (car lst1) lst2))
((null? (cdr lst2)) (comlist:adjoin (car lst2) lst1))
((< (length lst2) (length lst1)) (onion (reverse lst2) lst1))
(else (onion (reverse lst1) lst2))))))
;@
(define (intersection lst1 lst2)
(if (null? lst2)
lst2
(let build-intersection ((lst1 lst1)
(result '()))
(cond ((null? lst1) (reverse result))
((memv (car lst1) lst2)
(build-intersection (cdr lst1) (cons (car lst1) result)))
(else
(build-intersection (cdr lst1) result))))))
;@
(define (set-difference lst1 lst2)
(if (null? lst2)
lst1
(let build-difference ((lst1 lst1)
(result '()))
(cond ((null? lst1) (reverse result))
((memv (car lst1) lst2) (build-difference (cdr lst1) result))
(else (build-difference (cdr lst1) (cons (car lst1) result)))))))
;@
(define (subset? lst1 lst2)
(or (eq? lst1 lst2)
(let loop ((lst1 lst1))
(or (null? lst1)
(and (memv (car lst1) lst2)
(loop (cdr lst1)))))))
;@
(define (position obj lst)
(define pos (lambda (n lst)
(cond ((null? lst) #f)
((eqv? obj (car lst)) n)
(else (pos (+ 1 n) (cdr lst))))))
(pos 0 lst))
;@
(define (reduce-init pred? init lst)
(if (null? lst)
init
(comlist:reduce-init pred? (pred? init (car lst)) (cdr lst))))
;@
(define (reduce pred? lst)
(cond ((null? lst) lst)
((null? (cdr lst)) (car lst))
(else (comlist:reduce-init pred? (car lst) (cdr lst)))))
;@
(define (some pred lst . rest)
(cond ((null? rest)
(let mapf ((lst lst))
(and (not (null? lst))
(or (pred (car lst)) (mapf (cdr lst))))))
(else (let mapf ((lst lst) (rest rest))
(and (not (null? lst))
(or (apply pred (car lst) (map car rest))
(mapf (cdr lst) (map cdr rest))))))))
;@
(define (every pred lst . rest)
(cond ((null? rest)
(let mapf ((lst lst))
(or (null? lst)
(and (pred (car lst)) (mapf (cdr lst))))))
(else (let mapf ((lst lst) (rest rest))
(or (null? lst)
(and (apply pred (car lst) (map car rest))
(mapf (cdr lst) (map cdr rest))))))))
;@
(define (notany pred . ls) (not (apply comlist:some pred ls)))
;@
(define (notevery pred . ls) (not (apply comlist:every pred ls)))
;@
(define (list-of?? predicate . bound)
(define (errout) (apply slib:error 'list-of?? predicate bound))
(case (length bound)
((0)
(lambda (obj)
(and (list? obj)
(comlist:every predicate obj))))
((1)
(set! bound (car bound))
(cond ((negative? bound)
(set! bound (- bound))
(lambda (obj)
(and (list? obj)
(<= bound (length obj))
(comlist:every predicate obj))))
(else
(lambda (obj)
(and (list? obj)
(<= (length obj) bound)
(comlist:every predicate obj))))))
((2)
(let ((low (car bound))
(high (cadr bound)))
(cond ((or (negative? low) (negative? high)) (errout))
((< high low)
(set! high (car bound))
(set! low (cadr bound))))
(lambda (obj)
(and (list? obj)
(<= low (length obj) high)
(comlist:every predicate obj)))))
(else (errout))))
;@
(define (find-if pred? lst)
(cond ((null? lst) #f)
((pred? (car lst)) (car lst))
(else (comlist:find-if pred? (cdr lst)))))
;@
(define (member-if pred? lst)
(cond ((null? lst) #f)
((pred? (car lst)) lst)
(else (comlist:member-if pred? (cdr lst)))))
;@
(define (remove obj lst)
(define head (list '*head*))
(let remove ((lst lst)
(tail head))
(cond ((null? lst))
((eqv? obj (car lst)) (remove (cdr lst) tail))
(else
(set-cdr! tail (list (car lst)))
(remove (cdr lst) (cdr tail)))))
(cdr head))
;@
(define (remove-if pred? lst)
(let remove-if ((lst lst)
(result '()))
(cond ((null? lst) (reverse result))
((pred? (car lst)) (remove-if (cdr lst) result))
(else (remove-if (cdr lst) (cons (car lst) result))))))
;@
(define (remove-if-not pred? lst)
(let remove-if-not ((lst lst)
(result '()))
(cond ((null? lst) (reverse result))
((pred? (car lst)) (remove-if-not (cdr lst) (cons (car lst) result)))
(else (remove-if-not (cdr lst) result)))))
;@
(define nconc
(if (provided? 'rev2-procedures) append!
(lambda args
(cond ((null? args) '())
((null? (cdr args)) (car args))
((null? (car args)) (apply comlist:nconc (cdr args)))
(else
(set-cdr! (last-pair (car args))
(apply comlist:nconc (cdr args)))
(car args))))))
;;;@ From: hugh@ear.mit.edu (Hugh Secker-Walker)
(define (nreverse rev-it)
;;; Reverse order of elements of LIST by mutating cdrs.
(cond ((null? rev-it) rev-it)
((not (list? rev-it))
(slib:error "nreverse: Not a list in arg1" rev-it))
(else (do ((reved '() rev-it)
(rev-cdr (cdr rev-it) (cdr rev-cdr))
(rev-it rev-it rev-cdr))
((begin (set-cdr! rev-it reved) (null? rev-cdr)) rev-it)))))
;@
(define (last lst n)
(comlist:nthcdr (- (length lst) n) lst))
;@
(define (butlast lst n)
(comlist:butnthcdr (- (length lst) n) lst))
;@
(define (nthcdr n lst)
(if (zero? n) lst (comlist:nthcdr (+ -1 n) (cdr lst))))
;@
(define (butnthcdr k lst)
(cond ((negative? k) lst) ;(slib:error "negative argument to butnthcdr" k)
; SIMSYNCH FIFO8 uses negative k.
((or (zero? k) (null? lst)) '())
(else (let ((ans (list (car lst))))
(do ((lst (cdr lst) (cdr lst))
(tail ans (cdr tail))
(k (+ -2 k) (+ -1 k)))
((or (negative? k) (null? lst)) ans)
(set-cdr! tail (list (car lst))))))))
;@
(define (butnth k lst)
(cond ((negative? k) lst)
((null? lst) lst)
((zero? k) (cdr lst))
(else (let ((ans (list (car lst))))
(do ((lst (cdr lst) (cdr lst))
(tail ans (cdr tail))
(k (+ -2 k) (+ -1 k)))
((or (negative? k) (null? lst))
(cond ((not (null? lst))
(set-cdr! tail (cdr lst))))
ans)
(set-cdr! tail (list (car lst))))))))
;;;; CONDITIONALS
;@
(define (and? . args)
(cond ((null? args) #t)
((car args) (apply comlist:and? (cdr args)))
(else #f)))
;@
(define (or? . args)
(cond ((null? args) #f)
((car args) #t)
(else (apply comlist:or? (cdr args)))))
;;;@ Checks to see if a list has any duplicate MEMBERs.
(define (has-duplicates? lst)
(cond ((null? lst) #f)
((member (car lst) (cdr lst)) #t)
(else (comlist:has-duplicates? (cdr lst)))))
;;;@ remove duplicates of MEMBERs of a list
(define remove-duplicates
(letrec ((rem-dup
(lambda (lst nlst)
(cond ((null? lst) (reverse nlst))
((member (car lst) nlst) (rem-dup (cdr lst) nlst))
(else (rem-dup (cdr lst) (cons (car lst) nlst)))))))
(lambda (lst)
(rem-dup lst '()))))
;@
(define list*
(letrec ((list*1 (lambda (obj)
(if (null? (cdr obj))
(car obj)
(cons (car obj) (list*1 (cdr obj)))))))
(lambda (obj1 . obj2)
(if (null? obj2)
obj1
(cons obj1 (list*1 obj2))))))
;@
(define (atom? obj)
(not (pair? obj)))
;@
(define (delete obj lst)
(let delete ((lst lst))
(cond ((null? lst) '())
((equal? obj (car lst)) (delete (cdr lst)))
(else
(set-cdr! lst (delete (cdr lst)))
lst))))
;@
(define (delete-if pred lst)
(let delete-if ((lst lst))
(cond ((null? lst) '())
((pred (car lst)) (delete-if (cdr lst)))
(else
(set-cdr! lst (delete-if (cdr lst)))
lst))))
;@
(define (delete-if-not pred lst)
(let delete-if ((lst lst))
(cond ((null? lst) '())
((not (pred (car lst))) (delete-if (cdr lst)))
(else
(set-cdr! lst (delete-if (cdr lst)))
lst))))
;;; internal versions safe from name collisions.
;;(define comlist:make-list make-list)
;;(define comlist:copy-list copy-list)
(define comlist:adjoin adjoin)
;;(define comlist:union union)
;;(define comlist:intersection intersection)
;;(define comlist:set-difference set-difference)
;;(define comlist:subset? subset?)
;;(define comlist:position position)
(define comlist:reduce-init reduce-init)
;;(define comlist:reduce reduce) ; reduce is also in collect.scm
(define comlist:some some)
(define comlist:every every)
;;(define comlist:notevery notevery)
;;(define comlist:notany notany)
(define comlist:find-if find-if)
(define comlist:member-if member-if)
;;(define comlist:remove remove)
;;(define comlist:remove-if remove-if)
;;(define comlist:remove-if-not remove-if-not)
(define comlist:nconc nconc)
;;(define comlist:nreverse nreverse)
;;(define comlist:last last)
;;(define comlist:butlast butlast)
(define comlist:nthcdr nthcdr)
(define comlist:butnthcdr butnthcdr)
(define comlist:butnth butnth)
(define comlist:and? and?)
(define comlist:or? or?)
(define comlist:has-duplicates? has-duplicates?)
;;(define comlist:remove-duplicates remove-duplicates)
;;(define comlist:delete-if-not delete-if-not)
;;(define comlist:delete-if delete-if)
;;(define comlist:delete delete)
;;(define comlist:atom? atom?)
;;(define atom atom?)
;;(define comlist:atom atom?)
;;(define comlist:list* list*)
;;(define comlist:list-of?? list-of??)
|