56

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 tried scheme < test.scm and it still uses the REPL, the only difference is that scheme exits when the stream ends.

Flux
  • 9,805
  • 5
  • 46
  • 92

4 Answers4

50

scheme < file.scm should work (as long as you don't specify --interactive and stdin is not a terminal, scheme works non-interactively).

Julio Marins
  • 10,039
  • 8
  • 48
  • 54
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • This doesn't fulfill the OP's edited requirement of "not using the REPL". See [this answer](https://stackoverflow.com/a/47724861/5916915). – Flux Dec 10 '17 at 05:11
17

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

Flux
  • 9,805
  • 5
  • 46
  • 92
  • don't get the `<` the wrong way around or you will overwrite your own source code, or is it only me that does stuff like that? – Paul Rooney Jun 04 '19 at 11:17
  • @PaulRooney If you tend to make such mistakes, I would suggest writing a shell script or shell function for running your Scheme programs. Example shell function: `runscheme () { scheme --quiet < "$1" }`. – Flux Jun 04 '19 at 11:26
  • ok thank you. I dont tend to make such mistakes too often. Sorry it was not a criticism of the answer. I just did it and it made me laugh when I realised what had happened. – Paul Rooney Jun 04 '19 at 11:32
  • @PaulRooney Edited the answer so that others can avoid such mistakes in the future. Thank you for bringing this to my attention. – Flux Jun 04 '19 at 11:50
  • sorry I really didnt intend for you to change the answer. I just thought it was amusing. I would upvote, but I did that already. – Paul Rooney Jun 04 '19 at 12:06
7

I think what you want is SCM. You can execute a .scm script like this:

$ scm -f foo.scm arg1 arg2 arg3

See http://people.csail.mit.edu/jaffer/scm_3.html#SEC28 for more details.

The SCM homepage: http://people.csail.mit.edu/jaffer/SCM

Forrest
  • 89
  • 1
  • 3
0

checked chez --help, and then I found this(let's say that I'm using chez scheme):

chez --script ./temp.scm

Also, --verbose is very useful:

chez --verbose --script ./temp.scm
yanyingwang
  • 117
  • 6