Lab 3
Unix, Emacs, Chez, News, Bits, cond

Comp 210

Spring 1996

Contents

Unix file system

Unix stores files in a tree-structured hierarchical file system. A directory contains files and other directories; the directory structure may be arbitrarily deep. Your personal directory where your files (and your personal directories, which can contain more of your files) are kept is called /home/userid--for instance, mine is /home/mernst. The forward slash character separates directory names, so /home/mernst specifies that we can find my directory by starting at the root directory / (which is the top of the directory structure), then looking in directory home (which is a subdirectory of the root directory), then looking in directory mernst (which is a subdirectory of home). File and directory specifications such as /home/mernst are called pathnames.

Directory navigation

You can navigate among directories with the following commands:

cd
(change directory). With an argument, goes to the specified directory. For instance, cd /home makes future operations work in the /home directory. After that, cd mernst makes my personal directory the current directory again. cd without an argument goes to your home directory.
ls
(list contents of current directory). The contents are files and other directories. Files whose names start with a period (.) are not listed.
pwd
(print working directory). Shows which directory you are currently working in.
mkdir
(make directory). Create a new directory of the specified name. For instance, mkdir /home/mernst/comp210/lab03 creates a directory for the work done in this lab; from my home directory, I could also have said mkdir comp210/lab03, or from my personal comp210 directory, just mkdir lab03.

File manipulation

Use the following commands to manipulate files:

cp
(copy file). First give the name of the file to copy, then where you want to put it. You can give either a specific file name, as in cp original.ss xerox.ss (make a copy of original.ss and name the copy xerox.ss) or a directory name, as in cp /home/comp210/Hw/hw2/problem1.ss /home/mernst (in which case the name is the same as the original name, in the new directory). Change either file does not affect the other.
mv
(move or rename file). Again, first give the file name, then the place to move it.
rm
(remove file). Delete a file. Unix has no ``undelete'' mechanism, so be sure you want to delete a file before you issue the rm command.

Directory abbreviations

Remember that you can specify files (and directories) in the current directory by just giving their names. Specifying a file in a different directory takes more work. The most straightforward way is to just give the full pathname. There are a number of ways to save typing, however. You don't have to learn any of these to complete all your work; they are merely conveniences.

Your home directory /home/userid can be abbreviated as ~, so the file .emacs in your home directory can be specified~/.emacs. Sometimes when you do pwd, Owlnet specifies your home directory as something like /tmp_mnt/net/snowy/mernst instead of /home/mernst; you can ignore that and use the /home/ form instead.

Someone else's home directory can be abbreviated as ~ userid, so my personal .emacs file can be found in ~mernst/.emacs.

The abbreviation .. stands for the parent of the current directory. For instance, if I'm working in the /home/mernst/comp210, then .. stands for /home/mernst.

The abbreviation . stands for the current directory. For instance, the command cp otherdir/file . copies a file from elsewhere into the current directory.

Emacs

Start up Emacs by using the Comp 210 tools menu on the left mouse button or by typing gnuemacs & at your shell prompt. Emacs is an editor, but it is also a richly-featured program development environment and much, much more. Many people, including the lab coordinator for this course, do all their work from Emacs, including filesystem traversal, editing, reading news, mail, debugging, surfing the Web, etc.

Buffers and windows

Emacs can manage multiple buffers and multiple windows. Each buffer is associated with a file, process, or other information. Each window displays one buffer. Since there are generally more buffers than windows, usually some buffers aren't visible; all the windows are always visible. DrScheme is an imitation of Emacs, so DrScheme supports some Emacs keystrokes and features. (DrScheme also has its own features.)

Exercise:

"C-x" means control-x. Control is like the shift key; to type C-x, hold down control, type x, then let up on control. "M-x" means meta-x. Meta is another shift key; it's the diamond key next to the space bar.

Cut, copy, paste

You can cut, copy, and paste via the mouse or the keyboard. Since beginners find the mouse/menu commands easier, we will introduce those here.

You select text by moving the mouse to the beginning or end of the desired text, pressing the left mouse button, moving the mouse to the other end of the desired text, then letting up on the mouse button. (This is called ``dragging''.) Notice that the selected region is highlighted.

Now you can paste that text in any other x application, including Emacs, by clicking the middle mouse button.

To put the information in Emacs's clipboard (so that you can also use Paste on the Edit menu to retrieve it), just use Copy or Cut, which are also on the Edit menu. Cut deletes the text, while Copy leaves the text as is; they both also put the text in the Emacs clipboard.

Emacs uses the term ``kill'' for cut and ``yank'' for paste.

Other useful commands

Another useful command is Undo (on the Edit menu). To abort whatever you are in the middle of doing, type C-g. Typing ESC (the Escape key) three times will often gets you out of whatever you are doing.

