Clojure - 如何将列表和函数放入 defn 函数中

Clojure - How to put a list and a function inside a defn function

我是新手,正在做一些练习。如何在 defn 函数中放入带有句子列表和随机数发生器函数的 def?这是如何运作的?

(def list["test1", "test2", "test3"]) - 工作正常 (rand-nth list) - 工作正常

如何将其放入函数中 defn

感谢您的帮助。

IIUC 您只想重新实现 rand-nth,不是吗?

(defn wrapped-rand-nth [a-list]
  (rand-nth a-list))

如果您希望列表是静态的(不变的)

(defn randomize []
  (rand-nth ["test1" "test2" "test3"]))

有效,但它会在每次调用时创建向量,更好的方法是

(let [the-list ["test1" "test2" "test3"]]
  (defn randomize []
    (rand-nth the-list)))