“LISP is worth learning for a different reason - the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot.” – Eric Raymond, How To Become A Hacker
Scheme is a major dialect of the LISP programming language. R6RS is the latest standard of Scheme. DrRacket is a free IDE for Scheme programming. (Racket is an implementation of Scheme, with many of its own dialects). This short tutorial is about using the DrRacket IDE to begin Scheme programming.
My motivation to learn Scheme was to study Structure and Interpretation of Computer Programs. I installed DrRacket and learned Scheme using the excellent and free book The Scheme Programming Language. After that I was able to run examples and to work out exercises from Structure and Interpretation of Computer Programs. It worked out quit well. And I do feel gaining significant insight about programming through those studies.
DrRacket installation is straightforward. Just download the installer and execute it.
DrRacket Workbench
There are two editing panels on the DrRacket workbench. The panel on the top is for function definitions. The panel on the bottom is the Read-Eval-Print Loop (Read-Eval-Print Loop REPL).
Choose A Dialect
DrRacket allows one to use many dialects of Scheme. (In DrRacket’s term, each dialect of Scheme is a “language”). In this tutorial, we are going to use R6RS Scheme.
To let DrRacket know that we are going to use R6RS Scheme, on the DrRacket workbench, select the menu item Language -> Choose Language … A dialog box with the title Choose Language will pop up. On the dialog box, select the radio button with label “use the language declared in the source”, and click the OK button. Then on the top of the upper editing panel, type:
DrRacket allows one to use many dialects of Scheme. (In DrRacket’s term, each dialect of Scheme is a “language”). In this tutorial, we are going to use R6RS Scheme.
To let DrRacket know that we are going to use R6RS Scheme, on the DrRacket workbench, select the menu item Language -> Choose Language … A dialog box with the title Choose Language will pop up. On the dialog box, select the radio button with label “use the language declared in the source”, and click the OK button. Then on the top of the upper editing panel, type:
#!r6rs
(import (rnrs base (6)))
Then click the Run button on the tool bar above the upper editing panel to let everything on the upper editing panel “take effect”. (You need to click the Run button once every time you changed the content of the upper editing panel).
Execute Program in the REPL
Now let's try our first and simplest Scheme program. On the REPL, type
Now let's try our first and simplest Scheme program. On the REPL, type
(+ 2 5)
After you hit the Return key, DrRacket evaluates the expression, prints 7 below the expression, and prints the prompt again for you to enter new expression.
Create a Library
Reusable code is organized into libraries. Now we are going to create a simple library.
On the definition editing panel, type the following code fragment
#!r6rs
(library (ted-collection math)
(export triple)
(import (rnrs))
(define (triple x) (* x x x)))
This piece of code
- defines a library named ted-collection math
- defines a function called triple in the library. triple takes a parameter and returns the triple value of it as the result.
- exports the function for other code outside the library to use.
Add Library Path to DrRacket
We have to add the path to the library to DrRacket to let it recognize the library. From the workbench’s menu bar, click the menu item Language -> Choose language … When the dialog box with the title Choose Language pops up, click the Show Details button. Now the dialog box will look like the snapshot below. The portion of the dialog to add collection paths is marked out in the snapshot. Add path C:/programs to the Collection Paths.
Use a Library
Now we are going to use the library that we just created. On the definition editing panel, type
#!r6rs
(import (rnrs base (6))
(ted-collection math))
And hit the Run button.
On the REPL, type
(triple 9)
After hit the Return button, the REPL will show the result, 27.
The import expression imports two libraries. One is the library that we just created, another is the library that implements R6RS base features in DrRacket (as a Racket module). When you explore more features of R6RS Scheme, you may need to import more such libraries that implemented more advanced R6RS Scheme features in DrRacket. You can find all those Racket modules that implement R6RS in DrRacket under $Racket-Installation-Home/collects (substitute $Racket-Installation-Home by the actual directory where you installed Racket). (import (rnrs base (6))) imports the module stored in file rnrs/base-6.rkt). To import rnrs/io/ports-6.rkt, use (import (rnrs io ports (6))).