Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我是新手,正在做一些练习。如何将def带有句子列表和随机函数的函数放入defn函数中?这是如何运作的?
def
defn
(def list["test1", "test2", "test3"])- 工作正常 (rand-nth list)- 工作正常
(def list["test1", "test2", "test3"])
(rand-nth list)
如何将其放入函数中defn?
感谢帮助。
IIUC 你只是想重新实现rand-nth,不是吗?
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)))