Using the Interpreter


One of a series of tutorials about Scheme in general
and the Wraith Scheme interpreter in particular.

Copyright © 2011 Jay Reynolds Freeman, all rights reserved.
Personal Web Site: http://JayReynoldsFreeman.com
EMail: Jay_Reynolds_Freeman@mac.com.


A programming language interpreter, like Wraith Scheme, is a computer program that understands a particular programming language, and executes statements in it one at a time, as you provide them. You might think of it as a personal assistant that understands the language in question, and is willing to do what you tell it, as long as you speak to it in that language.

In order to use any particular programming language interpreter, you need to know the programming language it uses, and you also need to know how to use the interpreter program itself. If you start out not knowing either one, you can get very confused very fast. For the moment, though, let's forget about the Scheme programming language, and concentrate on how to use the Wraith Scheme program. I will have to provide some simple instances of Scheme statements as examples, but don't worry too much about what they mean or why they look the way they do -- that will come later. The goal here is to learn how to make Wraith Scheme do things.

The usual way to get to this tutorial is to open it from inside the Wraith Scheme program, so the fact that you are reading these words probably means that you have gotten Wraith Scheme running, but let's review: To start the program, you moused on its icon -- the one with the gray cat face -- and a window opened up that probably looked a lot like this:


Main Window

The first time you opened the window, it automatically played a short demonstration of Scheme. The demo had instructions for how to make it stop, and for how to change the Wraith Scheme preferences so that it never ran again. I hope you were able to follow those instructions and stop the demo when you were done with it, but if you need to deal with the demo program, scroll down to the end of this page to learn how, and then come back here.

The two panels -- Apple calls them "drawers" -- hanging off the left side and bottom of the Wraith Scheme window contain some items that are handy but not essential, so ignore them for the moment. (The drawer at the left has some useful buttons, and the drawer at the bottom displays quite a lot of information about what Wraith Scheme is doing.)

The place where you type statements for Wraith Scheme to act upon, and where you see what happened, is the central portion of the main window, without the drawers:



Specifically, you type in the short, wide yellow panel at the bottom of the main window -- the panel that is nearly the full width of the window but only one character high. I call that panel the Input Panel. When you have typed a line of text there, you get Wraith Scheme to process it by pressing the "return" key. Wraith Scheme will type back what you have given it in the much larger yellow panel that takes up most of the window, followed by whatever result it got from the processing. I call the larger panel the Main Display Panel.

At the risk of stating the obvious, let me mention that in order to provide typed input to Wraith Scheme, the Input Panel must be selected -- it must contain the blinking typing cursor. You generally select it by clicking on it with your mouse or trackpad. You cannot type into the Main Display Panel, and it is large compared to the Input Panel; therefore, when you are mousing on the Wraith Scheme window in order to move from one application to another, it is quite easy to select the Main Display Panel instead of the Input Panel, and then end up wondering why nothing happens when you type.

Let's try giving Wraith Scheme some simple input, like a number. In the image following, you have typed "42" in the Input Panel, but have not yet pressed "return". (You might want to try actually doing these things in Wraith Scheme as you read the tutorial, in order to get a little guided hands-on experience.)



In the next image, you have pressed "return", and Wraith Scheme has typed back your statement -- the number 42 -- followed by the result of evaluating that statement. Numbers evaluate to themselves -- there is no processing to be done -- so what gets put into the Main Display Panel is the number "42" repeated twice in a row.



The numbers appeared up near the top of the Main Display Panel simply because there was nothing else further down. Wraith Scheme always puts its output at the bottom end of what is already in the Main Display Panel, and scrolls that panel when necessary.

Things that evaluate to themselves aren't usually very interesting, so let's try something a bit more complicated. It turns out that the Scheme command to add two and two is "(+ 2 2)". Let's type that into the Input Panel and see what happens ...



There is the expression in the Input Panel. Now press "return" ...



Well, what do you know? Wraith Scheme can add two and two!

The expressions you provide to Wraith Scheme can use more than one line. Let's tell Wraith Scheme to add two and two by means of an input expression that is two lines long. The first line will be "(+ 2", and the second will be "2)".



Here is the first line, now let's press return ...



