;;;; ;;;; r7rs.stk -- R7RS support (Draft-3) ;;;; ;;;; Copyright © 2011 Erick Gallesio - Polytech'Nice-Sophia ;;;; ;;;; ;;;; 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) ;;;; Last file update: 31-Dec-2011 15:02 (eg) ;;;; ;;;; ---------------------------------------------------------------------- ;;;; 6.4 Pairs and lists ;;;; ---------------------------------------------------------------------- #| |# (define (make-list k :optional (fill (void))) (vector->list (make-vector k fill))) ;;;; ---------------------------------------------------------------------- ;;;; 6.8 Vectors ;;;; ---------------------------------------------------------------------- #| 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 ;;;; ---------------------------------------------------------------------- #| |# (define (make-bytevector size :optional (default 0)) (%make-uvector 1 size default)) #| |# (define (bytevector? obj) (%uvector? 1 obj)) #| |# (define (bytevector-length bv) (%uvector-length 1 bv)) #| |# (define (bytevector-u8-ref bv idx) (%uvector-ref 1 bv idx)) #| |# (define (bytevector-u8-set! bv idx val) (%uvector-set! 1 bv idx val)) #| |# (define (bytevector-copy bv) (unless (bytevector? bv) (error "bad bytevector ~S" bv)) (let* ((len (bytevector-length bv)) (new (make-bytevector len))) (dotimes (i len) (bytevector-u8-set! new i (bytevector-u8-ref bv i))) new)) #| |# (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)) (len-to (bytevector-length to))) (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))))) #| |# (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)) (new (make-bytevector len))) (dotimes (i len) (bytevector-u8-set! new i (bytevector-u8-ref bv (+ start i)))) new)) #| |# (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)) (to-len (bytevector-length to))) (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)) (k (- end 1))) (dotimes (i len) (bytevector-u8-set! to (- j i) (bytevector-u8-ref from (- k i )))))) (else ;; normal copy (dotimes (i len) (bytevector-u8-set! to (+ at i) (bytevector-u8-ref from (+ start i)))))))) ;;;; ---------------------------------------------------------------------- ;;;; 6.10 Control features ;;;; ---------------------------------------------------------------------- #| "abdegh" * * (string-map * (lambda (c) * (integer->char (+ 1 (char->integer c)))) * "HAL") * => "IBM" * * (string-map (lambda (c k) * (if (eqv? k #\u) * (char-upcase c) * (char-downcase c))) * "studlycaps" * "ululululul") * => "StUdLyCaPs" * @end lisp doc> |# (define (string-map proc . strings) (let* ((strs (map (lambda (x) (unless (string? x) (error 'string-map "bad string ~S" x)) (string->list x)) strings)) (res (apply map proc strs))) ;; 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))) #| #(b e h) * * (vector-map (lambda (n) (expt n n)) * '#(1 2 3 4 5)) * => #(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)))) #| integer c) v))) * "abcde") * v) * => (101 100 99 98 97) * @end lisp doc> |# (define (string-for-each proc . strings) (let ((strs (map (lambda (x) (unless (string? x) (error 'string-for-each "bad string ~S" x)) (string->list x)) strings))) (apply map proc strs) (void))) #| #(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))