How do I execute a .scm script (outside of the REPL) with MIT-Scheme?

后端 未结 4 715
离开以前
离开以前 2020-12-08 00:17

I want to type something like \'scheme file.scm\' and have it interpret the file, and then take me back to my shell, rather than loading it in the REPL.

edit: I trie

4条回答
  •  隐瞒了意图╮
    2020-12-08 00:50

    To run a scheme program using MIT Scheme:

    scheme --quiet < program.scm
    

    The --quiet option ensures that the output from your program is the only thing that is displayed (i.e. you won't see the REPL, as per your requirements).

    Caveat: This will not work if your program prompts the user for input using the input procedures (e.g. read, read-char, read-line, etc.). This is because of the shell input redirection (<) (See: relevant question). Unfortunately, there is currently no proper way of executing an MIT Scheme script from the command line when input procedures are used. The best option is probably mit-scheme --quiet --load 'myscript', but you'd have to manually exit MIT Scheme when the script finishes. Relevant mailing list thread: [MIT-Scheme-devel] How to run a script and exit?

    EDIT: Due to the possibility that you may mistype < as >, resulting in the overwrite of your source code, I would suggest encapsulating the above command within a shell script or a shell function. For example:

    runscheme () {
        scheme --quiet < "$1"
    }
    

    Then you can run runscheme program.scm without fear that your source code will be overwritten. (Special thanks to Paul Rooney for bringing this potential mistake to my attention).

    References

    scheme --help:

    --batch-mode, --quiet, --silent

    Suppresses the startup report of versions and copyrights, and the valediction.

    This command line option seems to have been mistakenly ommitted from the list of command line options in the documentation, but I think this is a legimate command line option because scheme --help shows it, and because --batch-mode is used in other parts of the reference manual (e.g. here).

提交回复
热议问题