Next: , Previous: , Up: using   [Index]

2.3 Executing Scheme scripts

Scheme scripts can be executed using the following command:

vicare --r6rs-script PROGRAM

or the shorter:

vicare PROGRAM

Vicare Scheme follows the R6RS recommendations to install a wrapper program called scheme-script only when the configure option --enable-scheme-script is used at package configuration time; this is to avoid conflicts with other Scheme implementations. In this scenario, it is possible to execute Scheme scripts as follows:

scheme-script PROGRAM [program options]

where the optional program options can be retrieved with command-line, command-line. When using scheme-script it is impossible to select command arguments to be passed to Vicare rather than to the script.

Here is a sample script (Pig Latin—“Igpay Atinlay”—is a code that was at one time popular among very young North American children).

(import (rnrs))

;;; Convert a string to its Pig Latin equivalent.
;;; If the first character is a vowel, append "yay".
;;; "egg" -> "eggyay"
;;; If the first character is a consonant, remove it,
;;; and append it plus "ay" to the string.
;;; "foo" -> "oofay"

(define pig-latin
  (lambda (str)
    (let ((first (string-ref str 0)))
      (if (memv first '(#\a #\e #\i #\o #\u))
	  (string-append str "yay")
 	    (substring str 1 (string-length str))
	    (string first) "ay")))))

 (map pig-latin
     (cdr (command-line))))
(flush-output-port (current-output-port))

Assuming we have stored the script in the file demo.sps, we can invoke this script via vicare:

$ vicare --r6rs-script demo.sps ice cream after dinner
(iceyay reamcay afteryay innerday)

On Unix/Linux systems, scripts generally start with a “shebang” line (sharp plus bang) which names an interpreter for the script. All modern systems have the env command which searches for a command in the user’s path.

#!/usr/bin/env vicare --r6rs-script
(import (rnrs))


Now we can make the file executable, and use it directly.

$ chmod +x demo.sps
$ ./demo.sps ice cream after dinner
(iceyay reamcay afteryay innerday)

There are a few fine points.

Next: , Previous: , Up: using   [Index]