gpt4 book ai didi

Clojure - (读取字符串字符串调用函数

转载 作者:行者123 更新时间:2023-12-04 01:44:40 25 4
gpt4 key购买 nike

我在 clojure 文件中有以下内容:

(ns helloworld
(:gen-class
:main -main))

(defn hello-world-fn []
(println "Hello World"))

(defn -main [& args]
(eval (read-string "(hello-world-fn)")))

我正在运行它

lein run helloworld

我收到以下错误:

Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol:
helloworld in this context, compiling:(helloworld.clj:12)

我觉得我需要用 ns-resolveresolve 做一些事情,但我没有取得任何成功。我在主要功能中尝试了以下内容:

(let [call-string  (read-string "(hello-world-fn)")
func (resolve (symbol (first call-string)))
args (rest call-string)]
(apply func args))

没有成功。

有人可以(a)指出我正确的方向吗? (b) 准确解释发生这种情况时 Clojure 阅读器中发生了什么?

最佳答案

尝试查看您的-main 中的实际命名空间。 .

(defn -main [& args]
(prn *ns*)
(eval (read-string "(hello-world-fn)")))

它输出 #<Namespace user>在轰炸之前除外。这暗示使用 lein run 执行程序开始于 user命名空间,显然不包含您的 hello-world-fn 的映射象征。您需要明确限定它。

(defn -main [& args]
(eval (read-string "(helloworld/hello-world-fn)")))

关于Clojure - (读取字符串字符串调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9159268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com