home *** CD-ROM | disk | FTP | other *** search
/ Dream 44 / Amiga_Dream_44.iso / RiscPc / programmation / scm4e2.arc / !Scm / slib / arraymap < prev    next >
Text File  |  1994-05-23  |  2KB  |  74 lines

  1. ;;;; "arraymap.scm", applicative routines for arrays in Scheme.  
  2. ;;; Copyright (c) 1993 Aubrey Jaffer
  3. ;
  4. ;Permission to copy this software, to redistribute it, and to use it
  5. ;for any purpose is granted, subject to the following restrictions and
  6. ;understandings.
  7. ;
  8. ;1.  Any copy made of this software must include this copyright notice
  9. ;in full.
  10. ;
  11. ;2.  I have made no warrantee or representation that the operation of
  12. ;this software will be error-free, and I am under no obligation to
  13. ;provide any services, by way of maintenance, update, or otherwise.
  14. ;
  15. ;3.  In conjunction with products arising from the use of this
  16. ;material, there shall be no use of my name in any advertising,
  17. ;promotional, or sales literature without prior written consent in
  18. ;each case.
  19.  
  20. (require 'array)
  21.  
  22. (define (array-map! ra0 proc . ras)
  23.   (define (ramap rshape inds)
  24.     (if (null? (cdr rshape))
  25.     (do ((i (cadar rshape) (+ -1 i))
  26.          (is (cons (cadar rshape) inds)
  27.          (cons (+ -1 i) inds)))
  28.         ((< i (caar rshape)))
  29.       (apply array-set! ra0
  30.          (apply proc (map (lambda (ra) (apply array-ref ra is))
  31.                   ras))
  32.          is))
  33.     (let ((crshape (cdr rshape))
  34.           (ll (caar rshape)))
  35.       (do ((i (cadar rshape) (+ -1 i)))
  36.           ((< i ll))
  37.         (ramap crshape (cons i inds))))))
  38.   (ramap (reverse (array-shape ra0)) '()))
  39.  
  40. (define (array-for-each proc . ras)
  41.   (define (rafe rshape inds)
  42.     (if (null? (cdr rshape))
  43.     (do ((i (caar rshape) (+ 1 i)))
  44.         ((> i (cadar rshape)))
  45.       (apply proc
  46.          (map (lambda (ra)
  47.             (apply array-ref ra (reverse (cons i inds)))) ras)))
  48.     (let ((crshape (cdr rshape))
  49.           (ll (cadar rshape)))
  50.       (do ((i (caar rshape) (+ 1 i)))
  51.           ((> i ll))
  52.         (rafe crshape (cons i inds))))))
  53.   (rafe (array-shape (car ras)) '()))
  54.  
  55. (define (shape->indexes shape)
  56.   (define ra0 (apply make-array '() shape))
  57.   (define (ramap rshape inds)
  58.     (if (null? (cdr rshape))
  59.     (do ((i (cadar rshape) (+ -1 i))
  60.          (is (cons (cadar rshape) inds)
  61.          (cons (+ -1 i) inds)))
  62.         ((< i (caar rshape)))
  63.       (apply array-set! ra0 is is))
  64.     (let ((crshape (cdr rshape))
  65.           (ll (caar rshape)))
  66.       (do ((i (cadar rshape) (+ -1 i)))
  67.           ((< i ll))
  68.         (ramap crshape (cons i inds))))))
  69.   (ramap (reverse shape) '())
  70.   ra0)
  71.  
  72. (define (array-indexes ra)
  73.   (shape->indexes (array-shape ra)))
  74.