Emacs -- 如何用 let 绑定变量替换列表的第 n 个元素 [英] Emacs -- How to replace nth element of a list with a let-bound variable

查看:17
本文介绍了Emacs -- 如何用 let 绑定变量替换列表的第 n 个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到任何示例说明如何在不先添加每个元素(一个-by-one) 与函数 add-to-ordered-list -- 例如, (add-to-ordered-list 'the-list 'a 1).这需要随后删除元素——例如,(delq a the-list).该过程的第三步是添加 new 元素——例如,(add-to-ordered-list 'the-list "HELLO-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<的示例/code> -- 这是一个冗长的三步过程(即,从一开始就一个接一个添加每个元素).

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

为了解决这两个问题,我正在寻找一些帮助,请替换 the-list17th 元素(这是字母 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)

<小时>

第 n 个元素 -- 备忘单/图例

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

推荐答案

您需要使用 setcarnthcdr:

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 绑定变量替换列表的第 n 个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