检查缺点列表中是否存在缺点
Checking If Cons exist in list of Cons
我正在构建一个程序和 lisp,需要检查缺点列表中是否存在缺点,但由于某种原因,它在 if 语句中保持 returning nil,这是我当前的代码正在使用它。
(defun countVertexTriangles (graph numOfVertices)
(findTriangle graph numOfVertices)
)
(defun findTriangle(graph numOfVertices)
(loop for (x y) in graph do
(loop for z from 1 to numOfVertices do
(write graph)
(terpri)
(write (cons z (cons y nil)))
(terpri)
(write (cons z (cons x nil)))
(terpri)
; (if (AND (member (cons z (cons y nil)) graph) (member (cons z (cons x nil)) graph))
; then (write (cons y z))
; )
)
(terpri)
)
)
; (defun findEdge(graph edge)
; (loop for x in graph do
; (write x)
; (write edge)
; (if (eql x edge)
; (write "A")
; (write "B")
; )
; )
; )
(defun testFunct ()
(setf g1 '((1 2)(2 3)(1 3)(2 4)(3 4)(4 5)(3 5)))
(countVertexTriangles g1 5)
)
(testFunct)
为什么 member(cons z (cons y nil)) return nil 即使在第一次迭代中我们可以看到 (1 2) 存在于列表中?
编辑:
目前即使它是真的 return 也没有,为什么给出以下代码会出现这种情况?
(defun countVertexTriangles (graph numOfVertices)
(findTriangle graph numOfVertices)
)
(defun findTriangle(graph numOfVertices)
(loop for (x y) in graph do
(loop for z from 1 to numOfVertices do
; (write graph)
; (terpri)
; (write (list z y ))
; (terpri)
(write (findEdge graph (list z y)))
(terpri)
; (if (AND (member (list z x) graph) (member (list z x ) graph))
; then (write "TEST")
; )
)
(terpri)
)
)
(defun findEdge(graph edge)
(loop for x in graph do
(if (equal x edge)
(return-true)
(return-false)
)
)
)
(defun return-true ()
t)
(defun return-false ()
nil)
(defun testFunct ()
(setf g1 '((1 2)(2 3)(1 3)(2 4)(3 4)(4 5)(3 5)))
(countVertexTriangles g1 5)
)
findEdge
没有 return 任何东西。调用 return-true
和 return-false
不会从 findEdge
调用 return。在找到匹配项之前,您也不应该 return。
(defun findEdge(graph edge)
(loop for x in graph do
(if (equal x edge)
(return-from findEdge (return-true))
)
)
(return-false)
)
这可以简化为:
(defun findEdge (graph edge)
(member edge findEdge :test #'equal))
我正在构建一个程序和 lisp,需要检查缺点列表中是否存在缺点,但由于某种原因,它在 if 语句中保持 returning nil,这是我当前的代码正在使用它。
(defun countVertexTriangles (graph numOfVertices)
(findTriangle graph numOfVertices)
)
(defun findTriangle(graph numOfVertices)
(loop for (x y) in graph do
(loop for z from 1 to numOfVertices do
(write graph)
(terpri)
(write (cons z (cons y nil)))
(terpri)
(write (cons z (cons x nil)))
(terpri)
; (if (AND (member (cons z (cons y nil)) graph) (member (cons z (cons x nil)) graph))
; then (write (cons y z))
; )
)
(terpri)
)
)
; (defun findEdge(graph edge)
; (loop for x in graph do
; (write x)
; (write edge)
; (if (eql x edge)
; (write "A")
; (write "B")
; )
; )
; )
(defun testFunct ()
(setf g1 '((1 2)(2 3)(1 3)(2 4)(3 4)(4 5)(3 5)))
(countVertexTriangles g1 5)
)
(testFunct)
为什么 member(cons z (cons y nil)) return nil 即使在第一次迭代中我们可以看到 (1 2) 存在于列表中?
编辑:
目前即使它是真的 return 也没有,为什么给出以下代码会出现这种情况?
(defun countVertexTriangles (graph numOfVertices)
(findTriangle graph numOfVertices)
)
(defun findTriangle(graph numOfVertices)
(loop for (x y) in graph do
(loop for z from 1 to numOfVertices do
; (write graph)
; (terpri)
; (write (list z y ))
; (terpri)
(write (findEdge graph (list z y)))
(terpri)
; (if (AND (member (list z x) graph) (member (list z x ) graph))
; then (write "TEST")
; )
)
(terpri)
)
)
(defun findEdge(graph edge)
(loop for x in graph do
(if (equal x edge)
(return-true)
(return-false)
)
)
)
(defun return-true ()
t)
(defun return-false ()
nil)
(defun testFunct ()
(setf g1 '((1 2)(2 3)(1 3)(2 4)(3 4)(4 5)(3 5)))
(countVertexTriangles g1 5)
)
findEdge
没有 return 任何东西。调用 return-true
和 return-false
不会从 findEdge
调用 return。在找到匹配项之前,您也不应该 return。
(defun findEdge(graph edge)
(loop for x in graph do
(if (equal x edge)
(return-from findEdge (return-true))
)
)
(return-false)
)
这可以简化为:
(defun findEdge (graph edge)
(member edge findEdge :test #'equal))