scheme - 等待几秒钟后调用Scheme中的过程?
问题描述
等待几秒钟后是否可以在 Scheme 中调用一个过程?
例如:
(define (facs)
(fac 3)
;wait 5 seconds
(fac 7))
解决方案
标准方案中没有睡眠程序的规定;您将需要依赖特定于实现的功能。
如果您使用的是 Guile Scheme,您可以使用sleep
to sleep for a number of seconds 或usleep
to sleep for a number of microseconds:
(define (delayed-5s msg)
(display "Waiting 5s...")
(force-output)
(sleep 5)
(newline)
(display msg)
(newline))
请注意,您应该刷新输出以确保在您希望看到消息时看到消息。通常,(但并非总是)打印换行符会刷新输出缓冲区,因此您可以提前移动(newline)
并(sleep 5)
合理地期望显示将按预期工作;但最好是显式刷新输出,以确保消息在您需要时从输出缓冲区中刷新。在上面的代码中,force-output
和sleep
都是 Guile 特定的程序。
如果你使用 Chez Scheme,你可以这样做:
(define (delayed-5s msg)
(display "Waiting 5s...")
(flush-output-port (current-output-port))
(sleep (make-time 'time-duration 0 5))
(newline)
(display msg)
(newline))
在 Chez Scheme 中,sleep
接受一个时间对象,并make-time
接受三个参数,返回一个时间对象。其中第一个是时间类型参数,第二个是纳秒数,第三个是秒数。同样,您应该刷新输出缓冲区,并flush-output-port
执行此操作,但它需要过程提供的output-portcurrent-output-port
。在上面的代码中,sleep
和make-time
是 Chez 特定的程序,但是flush-output-port
和current-output-port
是标准的 R6RS Scheme 库程序。
你可以看到,使用 Chez Scheme 提供的 sleep 工具比 Guile Scheme 提供的要复杂一些。其他实现可能有类似的规定,但不是必须的。
推荐阅读
- android - 如何在android中从剩余百分比下载文件(第一次下载停止)
- php - 我将如何回应尚未确认其电子邮件的用户
- compilation - Perl 6 中后缀或后缀之前的点是什么意思?
- postfix - 滥用 - SASL 登录身份验证失败:UGFzc3dvcmQ6
- ios - 如何更新另一个 UIViewController - SWIFT 上的进度视图?
- python-3.x - 使用 pip-python 3.6 安装 flask-pymongo
- assembly - 将 2 个寄存器作为一个移位
- c++ - C++ 代码在 Linux 上按预期工作,但在 Windows 上不工作
- c++ - CMake:从 src 和其他静态库构建共享库
- javascript - 用 Javascript 替换单词而不删除图像