unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* replace element in list
@ 2018-11-22  4:42 edgar
  2018-11-22  5:18 ` Eric Abrahamsen
                   ` (3 more replies)
  0 siblings, 4 replies; 23+ messages in thread
From: edgar @ 2018-11-22  4:42 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

I want to share and know if there is a better way (more efficient or 
clearer) to replace something within a list (I don't know LISP).  My 
code is like this, and it works for the given example.

(defun my-list-replace (obj orig new)
     "Replaces an element in a list with something else"
     (let* (
            ;; Position of the thing we need to remove
            (pos (cl-position orig obj :test 'equal))
            ;; If pos is nil, reset to zero
            (pos (if pos pos 0))
            ;; The length of the original object
            (objlen (length obj))
            ;; The elements before the element to remove
            (head (butlast obj (- objlen pos)))
            ;; The elements after the element to remove
            (trail (nthcdr (+ 1 pos) obj)))
       ;; Join (1) the sub-list before the element to be replaced
       ;; with (2) the new element and (3) the rest of the list
       (append head (append new trail))
       ))

;; Example simple
(my-list-replace '(("a" . "b") ("c" "d")) '("c" "d") '(":)"))
;; (("a" . "b") ":)")

(my-list-replace '(("a" . "b") ("c" "d")) '("c" "d") (list '("hi")))
;; (("a" . "b") ("hi"))


;; Example long
(setq org-latex-default-packages-alist
         (my-list-replace org-latex-default-packages-alist
                    '("T1" "fontenc" t ("pdflatex"))
                    (list
                     '("" "unicode-math" t ("xelatex" "xetex"))
                     '("" "lmodern" t ("pdflatex"))
                     '("QX" "fontenc" t ("pdflatex")))))

-------------------------------------------------

ONLY AT VFEmail! - Use our Metadata Mitigator to keep your email out of the NSA's hands!
$24.95 ONETIME Lifetime accounts with Privacy Features!  
15GB disk! No bandwidth quotas!
Commercial and Bulk Mail Options!  



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: replace element in list
  2018-11-22  4:42 replace element in list edgar
@ 2018-11-22  5:18 ` Eric Abrahamsen
  2018-11-22  5:39   ` Drew Adams
  2018-11-22 13:19 ` Stefan Monnier
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 23+ messages in thread
From: Eric Abrahamsen @ 2018-11-22  5:18 UTC (permalink / raw)
  To: help-gnu-emacs

edgar@openmail.cc writes:

> Hello,
>
> I want to share and know if there is a better way (more efficient or
> clearer) to replace something within a list (I don't know LISP).  My
> code is like this, and it works for the given example.
>
> (defun my-list-replace (obj orig new)
>     "Replaces an element in a list with something else"
>     (let* (
>            ;; Position of the thing we need to remove
>            (pos (cl-position orig obj :test 'equal))
>            ;; If pos is nil, reset to zero
>            (pos (if pos pos 0))
>            ;; The length of the original object
>            (objlen (length obj))
>            ;; The elements before the element to remove
>            (head (butlast obj (- objlen pos)))
>            ;; The elements after the element to remove
>            (trail (nthcdr (+ 1 pos) obj)))
>       ;; Join (1) the sub-list before the element to be replaced
>       ;; with (2) the new element and (3) the rest of the list
>       (append head (append new trail))
>       ))

You'll probably get a bunch of suggestions, but mine is to use `setf'
and `nth'. You can do:

(let ((orig '(("a" . "b") ("c" "d")))
      (obj  '("c" "d"))
      (new '(":)")))
  (setf (nth (cl-position obj orig :test #'equal) orig) new)
  orig)

Hope that's useful.

Eric




^ permalink raw reply	[flat|nested] 23+ messages in thread

* RE: replace element in list
  2018-11-22  5:18 ` Eric Abrahamsen
@ 2018-11-22  5:39   ` Drew Adams
  2018-11-22  6:04     ` Eric Abrahamsen
  0 siblings, 1 reply; 23+ messages in thread
From: Drew Adams @ 2018-11-22  5:39 UTC (permalink / raw)
  To: Eric Abrahamsen, help-gnu-emacs

> > (defun my-list-replace (obj orig new)
> >     "Replaces an element in a list with something else"
> >     (let* ((pos (cl-position orig obj :test 'equal))
> >            (pos (if pos pos 0))
> >            (objlen (length obj))
> >            (head (butlast obj (- objlen pos)))
> >            (trail (nthcdr (+ 1 pos) obj)))
> >       (append head (append new trail))))
> 
> (let ((orig '(("a" . "b") ("c" "d")))
>       (obj  '("c" "d"))
>       (new '(":)")))
>   (setf (nth (cl-position obj orig :test #'equal) orig) new)
>   orig)

(defun toto (xs old new)
  (let ((ms  (member old xs)))
    (unless ms (error "%S is not in %S" old xs))
    (setcar ms new)
    xs))



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: replace element in list
  2018-11-22  5:39   ` Drew Adams
@ 2018-11-22  6:04     ` Eric Abrahamsen
  0 siblings, 0 replies; 23+ messages in thread
From: Eric Abrahamsen @ 2018-11-22  6:04 UTC (permalink / raw)
  To: help-gnu-emacs

Drew Adams <drew.adams@oracle.com> writes:

>> > (defun my-list-replace (obj orig new)
>> >     "Replaces an element in a list with something else"
>> >     (let* ((pos (cl-position orig obj :test 'equal))
>> >            (pos (if pos pos 0))
>> >            (objlen (length obj))
>> >            (head (butlast obj (- objlen pos)))
>> >            (trail (nthcdr (+ 1 pos) obj)))
>> >       (append head (append new trail))))
>> 
>> (let ((orig '(("a" . "b") ("c" "d")))
>>       (obj  '("c" "d"))
>>       (new '(":)")))
>>   (setf (nth (cl-position obj orig :test #'equal) orig) new)
>>   orig)
>
> (defun toto (xs old new)
>   (let ((ms  (member old xs)))
>     (unless ms (error "%S is not in %S" old xs))
>     (setcar ms new)
>     xs))

Old school, and proper :)




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: replace element in list
  2018-11-22  4:42 replace element in list edgar
  2018-11-22  5:18 ` Eric Abrahamsen
@ 2018-11-22 13:19 ` Stefan Monnier
       [not found] ` <mailman.4442.1542892802.1284.help-gnu-emacs@gnu.org>
  2018-11-24  3:01 ` edgar
  3 siblings, 0 replies; 23+ messages in thread
From: Stefan Monnier @ 2018-11-22 13:19 UTC (permalink / raw)
  To: help-gnu-emacs

> I want to share and know if there is a better way (more efficient or
> clearer) to replace something within a list (I don't know LISP).

My suggestion is to not do it:
- if you do it by modifying the list in place, it means you're using
  nasty side-effects, which are better avoided when possible
  (especially with lists).
- if you want to do it without side-effects, your operation will
  inevitably be algorithmically inefficient because a list is not
  designed for that.


-- Stefan




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: replace element in list
       [not found] ` <mailman.4442.1542892802.1284.help-gnu-emacs@gnu.org>
@ 2018-11-23 12:05   ` Robert Munyer
  0 siblings, 0 replies; 23+ messages in thread
From: Robert Munyer @ 2018-11-23 12:05 UTC (permalink / raw)
  To: help-gnu-emacs

> edgar wrote:

>> (defun my-list-replace (obj orig new)
>>     "Replaces an element in a list with something else"
[...]

>> (my-list-replace '(("a" . "b") ("c" "d")) '("c" "d") (list '("hi")))
>> ;; (("a" . "b") ("hi"))

If nothing in the list matches your "orig" item, your function will
replace the _first_ item.  Did you intend that?

(my-list-replace '(("a" . "b") ("c" "d")) '("e" "f") (list '("hi")))
;; (("hi") ("c" "d"))

Stefan Monnier wrote:

> - if you do it by modifying the list in place, it means you're using
>   nasty side-effects, which are better avoided when possible
>   (especially with lists).

Good point.

> - if you want to do it without side-effects, your operation will
>   inevitably be algorithmically inefficient because a list is not
>   designed for that.

If he doesn't want to run it very frequently nor on very long lists,
moderate inefficiency is OK.

Edgar, here is one that avoids the nasty side-effects that Stefan
mentioned.  It isn't especially efficient, but it is simple and clear.
Warning: it behaves the same as your original version _only_ if there
is exactly one matching item.

(defun my-list-replace-2 (l old-item new-items)
  (apply 'append
         (mapcar (lambda (x)
                   (cond ((equal x old-item) new-items)
                         (t (list x))))
                 l)))

-- 
Robert Munyer
E-mail: (reverse (append '(com dot munyer at) (list (* 91837 99713))))


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: replace element in list
  2018-11-22  4:42 replace element in list edgar
                   ` (2 preceding siblings ...)
       [not found] ` <mailman.4442.1542892802.1284.help-gnu-emacs@gnu.org>
@ 2018-11-24  3:01 ` edgar
  2018-11-24  5:27   ` Drew Adams
  3 siblings, 1 reply; 23+ messages in thread
From: edgar @ 2018-11-24  3:01 UTC (permalink / raw)
  To: help-gnu-emacs

 From Eric
> You'll probably get a bunch of suggestions, but mine is to use `setf'
> and `nth'. You can do:
> 
> (let ((orig '(("a" . "b") ("c" "d")))
>       (obj  '("c" "d"))
>       (new '(":)")))
>   (setf (nth (cl-position obj orig :test #'equal) orig) new)
>   orig)
> 
> Hope that's useful.
> 
> Eric

Thank you, Eric.


 From Drew
> (defun toto (xs old new)
>   (let ((ms  (member old xs)))
>     (unless ms (error "%S is not in %S" old xs))
>     (setcar ms new)
>     xs))

Thanks (I wonder why toto :P ).

 From Stefan
> My suggestion is to not do it:
> - if you do it by modifying the list in place, it means you're using
>   nasty side-effects, which are better avoided when possible
>   (especially with lists).
> - if you want to do it without side-effects, your operation will
>   inevitably be algorithmically inefficient because a list is not
>   designed for that.
> 
> 
> -- Stefan

Thank you.


 From Robert
> Edgar, here is one that avoids the nasty side-effects that Stefan
> mentioned.  It isn't especially efficient, but it is simple and clear.
> Warning: it behaves the same as your original version _only_ if there
> is exactly one matching item.
> 
> (defun my-list-replace-2 (l old-item new-items)
>   (apply 'append
>          (mapcar (lambda (x)
>                    (cond ((equal x old-item) new-items)
>                          (t (list x))))
>                  l)))

Thanks! This is great :). I didn't know that the first element would be 
replaced :S !!


People, you are truly great. Thank you all.

-------------------------------------------------

ONLY AT VFEmail! - Use our Metadata Mitigator to keep your email out of the NSA's hands!
$24.95 ONETIME Lifetime accounts with Privacy Features!  
15GB disk! No bandwidth quotas!
Commercial and Bulk Mail Options!  



^ permalink raw reply	[flat|nested] 23+ messages in thread

* RE: replace element in list
  2018-11-24  3:01 ` edgar
@ 2018-11-24  5:27   ` Drew Adams
  2018-11-24  7:22     ` edgar
  0 siblings, 1 reply; 23+ messages in thread
From: Drew Adams @ 2018-11-24  5:27 UTC (permalink / raw)
  To: edgar, help-gnu-emacs

> > (defun toto (xs old new)...
> 
> Thanks (I wonder why toto :P ).

No special reason.  toto, titi, tata, tutu, foo, bar, baz,...



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: replace element in list
  2018-11-24  5:27   ` Drew Adams
@ 2018-11-24  7:22     ` edgar
  0 siblings, 0 replies; 23+ messages in thread
From: edgar @ 2018-11-24  7:22 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

On 2018-11-24 05:27, Drew Adams wrote:
>> > (defun toto (xs old new)...
>> 
>> Thanks (I wonder why toto :P ).
> 
> No special reason.  toto, titi, tata, tutu, foo, bar, baz,...

I thought it was a long joke related to the Wizard of Oz.

-------------------------------------------------

ONLY AT VFEmail! - Use our Metadata Mitigator to keep your email out of the NSA's hands!
$24.95 ONETIME Lifetime accounts with Privacy Features!  
15GB disk! No bandwidth quotas!
Commercial and Bulk Mail Options!  



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Replace element in list
@ 2019-09-02  9:44 Andreas Röhler
  2019-09-02 10:37 ` tomas
  2019-09-02 13:29 ` Stefan Monnier
  0 siblings, 2 replies; 23+ messages in thread
From: Andreas Röhler @ 2019-09-02  9:44 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

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

Thanks,

Andreas





^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02  9:44 Replace " Andreas Röhler
@ 2019-09-02 10:37 ` tomas
  2019-09-02 10:41   ` tomas
  2019-09-02 13:29 ` Stefan Monnier
  1 sibling, 1 reply; 23+ messages in thread
From: tomas @ 2019-09-02 10:37 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 515 bytes --]

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

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 10:37 ` tomas
@ 2019-09-02 10:41   ` tomas
  2019-09-02 10:46     ` tomas
  0 siblings, 1 reply; 23+ messages in thread
From: tomas @ 2019-09-02 10:41 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 548 bytes --]

On Mon, Sep 02, 2019 at 12:37:23PM +0200, tomas@tuxteam.de 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

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 10:41   ` tomas
@ 2019-09-02 10:46     ` tomas
  2019-09-02 11:03       ` Michael Heerdegen
  0 siblings, 1 reply; 23+ messages in thread
From: tomas @ 2019-09-02 10:46 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 781 bytes --]

On Mon, Sep 02, 2019 at 12:41:08PM +0200, tomas@tuxteam.de wrote:
> On Mon, Sep 02, 2019 at 12:37:23PM +0200, tomas@tuxteam.de 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

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 10:46     ` tomas
@ 2019-09-02 11:03       ` Michael Heerdegen
  2019-09-02 11:05         ` tomas
  0 siblings, 1 reply; 23+ messages in thread
From: Michael Heerdegen @ 2019-09-02 11:03 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs

tomas@tuxteam.de writes:

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

Another way to write that is

    (setf (nth 3 foo) 'mud)

Michael.



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 11:03       ` Michael Heerdegen
@ 2019-09-02 11:05         ` tomas
  2019-09-02 13:38           ` Andreas Röhler
  0 siblings, 1 reply; 23+ messages in thread
From: tomas @ 2019-09-02 11:05 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 341 bytes --]

On Mon, Sep 02, 2019 at 01:03:21PM +0200, Michael Heerdegen wrote:
> tomas@tuxteam.de 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

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02  9:44 Replace " Andreas Röhler
  2019-09-02 10:37 ` tomas
@ 2019-09-02 13:29 ` Stefan Monnier
  2019-09-02 13:43   ` tomas
  1 sibling, 1 reply; 23+ messages in thread
From: Stefan Monnier @ 2019-09-02 13:29 UTC (permalink / raw)
  To: help-gnu-emacs

> 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




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 11:05         ` tomas
@ 2019-09-02 13:38           ` Andreas Röhler
  0 siblings, 0 replies; 23+ messages in thread
From: Andreas Röhler @ 2019-09-02 13:38 UTC (permalink / raw)
  To: help-gnu-emacs


Am 02.09.19 um 13:05 schrieb tomas@tuxteam.de:
> On Mon, Sep 02, 2019 at 01:03:21PM +0200, Michael Heerdegen wrote:
>> tomas@tuxteam.de 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!




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 13:29 ` Stefan Monnier
@ 2019-09-02 13:43   ` tomas
  2019-09-02 14:00     ` Andreas Röhler
  0 siblings, 1 reply; 23+ messages in thread
From: tomas @ 2019-09-02 13:43 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 431 bytes --]

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

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 13:43   ` tomas
@ 2019-09-02 14:00     ` Andreas Röhler
  2019-09-02 14:57       ` Stefan Monnier
  0 siblings, 1 reply; 23+ messages in thread
From: Andreas Röhler @ 2019-09-02 14:00 UTC (permalink / raw)
  To: help-gnu-emacs


Am 02.09.19 um 15:43 schrieb tomas@tuxteam.de:
> 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)))





^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 14:00     ` Andreas Röhler
@ 2019-09-02 14:57       ` Stefan Monnier
  2019-09-02 20:29         ` Andreas Röhler
  0 siblings, 1 reply; 23+ messages in thread
From: Stefan Monnier @ 2019-09-02 14:57 UTC (permalink / raw)
  To: help-gnu-emacs

>>> 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




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 14:57       ` Stefan Monnier
@ 2019-09-02 20:29         ` Andreas Röhler
  2019-09-02 21:32           ` Stefan Monnier
  0 siblings, 1 reply; 23+ messages in thread
From: Andreas Röhler @ 2019-09-02 20:29 UTC (permalink / raw)
  To: help-gnu-emacs


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





^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 20:29         ` Andreas Röhler
@ 2019-09-02 21:32           ` Stefan Monnier
  2019-09-03  5:37             ` Andreas Röhler
  0 siblings, 1 reply; 23+ messages in thread
From: Stefan Monnier @ 2019-09-02 21:32 UTC (permalink / raw)
  To: help-gnu-emacs

> 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




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Replace element in list
  2019-09-02 21:32           ` Stefan Monnier
@ 2019-09-03  5:37             ` Andreas Röhler
  0 siblings, 0 replies; 23+ messages in thread
From: Andreas Röhler @ 2019-09-03  5:37 UTC (permalink / raw)
  To: help-gnu-emacs


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.




^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2019-09-03  5:37 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-11-22  4:42 replace element in list edgar
2018-11-22  5:18 ` Eric Abrahamsen
2018-11-22  5:39   ` Drew Adams
2018-11-22  6:04     ` Eric Abrahamsen
2018-11-22 13:19 ` Stefan Monnier
     [not found] ` <mailman.4442.1542892802.1284.help-gnu-emacs@gnu.org>
2018-11-23 12:05   ` Robert Munyer
2018-11-24  3:01 ` edgar
2018-11-24  5:27   ` Drew Adams
2018-11-24  7:22     ` edgar
  -- strict thread matches above, loose matches on Subject: below --
2019-09-02  9:44 Replace " Andreas Röhler
2019-09-02 10:37 ` tomas
2019-09-02 10:41   ` tomas
2019-09-02 10:46     ` tomas
2019-09-02 11:03       ` Michael Heerdegen
2019-09-02 11:05         ` tomas
2019-09-02 13:38           ` Andreas Röhler
2019-09-02 13:29 ` Stefan Monnier
2019-09-02 13:43   ` tomas
2019-09-02 14:00     ` Andreas Röhler
2019-09-02 14:57       ` Stefan Monnier
2019-09-02 20:29         ` Andreas Röhler
2019-09-02 21:32           ` Stefan Monnier
2019-09-03  5:37             ` Andreas Röhler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).