How to use (read) correctly in mit-scheme?

青春壹個敷衍的年華 提交于 2019-12-10 17:21:13

问题


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))

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


回答1:


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

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))

You get 3, as expected.

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.

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?.



来源:https://stackoverflow.com/questions/57837383/how-to-use-read-correctly-in-mit-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!