问题
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