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:
#!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
(+ 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
  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

#!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))).

25 comments:

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

Freddie King said...

Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
mc donalds gutscheine | startlr | salud limpia

Anonymous said...

Its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

MULESOFT TRAINING

gowsalya said...

Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks
Digital Marketing Training in Chennai

Digital Marketing Training in Bangalore

digital marketing training in tambaram

digital marketing training in annanagar

Unknown said...

I am really happy with your blog because your article is very unique and powerful for new reader.
Click here:
Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune

sai said...

Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in
Python training in usa
Python training in marathahalli
AWS Training in chennai

AWS Training in bangalore

ragul ragul said...

Well researched article and I appreciate this. The blog is subscribed and will see new topics soon.

Blueprism training in btm

Blueprism online training

shalinipriya said...

Read all the information that i've given in above article. It'll give u the whole idea about it.

Data Science training in kalyan nagar
Data Science training in OMR
selenium training in chennai
Data Science training in chennai
Data science training in velachery

pavithra dass said...

Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straightforward and to the point.
AWS Solution Architect
AWS Certified Solutions Architect
Best AWS Training in Bangalore
AWS Training in Thirumangalam
AWS Training in T nagar

hansika hans said...

Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
java training in chennai | java training in bangalore


java training in tambaram | java training in velachery

Anonymous said...

Wonderful article, very useful and well explanation. Your post is extremely incredible. I will refer this to my candidates...

angularjs Training in chennai
angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur

angularjs-Training in velachery

sumathi s said...

I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
occupational health and safety course in chennai

Swetha Gauri said...

Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.
industrial safety course in chennai

Unknown said...

myTectra a global learning solutions company helps transform people and organization to gain real, lasting benefits.Join Today.Ready to Unlock your Learning Potential ! Read More....

amsa leka said...

Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays. Well written article. Thank You Sharing with Us . please follow our android article.android best practices Github
learn android programming step by step

Dwarakesh babu said...


Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.



AWS Training in Bangalore | Amazon Web Services Training in Bangalore


AWS Training in Bangalore |Best AWS Training Institute in BTM ,Marathahalli


AWS Training in Rajaji Nagar | Amazon Web Services Training in Rajaji Nagar

AWS Training in Chennai |Best Amazon Web Services Training in Chennai

Sumaya Manzoor said...

I am a regular reader of your blog, Amazing content with proper examples. Thank you admin

Selenium Training in Chennai
selenium Classes in chennai
iOS Training in Chennai
Digital Marketing Training in Chennai
.Net coaching centre in chennai
android app development course in chennai
Android Training in Chennai

amsa leka said...

Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing.
Well written article.Thank You Sharing with Us Best angular training institute in chennai

pavithra dass said...

I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
Aws Certification in Chennai
Amazon Web Services Training in Chennai
AWS Training centers in Chennai
Android Training Chennai
Android Courses in Chennai
Android Mobile apps Development Training in Chennai

sathyaramesh said...

Good job! Fruitful article. I like this very much. It is very useful for my research. It shows your interest in this topic very well. I hope you will post some more information about the software. Please keep sharing!!
SEO Training in Chennai
SEO Training
SEO Course in Chennai
Digital Marketing Course in Chennai
Digital Marketing Course
Digital Marketing Training in Chennai

361online said...

Very useful post. The information stated in the article is very practical. The writing style is also easily understandable.

big data training online

big data analytics training online

pavithra dass said...

thanks for sharing such a nice info.I hope you will share more information like this. please keep on sharing!
Core Java Training in Chennai
Java Training center in Chennai
Java Certification course in Chennai
German Training in Chennai
german classes chennai
german teaching institutes in chennai