Emacs is extensively documented; it is very easy to get help or to learn more about it. You can get help via the keystroke C-h (to learn more about what sorts of things you can type after the first C-h, type a second one: C-h C-h) or from the Help menu. The online tutorial, invoked via C-h t or Emacs tutorial on the Help menu, is particularly helpful.

You should have received an Emacs quick reference guide in class; you can also see ~comp210/Labs/emacs-19.30-refcard.ps.

Running Chez Scheme under Emacs

When you are editing a Scheme program (ending with the .ss extension), Emacs is in Scheme Editing mode. It knows how to indent properly, how to evaluate Scheme expressions, and more. The word ``Scheme'' in the inverse-video mode line just beneath the window (which also gives the name of the buffer, which is usually just a file name, and other information) indicates that this buffer is in Scheme Editing mode.

We can also run Scheme programs inside Emacs. Choose Run Scheme from the Scheme menu to start Chez Scheme. This creates a buffer that is associated not with a file, but with a program being run. This buffer is in Scheme Interaction mode, not Scheme Editing mode. Notice that the Scheme menu remains, but it has different functionality, because different things make sense when you are editing programs as opposed to when you are running them.

You can type expressions to Chez Scheme just as if you were running it in a shell. Try typing a simple expression like (+ 1 2). You can also edit previous expressions: move to the 1 and insert another 1, so the expression reads (+ 11 2). Hit return, and that expression is copied to the end of the buffer and evaluated.

DrScheme has the "evaluate" button for sending definitions from its editing window to its experimentation window. In Emacs, Save and Load Buffer does the same thing: it saves the buffer, then executes an appropriate Scheme load command. Try this from your lab3.ss. buffer. In the Scheme Interaction buffer, you will only see an extra ``> '', but now the double function is defined. You can also re-evaluate singe definitions via the Load definition menu item.

Posting to newsgroups

To make sure you understand how to post newsgroup messages, we will post a test message to the rice.owlnews.test group.

When you're reading an article, there are three ways to respond to it:

  1. to author only: click on the highlighted name/address after ``From:''.
  2. to whole newsgroup, for everyone to read: click on Post Reply.
  3. to whole newsgroup, with a courtesy copy mailed directly to the author of the original message: click on Mail and Post Reply.

Advice

Read for a while, don't post immediately.

Read news.announce.newusers before posting.

Quote judiciously; don't indiscriminately include the entire text of the original article.

Lists of bits

This information has been moved to the lab 4 handout to consolidate it.

cond

cond is an extension of if which performs multiple tests (instead of just one). You can use cond to avoid deep nesting resulting from cascaded ifs, or to logically group things that are similar or belong together.

(cond ((test1 exp1)
       (test2 exp2)
       (test3 exp3)
       ...
       (testn expn)
       (else expelse)))
is precisely identical to
(if test1 
    exp1
    (if test2
        exp2
        (if test3
            exp3
            ...
            (if testn
                expn
                expelse))))

When you write a cond expression, be sure that some test always evaluates to true, or include an else clause. (It's a good idea to always include and else clause, even when you know it can't be executed, because you may have made a mistake, and returning a value like "This can't happen; got to else in function foo" can help you find the problem in your program.

Only one of the cond consequents is ever evaluated; for instance, when we supply 19 to foo:

    (define foo
      (lambda (x)
        (cond ((prime? x) (+ 1 2))
              ((even? x)  (* 1 2))
              ((odd? x)   (/ 1 2)))))
an addition is performed, but no division is performed. We get back the value 3, not a combination of the values 3 and 1/2.

Formatting cond

This is a continuation of ``Inserting Line Breaks'' from last week's tutorial.

  1. In a conditional, the keyword cond should appear on a separate line. Hence, the first three lines in one definition of the function list-of-nums? would read:

    (define list-of-nums?
      (lambda (l)
        (cond
  2. In conditionals, you may use square brackets rather than ordinary parentheses to enclose each question-and-answer pair.gif DrScheme generally ensures that paired brackets match. This convention is illustrated in the fourth line in the list-of-nums? example:

    (define list-of-nums?
      (lambda (l)
        (cond
          [(null? l) #t]
  3. Insert a line break between the question and the answer of a question-and-answer pair in a conditional does. This rule is illustrated in the second question-and-answer pair in the list-of-nums? example:

    (define list-of-nums?
      (lambda (l)
        (cond
          [(null? l)
           #t]
          [(cons? l) 
           (and (number? (car l)) (list-of-nums? (cdr l)))])))

    For the first clause of the cond, the line break is optional because the question and the answer are so short.

Comments

See the newsgroup posting regarding comments.

Loading and running the elevator problem

If there is time, we will make sure everyone can do this without difficulty.

...pair.
Scheme accepts brackets and parentheses as two different forms of parentheses. In Scheme program text, each open parenthesis must be matched by a corresponding closed parenthesis of the same kind.