From: Vladimir V. Zolotych
Subject: LOOP & multidim. arrays
Date: 
Message-ID: <3AA3DCEB.A4C5D0E5@eurocom.od.ua>
	Hello

Few questions if you pleased.

-  Is it possible to define my own accessors ?
   I've tried (g is a string)

   (defun grid-ref (g row col)
     (char g (+ (* row 4) col)))

   This seem didn't work
   Should I do instead
   
   (defmacro grid-ref (g row col)
     `(char ,g (+ (* ,row 4) ,col))) ?

   Or other method exists for writing such things ?

-  Why (string= 'foo "foo") returns T ? Does it imply (symbol-name 'foo)
?

-  What is the difference between
   a. (setf (the integer (cadr x)) (+ y 3))
   and
   b. (setf (cadr x) (+ y 3)) ?

-  Is it possible to organize nested loops inside (LOOP ...) ?
   E.g. for traversing two-dimensional arrays ? Could you give
   me example ?
   I only can think about

   (defun foo (a)
     (destructuring-bind (nr nc) (array-dimensions a)
       (loop for row from 0 below nr
	     do (loop for col from 0 below nc
		      do (<something>))))))

   but this makes impossible to use collect etc.

-  Is construct like across (for vectors) exist for multidim. arrays ?

-  It seems multidimensional arrays aren't sequences. Is it possible 
   make usage of various functions (applicable to sequences) for 
   multidim. array (their parts, splices etc) ?

	Best regards

-- 
Vladimir Zolotych                         ······@eurocom.od.ua

From: Kent M Pitman
Subject: Re: LOOP & multidim. arrays
Date: 
Message-ID: <sfwr90cqe7u.fsf@world.std.com>
"Vladimir V. Zolotych" <······@eurocom.od.ua> writes:

> -  Is it possible to define my own accessors ?
>    I've tried (g is a string)
> 
>    (defun grid-ref (g row col)
>      (char g (+ (* row 4) col)))

Nothing wrong with this, but you might want to look into displaced arrays.

>    This seem didn't work

What do you mean it didn't work?  It will work unless you don't have enough
elements in g.

>    Should I do instead
>    
>    (defmacro grid-ref (g row col)
>      `(char ,g (+ (* ,row 4) ,col))) ?
> 
>    Or other method exists for writing such things ?

Is your problem that you can't SETF it?  To do that, you don't need to do
the macro thing. You just have to add to the first definition above:

 (defun (setf grid-ref) (new g row col)
   (setf (char g (+ (* row 4) col)) new))

 
> -  Why (string= 'foo "foo") returns T ? Does it imply (symbol-name 'foo)
> ?

