如何在mit-scheme中正确使用(读取)? [英] How to use (read) correctly in mit-scheme?

查看:92
本文介绍了如何在mit-scheme中正确使用(读取)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了文档和Rosetta代码,其中(read)用于从控制台获取输入.所以我写了这段代码来检查:

I read in the documentation and rosetta code that (read) is used to get input from the console. So I wrote this code to check this:

(display (+ (read) 1))

但是mit-scheme从不要求用户输入,该程序只是终止.为什么会这样?

But mit-scheme never asks for user input and the program just terminates. Why is this the case?

推荐答案

在REPL中,(display (+ (read) 1))可以正常工作.

In the REPL, (display (+ (read) 1)) works as expected.

(display (+ (read) 1))放在源文件中,并且使用mit-scheme --quiet < program.scm(引用),mit-scheme从不要求用户输入,并且程序只是终止.为什么?

When (display (+ (read) 1)) is placed in a source file, and the file is run as a script using mit-scheme --quiet < program.scm (reference), mit-scheme never asks for user input and the program just terminates. Why?

要查看原因,请将其放在源文件中:

To see the reason, place this in the source file instead:

(define n (read))
2
(display (+ n 1))

您得到的3符合预期.

这都是由Shell输入重定向(即<)引起的. read默认情况下从当前输入端口获取其输入.使用shell输入重定向时,当前输入端口是源文件.因此,(read)不会提示用户输入,因为stdin是源文件.

This is all caused by the shell input redirection (i.e. <). read gets its input from the current input port by default. With shell input redirection, the current input port is the source file. Hence, (read) does not prompt for user input because stdin is the source file.

据我所知,目前尚没有简单的方法可以直接从命令行直接正确运行MIT方案脚本(惊讶!出人意料!MIT方案已过时).相关邮件列表讨论: [MIT-Scheme- devel]如何运行脚本并退出?.

To the best of my knowledge, there is currently no easy way to correctly run an MIT Scheme script directly from the command line (surprise! surprise! MIT Scheme is antiquated). Relevant mailing list discussion: [MIT-Scheme-devel] How to run a script and exit?.

这篇关于如何在mit-scheme中正确使用(读取)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