0

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.

XPD
  • 1,121
  • 1
  • 13
  • 26
SSS
  • 11
  • 3
  • I'd say, use `let`, but it's really not that clear, what you mean by "put inside a defn". Could you maybe add what you have tried and how it failed? – cfrick Nov 14 '19 at 20:36

1 Answers1

0

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)))
xificurC
  • 1,168
  • 1
  • 9
  • 17