gpt4 book ai didi

lisp - 在 Emacs Lisp 中获取指向列表元素的指针

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:16 24 4
gpt4 key购买 nike

例如,我有一个列表:

(setq foo '(1 2 3 4 5))

然后我需要获取指向其第 3 个索引元素的指针(在示例中包含 4):

(setq p (SOME_FUNCTION foo 3))

具有 p 地址的元素可以移动到另一个列表,所以我不能只保存其当前 foo 的索引。

我需要稍后再说:

(push 0 foo)
=> (0 1 2 3 4 5)
(setf p 444)

和列表 foo 之后必须是 (0 1 2 3 444 5)

这在 Emacs lisp 中可能吗?

最佳答案

一般来说,你不能存储一个对象的“地址”。但是,您可以引用一个 cons 单元格(cons 单元格是列表的组成部分)。稍后可以使用 setcarsetcdr 修改 cons 单元。

例如:

(defvar my-cons-cell nil)

(defun my-save-cons-cell (cons-cell)
(setq my-cons-cell cons-cell))

(defun my-set-car-in-saved-cons-cell (value)
(setcar my-cons-cell value))

;; Test

(setq foo '(1 2 3 4 5))

(my-save-cons-cell (cdr (cdr (cdr foo))))

(push 0 foo)

(my-set-car-in-saved-cons-cell 444)

这里,foo 的值为 (0 1 2 3 444 5)

请注意,这确实不像 lisp,并且打破了函数式编程范式...

关于lisp - 在 Emacs Lisp 中获取指向列表元素的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14707208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com