Symbol’s value as variable is void: false when run element-of-setp

流过昼夜 提交于 2019-12-13 20:09:24

问题


Following SICP's instruction, I rewrite its intersection-set as:

(defun intersection-set (set1 set2)
  (cond ((or (null set1) (null set2)) '())
        ((element-of-setp (car set1) set2)
         (cons (car set1)
               (intersection-set (cdr set1) set2)))
        (t (intersection-set (cdr set1) set2))))
(defun element-of-setp(x set)
  (cond ((null set) false)
        ((equal x (car set)) t)
        (t (element-of-setp x (cdr set)))))
(intersection-set  (list 1 2) (list 2 3 4))

Running it reports the following error:

element-of-setp: Symbol’s value as variable is void: false

However, element-of-setp on its own seems to work properly:

#+begin_src emacs-lisp :tangle yes
(defun element-of-setp(x set)
  (cond ((null set) false)
        ((equal x (car set)) t)
        (t (element-of-setp x (cdr set)))))
(element-of-setp 1 (list 1 2 3))
#+end_src

#+RESULTS:
: t  

What's the problem?


回答1:


However, element-of-setp on its own seems to work properly:

Unfortunately, the test you used did not cover all the possible cases.

If you try instead:

(element-of-setp 5 (list 1 2 3))

Then the function is going to reach the case where the list is empty, and in that it will evaluate false, which is most likely undefined; as stated in the comment, boolean values in Emacs-Lisp are represented by nil and non-nil values (atoms).



来源:https://stackoverflow.com/questions/58637196/symbol-s-value-as-variable-is-void-false-when-run-element-of-setp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!