octopusgrabbus
octopusgrabbus

Reputation: 10695

What is ? element and how to test for it?

Calling reverse on the output of the following

(def seq-counter (atom 0))


(defn tokenize-data [data]
    (reduce
        (fn [out-data-map token]
            (if (seq? token)
                (conj out-data-map {(keyword (str "DIRECTIVE_" (reset! seq-counter (inc @seq-counter))))token})
                (if-not (= token (first '(EQU)))
                    (conj out-data-map {(keyword (str "DATA_1")) token})
                    (conj out-data-map {:START '(EQU)}))))
        {}
        data))

called with

'(EQU (COLOR TABLE) ?)

produces

([:START (EQU)] [:DIRECTIVE_13 (COLOR TABLE)] [:DATA_1 ?])

My question is: What is the ? as a value and how do I compare it (other than what is below)?

I can't seem to test to see if ? is there using \?.

All I can do is compare it like this, and I get the result I want.

(= (last (nth (reverse (tokenize-data '(EQU (COLOR TABLE) ?))) 2)) (first '(?)))

Upvotes: 2

Views: 99

Answers (1)

wrongusername
wrongusername

Reputation: 18918

You can prevent Clojure from evaluating any expression by just putting a quote ' in front of that expression. So '(?) quotes a list with ? as its first element without Clojure trying to call the ? function, and (first '(?)) accesses the ? in that list.

To get at the ? symbol directly, simply do '?, which will let you access the symbol itself without Clojure trying to return the value (if any) that is assigned to ?

user=> (= '? (first '(?)))
true

Upvotes: 3

Related Questions