Wraith Scheme has echoed the first line, but has not printed any results of its own yet. That is because the part of the expression that it has seen so far is not finished: "(+ 2" is not a complete Scheme expression. In particular, Wraith Scheme is waiting for the right parenthesis, to tell it that the expression has ended. (If you have the left-hand drawer pulled out, you might notice that at its very bottom is a typed reminder that Wraith Scheme is expecting a right parenthesis.) Anyway, now type in the "2)" and press "return" ...



Wraith Scheme echoed the second half of the expression, then printed out the answer -- 4.

You may have noticed that the Input Panel has text-editing capability. You can move the cursor around and delete characters or add extra ones. So if you make a mistake in typing in the Input Panel, you can fix it. Yet suppose you make a mistake and do not notice it until you have pressed "return". Is there anything you can do about that?

If Wraith Scheme receives a complete Scheme expression when you press the "return" key, with balanced parentheses and all, then there is nothing you can do to fix your mistake. Wraith Scheme will do its best to process the incorrect expression, and will perhaps get the wrong answer or present some kind of error message. On the other hand, if the expression is not complete yet -- you have not yet provided the final right parenthesis or something -- there is a way to stop processing and start over.

Here is a silly example. In the next image, you have typed '(+ 2 "cat"' -- without the final right parenthesis -- and then pressed "return". That is a mistake, because Scheme has no way to add two to a cat. (Actually, the point is that Scheme has no way to add numbers to text strings, but I hope you get the idea.)



You can back out of this corner by using the "Discard Typing" item of the Interpreter Menu, or by using its command equivalent, command-D. The same command is also available by pressing the "Discard Typing" button in the drawer to the left of the Main Display Panel. Whichever of these you do, the result will be as shown in the next image.



Wraith Scheme has forgotten the incomplete partial expression, and has gone back to the beginning of its "top-level loop", in which it reads what you type, evaluates it, and prints the result, over and over. The mistake is gone for good, and Wraith Scheme is all set up to deal with whatever you type next.

If you are wondering what would have happened if you had gone ahead and typed the final parenthesis, here is what happens when you tell Wraith Scheme to add two to a cat:



Wraith Scheme is very pedantic about error messages, but it does try to give you a complete and clear description of what went wrong.

Incidentally, parentheses nest in Scheme. For example, an expression to calculate the hypotenuse of a right triangle with sides 3 and 4 is "(sqrt (+ (* 3 3) (* 4 4)))". The next image shows what happens when you type that into the Input Panel and then press return.



One more thing about the Main Display Panel: As I said above, you cannot type there -- no editing is allowed -- but if there is some text in the Main Display Panel that you would like to recover, you can select and copy it using the usual Apple commands. For instance, if you want to reuse the expression for the hypotenuse of the triangle again without bothering to retype it, you can mark it by dragging with the mouse, like this:



Now it is all set up to copy. There is an even easier way to recover previously typed expressions, though: Wraith Scheme has a command history mechanism. Try putting the typing cursor in the Input Panel, just as if you were going to type something, and then use the up- and down-arrow keys. You will find that you can recover all the previous lines of text that you have typed to Wraith Scheme, one at a time, for reuse or modification.

By now, even though you haven't learned very much Scheme, I hope you are beginning to understand how to sit down in front of the Wraith Scheme program and actually get something done.

We are now done with the main part of the tutorial. What follows is a short section on stopping the demo program.


Dealing With The Demo Program:

To make the demo program stop when it is running, either type control-option-command-delete -- that takes pressing "control", "option" and "command" all at once and then pressing "delete" while you have them all down -- or else pull down the Interpreter Menu, near the left end of the Wraith Scheme menu bar, and use the "Reset to Top-Level Loop" menu item. It may take from a few seconds to a few tens of seconds for the demo to stop, depending on what it is doing at the time.

To arrange that the demo never happens again, open the Wraith Scheme Preferences, either by typing command-, (command-comma) or by using the "Preferences..." item in the Wraith Scheme Menu. The Preferences Window will look rather like this:


General Preferences Window


See the line of text about a third of the way down the Preferences Window, that reads "Wraith Scheme.app/Contents/Resources/Demo.txt"? Edit that line so it is empty, like this:


Preferences Window, blank file field


Now press the "Accept These Preferences" button, at the lower right corner of the Preferences Window, and you are done: No more demos!


-- Jay Reynolds Freeman (Jay_Reynolds_Freeman@mac.com)


Wraith Face