Recursion Lisp中的合并列表
我试图获取一个表示二叉搜索树的列表,并按顺序输出列表元素,以便Recursion Lisp中的合并列表,recursion,lisp,common-lisp,Recursion,Lisp,Common Lisp,我试图获取一个表示二叉搜索树的列表,并按顺序输出列表元素,以便(displayBST'(10(5(3(2()())())())->(2 3 5 10)。我所能得到的似乎是列表看起来像((23)5)10),我不知道如何使所有数字都成为基本元素 (let((SUMS)) (defun displayBST(elements) ;IF NO ELEMENTS return SUMS (cond((null elements) nil) ;if both bran
(displayBST'(10(5(3(2()())())())
->(2 3 5 10)
。我所能得到的似乎是列表看起来像((23)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))))))))
考虑如何将给定元素与函数递归返回的值连接起来。如果想要X+Y=(xy),应该使用(cons X(list Y))。因此,基本情况(即(null(第二个元素))和(null(第三个元素))应该返回(列表(第一个元素))。 你想要的是这样的:
(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))))))))
考虑如何将给定元素与函数递归返回的值联接。如果希望X+Y=(xy),则应使用(cons X(list Y))。因此基本情况(即(null(第二个元素))和(null(第三个元素))应返回(list(第一个元素))。 你想要的是这样的:
(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))))))))
你可以在结果列表中使用
flatten
((23)5)10)你可以在结果列表中使用flatten
((23)5)10)在词法闭包中定义一个(顶级)函数是有效的,但可能会有有趣的(我说的“有趣”,可能是指“可怕的”)结果。最好将实现的适当较小部分封装在由函数中的let
创建的词法环境中(根据需要使用flet
或labels
)。在词法闭包中定义(顶级)函数是有效的,但可能会有有趣的(和“有趣的”)效果,我的意思可能是“可怕的”)后果。最好将实现中适当较小的部分封装在函数中由let
创建的词法环境中(根据需要使用flet
或标签)。