14

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.

Pranav
  • 3,340
  • 8
  • 35
  • 48

4 Answers4

9

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.

Pinochle
  • 5,515
  • 2
  • 26
  • 20
  • Note also that to test such a script from DrScheme, you can pop up the language selection dialog (ctrl+L), then click the "show details" button, and on the right you'll have a place to enter the command line arguments that your script will see. – Eli Barzilay Jul 25 '09 at 01:08
  • 1
    How would it work with MIT Scheme? I cannot find any documentation on this. – Giorgio Jul 23 '12 at 16:04
3

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))))))))
Louis Gerbarg
  • 43,356
  • 8
  • 80
  • 90
2

This solution works for me

#! /usr/bin/env guile
!#

(display "hello")
(newline)
avsej
  • 3,822
  • 4
  • 26
  • 31
1

Also how to accept arguments from the command-line in a Scheme program?

The R6RS library defines a function called command-line which returns the list of the arguments (the first one being the name of the program). Not all implementations of Scheme implement R6RS though; your implementation might have some other function for this.

How do you run Scheme programs from the terminal in linux(ubuntu)?

It depends on which implementation of Scheme you're using.

newacct
  • 119,665
  • 29
  • 163
  • 224