gpt4 book ai didi

scheme - 方案中的随机函数

转载 作者:行者123 更新时间:2023-12-01 08:40:06 26 4
gpt4 key购买 nike

我正在尝试从方案中的字符串列表中获取随机字符串。示例列表(“这个”“那个”“今天”“昨天”)因此,根据列表的长度创建一个随机数并输出该单词。但是不断出错!

我试过这样:

;; 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))]))

错误:

procedure application: expected procedure, given: 
("this" "that" "today" "yesterday") (no arguments)

当我尝试做的时候:

(random-function (random (length list)) 

在控制台上我得到一个随机数。

不明白为什么在我的程序中完成时它会在这里崩溃???

我能否以更好的方式来执行此操作,而不是循环多次。在 Java 中,我会使用一个数组并直接给出位置。无论如何也要在计划中做到这一点?

最佳答案

(define (random-element list)
(list-ref list (random (length list))))

关于scheme - 方案中的随机函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4174839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com