Sketchy LISP |
By Nils M Holm,
2006,2007,2008 Buy a copy at Lulu.com |
An Introduction to Functional Programming in Scheme
| Previous Section | - Contents - Index - | Next Section |
Because there is a lot of examples to try in this book, the first thing you will want is a Scheme environment. For your first steps you should choose a stable, well-documented, and easy-to-use implementation. Make sure to get an R5RS or R4RS-compliant version. Here are a few suggestions:
PLT Scheme comes in two flavors: MzScheme is a command
line environment with very good error reporting. For those who cannot
live without it: DrScheme has an IDE. The 3.x versions of both
variants use on the fly compilation. PLT Scheme is open source software
with an unrestrictive license. The 2.x and 3.x versions are fine.
URL: http://www.plt-scheme.org/
Scheme 48 is a very clean implementation that is quite picky
about the programs that it accepts (this is a good thing). It is free
software.
URL: http://www.s48.org/
Scheme 9 from Empty Space is a free, small, and portable
implementation that compiles out of the box on a wide variety of
systems. It implements only a subset of R4RS, though.
URL: http://www.t3x.org/bits/s9fes/
If you prefer the big picture, here is a collection of lots of
implementations. Have a look and see for yourself:
http://community.schemewiki.org/?scheme-faq-standards#implementations
The FreeBSD Project's ports collection has a Scheme section, too:
http://www.freebsd.org/ports/scheme.html
Once you have chosen an environment, installed it, and launched it, it will greet you with some kind of banner:
Foo Scheme ready > _
At this point you can type in or paste an expression. When the expression has a value, the environment will print it and prompt you for another expression:
Foo Scheme ready > (cons 1 2) (1 . 2) > _
When something goes wrong, an informative message will print:
> (car 'x) car: expected a pair, but got: x > _
An interactive environment is also called a read eval print loop (or in short: REPL), because this is what it does: read an expression, evaluate it, print the result, and loop.
To end a session it is normally sufficient to type an end-of-file character such as control-D or control-Z. If this does not work, typing (exit) or (quit) is worth a try.
You will not want to enter programs with a size of more than two or three lines directly at the REPL. Something you definitely want is a decent programmer's editor. Vi or Emacs are fine, if you are used to them. In fact, any editor will do, but one thing that really helps is the capability to show matching parentheses, so you should get an editor that can do this. When using vi, you can use the :set showmatch option and/or the % command.
To test a program, launch your editor in one command line window and the Scheme environment in another. Make sure that your editor saves to the directory in which you started Scheme.
Key in your program in the editor window and save the text when you are done. Here is a sample program to try:
(define (hello) (display "Hello, World!") (newline))
After keying in the program text, save the file. Scheme program files normally have a .scm suffix, so you might name your source file hello.scm, for example.
To load the program into the Scheme environment, type
(load "hello.scm")
at the REPL. You can now run the program by calling the hello function:
> (hello) Hello, World! > _
Above example program does not have a specific result. Some environments print unspecific results, some omit them.
If you change the program in your editor window, you have to re-load it at the REPL in order to transfer the changes to the Scheme environment.
Should your program hang
during execution, you can normally
hit control-C (or whatever key interrupts program execution
on your system) to stop it and return to the REPL.
Maybe you are used to working on a command line and prefer to run programs in batch mode. Most Scheme implementations can do this, too. See the documentation for the actual command line options that are required to do so. Because there is no interaction in batch mode, you have to include the function application that starts your program in the program file, so you would have to change above example to:
(define (hello) (display "Hello, World!") (newline)) (hello) ; start the program
You can then run the program from the shell prompt or DOS prompt using a command like this:
your-scheme -f hello.scm
Of course, you will have to replace your-scheme with the name of your Scheme system and -f with the option that actually loads and runs a program in batch mode.
In batch mode no greeting banner is printed and the environment does not enter the REPL. All expressions of the specified program are evaluated and then the Scheme process terminates. In case of an error or keyboard interrupt, the Scheme process prints an error message and terminates immediately.
When your Scheme system offers a stand-alone compiler, you can compile Scheme programs to executables and run them just like programs written in other languages. Many systems offer both an interactive environment and a stand-alone compiler.
Note that a REPL is not a reliable indicator for interpretive program execution. There are more and more systems that compile expressions entered at the REPL on the fly and execute them as native code, thereby combining the convenience of an interactive environment with the speed of compiled code.
| Previous Section | - Contents - Index - | Next Section |