From: Timofei Shatrov
Subject: (loop for foo across bar) and multiple-dimensional arrays
Date: 
Message-ID: <45f45020.38138049@news.readfreenews.net>
I noticed that both SBCL and CLISP refuse to iterate over multi-dimensional
arrays with LOOP's across keyword. I don't know about ANSI standard, but at
least Hyperspec says nothing about array being necessarily one-dimensional:

"In the for-as-across subclause the for or as construct binds the variable var
to the value of each element in the array vector. The loop keyword across marks
the array vector; across is used as a preposition in this syntax. Iteration
stops when there are no more elements in the supplied array that can be
referenced. Some implementations might recognize a the special form in the
vector form to produce more efficient code." 

Note that the word "vector" in the above snippet is printed in italics and is a
metasyntactic variable, not the name of System Class.

Are there any implementations that allow multi-dimensional arrays for across?
This can't be too hard to implement...

-- 
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|

From: Vassil Nikolov
Subject: Re: (loop for foo across bar) and multiple-dimensional arrays
Date: 
Message-ID: <yy8v64971pgd.fsf@eskimo.com>
On Sun, 11 Mar 2007 19:05:15 GMT, ····@mail.ru (Timofei Shatrov) said:

| I noticed that both SBCL and CLISP refuse to iterate over multi-dimensional
| arrays with LOOP's across keyword. I don't know about ANSI standard, but at
| least Hyperspec says nothing about array being necessarily one-dimensional:

| "In the for-as-across subclause the for or as construct binds the variable var
| to the value of each element in the array vector. The loop keyword across marks
| the array vector; across is used as a preposition in this syntax. Iteration
| stops when there are no more elements in the supplied array that can be
| referenced. Some implementations might recognize a the special form in the
| vector form to produce more efficient code." 

| Note that the word "vector" in the above snippet is printed in italics and is a
| metasyntactic variable, not the name of System Class.

  If something is called a vector, one normally expects that it is a
  vector, and indeed http://www.lisp.org/HyperSpec/Body/mac_loop.html#loop
  has, in the Arguments and Values section:

    vector---a form that evaluates to a vector

  where the second occurrence of "vector" is a reference to "a
  one-dimensional array".

  ---Vassil.


-- 
Is your code free of side defects?
From: Alexander Schmolck
Subject: Re: (loop for foo across bar) and multiple-dimensional arrays
Date: 
Message-ID: <yfsr6rva964.fsf@oc.ex.ac.uk>
····@mail.ru (Timofei Shatrov) writes:

> I noticed that both SBCL and CLISP refuse to iterate over multi-dimensional
> arrays with LOOP's across keyword. I don't know about ANSI standard, but at
> least Hyperspec says nothing about array being necessarily one-dimensional:
> 
> "In the for-as-across subclause the for or as construct binds the variable var
> to the value of each element in the array vector. The loop keyword across marks
> the array vector; across is used as a preposition in this syntax. Iteration
> stops when there are no more elements in the supplied array that can be
> referenced. Some implementations might recognize a the special form in the
> vector form to produce more efficient code." 
> 
> Note that the word "vector" in the above snippet is printed in italics and is a
> metasyntactic variable, not the name of System Class.
> 
> Are there any implementations that allow multi-dimensional arrays for across?
> This can't be too hard to implement...

IIRC ITERATE does.

'as
From: Tim Bradshaw
Subject: Re: (loop for foo across bar) and multiple-dimensional arrays
Date: 
Message-ID: <et1oeq$fma$1$8300dec7@news.demon.co.uk>
On 2007-03-11 19:05:15 +0000, ····@mail.ru (Timofei Shatrov) said:

> I don't know about ANSI standard, but at
> least Hyperspec says nothing about array being necessarily one-dimensional:

I think it does.

From http://www.lisp.org/HyperSpec/Body/mac_loop.html:

	for-as-across::= var [type-spec] across vector

and then later

	vector---a form that evaluates to a vector.

--tim
From: Harald Hanche-Olsen
Subject: Re: (loop for foo across bar) and multiple-dimensional arrays
Date: 
Message-ID: <pcoejnvsh6x.fsf@shuttle.math.ntnu.no>
+ ····@mail.ru (Timofei Shatrov):

| I noticed that both SBCL and CLISP refuse to iterate over multi-dimensional
| arrays with LOOP's across keyword. I don't know about ANSI standard, but at
| least Hyperspec says nothing about array being necessarily one-dimensional:

Oh yes it does, if you just look in the right place:

Go to  6.2 The Iteration Dictionary  and read about  the Macro LOOP

There, in the formal specification of the arguments to the LOOP macro,
you will find these:

  for-as-across::= var [type-spec] across vector

  vector---a form that evaluates to a vector.

To remove all doubt, the final word vector is a hyperlink to the
dictionary, which defines a vector to be a one-dimensional array.

