gpt4 book ai didi

LISP 在局部函数中更改全局变量值

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

我是 lisp 领域的新手......我正在编写代码来解决 bfs 中的 8 难题......

我想将访问过的列表存储在一个全局列表中,并在一个函数中定期更改它的值...

(defparameter *vlist* nil)
(defun bfs-core(node-list)
(let (cur-node tmp-node-list)
(if (null node-list)
NIL
(progn
; (if (= 1 (length node-list))
(setq cur-node (car node-list))
(setq tmp-node-list (cdr node-list))
(if (goalp cur-node)
cur-node
((setq *vlist* (append cur-node *vlist*))
(bfs-core (append tmp-node-list (expand cur-node))))))
)
)
)

defparameter 一个是我的全局变量……然后是我想用 setq 更改其值的函数……我还使用了 defvar、setf、set 和所有可能的组合……谁能帮帮我????

最佳答案

这是您的代码(针对标准 Lisp 风格重新格式化):

(defparameter *vlist* nil)
(defun bfs-core (node-list)
(let (cur-node tmp-node-list)
(if (null node-list)
NIL
(progn
; (if (= 1 (length node-list))
(setq cur-node (car node-list))
(setq tmp-node-list (cdr node-list))
(if (goalp cur-node)
cur-node
((setq *vlist* (append cur-node *vlist*))
(bfs-core (append tmp-node-list (expand cur-node)))))))))

您的代码是一个明确的问题。

表格((setq *vlist* ...) (bfs-core ...))最终成为一个函数调用。当您有类似 (exp1 exp2 exp3) 的表格时然后 exp1是应用于 exp2 参数值的函数和 exp3 .你的exp1(setq *vlist* ...)它不会计算为函数,当然,您从来不希望它这样做。

你的代码的重写版本,至少会删除错位的函数调用,是:

(defparameter *vlist* nil)
(defun bfs-core (node-list)
(let (cur-node tmp-node-list)
(if (null node-list)
NIL
(progn
; (if (= 1 (length node-list))
(setq cur-node (car node-list))
(setq tmp-node-list (cdr node-list))
(if (goalp cur-node)
cur-node
(progn
(setq *vlist* (append cur-node *vlist*))
(bfs-core (append tmp-node-list (expand cur-node)))))))))

关于LISP 在局部函数中更改全局变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19068669/

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