r7rs.stk 14.1 KB
Newer Older
1
;;;;
2
;;;; r7rs.stk   -- R7RS support (Draft-3)
3
;;;;
4
;;;; Copyright © 2011-2018 Erick Gallesio - Polytech'Nice-Sophia <eg@unice.fr>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
;;;;
;;;;
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
;;;; USA.
;;;;
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date: 17-Apr-2011 19:36 (eg)
24
;;;; Last file update: 26-Jun-2018 14:57 (eg)
25 26 27
;;;;

;;;; ----------------------------------------------------------------------
28
;;;; 6.4 Pairs and lists
29 30 31
;;;; ----------------------------------------------------------------------

#|
Erick's avatar
Erick committed
32
<doc R7RS make-list
33 34 35 36 37 38 39 40 41 42 43
 * (make-list k)
 * (make-list k fill)
 *
 * Returns a newly allocated list of k elements. If a second
 * argument is given, then each element is initialized to fill .
 * Otherwise the initial contents of each element is unspecified.
doc>
|#
(define (make-list k :optional (fill (void)))
  (vector->list (make-vector k fill)))

44
;;;; ----------------------------------------------------------------------
45
;;;; 6.7 Strings
46 47
;;;; ----------------------------------------------------------------------

48 49 50
;;
;; Generalized string comparison functions
;;
51 52
(define-macro (%generalize-string-compare func func2)
  `(begin
53
     ;; Keep the old function since it is twice faster than the general one
54
     (define ,func2 ,func)
55 56 57 58 59 60 61 62 63 64
     ;; define the generalized function
     (set! ,func (lambda (first . l)
                   (letrec ((compare (lambda (first . l)
                                       (or (null? l)
                                          (and (,func2 first (car l))
                                             (apply compare l))))))
                     (unless (string? first) (error "bad string ~W" first))
                     (apply compare first l))))
     ;; Set the name of the new function to the old one for better error messages
     (%set-procedure-name! ,func ',func)))
65 66 67 68 69 70 71 72 73 74 75 76 77

(%generalize-string-compare string=? string2=?)
(%generalize-string-compare string<? string2<?)
(%generalize-string-compare string<=? string2<=?)
(%generalize-string-compare string>? string2>?)
(%generalize-string-compare string>=? string2>=?)

(%generalize-string-compare string-ci=? string-ci2=?)
(%generalize-string-compare string-ci<? string-ci2<?)
(%generalize-string-compare string-ci<=? string-ci2<=?)
(%generalize-string-compare string-ci>? string-ci2>?)
(%generalize-string-compare string-ci>=? string-ci2>=?)

78 79 80 81 82 83 84
(define (s->l s :optional (start 0 start?) (end 0 end?))
  (if (or start? end?)
      (let ((end (if end? end (string-length s))))
        (with-handler (lambda (x)
                        (error 'string->list (condition-ref x 'message)))
                      (string->list (substring s start end))))
      (string->list s)))
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
;;
;; Generalized string->list
;;
(let ((s->l string->list)) ;; s->l is the R5RS function
  (set! string->list
        (lambda (str :optional (start 0 start?) (end 0 end?))
          (if (or start? end?)
              (let ((end (if end? end (string-length str))))
                (with-handler (lambda (x)
                                (error 'string->list
                                       (condition-ref x 'message)))
                              (s->l (substring str start end))))
              (s->l str))))
  (%set-procedure-name! string->list 'string->list))


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
;;;; ----------------------------------------------------------------------
;;;; 6.8 Vectors
;;;; ----------------------------------------------------------------------
#|
<doc R7RS vector->string string->vector
 * (vector->string string)
 * (string->vector vector)
 *
 * |Vector->string| returns a newly allocated string of the
 * objects contained in the elements of |vector|, which must
 * be characters allowed in a string. |String->vector| returns
 * a newly created vector initialized to the elements of the
 * string |string|.
doc>
|#
(define (vector->string v)
  (unless (vector? v)
    (error "bad vector ~S" v))
  (let ((l (vector->list v)))
    (unless (every char? l)
      (error "all elements of the vector ~S must be characters" v))
    (list->string l)))

(define (string->vector str)
  (unless (string? str)
    (error "bad string ~S" str))
  (list->vector (string->list str)))

;;;; ----------------------------------------------------------------------
;;;; 6.9 Bytevectors
;;;; ----------------------------------------------------------------------
#|
<doc R7RS make-bytevector
 * (make-bytevector)
 *
 * Returns a newly allocated bytevector of k bytes. The initial
 * contents of each element is 0.
doc>
|#
(define (make-bytevector size :optional (default 0))
  (%make-uvector 1 size default))

#|
<doc R7RS bytevector?
 * (bytevector? obj)
 *
 * Returns |!t| if |obj| is a bytevector and returns |!f| otherwise.
doc>
|#
(define (bytevector? obj)
  (%uvector? 1 obj))

#|
<doc R7RS bytevector-length
 * (bytevector-length bytevector)
 *
 * Returns the length of |bytevector| in bytes as an exact integer.
doc>
|#
(define (bytevector-length bv)
  (%uvector-length 1 bv))

#|
<doc R7RS bytevector-u8-ref
 * (bytevector-u8-ref bytevector k)
 *
 * Returns the byte at index |k| of |bytevector| as an exact integer in the
 * range [0..255].
doc>
|#
(define (bytevector-u8-ref bv idx)
  (%uvector-ref 1 bv idx))

#|
<doc EXT bytevector-u8-set!
 * (bytevector-u8-ref bytevector k u8)
 *
 * Stores |u8| in  the byte at index |k| of |bytevector|. |u8| must be an
 * exact integer in the range [0..255].  The value returned by
 * |bytevector-u8-set!| is ,(emph "void").
doc>
|#
(define (bytevector-u8-set! bv idx val)
  (%uvector-set! 1 bv idx val))

#|
<doc R7RS bytevector-copy
 * (bytevector-copy bytevector)
 *
 * Returns a newly allocated bytevector containing the same bytes as |bytevector|.
doc>
|#
(define (bytevector-copy bv)
  (unless (bytevector? bv)
    (error "bad bytevector ~S" bv))

  (let* ((len (bytevector-length bv))
199
         (new (make-bytevector len)))
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    (dotimes (i len)
      (bytevector-u8-set! new i (bytevector-u8-ref bv i)))
    new))

#|
<doc R7RS bytevector-copy!
 * (bytevector-copy! from to)
 *
 * Copy the bytes of bytevector |from| to bytevector |to|, which must not be shorter.
 * The value returned by |bytevector-copy!| is ,(emph "void").
doc>
|#
(define (bytevector-copy! from to)
  (unless (bytevector? from)
    (error "bad bytevector ~S" from))
  (unless (bytevector? to)
    (error "bad bytevector ~S" to))
  (let ((len-from (bytevector-length from))
218
        (len-to   (bytevector-length to)))
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    (when (> len-from len-to)
      (error "bytevector ~S is too long for copying it in ~S" from to))
    (dotimes (i len-from)
      (bytevector-u8-set! to i (bytevector-u8-ref from i)))))


#|
<doc R7RS bytevector-copy-partial
 * (bytevector-copy-partial bytevector start end)
 *
 * Returns a newly allocated bytevector containing the bytes in |bytevector|
 * between |start| (inclusive) and |end| (exclusive).
doc>
|#
(define (bytevector-copy-partial bv start end)
  (unless (bytevector? bv)
    (error "bad bytevector ~S" bv))
  (unless (integer? start)
    (error "bad starting index ~S" start))
  (unless (integer? end)
    (error "bad ending intex ~S" end))

  (let* ((len (- end start))
242
         (new (make-bytevector len)))
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
    (dotimes (i len)
      (bytevector-u8-set! new i (bytevector-u8-ref bv (+ start i))))
    new))


#|
<doc R7RS bytevector-copy-partial!
 * (bytevector-copy-partial! from start end to at)
 *
 * Copy the bytes of |bytevector| from between |start| and |end| to bytevector
 * |to|, starting at |at|. The order in which bytes are copied
 * is unspecified, except that if the source and destination
 * overlap, copying takes place as if the source is first copied
 * into a temporary bytevector and then into the destination.
 * The value returned by |partial-bytevector-copy!| is ,(emph "void").
doc>
|#
(define (bytevector-copy-partial! from start end to at)
  (unless (bytevector? from)
    (error "bad bytevector ~S" from))
  (unless (bytevector? to)
    (error "bad bytevector ~S" to))
  (unless (integer? start)
    (error "bad starting index ~S" start))
  (unless (integer? end)
    (error "bad ending index ~S" end))
  (unless (integer? at)
    (error "bad destination index ~S" at))

  (let ((len    (- end start))
273
        (to-len (bytevector-length to)))
274 275 276 277 278 279 280 281 282 283
    (when (> (+ at len) to-len)
      (error "cannot copy ~S bytes in ~S starting at index ~S" len to at))

    (cond
      ((and (eq? from to) (= start at))
       ;; nothing to do
       (void))
      ((and (eq? from to) (> (+ at len) end))
       ;; may overlap => copy in reverse
       (let ((j (- (+ at len) 1))
284 285 286
             (k (- end 1)))
         (dotimes (i len)
           (bytevector-u8-set! to (- j i) (bytevector-u8-ref from (- k i ))))))
287 288 289
      (else
       ;; normal copy
       (dotimes (i len)
290
         (bytevector-u8-set! to (+ at i) (bytevector-u8-ref from (+ start i))))))))
291 292


293
;;;; ----------------------------------------------------------------------
294
;;;; 6.10 Control features
295 296 297
;;;; ----------------------------------------------------------------------

#|
Erick's avatar
Erick committed
298
<doc R7RS string-map
299 300 301 302 303 304 305 306 307 308 309
 * (string-map proc string1 string2 ...)
 *
 * The |strings| must be strings, and |proc| must be a procedure taking as
 * many arguments as there are strings and returning a single
 * value. If more than one string is given and not all strings have the
 * same length, |string-map| terminates when the shortest list runs
 * out. |String-map| applies |proc| element-wise to the elements of the
 * strings and returns a string of the results, in order. The dynamic
 * order in which proc is applied to the elements of the |strings| is
 * unspecified.
 * @lisp
Erick's avatar
Erick committed
310
 * (string-map char-downcase "AbdEgH")
311 312 313 314 315 316 317 318 319
 *          => "abdegh"
 *
 * (string-map
 *   (lambda (c)
 *     (integer->char (+ 1 (char->integer c))))
 *   "HAL")
 *          => "IBM"
 *
 * (string-map (lambda (c k)
320 321 322 323 324 325
 *            (if (eqv? k #\u)
 *                (char-upcase c)
 *                (char-downcase c)))
 *          "studlycaps"
 *          "ululululul")
 *       => "StUdLyCaPs"
Erick's avatar
Erick committed
326 327
 * @end lisp

328 329 330 331
doc>
|#
(define (string-map proc . strings)
  (let* ((strs (map (lambda (x)
332 333 334 335 336
                      (unless (string? x)
                        (error 'string-map "bad string ~S" x))
                      (string->list x))
                    strings))
         (res (apply map proc strs)))
337 338 339 340 341 342 343
    ;; Verify that every compnent of the result is a character
    (unless (every char? res)
      (error 'string-map "bad character in ~S" res))
    ;; Return result
    (list->string res)))

#|
Erick's avatar
Erick committed
344
<doc R7RS vector-map
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
 * (vector-map proc vector1 vector2 ...)
 *
 * The |vectors| must be vectors, and |proc| must be a procedure
 * taking as many arguments as there are vectors and returning a single
 * value. If more than one vector is given and not all vectors have the
 * same length, |vector-map| terminates when the shortest list runs
 * out. |Vector-map| applies |proc| element-wise to the elements of the
 * vectors and returns a vector of the results, in order. The dynamic
 * order in which proc is applied to the elements of the |vectors| is
 * unspecified.
 * @lisp
 * (vector-map cadr '#((a b) (d e) (g h)))
 *     =>  #(b e h)
 *
 * (vector-map (lambda (n) (expt n n))
360
 *          '#(1 2 3 4 5))
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
 *     => #(1 4 27 256 3125)
 *
 * (vector-map + '#(1 2 3) '#(4 5 6))
 *     => #(5 7 9)
 *
 * (let ((count 0))
 *   (vector-map
 *     (lambda (ignored)
 *       (set! count (+ count 1))
 *       count)
 *     '#(a b)))
 *     => #(1 2) or #(2 1)
 * @end lisp
doc>
|#
(define (vector-map proc . vectors)
  (unless (every vector? vectors)
    (error 'vector-map "bad list of vectors ~S" vectors))
  (list->vector (apply map proc (map vector->list vectors))))

#|
Erick's avatar
Erick committed
382
<doc R7RS string-for-each
383 384 385 386 387 388 389 390 391 392 393 394 395
 * (string-for-each proc string1 string2 ...)
 *
 * The arguments to |string-for-each| are like the arguments to
 * |string-map|, but |string-for-each| calls |proc| for its side effects
 * rather than for its values. Unlike |string-map|, |string-for-each| is
 * guaranteed to call |proc| on the elements of the lists in order from
 * the first element(s) to the last, and the value returned by
 * |string-for-each| is unspecified. If more than one string is given and
 * not all strings have the same length, |string-for-each| terminates when
 * the shortest string runs out.
 * @lisp
 * (let ((v (list)))
 *   (string-for-each (lambda (c) (set! v (cons (char->integer c) v)))
396
 *                    "abcde")
397 398 399 400 401 402 403
 *    v)
 *        => (101 100 99 98 97)
 * @end lisp
doc>
|#
(define (string-for-each proc . strings)
  (let ((strs (map (lambda (x)
404 405 406 407
                     (unless (string? x)
                       (error 'string-for-each "bad string ~S" x))
                     (string->list x))
                   strings)))
408 409 410 411 412
    (apply map proc strs)
    (void)))


#|
Erick's avatar
Erick committed
413
<doc R7RS vector-for-each
414
 * (vector-for-each proc vector1 vector2 ...)
415 416 417 418 419 420 421 422 423 424 425 426
 *
 * The arguments to |vector-for-each| are like the arguments to
 * |vector-map|, but |vector-for-each| calls |proc| for its side effects
 * rather than for its values. Unlike |vector-map|, |vector-for-each| is
 * guaranteed to call |proc| on the elements of the lists in order from
 * the first element(s) to the last, and the value returned by
 * |vector-for-each| is unspecified. If more than one vector is given and
 * not all vectors have the same length, |vector-for-each| terminates when
 * the shortest vector runs out.
 * @lisp
 * (let ((v (make-vector 5)))
 *   (vector-for-each (lambda (i) (vector-set! v i (* i i)))
427
 *                 '#(0 1 2 3 4))
428 429 430 431 432 433 434 435 436 437
 *   v)
 *        => #(0 1 4 9 16)
 * @end lisp
doc>
|#
(define (vector-for-each proc . vectors)
  (unless (every vector? vectors)
    (error 'vector-for-each "bad list of vectors ~S" vectors))
  (apply map proc (map vector->list vectors))
  (void))