HOW TO RUN SCHEME by Gary T. Leavens Department of Computer Science, Iowa State University File $Date: 1993/10/07 22:44:00 $ This document gives basic ways to use Scheme on Project Vincent, and ways to use Scheme from within emacs. It also describes how to get Scheme for other computers. 1. USING CHEZ SCHEME ON PROJECT VINCENT The default for this course is to run Chez Scheme on Project Vincent. 1.1 STARTING CHEZ SCHEME Log in to your Vincent account. Then type (at the shell's prompt, %) add scheme and then hit the ``return'' (or enter) key. Be sure your input is in lower case (NOT CAPITALIZED). You will see something like the following. You have added Chez Scheme version 4.1 You can put the add scheme command in your .startup.tty file to have it automatically executed each time you log in. (We will do this in an exercise for homework 0.) Now just type scheme and hit ``return''. You will see the following: Chez Scheme Version 4.1 Copyright (c) 1991 Cadence Research Systems > with the prompt ``> '' indicating that Chez Scheme is waiting for your input. Now type your input, followed by a ``return''. (Try typing (+ 2 3) and then ``return'' for starters.) 1.2 EXITING CHEZ SCHEME To exit Chez Scheme type (exit) or type a control-d (hold down the ``control'' key and at the same time type a ``d'', without the quotes.). 1.3 MAKING A TRANSCRIPT To record your session with the Chez Scheme interpreter in a file named ``trans.out'' (without the quotes), that is to put your inputs and its outputs in the file ``trans.out'', type to Chez Scheme (transcript-on "trans.out") This must be typed exaclty as above, with the double quotes ("). To make a transcript in some other file, write that name instead of trans.out. 1.4 LOADING FILES It is very convenient to put your code for Scheme programs in files, and have Chez Scheme read those files. You can do this by simply listing the files you want to load on the command to start Chez Scheme. For example, to read in files named ``foo.ss'' and ``bar.ss,'' type to Vincent: scheme foo.ss bar.ss If your files have errors you will see an error message; for example, Error in read: unexpected end-of-file on #. is an error message that means that foo.ss is missing one or more right parentheses (a common mistake). Another way to have Chez Scheme read a file is to use the load procedure. The advantage of this is that load can be called from a program; for example, you can have one file that loads several others. Another advantage is that you can use load after you have already started Chez Scheme. An example of using load after starting Chez Scheme is as follows. (Recall that ``> '' is the Chez Scheme prompt, you don't type that.) > (load "foo.ss") Error in read: unexpected end-of-file on #. Type (debug) to enter the debugger. > This example still has a missing right parenthesis, and so load gives an error message. 1.5 LOADING SOME FILES BY DEFAULT There is one file you will always want to load in Scheme (once the course gets going). It is the file ``/home/cs227/lib/springer-friedman.ss''. To arrange for this to happen automatically whenever you start Chez Scheme do the following. * Be sure that you have added the Vincent locker cs227. To do this execute add cs227 from the Vincent shell. This is needed before you can get at files in ``/home/cs227''. (We will have an exercise to put this in your file named ``.startup.tty'' in homework 0.) * Make a directory, called bin for historical reasons, to contain the Unix shell script that will be made below. mkdir /bin * Create a file named ``~/bin/scheme'' (e.g., using emacs) that contains the following line. exec /home/scheme/bin/dec/scheme /home/cs227/lib/springer-friedman.ss $* Be sure that there is a newline following this line. That is, if emacs asks you if you want one, answer yes. The line says to start Chez Scheme and load the file ``/home/cs227/lib/springer-friedman.ss'' and then any other files you specify on the command line. (If you ever wish to have other files that you automatically load, you can list them after ``springer-friedman.ss'' and before the ``$*''.) * Change the unix file mode (permissions) on the file you just created to make it executable. To do this execute the following from the Vincent Shell. chmod +x ~/bin/scheme * To check that this has been set up properly, try the following from the Vincent shell. rehash which scheme echo ~/bin/scheme The responses to the last 2 commands should be the same. (Note, this will not affect any scheme interpreters that you already have running.) 1.6 CHEZ SCHEME ADVANCED TOPICS See the manual page for details about compiling files and saving your session so you can start where you left off. To see the manual page type man scheme (This will show one page; to see more type a space.) 2. EDITING SCHEME FILES 2.1 EDITING SCHEME FILES WITH EMACS Of the standard Unix editors, emacs has the best support for Scheme programming (and for C and C++ programming). In addition to matching parentheses (when you type ``)'' it shows which ``('' matches it) for you, emacs knows how to indent Scheme programs to make them look good. But before emacs will do this for you, it must know that you are editing a Scheme program. The best way to tell emacs that you are editing a Scheme program is to put the following lines in your file ``~/.emacs'' (We will do this as an exercise in Homework 0.) ;; tell emacs where to find the emacs-lisp files (setq load-path (cons "/home/cs227/emacs" load-path)) ;;; tell emacs what files should be loaded for what commands (autoload 'run-scheme "cmuscheme" "Run Scheme in an emacs buffer." t) (autoload 'scheme-mode "cmuscheme" "Mode for Scheme programming and interaction" t) ;;; define special behavior that I want for scheme program files (setq scheme-mode-hook '((lambda () (setq scheme-mit-dialect nil) (define-key scheme-mode-map "\n" 'newline) (define-key scheme-mode-map "\r" 'newline-and-indent)))) ;;; tell emacs that files that end in .ss are Chez Scheme files. (setq auto-mode-alist (append '(("\\.ss$" . scheme-mode)) auto-mode-alist)) You have to get these lines almost exactly as above for emacs to work right. The indentation above is good, but doesn't really matter except when you want to read it. The lines would normally be at the left margin, but that also doesn't matter much. What does matter are all the characters and spaces between characters in the lines not beginning with a ``;''. (Lines beginning with a ``;'' are comments.) To see if you got it right, quit emacs, and then start it again and edit a file whose name ends in ``.ss''. If you get an emacs error that says ``Error in init file'', then edit your ``~/.emacs'' file again until it matches the above exactly. (You can use emacs to do this, even if you have an error in your ``~/.emacs'' file, which is your init file) (You can have other emacs-lisp in your ``~/.emacs'' file too.) With the above in your ``~/.emacs'' file, If you are editing a file whose name ends in ``.ss'' then that is a Scheme file. Be sure that all your Scheme file names end in ``.ss''! For example ``member.ss'' is a good name, but not ``junk''. To find out about scheme-mode in emacs, edit a scheme file and then type C-h m (that is, control-h and then ``m''). You will see a table of commands that looks like the following (which has been simplified; what you see in emacs by typing C-h m is the truth). What you type What it does ============= ============================= RET newline-and-indent LFD newline DEL backward-delete-char-untabify TAB scheme-indent-line C-c C-k scheme-compile-file C-c C-l scheme-load-file C-c C-z switch-to-scheme C-c C-c scheme-compile-definition C-c C-r scheme-send-region C-c C-e scheme-send-definition C-x C-e scheme-send-last-sexp ESC C-x scheme-send-definition ESC C-q scheme-indent-sexp For example, if you type control-x and then control-e, it sends the previous s-expression (a parentheses balanced thing you typed) to the Scheme process. 2.2 INTERACTING WITH SCHEME FROM EMACS To run Scheme from within Emacs, start emacs, and then use the command M-x run-scheme. (That is, type ESC, then ``x'' then at the prompt ``run-scheme''. This will put you in ``inferior scheme mode'' in a buffer called *scheme*. You will soon see the Chez scheme system starting, just as if you had run it directly from the project vincent shell. You can type expressions at the > prompt, and then by typing ``return'', you will see them evaluated. To find out about inferior scheme mode in emacs, from the *scheme* buffer type C-h m. You will see a table of commands that includes those in the table above, and some additional ones. The basic addition is that when you type a return (RET), what you typed is sent to Scheme. You can edit the input using emacs commands, but note that whatever is on a line at the time you type return (RET) is sent to Scheme, and is not affected by further editing. However you can go back up in the buffer and send an edited expression again by placing your cursor just to the right of the last right parentheses, and typing return. You do not need to separately exit Scheme and emacs, as exiting emacs will kill Scheme. The scheme running under emacs does not automatically know about changes you make to Scheme code files or other emacs Scheme buffers. You have to either explicitly load the file (use load-file in Scheme as described above), or use a scheme-send command (such as C-x C-e) in the buffer where you are editing your code. (Note that these have to be in the same emacs process, if you are on a workstation and you can move two emacs windows independently, they are not the same.) 2.3 EDITING SCHEME WITH VI If you don't already know how to use vi, don't bother to learn. The vi editor does not know how to format Scheme code. If you must use vi, invoke it with the ``-l'' option, which will make it match parentheses for you. 3. OTHER SCHEMES The following information is included for completeness. You probably don't need it, unless you have a home computer. 3.1 MIT C-SCHEME ON PROJECT VINCENT Another Scheme on Project Vincent is MIT's C-Scheme. To invoke it type add mit_scheme and then scheme You can't easily use both Chez Scheme and C-Scheme. C-Scheme also has a manual page. Extensive information about it is found in the info files in /home/mit_scheme/info that you can read with xinfo on a workstation, or by using info (C-h i) within emacs. 3.2 SCM ON PROJECT VINCENT When you add the cs227 locker, you also get access to another version of Scheme called scm. To invoke it, type scm Details on its use in its manual page. This may go away at any time, but it is free software. This version of Scheme also runs on PCs, etc. 3.2 SCHEME FOR A PC If you want a commercial PC version, you can buy, among others, EdScheme from Schemer's Inc. EdScheme runs on Macintosh, DOS and Atari ST and costs $50. It includes an incremental compiler, and editor, and is a close match to the IEEE standard. Implemented by Iain Ferguson, Edward Martin, and Burt Kaufman. The book (The Schemer's Guide) is 328 pages long costs $30. Write to: Schemers Inc., 2136 NE 68 Street, Suite 401, Fort Lauderdale, FL 33308, call (305) 776-7376, or fax (305) 776-6174. You can also send email to 71020.1774@compuserve.com. They also have a product (3DScheme) for three-dimensional modelling in Scheme. If you have an IBM PC or compatible you can get a free scheme interpreter for it. The free system is called PC Scheme. One way to get this is to buy the book ``PC Scheme'' (1988, Scientific Press). The advantage of doing that is that you get the documentation. (You may want this documentation even if you get the free version.) PC Scheme includes an optimizing compiler, an emacs-like editor, inspector, debugger, performance testing, foreign function interface, window system and an object-oriented subsystem. Conforms to the Revised^3 Report on Scheme. However, the version of PC Scheme in the book ``PC Scheme'' is that it does not work on '486-class PCs. To get a version that works, bring a 3.5-inch high density formatted floppy disk to the instructor, or, if you know how, download the files in the directory /home/cs227/PCScheme. (It is possible to copy them onto floppies from the PCs in Durham center, and even from some Vincent workstations.) This version is PCS/Geneva, which is a cleaned-up version of PC Scheme developed at the University of Geneva. The main extensions to PC Scheme are 486 support, BGI graphics, LIM-EMS pagination support, line editing, and assmebly-level interfacing. (TI's PC Scheme gives users full Revised^3 support along with many primitives for DOS, Graphics and Text Windows. A powerful built-in optimizing compiler produces fast code.) The beta version of PCS/Geneva has been tested on XTs, ATs, AT386s and AT486s under various DOS and OS/2 versions. It even runs on Hewlett-Packard's HP95LX. (Those who know what ftp is can also ftp it from cui.unige.ch in the directory PUBLIC/pcs.) 3.3 SCHEME FOR A MACINTOSH If you have an Apple Macintosh, you can get MacScheme from Lightship Software. MacScheme is a Scheme interpreter and compiler for the Apple Macintosh, and includes an editor, debugger and object system. MacScheme costs $125 (includes compiler) and Scheme Express costs $70 (interpreter only). It requires 1mb RAM. A development environment (MacScheme+Toolsmith) costs $495. MacScheme+Toolsmith includes support for menus, windows, and interfaces to the Macintosh Toolbox, and can create small standalone Macintosh executables. Write to: Lightship Software, PO Box 1636, Beaverton, OR 97075, or call (503) 292-8765. They're moving to California. The temporary phone number is 415-940-4008 (Liz Heller). The new phone number will be 415-694-7799. MacScheme is distributed by ACS, 2015 East 3300 South, Salt Lake City, UT 84109-2630, 1-800-531-3227 (801-484-3923). Other places to call are Academic Software at 800-531-3227, or Ted Essen of Computer Biz at 408-456-0100. There is a book "MacScheme" published by MIT Press which includes a manual and a disk (perhaps not for the interpreter, but merely some programs, I'm not sure), reportedly for $37.50. MacScheme+ToolSmith is documented in a book by the same name (Semantic Microsystems, 1987). There is a free Scheme interpreter for the Macintosh called Gambit 1.9, which is available in /home/cs227/Gambit. (You can also get it by anonymous ftp from trex.iro.umontreal.ca.) Gambit is an optimizing Scheme compiler/system. It conforms to the IEEE-Scheme standard (IEEE P1178) and the Revised^4 Report on Scheme (R4RS). The system supports the whole numeric tower (i.e. integer, rational, real and complex numbers). It also has several extensions to the standards including: weak pairs, string ports, property lists, futures, pretty printer, debugger, compiler and multitasking. The latest version for the Mac running the normal System/Finder is MacGambit release 1.9.1.