在递归过程中显示到输出端口 - 方案
我正在学习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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个函数调用,其中
(display "test" console-i/o-port)
是应该调用的函数,(dummy (- count 1))
是该函数的参数。由于 `(display "test" console-i/o-port) 实际上并不返回函数,因此这将导致错误(在打印 test 后)。要执行您真正想做的事情(首先执行
(display ...)
然后执行(dummy ...)
),您可以使用begin
形式如下: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 thebegin
form like this:如果您想要做的是显示“test”
count
次(示例中为 10),您可以执行类似以下操作(假设count
为正):If what you want to do is displaying "test"
count
number of times (10 in the example) you can do something like this (assuming thatcount
is positive):