Spring 1996
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.
You can navigate among directories with the following commands:
Use the following commands to manipulate files:
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.
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.
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:
~/comp210/lab3.ss
by using
Open File on the Files menu. (You can specify an
existing file or a brand-new file.)double
function that takes a number
as its argument and returns a number twice as large.~/comp210/lab3a.ss
, and define
the length
function.lab3.ss
.lab3a.ss
from the Buffers menu. Now you
have two windows, each associated with a different buffer, and you
can view both files simultaneously.lab3a.ss
via C-x C-s. Note keyboard equivalents.
See below for decoding of "C-x"."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.
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.
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
.
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
. You can also
edit previous expressions: move to the (+ 1 2)
and insert another
1
, so the expression reads 1
. Hit return, and
that expression is copied to the end of the buffer and evaluated.
(+ 11 2)
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
command. Try this from your
load
lab3.ss.
buffer. In the Scheme Interaction buffer, you will
only see an extra ``>
'', but now the
function is defined. You can also re-evaluate singe definitions via the
Load definition menu item.
double
To make sure you understand how to post newsgroup messages, we will post a
test message to the rice.owlnews.test
group.
news:rice.owlnews.test
URL.When you're reading an article, there are three ways to respond to it:
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.
This information has been moved to the lab 4 handout to consolidate it.
is an extension of cond
which performs multiple tests
(instead of just one). You can use if
to avoid deep nesting
resulting from cascaded cond
s, or to logically group things that are
similar or belong together.
if
(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
expression, be sure that some test always
evaluates to true, or include an cond
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
else
can help you find
the problem in your program.
"This can't happen; got to else in function foo"
Only one of the cond consequents is ever evaluated; for instance, when we
supply
to 19
:
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
.
This is a continuation of ``Inserting Line Breaks'' from last week's tutorial.
list-of-nums?
would read:
(define list-of-nums? (lambda (l) (cond
list-of-nums?
example:
(define list-of-nums? (lambda (l) (cond [(null? l) #t]
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
, the line break is optional
because the question and the answer are so short.
cond
See the newsgroup posting regarding comments.
If there is time, we will make sure everyone can do this without difficulty.