(string= 'foo "foo") should return NIL, not T.  The notation 'foo reads
as (QUOTE FOO).  FOO is a symbol designator for the string "FOO".  string=
is case-sensitive, so (string= "FOO" "foo") => NIL

(string= 'foo "FOO") should return T.

> -  What is the difference between
>    a. (setf (the integer (cadr x)) (+ y 3))
>    and
>    b. (setf (cadr x) (+ y 3)) ?

The same as the difference between 

 (setf (cadr x) (+ y 3))
and
 (setf (cadr x) (the integer (+ y 3)))

It declares to the system that the value of the (+ y 3) will be an
integer.  This implies that y is not a float, nor a complex float
because they can't be added without getting a float result.  Also,
I think it rules out it being a complex rational because it would have
to have a zero imaginary part and #C(0 integer) reduces to integer
before you can get your hands on it.  So I *think* it implies that
Y is just an integer, and is the same as

 (setf (cadr x) (+ (the integer y) 3))

Note that it is NOT the same as declaring that it will fit in a machine
word.  Saying (the fixnum x) is a way to say that it's a small integer,
such as would be used for an array index.  

> -  Is it possible to organize nested loops inside (LOOP ...) ?
>    E.g. for traversing two-dimensional arrays ? Could you give
>    me example ?
>    I only can think about
> 
>    (defun foo (a)
>      (destructuring-bind (nr nc) (array-dimensions a)
>        (loop for row from 0 below nr
> 	     do (loop for col from 0 below nc
> 		      do (<something>))))))
> 
>    but this makes impossible to use collect etc.

Use collection in the inner loop as well.

 (loop for row from 0 below nr
       nconc (loop for col from 0 below nc
                   collect (some-op row col)))

> -  Is construct like across (for vectors) exist for multidim. arrays ?

I can't parse this.

> -  It seems multidimensional arrays aren't sequences. Is it possible 
>    make usage of various functions (applicable to sequences) for 
>    multidim. array (their parts, splices etc) ?

If they are linearly arranged in memory (assuming row major ordering).
Learn about displaced arrays.

If you want to write your own tools to go beyond this, you probably want
to learn about row-major-aref which will allow you to get array elements
of an arbitrary arity array using a single index.
From: Barry Margolin
Subject: Re: LOOP & multidim. arrays
Date: 
Message-ID: <i7To6.24$8q1.25244@burlma1-snr2>
In article <···············@world.std.com>,
Kent M Pitman  <······@world.std.com> wrote:
>"Vladimir V. Zolotych" <······@eurocom.od.ua> writes:
>> -  Why (string= 'foo "foo") returns T ? Does it imply (symbol-name 'foo)
>> ?
>
>(string= 'foo "foo") should return NIL, not T.

I didn't comment on this in my reply because I suspect that if it really
did return T for him, he might be using ACL in its lowercase mode (which I
think is the default for some distributions).  Or maybe he actually used
STRING-EQUAL, but typed STRING= by mistake in the post.  The gist of his
question was about the automatic stringification.

>> -  Is construct like across (for vectors) exist for multidim. arrays ?
>
>I can't parse this.

You're not trying very hard.  Translation: Does anything like LOOP's ACROSS
keyword (for vectors) exist for multidimensional arrays?

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Kent M Pitman
Subject: Re: LOOP & multidim. arrays
Date: 
Message-ID: <sfwbsrfev6f.fsf@world.std.com>
Barry Margolin <······@genuity.net> writes:

> In article <···············@world.std.com>,
> Kent M Pitman  <······@world.std.com> wrote:
> >"Vladimir V. Zolotych" <······@eurocom.od.ua> writes:
> >> -  Why (string= 'foo "foo") returns T ? Does it imply (symbol-name 'foo)
> >> ?
> >
> >(string= 'foo "foo") should return NIL, not T.
> 
> I didn't comment on this in my reply because I suspect that if it really
> did return T for him, he might be using ACL in its lowercase mode (which I
> think is the default for some distributions).  Or maybe he actually used
> STRING-EQUAL, but typed STRING= by mistake in the post.  The gist of his
> question was about the automatic stringification.

All the better to highlight that this "feature" (read: "nonconformance") of 
ACL is not to be anticipated in other, more conforming implementations.

This is why it bugs me that they start in the wrong mode, and why I continue
in my pledge not to refer newbies to using ACL until I hear that the default
mode has been changed back to ANSI-compliant.  (This is not singling out
Franz in any way; just the opposite.  I have always declined to refer people
to implementations that I believe are materially or willfully incomplete
or nonstandard; I'm not going to make a special exception for Franz just 
because they've stuffed away a hidden conforming version where people who
don't know one from the other can't reasonably be expected to go looking
for it.  This newsgroup post is exactly an example of the burden this policy
places on those of us not party to this decision in terms of the complexity
of giving a correct answer to a simple question.)

> >> -  Is construct like across (for vectors) exist for multidim. arrays ?
> >
> >I can't parse this.
> 
> You're not trying very hard.  Translation: Does anything like LOOP's ACROSS
> keyword (for vectors) exist for multidimensional arrays?

I don't use ACROSS and had forgotten this keyword was there.  I thought
he was referring to a function, and I was sure there was no such function.
(My best guess had been he was referring to some APL functionality, but I
decided not to pursue that...)
From: Vladimir V. Zolotych
Subject: Re: LOOP & multidim. arrays
Date: 
Message-ID: <3AA5317F.E4AF7A00@eurocom.od.ua>
Barry Margolin wrote:
> 
> [snip].  Or maybe he actually used
> STRING-EQUAL, but typed STRING= by mistake in the post.

Yes, things were in that way. Sorry.

-- 
Vladimir Zolotych                         ······@eurocom.od.ua
From: Vladimir V. Zolotych
Subject: Re: LOOP & multidim. arrays
Date: 
Message-ID: <3AA52EF6.5E4F2B1@eurocom.od.ua>
Kent M Pitman wrote:
> 
> Is your problem that you can't SETF it?

Exactly.

>  (defun (setf grid-ref) (new g row col)
>    (setf (char g (+ (* row 4) col)) new))

This above is the one I've tried to ask about.

> > -  Is construct like across (for vectors) exist for multidim. arrays ?
> 
> I can't parse this.

Please forgive me poor English. I've mean the following.
I can write 
(loop for i across seq <do something>) for vectors.
I've tried to ask is it possible to write anything similar
for multidimensional arrays.
ROW-MAJOR-AREF and displaced array give me what I want.

Thank you!

-- 
Vladimir Zolotych                         ······@eurocom.od.ua
From: Barry Margolin
Subject: Re: LOOP & multidim. arrays
Date: 
Message-ID: <gJQo6.17$8q1.21902@burlma1-snr2>
In article <·················@eurocom.od.ua>,
Vladimir V. Zolotych <······@eurocom.od.ua> wrote:
>	Hello
>
>Few questions if you pleased.
>
>-  Is it possible to define my own accessors ?
>   I've tried (g is a string)
>
>   (defun grid-ref (g row col)
>     (char g (+ (* row 4) col)))
>
>   This seem didn't work

It looks fine to me.  Why didn't it work?

>   Should I do instead
>   
>   (defmacro grid-ref (g row col)
>     `(char ,g (+ (* ,row 4) ,col))) ?
>
>   Or other method exists for writing such things ?

You should write things as functions unless there's a good reason why it
should be a macro.  In this case, since you're evaluating all the
arguments, there's no reason to use a macro.

>-  Why (string= 'foo "foo") returns T ? Does it imply (symbol-name 'foo)
>?

All the string function accept symbols, and will use their printnames.

>-  What is the difference between
>   a. (setf (the integer (cadr x)) (+ y 3))
>   and
>   b. (setf (cadr x) (+ y 3)) ?

a declares that the result of the addition will be an integer.  If it's not
(for instance, if y is a float), the consequences are undefined; this
allows for better optimization.  b doesn't declare the type being assigned,
so it will work for any type of number.

>-  Is it possible to organize nested loops inside (LOOP ...) ?
>   E.g. for traversing two-dimensional arrays ? Could you give
>   me example ?
>   I only can think about
>
>   (defun foo (a)
>     (destructuring-bind (nr nc) (array-dimensions a)
>       (loop for row from 0 below nr
>	     do (loop for col from 0 below nc
>		      do (<something>))))))
>
>   but this makes impossible to use collect etc.

You can do things like:

(loop for row from 0 below nr
      nconcing (loop for col from 0 below nc
                      collect ...))

This will return a list that's the conctenation of the results of the inner
loops.

>-  Is construct like across (for vectors) exist for multidim. arrays ?

No.

>-  It seems multidimensional arrays aren't sequences. Is it possible 
>   make usage of various functions (applicable to sequences) for 
>   multidim. array (their parts, splices etc) ?

You can use an indirec array to map a one-dimensional array onto all or a
part of a multidimensional array.  Or you can use ROW-MAJOR-AREF to access
a multidimensional array as if it were one-dimensional.

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Vladimir V. Zolotych
Subject: Re: LOOP & multidim. arrays
Date: 
Message-ID: <3AA5310C.E253D9A6@eurocom.od.ua>
Barry Margolin wrote:
> 
> It looks fine to me.  Why didn't it work?

The problem was that I can't SETF it.
(defun (setf grid-ref) (new s row col)
  (setf (char s (+ (* row 3) col)) new))
gives me what I want.

> You can use an indirec array to map a one-dimensional array onto all or a
> part of a multidimensional array.  Or you can use ROW-MAJOR-AREF to access
> a multidimensional array as if it were one-dimensional.

I've learned about ROW-MAJOR-AREF and displaced arrays.
These features give me enough.  Do you use 'indirec' to
name displaced arrays ?

Thanks for help!

-- 
Vladimir Zolotych                         ······@eurocom.od.ua