这是Common Lisp的代码:相互递归Common Lisp中
(defun take (L)
(if (null L) nil
(cons (car L) (skip (cdr L)))))
(defun skip (L)
(if (null L) nil
(cons (car L) (take (cdr L)))))
这里的想法是,“走”将给所有在输入列表中的奇数序列的元素和“跳过”将会给所有的甚至是输入列表中的序列元素。但是,在这两种情况下,整个列表都会返回。
此代码中的错误是什么?这与CL如何处理列表有关,因为SML中的类似代码提供了所需的输出。
fun take(lst) =
if lst = nil then nil
else hd(lst)::skip(tl(lst))
and
skip(lst) =
if lst = nil then nil
else hd(lst)::take(tl(lst));
您的'take'不会返回奇怪的索引元素,而是返回偶数个元素。索引从'0'开始,如果它包括'(car L)==(nth 0 L)','take'将返回元素'0','2','4'等。 –