PLT 方案中的循环

发布于 2024-07-24 20:57:31 字数 220 浏览 9 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

舟遥客 2024-07-31 20:57:31

您的 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 和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:

(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

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.

默嘫て 2024-07-31 20:57:31

在 PLT 中你可以这样做:

(for ([i (in-range 10)])
  (for ([j (in-range 3)]) (printf "~s\n" j))
  (printf "~s\n" i))

In PLT you can do this:

(for ([i (in-range 10)])
  (for ([j (in-range 3)]) (printf "~s\n" j))
  (printf "~s\n" i))
终陌 2024-07-31 20:57:31

Scheme中的迭代结构是“do”,你可以在R5RS 规范

您给出的示例如下所示:

(do ((i 0 (+ i 1))) ((> i 9))
  (do ((j 0 (+ j 1))) ((> j 2))
    (display j)
    (newline))
  (display i)
  (newline))

(do ...) 比本示例中显示的内容更为笼统。 例如,您可以让它返回一个值,而不是仅仅将其用于其副作用。 也可以有许多“计数器”:

(do ((i 0 (+ i 1) 
     (j 0 (+ j 2)) 
    ((stop? i j) <return-value>)
   exprs...)

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 ((i 0 (+ i 1))) ((> i 9))
  (do ((j 0 (+ j 1))) ((> j 2))
    (display j)
    (newline))
  (display i)
  (newline))

(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":

(do ((i 0 (+ i 1) 
     (j 0 (+ j 2)) 
    ((stop? i j) <return-value>)
   exprs...)
巷雨优美回忆 2024-07-31 20:57:31

我建议你看看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文