0

我已经通过 Leiningen 使用 core.clj 创建了项目:

(ns cotd.core
  (:gen-class)
  (:use [clojure.repl :only (doc)]))

(defmacro eval-doc
  [form]
  (let [resulting-symbol (eval form)]
    `(doc ~resulting-symbol)))

(defn- random-function-name []
  (rand-nth (keys (ns-publics 'clojure.core))))

(defn -main
  "Display random doc page"
  [& args]
  (eval-doc (random-function-name)))

在编译和运行之后,它总是产生相同的结果:

$ java -jar cotd.jar
-------------------------
clojure.core/unchecked-negate
([x])
  Returns the negation of x, a long.
  Note - uses a primitive operator subject to overflow.
$ java -jar cotd.jar
-------------------------
clojure.core/unchecked-negate
([x])
  Returns the negation of x, a long.
  Note - uses a primitive operator subject to overflow.

但是连续两次调用:

(do
  (eval-doc (random-function-name))
  (eval-doc (random-function-name))))

它在单个“调用”中产生两个不同的结果。

我尝试过的是谷歌搜索、阅读等,但我不知道发生了什么……

如何动态调用这个 rand-nth?

4

1 回答 1

2

问题不在于 rand-nth,而是因为 let 语句中的结果符号是在编译阶段产生的。@beyamor 在这里提供了答案:Unable to get random (doc) from a namespace

于 2012-11-25T22:35:27.413 回答