loops - Racket 中的 While 循环

标签 loops while-loop scheme conditional-statements racket

在 Racket 中编写一个过程,其作用类似于 Java 等语言中的“while 循环”。 while 循环需要运行两段代码:条件和主体。它运行主体,然后再次尝试条件,如果为真,则再次运行主体。这一直持续到条件返回 false。

我就是这样开始的:然后我就卡住了。

(define (while condition body)
   (when (true? condition)
       (cons (proc body) (proc (rest body)))

最佳答案

conditionbody 是过程,所以你必须调用它们。

调用正文后,必须递归调用while来重复。

(define (while condition body)
  (when (condition)
    (body)
    (while condition body)))

您不需要 true?,因为 when 只是检查条件是否为 #f

关于loops - Racket 中的 While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60420545/

相关文章:

bitmap - 在 Racket 中设置位图的 alpha

java - 在java循环中节省内存和CPU

c - while循环不执行

c - 使用循环理解数字反转

python - 使用 PYTHON 计算在 while 循环中设置新变量

c - 修改并替换C中char数组中的值

c++ - 如何使用 ifstream 打开一个文件并一直读到最后

lisp - 计算 1000 以下的 3 和 5 的倍数之和

c - 从二进制文件写入和读取整数数组

functional-programming - 函数式编程语言的静态分析器,例如Scheme