- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 LISP 中,我有一个传递列表的函数。我想在不更改原始列表的情况下更改此列表的一个元素。通常,我会使用 copy-list
创建我将更改的列表的本地副本,但这似乎不起作用:
CL-USER> (defun test (item)
(let ((copy (copy-list item)))
(setf (nth 0 (nth 0 (nth 0 copy))) t)
(print item)
(print copy)))
CL-USER> (defparameter item `(((NIL NIL) (NIL NIL) (NIL NIL))
((NIL NIL NIL) (NIL NIL NIL))
((3 3) (NIL NIL))))
CL-USER> (test item)
(((T NIL) (NIL NIL) (NIL NIL)) ((NIL NIL NIL) (NIL NIL NIL)) ((3 3) (NIL NIL)))
(((T NIL) (NIL NIL) (NIL NIL)) ((NIL NIL NIL) (NIL NIL NIL)) ((3 3) (NIL NIL)))
(((T NIL) (NIL NIL) (NIL NIL)) ((NIL NIL NIL) (NIL NIL NIL)) ((3 3) (NIL NIL)))
CL-USER> item
(((T NIL) (NIL NIL) (NIL NIL)) ((NIL NIL NIL) (NIL NIL NIL)) ((3 3) (NIL NIL)))
如您所见,item
的值已被 test
更改,即使我将列表复制到局部变量并更改了本地副本。这似乎是使用 nth
的症状。如果我使用 car
的单个调用而不是对 nth
的重复调用,该函数将按预期工作并且 item
在调用后保持不变.
为什么 nth
会这样,我如何才能继续使用 nth
而不改变传递给 test
的值?
我正在使用 Common Lisp。
最佳答案
您可能想用 copy-tree 复制整棵树. copy-list 制作的副本只生成新的“主干”;您会得到一个新列表,但具有相同 元素。 Copy-tree 将复制构成树的所有 cons-tree 结构。
文档中引用了此处的背景。来自 HyperSpec:
Function COPY-LIST
Only the list structure of list is copied; the elements of the resulting list are the same as the corresponding elements of the given list.
列表结构的词汇表条目很重要:
list structure n. (of a list) the set of conses that make up the list. Note that while the car component of each such cons is part of the list structure, the objects that are elements of the list (i.e., the objects that are the cars of each cons in the list) are not themselves part of its list structure, even if they are conses, except in the (circular) case where the list actually contains one of its tails as an element. (The list structure of a list is sometimes redundantly referred to as its "top-level list structure" in order to emphasize that any conses that are elements of the list are not involved.)
作为一个非常简单的例子,我们可以利用 *print-circle* 向我们展示共同子结构的事实:
CL-USER> (setf *print-circle* t)
T
CL-USER> (let ((l '((a b c) (d e f))))
(list l (copy-list l)))
;=> ((#1=(A B C) #2=(D E F)) (#1# #2#))
CL-USER> (let ((l '((a b c) (d e f))))
(list l (copy-tree l)))
;=> (((A B C) (D E F)) ((A B C) (D E F)))
copy-tree 上的 HyperSpec 条目没有链接到树结构,但是是一个词汇表条目:
tree structure n. (of a tree) the set of conses that make up the tree. Note that while the car component of each such cons is part of the tree structure, the objects that are the cars of each cons in the tree are not themselves part of its tree structure unless they are also conses.
第二句话有点奇怪,但它可能只是作为 列表结构 条目的最小调整复制和粘贴而存在的。参见 my answer至 Definition of tree structure in Lisp有关更多信息。
关于lisp - 在 LISP 中更改列表的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26791982/
不确定我的标题措辞是否正确,但请耐心等待,所有内容都会得到解释... 我们有一组代码不是在这里发明的,它使用进程间通信(IPC 消息传递)。该方案的大致轮廓是这样的: comms.c 包含: stat
你怎么能列出所有的颠覆合并? 例如: Trunk ____9_____14____20___ \ \ \ \______\_____\___
是否有一个集合的标准 Java(1.5+)实现(即无第三方),允许我将多个集合粘合到一个集合中? 这是其工作原理的草图: final SomeCollection x = new SomeCollec
有没有办法让sql查询返回拆分行。我什至不知道怎么问。下面有'index_tag'。 select event.name, tb_ev.start_time, tb_ev.end_time from
我正在尝试使用 postgresql COPY 命令从 CSV 加载一些数据。诀窍是我想在用户标识(包含在 CSV 中)上实现 Multi-Tenancy 。加载 csv 时,是否有一种简单的方法告诉
我正在尝试使用 bash 脚本将文件复制到当前目录。 为了处理需要转义的路径,使用了一个变量,该变量被转义然后提供给 cp 命令。 cp 命令提示: usage: cp [-R [-H | -L |
我正在尝试每 20 毫秒向给定的 x 和 y 坐标添加一次 CAShapelayer。我希望形状在一秒钟内消失(就像示踪剂一样)。我创建的功能有效,形状在正确的位置创建并消失。但是我留下了额外的形状,
我是 Python 新手。我正在尝试创建一个程序来打印我通常每周手动打印的一组文档,但是我遇到了几个问题: 这是代码: import os file_list = os.listdir("C:/Pyt
我有一个大小为 10 的 ArrayList l1。我将 l1 分配给新的列表引用类型 l2。 l1 和 l2 会指向同一个 ArrayList 对象吗?或者是 ArrayList 对象的副本分配给
我这周花了一个自由职业者创建的 Mongo 4.4 PSA 副本来工作。我放弃了,从所有三台服务器上删除了完整的 mongod,然后按照 Mongo doc 从头开始安装。 .唯一的变化是在副本初
设置信息: 我有两个数据中心,每个 DC 中有 5 个节点。 我知道插入到表中的每一行都是根据使用的数据分区方案存储的;生成必要的副本并将它们存储在集群中的其他节点(根据复制策略选择节点)上。给定一行
我对 XSLT 完全陌生,所以请耐心等待。 我有两个 xml 文件,我试图使用 XSLT 将它们连接在一起。我想合并这些文件,以便第二个文件中指定的任何值覆盖第一个文件。例如 firstFile.xm
这里肯定有一个初学者问题,为什么 F# 编译器会制作不必要的 DateTimeOffset 副本,我该如何阻止它?我不记得这是个问题,但也许自从我在 F# 中使用 DateTimeOffset 以来已
我有一个用 C# 编写的 WinForms 应用程序,在将数据从 SQL 数据库导出到模板的工作表之前,它使用以下代码打开 Excel 模板。 Microsoft.Office.Interop.Exc
我从这个 post 得到的 xsl 中有这个函数 用“换行符”替换“cr” 我是这样调用它的: 我正在做文章链接,点击文本“阅读更多”
所以这可能有点难以解释...... 目前我这样做: SomeInterface xyz1 = SomeInterface.method(data); SomeInterfaceCopy xyz2 =
我有一个包含指针 p 的类型 var。我需要在另一个与 var 类型相同的变量 var1 上复制 var(通过在引号中执行 var1 "="var,因为我不知道这是否是正确的方法,请参见下文)。 在我
出于某种原因,我需要同时运行两个 xampp 副本。我在互联网上阅读了很多教程,但如果我需要运行另一个,他们最终会告诉我关闭当前的 xampp。这有可能实现吗? 最佳答案 您可以使用不同的端口同时运行
在aws中,“upload-part-copy”具有字节范围选项。如果我想将两个对象的一部分复制到云中的新对象,我可以使用“upload-part-copy”命令进行复制。 我找不到任何此类方法或机制
我有一个带栏的表 foo foo --- bar 我使用 Postgres 的 Copy 命令 COPY (select * from foo) TO 'complete_file_path' WIT
我是一名优秀的程序员,十分优秀!