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

63 Views Asked by At

I am a newbie and making some exercises. How can I put a def with a list of sentences and a randomizer function inside a defn function? How does that work?

(def list["test1", "test2", "test3"]) - works fine (rand-nth list) - works fine

How do I put it inside a function defn?

Thanks for help.

1

There are 1 best solutions below

1
On BEST ANSWER

IIUC you just want to reimplement rand-nth, no?

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

If you want the list to be static (non-changing)

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

works, but it creates the vector upon each call, a better way is to

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