gpt4 book ai didi

lisp - LIST 在嵌套条件中返回

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

我不明白为什么我的程序会这样。

(defvar A '((X Y Z) (J L O P) (G W U))

(defvar Z '(X W D U G))

(defvar STOP 'G)

(defun ADD_to_Z(A2)
(prog()
(cond
((equal (Member_Of_Z (list A2)) 0) )
(t (setq Z (append Z (list A2))) )
)
)
)

(defun Member_of_Z(cdr_A1)

(prog(n temp)
(setq n 0)
(setq temp cdr_A1)
repeat
(cond
((null temp) (return n))
((null (member (car temp) Z) ) (setq n (+ n 1)) (setq temp (cdr temp)))
(t (setq n (+ n 0)) (setq temp (cdr temp)))
)
(go repeat)
)
)



(defun TEST(A)

(prog(A1 A2)

(cond
((null A ) (return 'Fail))
(t (setq A1 (car A)) (setq A (cdr A)) (setq A2 (car A1))
(cond
((equal (Member_Of_Z (cdr A1)) 0)
(cond
((equal A2 STOP) (return 'SUCCESS))
(t (ADD_to_Z A2) (setq A (cdr A)) (TEST A) )
)
)
(t (TEST A) )
)
)
)

)
)

功能目标:- Member_of_Z 将验证 cdr A1所有元素 是否属于 Z。如果是,它将返回 0(否则返回其他数字) . 这是没有发生的事情 当 A 等于 ((G W U))

时应该返回 SUCCESS
  • ADD_to_Z 将在 Member_Of_Z 不返回 0 时将 A2 添加到 Z

问题:不仅 A 似乎从未被修改(在函数 TEST 的末尾,A 仍然等于其由 defvar 设置的原始值,即使我正在使用 (setq A (cdr A))。也永远不会返回 SUCCESS。

你能帮帮我吗?

最佳答案

第 1 步:使用标准格式(修复第一个顶层表单)。

(defvar A '((X Y Z) (J L O P) (G W U)))

(defvar Z '(X W D U G))

(defvar STOP 'G)

(defun ADD_to_Z (A2)
(prog ()
(cond ((equal (Member_Of_Z (list A2)) 0))
(t (setq Z (append Z (list A2)))))))

(defun Member_of_Z (cdr_A1)
(prog (n temp)
(setq n 0)
(setq temp cdr_A1)
repeat
(cond ((null temp) (return n))
((null (member (car temp) Z) ) (setq n (+ n 1)) (setq temp (cdr temp)))
(t (setq n (+ n 0)) (setq temp (cdr temp))))
(go repeat)))

(defun TEST (A)
(prog (A1 A2)
(cond ((null A ) (return 'Fail))
(t (setq A1 (car A))
(setq A (cdr A))
(setq A2 (car A1))
(cond ((equal (Member_Of_Z (cdr A1)) 0)
(cond ((equal A2 STOP) (return 'SUCCESS))
(t (ADD_to_Z A2) (setq A (cdr A)) (TEST A) )))
(t (TEST A)))))))

第 2 步:使用标准命名。

(defvar *a* '((x y z) (j l o p) (g w u)))

(defvar *z* '(x w d u g))

(defvar *stop* 'g)

(defun add-to-z (a2)
(prog ()
(cond ((equal (member-of-z (list a2)) 0))
(t (setq *z* (append *z* (list a2)))))))

(defun member-of-z (cdr-a1)
(prog (n temp)
(setq n 0)
(setq temp cdr-a1)
repeat
(cond ((null temp) (return n))
((null (member (car temp) *z*)) (setq n (+ n 1)) (setq temp (cdr temp)))
(t (setq n (+ n 0)) (setq temp (cdr temp))))
(go repeat)))

(defun test (a)
(prog (a1 a2)
(cond ((null a) (return 'fail))
(t (setq a1 (car a))
(setq a (cdr a))
(setq a2 (car a1))
(cond ((equal (member-of-z (cdr a1)) 0)
(cond ((equal a2 *stop*) (return 'success))
(t (add-to-z a2) (setq a (cdr a)) (test a))))
(t (test a)))))))

第 3 步:摆脱 PROG。

(defvar *a* '((x y z) (j l o p) (g w u)))

(defvar *z* '(x w d u g))

(defvar *stop* 'g)

(defun add-to-z (a2)
(cond ((equal (member-of-z (list a2)) 0))
(t (setq *z* (append *z* (list a2))))))

(defun member-of-z (cdr-a1)
(let ((n 0)
(temp cdr-a1))
repeat
(cond ((null temp) (return n))
((null (member (car temp) z)) (setq n (+ n 1)) (setq temp (cdr temp)))
(t (setq n (+ n 0)) (setq temp (cdr temp))))
(go repeat)))

(defun test (a)
(cond ((null a) (return 'fail))
(t (let ((a1 (car a))
(a (cdr a))
(a2 (car a1)))
(cond ((equal (member-of-z (cdr a1)) 0)
(cond ((equal a2 *stop*) (return 'success))
(t (add-to-z a2) (setq a (cdr a)) (test a))))
(t (test a)))))))

第 4 步:用结构化循环替换手卷循环。

(defvar *a* '((x y z) (j l o p) (g w u)))

(defvar *z* '(x w d u g))

(defvar *stop* 'g)

(defun add-to-z (a2)
(cond ((equal (member-of-z (list a2)) 0))
(t (setq *z* (append *z* (list a2))))))

(defun member-of-z (cdr-a1)
(let ((n 0)
(temp cdr-a1))
(loop :for element :in temp
:unless (member element *z*)
:do (incf n))
n))

(defun test (a)
(cond ((null a) (return 'fail))
(t (let ((a1 (car a))
(a (cdr a))
(a2 (car a1)))
(cond ((equal (member-of-z (cdr a1)) 0)
(cond ((equal a2 *stop*) (return 'success))
(t (add-to-z a2) (setq a (cdr a)) (test a))))
(t (test a)))))))

第 5 步:用 IF 替换两个子句的 COND。减少 RETURN 表格无论如何都是尾部位置(它们不是那样工作的)。

(defvar *a* '((x y z) (j l o p) (g w u)))

(defvar *z* '(x w d u g))

(defvar *stop* 'g)

(defun add-to-z (a2)
(if (equal (member-of-z (list a2)) 0)
nil
(setq *z* (append *z* (list a2)))))

(defun member-of-z (cdr-a1)
(let ((n 0)
(temp cdr-a1))
(loop :for element :in temp
:unless (member element *z*)
:do (incf n))
n))

(defun test (a)
(if (null a)
'fail
(let ((a1 (car a))
(a (cdr a))
(a2 (car a1)))
(if (equal (member-of-z (cdr a1)) 0)
(if (equal a2 *stop*)
'success
(progn (add-to-z a2) (setq a (cdr a)) (test a)))
(test a)))))

第 6 步:用简单的计数函数替换循环。

(defvar *a* '((x y z) (j l o p) (g w u)))

(defvar *z* '(x w d u g))

(defvar *stop* 'g)

(defun add-to-z (a2)
(if (equal (member-of-z (list a2)) 0)
nil
(setq *z* (append *z* (list a2)))))

(defun member-of-z (cdr-a1)
(count-if-not (lambda (element)
(member element *z*))
cdr-a1))

(defun test (a)
(if (null a)
'fail
(let ((a1 (car a))
(a (cdr a))
(a2 (car a1)))
(if (equal (member-of-z (cdr a1)) 0)
(if (equal a2 *stop*)
'success
(progn
(add-to-z a2)
(setq a (cdr a))
(test a)))
(test a)))))

此时,我仍然不知道您要做什么。也许你想要在 *a* 中查找完全包含在 *z* 中的列表:

(defun test (a)
(find-if (lambda (list)
(every (lambda (element)
(member element *z*))
list))
a))

关于lisp - LIST 在嵌套条件中返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38620461/

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