help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Replace element in list


From: Stefan Monnier
Subject: Re: Replace element in list
Date: Mon, 02 Sep 2019 10:57:06 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

>>> 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 via email to

[Prev in Thread] Current Thread [Next in Thread]