-1

所以我昨天开始学习 Lisp 并开始做一些问题。

我很难做的事情是在列表中插入/删除原子,同时保持列表相同 ex: (delete 'b '(g a (b) l))will give me (g a () l).

我遇到的问题也是这个问题。我想检查列表中的任何地方是否存在原子。

我追踪了它,它说它T在某个时候返回,但随后被 a 覆盖nil

你们能帮忙吗:)?

我正在使用(appear-anywhere 'a '((b c) g ((a))))

在第 4 个函数调用时它返回T但随后变为nil.

(defun appear-anywhere (a l)
  (cond
   ((null l) nil)
   ((atom (car l))
    (cond
     ((equal (car l) a) T)
     (T (appear-anywhere a (cdr l)))))
   (T (appear-anywhere a (car l))(appear-anywhere a (cdr l)))))
4

1 回答 1

3

让我们看一个明显的问题:

(defun appear-anywhere (a l)
  (cond
   ((null l) nil)
   ((atom (car l))
    (cond
     ((equal (car l) a) T)
     (T (appear-anywhere a (cdr l)))))
   (T (appear-anywhere a (car l))(appear-anywhere a (cdr l)))))

想想上面的最后一行。

让我们稍微改变一下它的格式。

(defun appear-anywhere (a l)
  (cond
   ((null l) nil)
   ((atom (car l))
    (cond
     ((equal (car l) a) T)
     (T (appear-anywhere a (cdr l)))))
   (T
    (appear-anywhere a (car l))
    (appear-anywhere a (cdr l)))))

The last three lines: So as a default (that's why the T is there) the last two forms will be computed. First the first one and then the second one. The value of the first form is never used or returned.

That's probably not what you want.

Currently your code just returns something when the value of a appears anywhere in the rest of the list. The first form is never really used.

Hint: What is the right logical connector?

于 2012-09-20T16:29:16.340 回答