Emacs - 如何用let-bound变量替换列表的第n个元素 [英] Emacs -- How to replace nth element of a list with a let-bound variable
问题描述
我没有发现任何示例,如何替换列表的 nth
元素,而不先添加每个元素 add-to-ordered-list - (一个一个),例如(add -to-ordered-list'the-list'a 1)
。这需要随后删除元素 - 例如,(delq a-list)
。该过程的第三步是添加新的元素,例如(add-to-ordered-list'the-listHELLO-WORLD!1)
。由于以下函数名为 variable-list-example
使用26个元素,所以我寻找一个比首先添加所有26个元素更好的方法(一个接一个)基本上为每个元素分配一个位置号。函数 add-to-ordered-list
使用与标准 nth
元素方法不同的数字分配其中第一个元素的值为 0
,而 add-to-ordered-list
使用 1
为第一个元素。以下是 add-to-ordered-list
的文档链接: http://www.gnu.org/software/emacs/manual/html_node/elisp/List-Variables.html
I have not found any examples of how to replace the nth
element of a list without first adding every element (one-by-one) with the function add-to-ordered-list
-- e.g., (add-to-ordered-list 'the-list 'a 1)
. That requires subsequently deleting the element -- e.g., (delq a the-list)
. The third step in the process is to add the new element -- e.g., (add-to-ordered-list 'the-list "HELLO-WORLD!" 1)
. As the following function named variable-list-example
uses 26 elements, I'm looking for a better way than first adding all 26 elements (one-by-one) to essentially assign each element a position number. The function add-to-ordered-list
uses a number assignment that is one digit different than the standard nth
element approach where the first element has a value of 0
, whereas add-to-ordered-list
uses the value of 1
for the first element. Here is the link to the documentation for add-to-ordered-list
: http://www.gnu.org/software/emacs/manual/html_node/elisp/List-Variables.html
此外,使用列表开头的组合'(
似乎阻止在列表中使用let-bound变量) - 例如,let-bound变量作为 my-variable
而不是 HELLO-WORLD!
出现在列表中。我发现只有解决方法是上述使用 add-to-ordered-list
的示例 - 这是一个漫长的三步过程(即添加每个元素
In addition, using the combination '(
at the beginning of a list seems to prevent using a let-bound variable inside the list -- e.g., the let-bound variable appears in the list as my-variable
instead of HELLO-WORLD!
. The only work-around I have found is the above-mentioned example using add-to-ordered-list
-- which is a lengthy three-step process (i.e., adding each element one-by-one at the outset).
为了解决这两个问题,我正在寻找一些帮助,以取代第17个
元素 the-list
(这是字母 r
) with my-variable
(这是 HELLO-WORLD!
)。
To resolve both issues, I am looking for some assistance please to replace the 17th
element of the-list
(which is the letter r
) with my-variable
(which is HELLO-WORLD!
).
(defun variable-list-example ()
(interactive)
(let* (
(my-variable "HELLO-WORLD!")
(the-list '(a b c d e f g h i j k l m n o p q r s t u v w x y z)))
;; replace the 17th element of `the-list` with `my-variable`
(message "Presto -- this is your new list: %s" the-list)
))
列表
'(a b c d e f g h i j k l m n o p q r s t u v w x y z)
< hr>
第n个元素 - 图表/ LEGEND
element 0: a
element 1: b
element 2: c
element 3: d
element 4: e
element 5: f
element 6: g
element 7: h
element 8: i
element 9: j
element 10: k
element 11: l
element 12: m
element 13: n
element 14: o
element 15: p
element 16: q
element 17: r
element 18: s
element 19: t
element 20: u
element 21: v
element 22: w
element 23: x
element 24: y
element 25: z
推荐答案
You need to use setcar
and nthcdr
:
(setcar (nthcdr 17 the-list) my-variable)
更常见的类似lisp的方法是使用广义变量:
The more common-lisp-like approach is to use Generalized Variables:
(setf (nth 17 the-list) my-variable)
这篇关于Emacs - 如何用let-bound变量替换列表的第n个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!