Sunday, August 14, 2011

Learning Scheme R6RS Using the DrRacket IDE

“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:
(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
(+ 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

(library (ted-collection math)
         (export triple)
         (import (rnrs))

         (define (triple x) (* x x x)))

This piece of code
  1. defines a library named ted-collection math
  2. defines a function called triple in the library.  triple takes a parameter and returns the triple value of it as the result. 
  3. exports the function for other code outside the library to use.
Now save the code in a file named math.sls, and store the file under C:/programs/ted-collection. To save the content of the definition editing panel to a file, click the menu item File -> Save Definitions.

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

(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))).


Anonymous said...

The quotation at the beginning of this article should not be attributed to Paul Graham. Where it appears in his essay, he is quoting Eric Raymond, and in fact he proceeds to criticize this attitude.

Ted Gao said...

Thanks for pointing it out. The incorrect attribution has been corrected. Your comment, that "in fact he proceeds to criticize this attitude", may confuse people. Paul Graham certainly believe that "that experience will make you a better programmer for the rest of your days".

Anonymous said...

The function triple is not actually multiplying the input argument by three but returning a cube of it.

Priya Kannan said...

This blog is having the general information. Got a creative work and this is very different one.We have to develop our creativity mind.This blog helps for this. Thank you for this blog. This is very interesting and useful.
PHP Training in Chennai