如何像Java-中那样在plt-scheme中实现循环
for(int i=0;i<10;){
for(int j=0;j<3;){
System.out.println(""+j);
j++;
}
System.out.println(""+i);
i++;
}
最佳答案
您在Java中的示例仅通过学习一些新关键字就不会直接映射到Scheme语言,因为没有在Scheme中实现for循环的显式构造(除非您自己编写一个构造!)。在Scheme中,完成此操作的方法是定义一个遍历列表的递归函数。这是一个如何在Scheme中执行for循环样式函数的示例:
(define (doit x x-max dx)
(if (<= x x-max)
(begin
;;...perform loop body with x...
(doit (+ x dx) x-max dx))))
(doit a b dx) ; execute loop from a to b in steps of dx
取自此页面:
Guile and Scheme Links
这是指向页面的另一个链接,该页面描述了将循环从命令式语言转换为Scheme时需要理解的想法:
Scheme Looping Constructs
Scheme是一门非常有趣的语言,您还应该阅读Structure and Interpretation of Computer Programs,它是MIT以前用于Scheme教学的教科书。
关于syntax - 在PLT方案中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/985188/