From: Erann Gat
Subject: Re: iteration over multidimensional array
Date: 
Message-ID: <gat-0804981712430001@milo.jpl.nasa.gov>
In article <··············@mute.eaglets.com>, Sam Steingold <···@usa.net> wrote:

> to iterate over a 2-dim array AA, we do
> 
> (let ((dims (array-dimensions AA)))
>   (dotimes (ii (first dims)) (dotimes (jj (second dims)) do something)))
> 
> what if the dimension (length (array-dimensions AA)) is unknown?
> how hard would it be to write a macro

You can't do it with a macro (at least not in any straightforward way)
because the array dimensions would need to be known when the macro
was expanded.  But there are several ways to iterate over an arbitrary
array with a function.  The most succinct is to create a one-dimensional
array displaced to the array over which you want to iterate, e.g.:

(defun iterate-array (fn a)
  (map nil fn (make-array (apply #'* (array-dimensions a)) :displaced-to a)))

For example:

(iterate-array #'print #4a((((0 1) (2 3)) ((4 5) (6 7)))
                           (((8 9) (10 11)) ((12 13) (14 15)))))

0 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
NIL

Erann Gat
···@jpl.nasa.gov

Furious activity is no substitute for understanding.
    -- H. H. Williams

-- 
Erann Gat    gat at jpl.nasa.gov or gat at jetcafe.org