clojure 的新手,尝试为布尔表达式求值器编译以下代码
;core.clj
(ns my-app.core
(:gen-class))
(defn t [& args] (if (empty? args) t
((first args) t (first (rest args)))))
(defn f [& args] (if (empty? args) f
((first args) f (first (rest args)))))
(defn | [cond1 cond2] (if (= cond1 t) t
(if (= cond2 t) t f)))
(defn & [cond1 cond2] (if (= cond1 f) f
(if (= cond2 f) f t)))
(defn ! [& args] (if (= (first args) t)
(apply f (rest args))
(if ( = (first args) f)
(apply t (rest args))
(! (apply (first args) (rest args))))))
(defn -main [& args]
(loop [line (read-line)]
(do
(println (eval (read-string (apply str "" (interpose \space (seq (str "(" line ")")))))))
(if (= (count line) 1) nil (recur (read-line))))))
每次我执行“lein run”并输入字符串“(t|t)=t”时,都会出现以下错误
Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: t in this context
但是,如果我在“src/my_app/”目录中打开一个新的 nrepl,然后输入命令
(-main)
我得到正确的字符串和结果
( ( t | t ) = t )
true
我应该注意,在运行 lein run
其他字符串字符串 "+ 1 2 3 4 5 6" 将正确评估,但由于某种原因它无法识别函数 (t ...)
有人知道发生了什么吗?