Search code examples
clojureset

Trying to create an empty set in Clojure


I have this function called not-elimination which takes an argument and applies the not inference rule which states: (not (not x)) infer x. So for example if my argument is '(not (not a)) then #{a} would be my output. Example 2, Argument: '(not (not (not a))) Output: #{(not a)}

The problem I am running into is the case where my argument is '(not x) which is supposed to return #{} (empty set), but I am getting the error below. Any ideas on what the problem is?

    Execution error (IllegalArgumentException) at microproject2.core/not-elimination (core.clj:7).
Don't know how to create ISeq from: clojure.lang.Symbol

My code:

(ns microproject2.core)

(defn not-elimination [expression]
  (if(not-empty expression)
  (if (= (nth expression 1) "x" )
    (set {})
   (sorted-set-by > (last (last expression))))
  (println "The list is empty")))

Solution

  • Here is an example that works, including some unit tests:

    (ns tst.demo.core
      (:use tupelo.core tupelo.test))
    
    (defn not-elimination
      [expr]
      (if (not-empty? expr)
        (if (= (symbol "x") (second expr))
          #{}   ; and empty set, could use `(hash-set)`
          (sorted-set-by > (last (last expr))))
        :the-list-is-empty))
    
    (dotest
      (is= #{} (hash-set)) ; both are equivalent
    
      (is= :the-list-is-empty (not-elimination '()))
      (is= (hash-set 'x) (not-elimination '(not (not x))))
      (is= #{ '(not x) } (not-elimination '(not (not (not x)))))
      )
    

    It is based on this template project.