在递归过程中显示到输出端口 - 方案

发布于 2024-12-23 02:09:45 字数 321 浏览 4 评论 0原文

我正在学习Scheme,想编写一个递归过程,在每个运行级别输出到控制台:

(define (dummy count)
    (if (= 0 count)        
        (runtime)
        ((display "test" console-i/o-port) (dummy (- count 1)))))

然后测试:

(dummy 10)

但似乎只有最后调用的过程的输出才会被打印出来。 我应该做什么才能实现它?谢谢。 (我正在使用 Mit-scheme)

I am learning Scheme and want to write a recursive procedure which output to the console in each run level:

(define (dummy count)
    (if (= 0 count)        
        (runtime)
        ((display "test" console-i/o-port) (dummy (- count 1)))))

And then test with:

(dummy 10)

But it appears that only the output of the last procedure called will be printed out.
What should I do to make it happen? Thanks. (I am using Mit-scheme)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

南街女流氓 2024-12-30 02:09:45
((display "test" console-i/o-port) (dummy (- count 1)))

这是一个函数调用,其中 (display "test" console-i/o-port) 是应该调用的函数,(dummy (- count 1))是该函数的参数。由于 `(display "test" console-i/o-port) 实际上并不返回函数,因此这将导致错误(在打印 test 后)。

要执行您真正想做的事情(首先执行 (display ...) 然后执行 (dummy ...)),您可以使用 begin 形式如下:

(begin (display "test" console-i/o-port) (dummy (- count 1)))
((display "test" console-i/o-port) (dummy (- count 1)))

This is a function call where (display "test" console-i/o-port) is the function that's supposed to be called and (dummy (- count 1)) is the argument to that function. Since `(display "test" console-i/o-port) does not actually return a function, this will cause an error (after printing test).

To do what you actually want to do (first execute (display ...) and then execute (dummy ...)), you can use the begin form like this:

(begin (display "test" console-i/o-port) (dummy (- count 1)))
落墨 2024-12-30 02:09:45

如果您想要做的是显示“test”count 次(示例中为 10),您可以执行类似以下操作(假设 count 为正):

(define (dummy count)
  (if (> count 0)
      (begin 
        (display "test" console-i/o-port)
        (dummy (- count 1)))))

If what you want to do is displaying "test" count number of times (10 in the example) you can do something like this (assuming that count is positive):

(define (dummy count)
  (if (> count 0)
      (begin 
        (display "test" console-i/o-port)
        (dummy (- count 1)))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文