rand-nth在编译后总是产生相同的结果

时间:2012-11-25 21:45:20

标签: clojure

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

(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?

1 个答案:

答案 0 :(得分:2)

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