The tricky thing about reading the hyperspec is that it tries very
hard not to say the same thing in two different places:  This is to
avoid edits to the standard to miss one and ending up with
inconsistencies in the standard.  Hence, for complex macros like LOOP,
the total specification gets spread out quite a bit, and you have to
read all the pieces to get the complete specification.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell
From: John Thingstad
Subject: Re: (loop for foo across bar) and multiple-dimensional arrays
Date: 
Message-ID: <op.to1nx0h2pqzri1@pandora.upc.no>
On Sun, 11 Mar 2007 20:39:34 +0100, Harald Hanche-Olsen  
<······@math.ntnu.no> wrote:

> + ····@mail.ru (Timofei Shatrov):
>
> | I noticed that both SBCL and CLISP refuse to iterate over  
> multi-dimensional
> | arrays with LOOP's across keyword. I don't know about ANSI standard,  
> but at
> | least Hyperspec says nothing about array being necessarily  
> one-dimensional:
>
> Oh yes it does, if you just look in the right place:
>
> Go to  6.2 The Iteration Dictionary  and read about  the Macro LOOP
>
> There, in the formal specification of the arguments to the LOOP macro,
> you will find these:
>
>   for-as-across::= var [type-spec] across vector
>
>   vector---a form that evaluates to a vector.
>
> To remove all doubt, the final word vector is a hyperlink to the
> dictionary, which defines a vector to be a one-dimensional array.
>
> The tricky thing about reading the hyperspec is that it tries very
> hard not to say the same thing in two different places:  This is to
> avoid edits to the standard to miss one and ending up with
> inconsistencies in the standard.  Hence, for complex macros like LOOP,
> the total specification gets spread out quite a bit, and you have to
> read all the pieces to get the complete specification.
>

True. But what prevents a implementation from interpreting
a multidimensional array as one dimensional vector using row-major-aref?
This makes the multidimensional array look like a vector.
(At least if you don't specify a :element-type After all a vector is
a one dimensional array of pointers to elements.)

(let ((array (make-array '(5 5))) ; simple-array
    ...
    (loop for i from 0 to 25 collect (row-major-aref array i)))

or ie (loop for e across array collect e)

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: Harald Hanche-Olsen
Subject: Re: (loop for foo across bar) and multiple-dimensional arrays
Date: 
Message-ID: <pco7itnwfsl.fsf@shuttle.math.ntnu.no>
+ "John Thingstad" <··············@chello.no>:

| True. But what prevents a implementation from interpreting a
| multidimensional array as one dimensional vector using
| row-major-aref?

Nothing at all prevents an implementation from doing whatever pleases
the implementors, so long as it is not explicitly forbidden by the
specification (such as adding new external symbols to the COMMON-LISP
package).  But portable programs don't rely on such nonstandard
behaviour.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell
From: Pascal Bourguignon
Subject: Re: (loop for foo across bar) and multiple-dimensional arrays
Date: 
Message-ID: <87zm6i6i1t.fsf@voyager.informatimago.com>
Harald Hanche-Olsen <······@math.ntnu.no> writes:

> + "John Thingstad" <··············@chello.no>:
>
> | True. But what prevents a implementation from interpreting a
> | multidimensional array as one dimensional vector using
> | row-major-aref?
>
> Nothing at all prevents an implementation from doing whatever pleases
> the implementors, so long as it is not explicitly forbidden by the
> specification (such as adding new external symbols to the COMMON-LISP
> package).  But portable programs don't rely on such nonstandard
> behaviour.

Portable programs can use a displaced array:

(loop :for i :across (make-array (array-total-size multi-dimensional-array)
                                 :displaced-to  multi-dimensional-array
                                 :element-type (array-element-type 
                                                    multi-dimensional-array))
      :do ...)

                                 
-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
        Un chat errant
se soulage
        dans le jardin d'hiver
                                        Shiki
From: John Thingstad
Subject: Re: (loop for foo across bar) and multiple-dimensional arrays
Date: 
Message-ID: <op.to2zbmu3pqzri1@pandora.upc.no>
On Mon, 12 Mar 2007 08:24:14 +0100, Pascal Bourguignon  
<···@informatimago.com> wrote:

> Harald Hanche-Olsen <······@math.ntnu.no> writes:
>
>> + "John Thingstad" <··············@chello.no>:
>>
>> | True. But what prevents a implementation from interpreting a
>> | multidimensional array as one dimensional vector using
>> | row-major-aref?
>>
>> Nothing at all prevents an implementation from doing whatever pleases
>> the implementors, so long as it is not explicitly forbidden by the
>> specification (such as adding new external symbols to the COMMON-LISP
>> package).  But portable programs don't rely on such nonstandard
>> behaviour.
>
> Portable programs can use a displaced array:
>
> (loop :for i :across (make-array (array-total-size  
> multi-dimensional-array)
>                                  :displaced-to  multi-dimensional-array
>                                  :element-type (array-element-type
>                                                     multi-dimensional-array))
>       :do ...)
>
>

Interesting. I would never have thought of that..
So a displacement array does not actually store the elements it just  
contains a pointer to another array.
Then you can adjust the array element access in any (?) way you please as  
long as it is type equivalent and the total-size (+ offset) doesn't change.
Since elements in a displacement array are treated in row major order the  
effect of
your declaration is the same as iterating over the array using  
row-major-aref

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/