"Fossies" - the Fresh Open Source Software Archive

Member "slib-3b6/comlist.scm" (7 Feb 2020, 10700 Bytes) of package /linux/privat/slib-3b6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lisp source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "comlist.scm": 3b5_vs_3b6.

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