Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Scheme from the command line

How do you run Scheme programs from the terminal in linux(ubuntu)? Also how to accept arguments from the command-line in a Scheme program?

Edit: Im using the DrScheme implementation.

like image 627
Pranav Avatar asked Jul 24 '09 06:07

Pranav


People also ask

How do I run a Scheme code?

The easiest way to write Scheme code is to use an editor like Emacs and execute run-scheme . After that you can send any s-expression with Ctrl-x Ctrl-e from the editor to the interpreter. Emacs shows the result in the REPL window.

How do I run a Scheme in Emacs?

Start up Emacs and type the following sequence of keys: M-x (which means hold the "Alt" key and hit "x"). At the bottom of the emacs window "M-x" should appear. Type the command "run-scheme".

How do I load a Scheme file?

To load your file of Scheme definitions into the interpreter, type control-c control-l in either buffer. (The Scheme command (load "file. scm") at the Scheme prompt still works as well.) If you modify and save your Scheme source file, you should reload it by using the load command again.


2 Answers

The DrScheme scheme implementation, and the name you use to execute it from the command line, is mzscheme. The documentation for starting a command line script is found here: Unix Scripts (PLT Scheme documentation). Use of the command line args is explained here: Command-line Parsing (PLT Scheme Documentation).

The upshot is that you can use shebang scripts like this:

#! /usr/bin/env mzscheme
#lang scheme/base
(...scheme s-exps...)

or if you want more control over the command line flags for mzscheme, you need to start the script like this:

#! /bin/sh
#|
exec mzscheme -cu "$0" ${1+"$@"}
|#
#lang scheme/base
(...scheme s-exps...)

The function you use to process command line args is command-line. You will find examples of how to use it in the article linked to by the second link.

like image 116
Pinochle Avatar answered Oct 12 '22 10:10

Pinochle


It is not standardized in the R6RS. There is a recommendation SRFI-22, which some interpreters support. If your interpreter does not support SRFI-22 then it depends on your implementation.

Below is an example from the SRFI. It assumes your interpreter is a binary named scheme-r5rs. Basically it calls a function named main with a single arg that is a list of command line args.

#! /usr/bin/env scheme-r5rs

(define (main arguments)
  (for-each display-file (cdr arguments))
  0)

(define (display-file filename)
  (call-with-input-file filename
    (lambda (port)
      (let loop ()
    (let ((thing (read-char port)))
      (if (not (eof-object? thing))
          (begin
        (write-char thing)
        (loop))))))))
like image 20
Louis Gerbarg Avatar answered Oct 12 '22 08:10

Louis Gerbarg