我试图从方案中的字符串列表中获取随机字符串.示例列表("this""that""today""yesterday")因此,基于列表的长度,创建随机数并输出该字.但不断收到错误!
我试过这样的:
;; produces random number that should be input to the random-function
(define (random-num list)
(random-function ((random (length (list))) list)))
;; loops the number of times till random number is 0 and outputs the list value
(define (random-function num list )
(cond
[(zero? num) (car list)]
[else (random-function (- num 1) (cdr list))]))
Run Code Online (Sandbox Code Playgroud)
错误:
procedure application: expected procedure, given:
("this" "that" "today" "yesterday") (no arguments)
Run Code Online (Sandbox Code Playgroud)
当我尝试做的时候:
(random-function (random (length list))
Run Code Online (Sandbox Code Playgroud)
在控制台上我得到一个随机数.
在我的程序里完成时,不明白为什么它会崩溃?
我能以更好的方式做到这一点,而不是循环这么多次.在Java中,我会使用一个数组并直接给出位置.无论如何也要在计划中做到这一点?
Eli*_*lay 10
(define (random-element list)
(list-ref list (random (length list))))
Run Code Online (Sandbox Code Playgroud)