Previous: , Up: scheme overview   [Index]


3.1.15 Top–level programs

A Scheme program is invoked via a top–level program. Like a library, a top–level program contains imports, definitions and expressions, and specifies an entry point for execution. Thus a top–level program defines, via the transitive closure of the libraries it imports, a Scheme program.

The following top–level program obtains the first argument from the command line via the command-line procedure from the (rnrs programs (6)) library. It then opens the file using open-file-input-port, yielding a port, i.e. a connection to the file as a data source, and calls the get-bytes-all procedure to obtain the contents of the file as binary data. It then uses put-bytes to output the contents of the file to standard output:

#!r6rs
(import (rnrs base)
        (rnrs io ports)
        (rnrs programs))
(let ((p (standard-output-port)))
  (put-bytevector p
                  (call-with-port
                      (open-file-input-port
                        (cadr (command-line)))
                    get-bytevector-all))
  (close-port p))