在 Racket 中编写一个过程,其作用类似于 Java 等语言中的“while 循环”。 while 循环需要运行两段代码:条件和主体。它运行主体,然后再次尝试条件,如果为真,则再次运行主体。这一直持续到条件返回 false。
我就是这样开始的:然后我就卡住了。
(define (while condition body)
(when (true? condition)
(cons (proc body) (proc (rest body)))
最佳答案
condition
和 body
是过程,所以你必须调用它们。
调用正文后,必须递归调用while
来重复。
(define (while condition body)
(when (condition)
(body)
(while condition body)))
您不需要 true?
,因为 when
只是检查条件是否为 #f
。
关于loops - Racket 中的 While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60420545/