【发布时间】:2016-04-03 22:18:38
【问题描述】:
我是 Lisp 新手,我需要创建一个函数,才能返回包含特殊数字的子列表的第二个值。
例如,我有两个参数的函数:其中一个是带有子列表的列表,第二个是我要搜索的特殊编号:
(find_neighbours '((1 2) (3 1) (4 5) (9 1) (2 3) (1 5)) 1)
那个函数应该返回类似的东西:
(2 3 9 5)
因为我们在 (1 2) (3 1) ... 中有包含 1 的子列表。
这是我的解决方案:
(defun find_neighbours (lst node)
(if lst
(cond
((= node (caar lst))
(cons (cadar lst)
(find_neighbours (cdr lst) node))
)
((= node (cadar lst))
(cons (caar lst)
(find_neighbours (cdr lst) node))
)
(T (find_neighbours (cdr lst) node))
)
)
)
【问题讨论】:
-
我知道@sds 已经回答了,但是您应该通过展示您已经拥有的代码、您尝试过的东西、您搜索过的地方等来表现出一些努力。