Search code examples
debuggingemacssicp

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


I minic a intersction-set operation from sicp

#+name: case-2.3.3-element-of-set-p
#+BEGIN_SRC elisp :session sicp :results value
(defun element-of-set-p(x set)
  (cond ((null set) false)
        ((equal x (car set)) t)
        (t (element-of-setp x (cdr set)))))
(element-of-set-p 1 (list 1 2 3))
#+END_SRC

#+RESULTS: case-2.3.3-element-of-set-p
: t

and the intersection-set

#+BEGIN_SRC elisp :session sicp :results value
(defun intersection-set (set1 set2)
  (cond ((or (null set1) (null set2))
         '())
        ((element-of-set-p (car set1) set2)
         (cons (car set1)
               (intersection-set (cdr set1)
                                 set2)))
        (t (intresection-set (cdr set1)
                                set2))))
(trace-function #'intersection-set)
(intersection-set  (list 1 2) (list 2 3 4))
#+END_SRC

It report error:

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

What's the problem?


Solution

  • You have a couple of typos in your code:

    • It's not element-of-setp, it's element-of-set-p
    • It's not intresection-set, it's intersection-set
    • It's not false, it's nil

    This should work:

    (defun element-of-set-p (x set)
      (cond ((null set) nil)
            ((equal x (car set)) t)
            (t (element-of-set-p x (cdr set)))))
    
    (defun intersection-set (set1 set2)
      (cond ((or (null set1) (null set2))
             '())
            ((element-of-set-p (car set1) set2)
             (cons (car set1)
                   (intersection-set (cdr set1) set2)))
            (t (intersection-set (cdr set1) set2))))