From: sungwoo
Subject: Substitute a specific item in ARRAY?
Date: 
Message-ID: <5f917204.0109040209.1a124452@posting.google.com>
Hi,

How can I substitute a specific item in ARRAY?
Suppose I got an array virz-array as #(0 0 0 0 0 0 0 0)
I would like to substitute the (aref virz-array 3) to 7.

NSUBSTITUTE or NSUBST simply change all items as #(7 7 7 7 7 7 7 7), 
but I want to get #(0 0 0 7 0 0 0 0).
(I want to get a destructive result)

I took a look the chapter 14, Sequences and chapter 15, Lists, of
Common Lisp reference, but no luck...
Is there any kind of this function I missed? 
Thanks,

Sungwoo

From: sungwoo
Subject: Re: Substitute a specific item in ARRAY?
Date: 
Message-ID: <5f917204.0109040710.42b6f9ef@posting.google.com>
God,,,,,, why I couldn't think THAT easy way. ;(

	(setf (aref arr 3) 7)

Yeah, this is what I want.
Thanks alot guys. =0)

Sungwoo
From: Tim Bradshaw
Subject: Re: Substitute a specific item in ARRAY?
Date: 
Message-ID: <nkjn14bgsvn.fsf@davros.tardis.ed.ac.uk>
·······@cad.strath.ac.uk (sungwoo) writes:

> Hi,
> 
> How can I substitute a specific item in ARRAY?
> Suppose I got an array virz-array as #(0 0 0 0 0 0 0 0)
> I would like to substitute the (aref virz-array 3) to 7.

(setf (aref virz-array 3) 7)

--tim
From: Tijs van Bakel
Subject: Re: Substitute a specific item in ARRAY?
Date: 
Message-ID: <lumr8tnjluf.fsf@dutiaw13.twi.tudelft.nl>
·······@cad.strath.ac.uk (sungwoo) writes:

> How can I substitute a specific item in ARRAY?
> Suppose I got an array virz-array as #(0 0 0 0 0 0 0 0)
> I would like to substitute the (aref virz-array 3) to 7.

The short answer, if I understand your question correctly:
(setf (aref virz-array 3) 7)

It boils down to SETF being somewhat more powerful than one would
expect at first.  One can define the action of SETF on a function, and
such has happened with AREF.  If you look at the syntax for AREF in
the HyperSpec, you will notice that there are both a plain AREF and a
SETF AREF definition.  (I think people say AREF is "SETF'able.")

Have a look at chapter 5.1 in the HyperSpec for more information on
this subject.

Hope this helps,

--
Tijs van Bakel, <·····@wanadoo.nl>
From: Ole Rohne
Subject: Re: Substitute a specific item in ARRAY?
Date: 
Message-ID: <ebw1ylnfe4f.fsf@lxplus026.cern.ch>
·······@cad.strath.ac.uk (sungwoo) writes:

> I would like to substitute the (aref virz-array 3) to 7.

Are you looking for (SETF AREF)? 

(setf (aref virz-array 3) 7) => #(0 0 0 7 0 0 0 0)

	Ole
From: Kalle Olavi Niemitalo
Subject: Re: Substitute a specific item in ARRAY?
Date: 
Message-ID: <izn8zfvp7mm.fsf@stekt29.oulu.fi>
·······@cad.strath.ac.uk (sungwoo) writes:

> Suppose I got an array virz-array as #(0 0 0 0 0 0 0 0)
> I would like to substitute the (aref virz-array 3) to 7.

[1]> (defvar *virz-array* (make-array 8 :initial-element 0))
*VIRZ-ARRAY*
[2]> *virz-array*
#(0 0 0 0 0 0 0 0)
[3]> (setf (aref *virz-array* 3) 7)
7
[4]> *virz-array*
#(0 0 0 7 0 0 0 0)
From: Kent M Pitman
Subject: Re: Substitute a specific item in ARRAY?
Date: 
Message-ID: <sfwg0a3jco3.fsf@world.std.com>
·······@cad.strath.ac.uk (sungwoo) writes:

> How can I substitute a specific item in ARRAY?
> Suppose I got an array virz-array as #(0 0 0 0 0 0 0 0)
> I would like to substitute the (aref virz-array 3) to 7.

As many others have noted, (setf (aref virz-array 3) 7).
HOWEVER, read on...
 
> NSUBSTITUTE or NSUBST simply change all items as #(7 7 7 7 7 7 7 7), 
> but I want to get #(0 0 0 7 0 0 0 0).
> (I want to get a destructive result)

The critical piece of knowledge you want to learn here is that we 
in the CL community do not refer to this as "substitution".  We call 
it "setting" or "assignment".  

We almost always mean something non-destructive by the word "substitute"
and it might seem funny to have a destructive version of a non-destructive
concept, but you should know that the destructive functions NSUBST
and NSUBSTITUTE do not just substitute into but also potentially destroy
the original data.  So you should never use them for just side-effect,
and you should never assume that those who share structure with something
you have called NSUBSTITUTE or NSUBST on will still have valid  structure.
In effect, when you call one of these, you are asking for a "copy" but you
are allowing returning to the heap the old version just before the copy is
made so that it can be made out of the same parts.  

The reason this distinction is important is that we convey subtlety by
our choice of words.  Substitution and assignment, while perhaps conceptually
related at some level, are just logically different to most programmers.
Substitution is an operation on data structures, while assignment is an
operation on storage places.  One can be seen as the other, but the
choice of terms conveys your point of view.

> I took a look the chapter 14, Sequences and chapter 15, Lists, of
> Common Lisp reference, but no luck...
> Is there any kind of this function I missed? 

Yes.  AREF.  But more generally you should also read:

 Section 5.1 Generalized Reference

and

 Chapter 15 Arrays (hard to tell from your list above; maybe you looked
    at this already, but a re-read after you read 5.1 will help)
From: sungwoo
Subject: Re: Substitute a specific item in ARRAY?
Date: 
Message-ID: <5f917204.0109040941.559cd43b@posting.google.com>
Cheers, ;0)

Sungwoo