Replace element in list

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Replace element in list

Andreas Röhler
Hi,

is there a recommended way to replace element x at index i of somelist 
y by newelement?

Thanks,

Andreas



Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Tomas Zerolo
On Mon, Sep 02, 2019 at 11:44:10AM +0200, Andreas Röhler wrote:
> Hi,
>
> is there a recommended way to replace element x at index i of
> somelist  y by newelement?

Like this?

  scheme@(guile-user)> (define lst (list-copy '(fire water air earth)))
  scheme@(guile-user)> (list-set! lst 2 'mud)
  $3 = mud
  scheme@(guile-user)> lst
  $4 = (fire water mud earth)
 
Note the "list-copy" above. Strange things might happen to your computer
if you try to mutate immutable data ;-D

Cheers
-- t

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Tomas Zerolo
On Mon, Sep 02, 2019 at 12:37:23PM +0200, [hidden email] wrote:

> On Mon, Sep 02, 2019 at 11:44:10AM +0200, Andreas Röhler wrote:
> > Hi,
> >
> > is there a recommended way to replace element x at index i of
> > somelist  y by newelement?
>
> Like this?
>
>   scheme@(guile-user)> (define lst (list-copy '(fire water air earth)))
>   scheme@(guile-user)> (list-set! lst 2 'mud)
>   $3 = mud
>   scheme@(guile-user)> lst
>   $4 = (fire water mud earth)
Oops, sorry. Wrong language, wrong mailing list. Embarrasing.

Cheers

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Tomas Zerolo
On Mon, Sep 02, 2019 at 12:41:08PM +0200, [hidden email] wrote:

> On Mon, Sep 02, 2019 at 12:37:23PM +0200, [hidden email] wrote:
> > On Mon, Sep 02, 2019 at 11:44:10AM +0200, Andreas Röhler wrote:
> > > Hi,
> > >
> > > is there a recommended way to replace element x at index i of
> > > somelist  y by newelement?
> >
> > Like this?
> >
> >   scheme@(guile-user)> (define lst (list-copy '(fire water air earth)))
> >   scheme@(guile-user)> (list-set! lst 2 'mud)
> >   $3 = mud
> >   scheme@(guile-user)> lst
> >   $4 = (fire water mud earth)
>
> Oops, sorry. Wrong language, wrong mailing list. Embarrasing.
In Emacs Lisp you could try:

  (setcar (nthcdr 3 foo) 'mud)

...but remember, you are leaving the functional sector ;-)

Cheers
-- t

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Michael Heerdegen
[hidden email] writes:

> In Emacs Lisp you could try:
>
>   (setcar (nthcdr 3 foo) 'mud)

Another way to write that is

    (setf (nth 3 foo) 'mud)

Michael.

Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Tomas Zerolo
On Mon, Sep 02, 2019 at 01:03:21PM +0200, Michael Heerdegen wrote:
> [hidden email] writes:
>
> > In Emacs Lisp you could try:
> >
> >   (setcar (nthcdr 3 foo) 'mud)
>
> Another way to write that is
>
>     (setf (nth 3 foo) 'mud)

Yes, much nicer. I always forget about setf. Thanks for the
reminder!

Cheers
-- tomás

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Stefan Monnier
In reply to this post by Andreas Röhler
> is there a recommended way to replace element x at index i of somelist 
> y by newelement?

What Thomas is hinting at with his "copy-list" and "functional sector"
is that if you need to do that, there's a problem upstream.
E.g. could you use a struct instead of a list?


        Stefan


Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Andreas Röhler
In reply to this post by Tomas Zerolo

Am 02.09.19 um 13:05 schrieb [hidden email]:

> On Mon, Sep 02, 2019 at 01:03:21PM +0200, Michael Heerdegen wrote:
>> [hidden email] writes:
>>
>>> In Emacs Lisp you could try:
>>>
>>>    (setcar (nthcdr 3 foo) 'mud)
>> Another way to write that is
>>
>>      (setf (nth 3 foo) 'mud)
> Yes, much nicer. I always forget about setf. Thanks for the
> reminder!
>
> Cheers
> -- tomás

Thanks all!


Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Tomas Zerolo
In reply to this post by Stefan Monnier
On Mon, Sep 02, 2019 at 09:29:39AM -0400, Stefan Monnier wrote:
> > is there a recommended way to replace element x at index i of somelist 
> > y by newelement?
>
> What Thomas is hinting at with his "copy-list" and "functional sector"
> is that if you need to do that, there's a problem upstream.
> E.g. could you use a struct instead of a list?

Thanks for putting my mumbling into civilised words :-)

Cheers
-- t

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Andreas Röhler

Am 02.09.19 um 15:43 schrieb [hidden email]:

> On Mon, Sep 02, 2019 at 09:29:39AM -0400, Stefan Monnier wrote:
>>> is there a recommended way to replace element x at index i of somelist
>>> y by newelement?
>> What Thomas is hinting at with his "copy-list" and "functional sector"
>> is that if you need to do that, there's a problem upstream.
>> E.g. could you use a struct instead of a list?
> Thanks for putting my mumbling into civilised words :-)
>
> Cheers
> -- t


Hmm, what means struct here?

Tried a little program to answer a quiz in

https://www.futurelearn.com/courses/how-computers-work/4/steps/551852/questions/3:

;; If the head is in state A at the position shown below, what would the
final output of this Turing machine be?

;; |0|0|0|0|1|1|0|0|0|
;;                ^

Started that way:

[leaving out here z_B - z_E]

(defun z_A (zeichen pos)

   ;; Char: 1 (49, #o61, #x31)
   (when (eq 49 (aref zeichen pos))
     (aset zeichen pos 48)
     (z_B zeichen (1- pos))))

(defun zeichenmachine ()
   (interactive)
   (let ((zeichen "000011000")
     (pos 5))
     (message "%s" zeichen)
     (z_A zeichen pos)))

But that was easier to read:

[leaving out here state_B - state_E]

(defun state_A (elist pos)
   (if (eq 1 (nth pos elist))
       (progn
     (setf (nth pos elist) 0)
     (state_B elist (1- pos)))
     (message "Fertig %s" elist)))

(defun statemachine ()
   (interactive)
   (let ((elist (list 0 0 0 0 1 1 0 0 0))
     (pos 5))
     (state_A elist pos)))



Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Stefan Monnier
>>> is that if you need to do that, there's a problem upstream.
                                          ^^^
                                        probably

>>> E.g. could you use a struct instead of a list?
>> Thanks for putting my mumbling into civilised words :-)
> Hmm, what means struct here?

Something defined with `cl-defstruct`.

But in your example, you want to use a vector instead.

> But that was easier to read:

I must admit I do not see which aspect of the change you think makes it
more readable, nor why using a list is related to it.

> (defun state_A (elist pos)
>   (if (eq 1 (nth pos elist))
>       (progn
>     (setf (nth pos elist) 0)
>     (state_B elist (1- pos)))
>     (message "Fertig %s" elist)))
>
> (defun statemachine ()
>   (interactive)
>   (let ((elist (list 0 0 0 0 1 1 0 0 0))
>     (pos 5))
>     (state_A elist pos)))

Replace `nth` with `aref` (and swap the args accordingly) and replace
(list ...) with (vector ...) and you've got a solution using vectors
instead of lists.

BTW, the traditional name for your "elist" is "tape" ;-)


        Stefan


Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Andreas Röhler

On 02.09.19 16:57, Stefan Monnier wrote:
>
>> Hmm, what means struct here?
> Something defined with `cl-defstruct`.
>
> But in your example, you want to use a vector instead.
>
>> But that was easier to read:
> I must admit I do not see which aspect of the change you think makes it
> more readable, nor why using a list is related to it.


It's about the comparison, where the "1" appears as 49

(when (eq 49



Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Stefan Monnier
> It's about the comparison, where the "1" appears as 49
>
> (when (eq 49

Ah, for that you could have written `?1` instead of `49`,


        Stefan


Reply | Threaded
Open this post in threaded view
|

Re: Replace element in list

Andreas Röhler

On 02.09.19 23:32, Stefan Monnier wrote:
>> It's about the comparison, where the "1" appears as 49
>>
>> (when (eq 49
> Ah, for that you could have written `?1` instead of `49`,
>
>
>          Stefan
>
>
Indeed, thanks.