/usr/share/gauche-0.9/0.9.1/lib/util/list.scm is in gauche 0.9.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ;;;
;;; util/list.scm - more list library
;;;
;;; Copyright (c) 2003-2010 Shiro Kawai <shiro@acm.org>
;;; Copyright(C) 2003 by Alex Shinn (foof@synthcode.com)
;;;
;;; Permission to use, copy, modify, distribute this software and
;;; accompanying documentation for any purpose is hereby granted,
;;; provided that existing copyright notices are retained in all
;;; copies and that this notice is included verbatim in all
;;; distributions.
;;; This software is provided as is, without express or implied
;;; warranty. In no circumstances the author(s) shall be liable
;;; for any damages arising out of the use of this software.
;;;
;; This module adds useful list utility procedures that are not in SRFI-1.
(define-module util.list
(use srfi-1)
(export take* drop* take-right* drop-right* split-at*
slices intersperse cond-list
alist->hash-table hash-table->alist
rassq rassv rassoc
assq-ref assv-ref assoc-ref
rassq-ref rassv-ref rassoc-ref
assq-set! assv-set! assoc-set!)
)
(select-module util.list)
;;-----------------------------------------------------------------
;; permissive take and drop - if the length of given list is shorter
;; than index, returns shorter list or fills the rest.
(define (split-at* lis k :optional (fill? #f) (filler #f))
(when (or (not (integer? k)) (negative? k))
(error "index must be non-negative integer" k))
(let loop ((i 0)
(lis lis)
(r '()))
(cond [(= i k) (values (reverse! r) lis)]
[(null? lis)
(values (if fill?
(append! (reverse! r) (make-list (- k i) filler))
(reverse! r))
lis)]
[else (loop (+ i 1) (cdr lis) (cons (car lis) r))])))
(define (take* lis k . args)
(receive (h t) (apply split-at* lis k args) h))
(define (drop* lis k)
(when (or (not (integer? k)) (negative? k))
(error "index must be non-negative integer" k))
(let loop ((i 0)
(lis lis))
(cond [(= i k) lis]
[(null? lis) '()]
[else (loop (+ i 1) (cdr lis))])))
(define (take-right* lis k :optional (fill? #f) (filler #f))
(when (or (not (integer? k)) (negative? k))
(error "index must be non-negative integer" k))
(let1 len (length lis)
(cond [(<= k len) (drop lis (- len k))]
[fill? (append! (make-list (- k len) filler) lis)]
[else lis])))
(define (drop-right* lis k)
(let1 len (length lis)
(if (<= k len) (take lis (- len k)) '())))
;;-----------------------------------------------------------------
;; slices - split a list to a bunch of sublists of length k
;;
(define (slices lis k . args)
(unless (and (integer? k) (positive? k))
(error "index must be positive integer" k))
(let loop ((lis lis)
(r '()))
(if (null? lis)
(reverse! r)
(receive (h t) (apply split-at* lis k args)
(loop t (cons h r))))))
;;-----------------------------------------------------------------
;; intersperse - insert ITEM between elements in the list.
;; (the order of arguments is taken from Haskell's intersperse)
(define (intersperse item lis)
(define (rec l r)
(if (null? l)
(reverse! r)
(rec (cdr l) (list* (car l) item r))))
(if (null? lis)
'()
(rec (cdr lis) (list (car lis)))))
;;-----------------------------------------------------------------
;; cond-list - a syntax to construct a list
;;
;; (cond-list clause clause2 ...)
;;
;; clause : (test expr ...)
;; | (test => proc)
;; | (test @ expr ...) ;; intersperse
;; | (test => @ proc) ;; intersperse
(define-syntax cond-list
(syntax-rules (=> @)
((_) '())
((_ (test) . rest)
(let* ((tmp test)
(r (cond-list . rest)))
(if tmp (cons tmp r) r)))
((_ (test => proc) . rest)
(let* ((tmp test)
(r (cond-list . rest)))
(if tmp (cons (proc tmp) r) r)))
((_ (test => @ proc) . rest)
(let* ((tmp test)
(r (cond-list . rest)))
(if tmp (append (proc tmp) r) r)))
((_ (test @ . expr) . rest)
(let* ((tmp test)
(r (cond-list . rest)))
(if tmp (append (begin . expr) r) r)))
((_ (test . expr) . rest)
(let* ((tmp test)
(r (cond-list . rest)))
(if tmp (cons (begin . expr) r) r)))
))
;;-----------------------------------------------------------------
;; Associative list library - based on Alex Shinn's implementation
;;
;; conversion to/from hash-table
(define (alist->hash-table a . opt-eq)
(let ((tb (apply make-hash-table opt-eq)))
(for-each (lambda (x) (hash-table-put! tb (car x) (cdr x))) a)
tb))
(define (hash-table->alist h)
(hash-table-map h cons))
;; `reverse' alist search fn
(define (rassoc key alist :optional (eq equal?))
(find (lambda (elt) (and (pair? elt) (eq (cdr elt) key))) alist))
(define rassq (cut rassoc <> <> eq?))
(define rassv (cut rassoc <> <> eqv?))
;; 'assoc-ref', a shortcut of value retrieval w/ default value
;; Default parameter comes first, following the convention of
;; other *-ref functions.
(define (assoc-ref alist key :optional (default #f) (eq equal?))
(cond [(assoc key alist eq) => cdr]
[else default]))
(define (assq-ref alist key . opts)
(assoc-ref alist key (get-optional opts #f) eq?))
(define (assv-ref alist key . opts)
(assoc-ref alist key (get-optional opts #f) eqv?))
(define (rassoc-ref alist key :optional (default #f) (eq equal?))
(cond [(rassoc key alist eq) => car]
[else default]))
(define (rassq-ref alist key . opts)
(rassoc-ref alist key (get-optional opts #f) eq?))
(define (rassv-ref alist key . opts)
(rassoc-ref alist key (get-optional opts #f) eqv?))
;; 'assoc-set!'
(define (assoc-set! alist key val :optional (eq equal?))
(cond [(assoc key alist eq)
=> (lambda (p) (set-cdr! p val) alist)]
[else (acons key val alist)]))
(define assq-set! (cut assoc-set! <> <> <> eq?))
(define assv-set! (cut assoc-set! <> <> <> eqv?))
|