Clojure 林润,元';t正确运行-main,尽管干净的nrepl将

Clojure 林润,元';t正确运行-main,尽管干净的nrepl将,clojure,leiningen,Clojure,Leiningen,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 (firs

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时

其他字符串“+123456”将正确计算,但由于某些原因,它无法识别函数(t…)


有人知道发生了什么吗?

eval
使用
*ns*
的当前(线程绑定)值来确定当前“所在”的命名空间,该命名空间控制如何解析不合格符号。在运行
(ns my-app.core…
)后的repl中,您位于
my-app.core
命名空间中,因此eval会找到您在其中定义的
t
。但是,在编译之后,即使您仍然在
我的app.core
中定义了
t
,当您的程序开始运行时,您处于
用户
命名空间中,eval无法找到
t

因此,您需要做的就是将
-main
中的命名空间更改为
my app.core
,使用
binding
如下所示:

(defn -main [& args] (binding [*ns* (the-ns 'my-app.core)] (loop [line (read-line)] (println (eval (read-string (apply str (interpose \space (str "(" line ")")))))) (when-not (= (count line) 1) (recur (read-line)))))) (定义-主参数[&args] (绑定[*ns*(ns'my app.core)] (循环[行(读取行)] (println(eval(读取字符串(应用str(interpose\space(str)(“line”)))))))) (不在时(=(计数行)1) (重复(读取第(()()))))
谢谢,这完全解决了所有问题。