Pragmattica

January 15, 2008

My First Scheme Program

Filed under: Scheme — bnsmith @ 4:15 am

Today, I am releasing my first (more or less) working Scheme program. I’m still only learning Scheme, so my work is not very sophisticated, but I hope this is just the beginning.

I named the program “Conversation Tree Writer.” It’s purpose is to allow someone to more easily write the “conversation trees” that are commonly used to express the plot in role-playing games (RPGs). Once the conversation trees have been written, the program then allows the user to step through the conversations and see if they work as intended.

If I understand correctly, my program implements a domain specific language (DSL) for expressing conversation trees. I implemented it with the ‘define-macro’ syntax, because it is a little easier for novices to pick up.

As you’ve probably guessed, the primary reason that I chose to write this program was as a learning exercise. However, I still hope that it might become useful to someone after a few more revisions. Someone working on a video game would probably find it very difficult to write these trees in a standard word processor. It would become something like a choose-your-own adventure book; adding a page would mean having to manually update all of the instructions for what page to turn to next.

Presumably, commercial game developers have some internal tools to ease this process, but I was not able to find any such open-source programs.

I’m currently distributing the source-code only, so you will need to install Gambit Scheme and SLIB to run it. See Getting Started with Scheme on Ubuntu Part 1 and Part 2 for instructions. After Scheme is set up, download the source code here, open a terminal, move to the directory where you extracted the source code and run this command:

./ctw.scm secure.ctw

You can then choose between the available options by entering a number.

November 25, 2007

Getting Started with Scheme on Ubuntu — Part 2

Filed under: Linux,Scheme — bnsmith @ 11:59 pm

Installing and Using SLIB

Scheme is a very minimalistic language. This is Scheme’s great strength, but this trait can also make the language seem a little bit barren. There is a collection of excellent libraries that can help allieviate this, though using them requires some set-up. First, follow the directions from Part 1, if you haven’t already. As with Gambit Scheme itself, the version of SLIB in the Ubuntu repositories is not the latest, so we will install from source.

Go to the SLIB website and download slib3a5.zip to your home directory. The download link is under the “Quick Start” heading.
pic0.png

Next, open a terminal (Applications -> Accessories -> Terminal) and enter the following commands:

unzip slib3a5.zip
cd slib
sudo make install

Then, edit the SLIB configuration file with the command:

sudo gedit /usr/local/lib/slib/gambit.init

Find the line:

  ((unix)    (lambda () "/usr/local/share/gambc/"))

pic12.png

and change it to:

  ((unix)    (lambda () "/usr/local/Gambit-C/current/"))

pic22.png

Now start the interactive interpreter with root permissions:

sudo gsi -:s /usr/local/lib/slib/gambit.init -

In the interpreter, enter the command:

