PLT 方案中的循环
如何在 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++;
}
How can I implement loop in plt-scheme like in java-
for(int i=0;i<10;){
for(int j=0;j<3;){
System.out.println(""+j);
j++;
}
System.out.println(""+i);
i++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 Java 示例并不能通过学习一些新关键字来直接映射到Scheme 语言,因为在Scheme 中没有用于实现for 循环的显式构造(除非您自己编写构造!)。 在Scheme中执行此操作的常规方法是定义一个循环列表的递归函数。 以下是如何在Scheme中执行for循环样式函数的示例:
取自此页面:
Guile 和Scheme 链接
这是另一个页面链接,该页面描述了将循环从命令式语言翻译为Scheme 所需理解的想法:
Scheme 循环结构
Scheme 是一门非常有趣的语言,您还应该阅读 Structure and Interpretation of Computer Programs,这是麻省理工学院以前用于教学Scheme的教科书。
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:
Taken from this page:
Guile and Scheme Links
Here's another link to a page that describes the ideas you need to understand to translate loops from imperative languages to Scheme:
Scheme Looping Constructs
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 中你可以这样做:
In PLT you can do this:
Scheme中的迭代结构是“
do
”,你可以在R5RS 规范。您给出的示例如下所示:
(do ...)
比本示例中显示的内容更为笼统。 例如,您可以让它返回一个值,而不是仅仅将其用于其副作用。 也可以有许多“计数器”:The iteration construct in Scheme is "
do
", you can look it up in the R5RS specification.The example you gave would look something like this:
(do ...)
is a bit more general than what shows up in this example. You can for example make it return a value instead of just using it for its side effects. It is also possible to have many "counters":我建议你看看 Michele Simionato 的 “Pythonista 的冒险”在计划中”。 它适用于 python-> 方案,但是,它写得确实很好,更重要的是,它来自过程-> 功能。
I'm suggesting you to take a look to Michele Simionato's "The adventures of a pythonista in schemeland". It's for python->scheme, but, it's really well written and, more importantly, it's from procedural->functional.