在PLT方案中循环 [英] Loop in PLT Scheme
问题描述
如何像在Java-
for(int i=0;i<10;){
for(int j=0;j<3;){
System.out.println(""+j);
j++;
}
System.out.println(""+i);
i++;
}
推荐答案
您在Java中的示例仅通过学习一些新关键字就不会直接映射到Scheme语言,因为没有显式构造可在其中实现for循环方案(除非您自己编写一个构造!).在Scheme中,完成此操作的方法是定义一个遍历列表的递归函数.这是一个如何在Scheme中执行for循环样式函数的示例:
Your example in Java doesn't directly map onto the Scheme language by just learning a few new keywords as there aren't explicit constructs for implementing a for loop in Scheme (unless you write a construct yourself!). The cookbook way to do this in Scheme is to define a recursive function that loops over a list. Here's an example of how to do a for-loop style function in Scheme:
(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
从此页面拍摄:
这是指向页面的另一个链接,该页面描述了将循环从命令式语言转换为Scheme时需要理解的思想:
Here's another link to a page that describes the ideas you need to understand to translate loops from imperative languages to Scheme:
方案是一种非常有趣的语言,您还应该阅读计算机程序的结构和解释,这是麻省理工学院以前用于教学计划的教科书.
Scheme is a really interesting language to learn, you should also read the Structure and Interpretation of Computer Programs, which is the textbook formerly used for teaching Scheme at MIT.
这篇关于在PLT方案中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!