(require 'new-catalog)

Exit the interpreter by pressing CTRL-D twice. Finally, copy and paste this command into your terminal:

echo "(include \"/usr/local/lib/slib/gambit.init\")" > ~/.gambcini

SLIB is now fully installed and can be used by the Gambit Scheme interpreter. To test the installation, run the interpreter by typing gsi and paste in the following code:

(require 'random)
(random 10)

This should display a random number from zero to nine. It is similarly possible to use SLIB with programs saved in files. Copy the following code into a text file named slibtest.scm:

(require 'printf)
(require 'srfi-1)
(define userlist '((45607 "Ted Randalph" 34.90 5)
                   (91238 "Rob Smith" 32.45 3)))
(for-each
  (lambda (userinfo)
    (printf "Employee %s (%d) has %d years experience and makes $%.2f per hour.\n"
      (second userinfo) (first userinfo) (fourth userinfo) (third userinfo))) userlist)

This program can be run by entering gsi slibtest.scm and should display the following output:
pic5.png
Check out the SLIB documentation for a complete list of what is available.

November 23, 2007

Getting Started with Scheme on Ubuntu — Part 1

Filed under: Linux,Scheme — bnsmith @ 3:44 am

Installing Gambit Scheme

As I described in my previous post, I am a fan of the Gambit implementation of Scheme. While following these instructions, try to keep in mind that the Scheme community is smaller than the communities of the more major languages like Python or Ruby, so there are still rough edges that get in the way. I hope that my experiences can speed things up for other fledgling Schemers.

It is possible to install Gambit Scheme from the Ubuntu repositories. However, the repositories contain an older version, so I recommend building Gambit Scheme from source instead. First, go to the Gambit Scheme website.

pic11.png

Click on the “Sources” link and save the gambc-v4_1_0.tgz file to your home directory. Next, open up a terminal (Applications -> Accessories -> Terminal), and enter these commands:

tar -xzvf gambc-v4_1_0.tgz
cd gambc-v4_1_0
./configure && make
make check
sudo make install
sudo make bootstrap
cd /usr/bin
sudo ln -s /usr/local/Gambit-C/current/bin/gsi gsi
cd ~

Once the installation is complete, conduct a few tests. From your terminal, enter the command:

gsi

You should then see Gambit’s interactive interpreter prompt. Enter the following code:

(define numlist '(2 10 6))
(map (lambda (x) (* x x)) numlist)

You should receive these results:

pic21.png

Exit the interpreter, by pressing CTRL-D twice. You can also use the Gambit Scheme interpreter to run Scheme programs stored in files. As an example, put the following text into a file named hello.scm:

(display "Hello World!\n")

Run the file with the command:

gsi hello.scm

That should be enough to start experimenting with Scheme. Part 2 will describe the installation and use of the SLIB libraries.

November 17, 2007

On the Importance of Programming Language Popularity

Filed under: Scheme — bnsmith @ 6:20 pm

I recently decided to learn Scheme. I’ve only just begun this journey, but already I’ve learned a great deal, some of which has nothing to do with Scheme programming.

My first step in learning Scheme was to choose an implementation. I knew that there were numerous Scheme implementations to choose from, as one would expect, given how easy it is to implement the language. This was, in fact, one of the things that drew me to the language in the first place. I was still just starting to learn Scheme, so I figured that it wouldn’t really matter which implementation I selected. As long as I was just playing around and not implementing real software, I could always switch later with relative ease.

So I installed MIT Scheme, fired up the interpreter and started typing in my first tentative lines of code:

pic1.png

It worked! I had successfully calculated that seven times two is fourteen! I was clearly well on my way to becoming an omnipotent programmer deity. Now, how about changing that first line to use the number 13 instead of 3? I tapped the up-arrow key on my keyboard and:

pic2.png

Well, apparently the MIT Scheme interpreter doesn’t support that. This discovery gave me pause. I really like learning with an interactive interpreter, and not having the ability to bring back the previous line would make things much more difficult. No worries though; there are plenty of other Scheme implementations out there. There has to be at least one that does what I want. Chicken Scheme seemed pretty popular. Trying the same code again:

pic3.png

And the up-arrow:

pic4.png

Chicken Scheme’s interpreter has the same problem. I also tried MZScheme, Scheme48 and Guile, with the exact same result. Five Scheme implementations, and they all suffer from this same issue.

And then it hit me. This is the sort of thing that causes newbies like me to give up and move on to something else. In fact, I think that I may have actually done just that after a previous attempt at learning Scheme, months ago.

Well, I didn’t give up this time, and I eventually found a Scheme implementation that supports my beloved arrow keys in its interpreter. And the winner is: Gambit Scheme. This excellent implementation seems to have been influenced by the “batteries included” philosophy that I love about Python. And given the state of the programming language world right now, I don’t think that “batteries included” is optional anymore. For a language to win users, and all of the benefits that a large community brings, at some point someone must create a polished implementation without the rough edges that I had to wrestle with. To some degree, this is a chicken and egg problem. On one hand, you must deal with all of the boring little details to grab users and build a big community. On the other hand, a big community will mean more contributors willing to tackle the boring details. I suppose that the polish of the implementation and the size of the community must move in lock-step. The only thing that anyone can do to speed this process is to adhere to the “batteries included” philosophy and encourage others to do the same.

If you’re thinking about learning Scheme, I highly recommend that you give Gambit a try. My next posting will explain how to install Gambit and set up the SLIB libraries on Ubuntu 7.10.

Blog at WordPress.com.