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

1

4

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?

Maddeu answered 7/9, 2019 at 20:52 Comment(1)
Can you show how you run the code? Did you type the code directly into the REPL? Is the code in a file? How did you run the file?Klimesh
K
3

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

Klimesh answered 8/9, 2019 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.