如何从 Racket 中的字符串列表中打印随机元素?

How can I print a random element from a list of strings in Racket?

我有一个单词列表 (define list-of-elements'(apple, carrot, tomato, cucumber)) 我曾尝试使用 (random) 来打印列表中的随机元素,但从我在错误中看到的 (random) 用于整数。 我如何打印列表中的随机元素?

使用 random with length 获取随机索引,然后从该索引的列表中获取 return 元素。

(define (nth lst i)
  (if (= i 0) (car lst)
      (nth (cdr lst) (sub1 i))))

(define (random-element lst)
  (nth lst (random (length lst))))

(random-element '(apple carrot tomato cucumber))