在 Lisp 中合并列表
Merge lists in Lisp
我正在尝试获取表示二叉搜索树的列表并按顺序输出列表元素,因此 (displayBST '(10(5(3(2()())())())()))
-> (2 3 5 10)
。我似乎能得到的只是看起来像 (((2 3) 5) 10)
的列表,我不确定如何制作所有数字的基本元素。
(let((SUMS))
(defun displayBST(elements)
;IF NO ELEMENTS return SUMS
(cond((null elements)
nil)
;if both branches null return first element
((and(null (second elements))(null (third elements)))
(print (first elements))
(first elements))
;if left branch not null
((not(null (second elements)))
;if right branch null
(cond((null (third elements))
;set SUMS to (left branch) and first element
(setf SUMS (list (displayBST(second elements)) (first elements))))
;else set SUMS to (left branch) and first element and (right branch)
(t(SETF sums (append (displayBST(second elements))(first elements)(displayBST(third elements)))))))
;if left branch null and right not null
((not (null(third elements)))
;set SUMS to first element and (right branch)
(setf SUMS (list (first elements) (displayBST(third elements))))))))
想想如何将给定元素连接到函数递归地 returned 的值。如果你想要 X + Y = (X Y) 你应该使用 (cons X (list Y))。因此,基本情况(即(null(第二个元素))和(null(第三个元素))应该return(列表(第一个元素))。
你想要的是这样的:
(let((SUMS))
(defun displayBST(elements)
;IF NO ELEMENTS return SUMS
(cond((null elements)
nil)
;if both branches null return first element
((and(null (second elements))(null (third elements)))
(print (first elements))
(list (first elements)))
;if left branch not null
((not(null (second elements)))
;if right branch null
(cond((null (third elements))
;set SUMS to (left branch) and first element
(setf SUMS (append (displayBST(second elements)) (list (first elements)))))
;else set SUMS to (left branch) and first element and (right branch)
(t(SETF sums (append (displayBST(second elements))(first elements)(displayBST(third elements)))))))
;if left branch null and right not null
((not (null(third elements)))
;set SUMS to first element and (right branch)
(setf SUMS (cons (first elements) (displayBST(third elements))))))))
我正在尝试获取表示二叉搜索树的列表并按顺序输出列表元素,因此 (displayBST '(10(5(3(2()())())())()))
-> (2 3 5 10)
。我似乎能得到的只是看起来像 (((2 3) 5) 10)
的列表,我不确定如何制作所有数字的基本元素。
(let((SUMS))
(defun displayBST(elements)
;IF NO ELEMENTS return SUMS
(cond((null elements)
nil)
;if both branches null return first element
((and(null (second elements))(null (third elements)))
(print (first elements))
(first elements))
;if left branch not null
((not(null (second elements)))
;if right branch null
(cond((null (third elements))
;set SUMS to (left branch) and first element
(setf SUMS (list (displayBST(second elements)) (first elements))))
;else set SUMS to (left branch) and first element and (right branch)
(t(SETF sums (append (displayBST(second elements))(first elements)(displayBST(third elements)))))))
;if left branch null and right not null
((not (null(third elements)))
;set SUMS to first element and (right branch)
(setf SUMS (list (first elements) (displayBST(third elements))))))))
想想如何将给定元素连接到函数递归地 returned 的值。如果你想要 X + Y = (X Y) 你应该使用 (cons X (list Y))。因此,基本情况(即(null(第二个元素))和(null(第三个元素))应该return(列表(第一个元素))。 你想要的是这样的:
(let((SUMS))
(defun displayBST(elements)
;IF NO ELEMENTS return SUMS
(cond((null elements)
nil)
;if both branches null return first element
((and(null (second elements))(null (third elements)))
(print (first elements))
(list (first elements)))
;if left branch not null
((not(null (second elements)))
;if right branch null
(cond((null (third elements))
;set SUMS to (left branch) and first element
(setf SUMS (append (displayBST(second elements)) (list (first elements)))))
;else set SUMS to (left branch) and first element and (right branch)
(t(SETF sums (append (displayBST(second elements))(first elements)(displayBST(third elements)))))))
;if left branch null and right not null
((not (null(third elements)))
;set SUMS to first element and (right branch)
(setf SUMS (cons (first elements) (displayBST(third elements))))))))