1 Communicating with the Scheme interpreter
(7 activities)
1.1 Start "conversing" with the Scheme interpreter.(3 steps)
1.1.1 (Display page) Here is some information about the Scheme interpreter
You will be using a programming language named Scheme for all your work in this course. The Scheme "interpreter" is a program that you will engage in "conversations". In this activity, you will experiment with typing things to the Scheme interpreter and observing how it responds. Some responses will seem reasonable. Others will be error messages that indicate that Scheme doesn't understand what you typed. Others will seem like nonsense. The easiest way to access scheme is by running the stk program from a unix terminal. From within unix, type stk
h30 [1] ~ > stk
Welcome to the STk interpreter version 4.0.1-ucb1.16 [SunOS-5.9-sun4]
Copyright (c) 1993-1999 Erick Gallesio - I3S - CNRS / ESSI 
Modifications by UCB EECS Instructional Support Group
Questions, comments, or bug reports to .
STk> 
At the STk> prompt, you can now type something to the scheme interpreter. The second way to run the scheme interpreter is to do so from within the emacs editor. We will explore this in more detail later, but when you are editing a scheme file (a file with the .scm extension), emacs will present a "Scheme" menu with menu items that can access stk and send it commands.
1.1.2 (Display page) Start *conversing* with Scheme.
With the person sitting next to you, experiment with the Scheme interpreter by typing words and numbers to it, one per line. Do this until together you have collected the following responses:
unbound variable
bad syntax
#[closure arglist=
as well as the response that simply echoes what you typed in.
1.1.3 (Display page) Scheme *evaluates* your input.
The process used by the Scheme interpreter to "understand" what you type is called evaluation. Scheme evaluates what you type, prints the resulting value, and waits for you to type some more. Scheme understands more than just numbers and a few symbols. The symbols to which Scheme responded with "#[closure arglist=args 1dec90]" or "#[subr and]" are names of procedures that you may use to produce more complicated results. We will study some of those now, in particular the procedures that correspond to elementary arithmetic operations.
1.2 Have some complicated "conversations".(7 steps)
1.2.1 (Display page) Start using builtin Scheme procedures.
The Scheme interpreter knows about arithmetic operators, including + (plus), – (minus), and * (times). However, the way it wants you to specify an addition or subtraction differs from how we do it in English. In Scheme, you type a left parenthesis, then an arithmetic operator, then the operands, then a right parenthesis. Here are some examples.
EnglishScheme
3 + 5(+ 3 5)
3 – 5(– 3 5)
Adding or multiplying more than two numbers can be done in a sort of shorthand:
EnglishScheme
3 + 5 + 4(+ 3 5 4)
3 * 5 * 2(* 3 5 2)
The operands we supply to * or + are called inputs or arguments. The parenthesized uses of + and * are called expressions. For example, suppose you type the expression (+ 3 5 4). This is a request to evaluate the expression by adding the three inputs 3, 5, and 4. The value, or result of evaluation, is 12.
1.2.2 (Display page) Experiment with expressions.
Continue experimenting with the Scheme interpreter. Note in particular that when you type a right parenthesis, the interpreter highlights the corresponding left parenthesis. This helps you detect missing parentheses. Find out three more things.
  • What happens when you type more than one expression on a line?
  • In English, you can write 3+5, without blanks. How do you think the Scheme interpreter understands (+3 5) (with no space after the +)?
  • What results from providing no inputs to +? Is this reasonable behavior?
Explain your answers to the person sitting next to you.
1.2.3 (Display page) Use arithmetic operators in combination.
Type the Scheme equivalent of each of the following arithmetic expressions into the interpreter. If you get any of them wrong, add an entry to your Extra Brain explaining why.
    3 + (7-2)
    3 + 4 * 5
    (3 + 4) * 5
1.2.4 (Display page) Evaluation is inside-out.
Evaluation in Scheme is inside-out. That is, it looks for an innermost parenthesized expression, and evaluates that, then repeats the process. For example, evaluation of the expression
    (+ (* 5 2) (- 7 4))
might proceed by evaluating (* 5 2) and getting 10, then evaluating (- 7 4) and getting 3, then evaluating (+ 10 3) to get 13.
1.2.5 (Evidence) Here's a visual way to view procedures.
The Simply Scheme textbook describes a visual way of viewing Scheme procedures. Click here to get a detailed explanation of this representation.
1.2.6 (Brainstorm) Why no parentheses in some arithmetic expressions?
Parentheses weren't needed for the arithmetic expression 3 + 4 * 5, but they were needed for the equivalent Scheme expression. Explain why parentheses aren't needed for 3 + 4 * 5.
1.2.7 (Brainstorm) Explain how Scheme subtraction works.
Figure out what Scheme evaluates a subtraction with more than two inputs, for example, (- 7 2 3 8).
1.3 Make your own procedures!(10 steps)
1.3.1 (Display page) Here's how to define a Scheme procedure.
Operators like + and * are names of Scheme procedures. Thus you've just seen how to use a Scheme procedure: you form a parenthesized expression in which the first thing after the left parenthesis is the name of the procedure and the rest of the things are the operands—the inputs or arguments—that the procedure will use to produce its answer. (Using a procedure in this way is called calling or invoking it.) Scheme has around 100 built-in procedures. They form a general-purpose set of building blocks. Programmers use these built-in procedures to build their own procedures, in effect extending the language. These new procedures can then be used to define even more complex and sophisticated procedures. Using building blocks in this way is a good way to solve difficult problems. Here's an example of how to define a Scheme procedure.
(define (square x)
  (* x x) )
The definition has four parts:
  • the word define, which tells the Scheme interpreter you are defining something;
  • the name of the procedure you're defining, square in this case;
  • the names of placeholders for information that the procedure will use to produce its result;
  • the body of the procedure, the Scheme expression that will be evaluated to produce the result.
There should be one placeholder name for each argument that's expected when the procedure is invoked. The square procedure needs only one piece of information, the number to be squared. Thus it has only one placeholder, which we arbitrarily name x. The procedure name and the placeholder names all go within a second set of parentheses. To square a number, we multiply it by itself. Thus the body of the square procedure represents the result of multiplying whatever the number to be squared is, by itself.
1.3.2 (Display page) Experiment with the square procedure.
First type square to the Scheme interpreter to verify that the name "square" is currently undefined. Type the definition of square into the Scheme interpreter, and type square to see what has changed. Then, in a single expression, use it to find the square of the square of 49 (namely, 5764801). Then provide erroneous calls to square that produce the following error messages.
too many arguments to ...
too few arguments to ...
not a number: ...
1.3.3 (Display page) Put the interesting errors in your "Extra Brain".
Put a note in your "Extra Brain" about the errors that are most likely for you to make and the error messages that result.
1.3.4 (Display page) What are placeholders for?
Placeholders, called parameters in the textbook, provide a mechanism for making procedures more general. For example, one might have a lot of special-purpose squaring procedures:
(define (square-of-4) (* 4 4))
(define (square-of-7) (* 7 7))
(define (square-of-92) (* 92 92))
But these procedures are all doing essentially the same thing. We capture that similarity by representing the differences with a variable, the placeholder name.
1.3.5 (Display page) How are procedure calls evaluated?
Earlier, we discussed rules for evaluating Scheme expressions involving + and *. Here is how evaluation of a call to a user-defined procedure works.
  1. The first word after the left parenthesis is looked up among the name of procedures that are either built-in or that have been defined by the user.
  2. The arguments are counted to make sure they match the number of placeholder names.
  3. The arguments are evaluated. (This has the effect of the "inside-out" evaluation we did with expressions involving + and *.)
  4. The argument values are substituted for the corresponding placeholder names throughout the body of the procedure.
  5. The body expression is evaluated, and the result is the value of the procedure call.
Here's how to evaluate the expression
    (square (+ 2 1))
  1. The Scheme interpreter notes that square has been defined to take 1 argument, and 1 argument has been provided in the call.
  2. The argument expression (+ 2 1) is evaluated, producing a value of 3.
  3. Since the placeholder for square is named x, 3 is substituted for every occurrence of x in the body of square, (* x x), giving (* 3 3).
  4. (* 3 3) is evaluated, giving 9.
1.3.6 (Evidence) Diagrams can represent procedures you define.
We talked about a visual representation of procedures earlier. It works for procedures that you write as well as the builtin ones. What would the machine be for this Scheme code?
(define (dist x1 y1 x2 y2)
  (sqrt (+ (square (- x1 x2)) (square (- y1 y2)))))
Before clicking here , sketch just the
(sqrt (+ (square (- x1 x2)) (square (- y1 y2)))) part.
1.3.7 (Display page) Here's how tasks for writing procedures will look.
Many of the tasks we'll be asking you to do in this course will involve writing procedures. Here's an example problem statement.
  • Write a procedure named sales-tax that, given the amount of a taxable sale, returns the sales tax applied to that sale. (The Alameda County sales tax rate is 8.25%.) For example, the call
    
        (sales-tax 100.00)
    
    should return 8.25.
Here is one way to solve this problem.
  1. From "write a procedure named sales-tax ...", we get the following framework.
        (define (sales-tax ...)
          ... )
    
    
  2. From "given the amount of a taxable sale" and the example, we conclude that sales-tax will take one argument and that we will need one placeholder. We choose to name it "amount".
        (define (sales-tax amount)
          ... )
    
  3. We know that to get the amount of sales tax, we multiply the tax rate by the amount. That gives us the body of the procedure:
        (define (sales-tax amount)
          (* .0825 amount) )
    
    
1.3.8 (Display page) Now you do it.
Write a procedure named discount-price. This procedure is given two arguments:
  1. the price of an item;
  2. a discount rate (a value between 0 and 1.0) to be applied to that item. For example, a discount rate of .25 means that the selling price of the item will be three-fourths of its regular price.
Here are some sample calls to discount-price and their intended results.
expression                        value
(discount-price 1000.0 .25)       750.0
(discount-price   10.0 .40)         6.0
Type your procedure into the Scheme interpreter, then test it by evaluating the two expressions above.
1.3.9 (Display page) Use the Editor.
When preparing a presentation, one organizes it ahead of time rather than just giving it "off the cuff". Similarly, when building a program, we generally don't type the procedures directly into the Scheme interpreter. We use a "scratch pad" called an editor, save the program we type there into a file, and then load it into the Scheme interpreter all at once. An editor is much like Microsoft Word, for example, except that it has features that make writing Scheme code easy. We will use the "emacs" editor in this class. Emacs is very full featured, but you only need to use a few. It has menus at the top like many other editors. We will go into more detail about the emacs editor later today, but you use it a little bit now. (You might skip ahead to the Emacs and Unix Tutorial below). You start the editor by typing emacs& at the unix prompt:
h30 [1] ~/ > emacs&
[1] 22588
h30 [3] ~/ >
Note that you need to type this at the unix prompt, not the STk prompt! You can get another terminal, with a unix prompt, at any time via the right mouse button. When starting emacs in this way, you will first need to open a file or start a new one. Do this via the "File" menu, "Open File", and typing the file name (which will appear in the area at the bottom of the emacs window). Start the new file "discount-price.scm" by going to File, Open File, and typing in "discount-price.scm" at the bottom of your Emacs window. Type your definition of the discount-price procedure into the editor and save the file. You can run STk from inside Emacs by holding the diamond key (by the space bar) and pressing the 's' key. This splits your Emacs window into two parts. The top part is where you edit your file, and the bottom part is where you run STk. You can copy and paste code from the top part to the STk below. But Ctrl+C and Ctrl+V don't work in emacs so you will need to use the edit menu or learn the emacs short-cuts. You can also highlight the code in the top and use the Scheme menu to "Send region" Once you do this, evaluate a call to discount-price to make sure the procedure was loaded successfully.
1.3.10 (Display page) What is Scheme waiting for?
Summary:Explanation of why "send region" seems to not work if the parenthesis are incorrect.
1.4 Think about possible errors.(3 steps)
1.4.1 (Brainstorm) What change causes an "unbound variable: x" message?
Recall the square procedure:
(define (square x)
  (* x x) )
One of your classmates changes a single symbol in the procedure so that, when the expression (square 4) is evaluated, the error message
Error: unbound variable: x

is produced. What was the symbol that was changed, and how did you figure out what it was?
1.4.2 (Brainstorm) What does this procedure do?
A student enters the following definition into the Scheme interpreter:
(define (f x)
  (* x 3)
  (+ x 10) )
Experiment with this procedure using the interpreter, then explain what it returns for a given x.
1.4.3 (Brainstorm) What might the procedure's author expect it to do?
The author of the procedure just described (it also appears below) may have been confused about how Scheme evaluation works. What might this person have expected it to do, and why?
    (define (f x)
      (* x 3)
      (+ x 10) )
1.5 Solve some problems that use more than one procedure.(3 steps)
1.5.1 (Display page) Find the total selling price of an item.
Copy the sales-tax and discount-price procedures into the Scheme editor. Then use them to help define a procedure named selling-price, which when given the undiscounted price of an item and a discount rate (a number between 0 and 1.0), returns the total selling price of the discounted item, that is, discounted price plus 8.25% sales tax. Save the three procedures into a file named "sales.scm", load them into the Scheme interpreter, and then test them.
1.5.2 (Brainstorm) Figure out what a procedure does.
With the person sitting next to you, experiment with the mystery procedure defined below and come up with a better name for it.
(define (mystery x)
  (square (+ 1 (truncate (sqrt (- x 1))))) )
The sqrt procedure returns the square root of its argument (the value that, when squared, produces the argument). The truncate procedure returns the result of subtracting any fractional part from its argument; thus the value of
(truncate 4.79)
is 4.0.
1.5.3 (Display page) Find the day of the year for a given date in the French Revolutionary calendar.
The French Revolutionary calendar, in use in France between 1793 and 1806, divided the year into 12 months of 30 days each, followed by 5 monthless days. Write a procedure named day-of-year that, given a month number and a date in that month in the French Revolutionary calendar, returns the day of the year. The month number argument will be a number between 1 and 12, inclusive; the date in that month will be a number between 1 and 30, inclusive. Here are some examples of how day-of-year should work.
expression           value
(day-of-year 1 1)       1
(day-of-year 1 30)     30
(day-of-year 2 1)      31
(day-of-year 12 30)   360
1.6 Review new vocabulary.(1 step)
1.6.1 (Display page) Make sure you understand terms we've used today.
The following terms were discussed today:
  • argument
  • body
  • expression
  • evaluation
  • input
  • placeholder
  • procedure
  • result
Review these terms with a person sitting next to you, and make sure you both understand them. Provide each other with examples. Put an entry in your Extra Brain for each of the definitions that you expect to have a hard time remembering.
1.7 Homework(3 steps)
1.7.1 (Display page) Reading in Simply Scheme
The majority of time, you will have homework for each lab session. Why should the first session be any different? Generally, homework will come at the end of the day's activities, and may have several parts. This one, for instance, has three parts. You are, of course, encouraged to work on your homework within lab!
  1. Read chapters 3 and 4 in Simply Scheme, which reviews the material from today. Also read chapter 5, which covers material for next session.
    Note that reading assignments are shown at the bottom of the calendar, in the course portal, in red.
  2. The page (step) after this one contains several exercises. Do them.
  3. The third step contains a new tool called a "Discussion Forum". You can create discussion threads here; that is, you can start new topics, or reply to existing comments. Contribute a post and comment on two posts from other students.
1.7.2 (Display page) Exercises

Exercises

  1. Write a procedure named fifteen that takes no arguments, and returns 15.
  2. Write a procedure named identity that, given any argument, returns the argument.
  3. Do three of the five parts to exercise 4.4 in Simply Scheme (be sure you explain what was wrong with the original code), and test your procedures. The text of exercise 4.4 is available here.

How to submit

You will need both the editor and a unix (xterm) window. Use the editor to create a file named hwk1.scm, and put it in a directory (folder) named "hwk1". Here's how you do this:
  1. To make the hwk1 folder, type mkdir hwk1 in the unix window and press return.
  2. To get into the directory, type cd hwk1 in the unix window and press return.
  3. To make the file, use emacs like normal.
Then put the definitions of the homework procedures into this file:
  • fifteen
  • identity

  • (three of the following five):
  • sphere-volume
  • next
  • square
  • triangle-area
  • sum-of-squares
After the definitions of fifteen and identity, include test call(s). After the other three definitions, include an explanation of why the original version was wrong and give the tests that you used to make sure your new procedures worked. Remember, you can copy from the stk text and paste into your editor. Save the file. Print the definitions of all these procedures (you can print from emacs via the "File" menu). Hand the printed definitions to your instructor at the start of the next lab.
1.7.3 (Discussion Forum) What's the best name for the mystery procedure?
You chose a better name for the mystery procedure in lab, then you reviewed the choices that your classmates made. What's the best name you saw? In a post, defend your choice; then in responses, (politely) criticize two of your classmates' choices.
2 Emacs and Unix Tutorial
(4 activities)
2.1 Working with Unix(5 steps)
2.1.1 (Display page) The Basics
If you want to use a computer, you need software that lets you talk to your machine. Most of you use Windows or MacOS for this. However, in CS 3, we use something called Unix. Some parts of Unix might seem familiar to you. You can open windows, run a web browser, and use the mouse in pretty much the same way you would with Windows or MacOS. However, you will also need to use the keyboard with Unix. You type commands into Unix in special windows called xterm windows. One should be on your screen right now. If the top part of the window is white, that means it is active. You can type into it. If the top part is blue, it is not active and you can't type into it. About the only way to make a window active is by clicking on the top part. If you close your xterm window or need another one, right-click on the background. A menu will pop up, and you can select "xterm."
We haven't mentioned this, but you can easily quit from the stk program by typing (exit) at the stk prompt. This will bring you back to a unix prompt.
Your reader contains a simple one-page cheat sheet on Unix that you may find useful. You will also find the page under the "Resources" menu in the course portal.
2.1.2 (Display page) Looking at directories
Files in Unix are organized a lot like they are on other computers. You keep them inside folders. However, you can't really see you folders on Unix. You can type "ls" to get a list of everything in your current folder.
h30 [3] ~ > ls

discount-price.scm   sales.scm            ucwise@
h30 [4] ~ >
You start with one folder, and it is called "ucwise." (You made the two scheme files (and possibly more) in earlier steps) The "ucwise" folder is where you will keep all of your files. Type "cd ucwise" to go into this folder. (Folders are also called "directories," so "cd" means "change directory.")
h30 [4] ~ > cd ucwise
h30 [5] ~/ucwise >
So now you're in your ucwise folder. You will need to keep each homework assignment in its own folder. Type "mkdir hw1" to make a folder called "hw1."
h30 [12] ~/ucwise > mkdir hw1

h30 [13] ~/ucwise > ls
hw1/
h30 [14] ~/ucwise >
Now you can see your hw1 folder. Now let's move into that folder.
h30 [14] ~/ucwise > cd hw1
h30 [15] ~/ucwise/hw1 > ls
h30 [16] ~/ucwise/hw1 >
Okay, there are no files inside hw1. How do we get back out? Well, you can type "cd .." to go up one folder.
h30 [16] ~/ucwise/hw1 > cd ..
h30 [17] ~/ucwise >
Now you are back to your good old ucwise folder.
2.1.3 (Display page) Moving files around
You can also do things like copy files and folders, move them around, rename them, and delete them. You'll probably want to wait until after you have some files, but here's how to do each of those things:
to do thisyou typeexample
copy a file or foldercp [from here] [to here] cp hw1answers.scm hw1 copies the file "hw1answers.scm" to the hw1 folder.
move a file or foldermv [from here] [to here] mv hw1answers.scm hw1 moves the file "hw1answers.scm" to the hw1 folder.
rename a file or folder mv [old name] [new name] mv hw2a hw2answers.scm renames the file "hw2a" to "hw2answers.scm"
delete a file rm [filename] rm hw2answers.scm erases the file "hw2answers.scm"
Here's another little trick. The ".." that lets you move up one folder will let you do a lot more. Let's say you are in your hw1 directory and you want to see what's in your ucwise folder (the one above hw1).
h30 [22] ~/ucwise/hw1 > ls ..
example       hw1/ 
h30 [23] ~/ucwise/hw1 >
This shows you everything in your ucwise folder. In this case, we also have a file called "example." What if you want to move this file into your hw1 folder? Well, ".." means "one folder up," and "." means "this folder."
h30 [23] ~/ucwise/hw1 > mv ../example .

h30 [24] ~/ucwise/hw1 > ls
example
h30 [25] ~/ucwise/hw1 > ls ..
hw1/        
h30 [26] ~/ucwise/hw1 >
What, exactly, does "mv ../example ." mean? You know what mv means. The .. means "go up one folder." "../example" means "the file called 'example' in the next folder up." The . means "the folder I'm in right now." Thus, "mv ../example ." means, "Take the file called 'example' in the next folder up and put it in this folder." Now let's go back to your ucwise folder. How do we get the example file out of the hw1 folder?
h30 [26] ~/ucwise/hw1 > cd ..
h30 [27] ~/ucwise > mv hw1/example .
h30 [28] ~/ucwise > ls

example      hw1/    
h30 [29] ~/ucwise >
In this case, "mv hw1/example ." means, "Take the file called 'example' in the hw1 folder and move it to this folder."
2.1.4 (Display page) Move those scheme files!
With your new found skills, move the two Scheme files that you made earlier today into your "ucwise" directory. Right now, they are in your home directory (the one containing ucwise).
2.1.5 (Display page) Running other programs
There are three major programs you'll want to run in this class. The first is called Mozilla. Mozilla is a web browser that is a lot like Netscape, but much, much better. You'll use Mozilla for every lab, since most of the lab materials are on the web. The second is called STk. STk is the Scheme interpreter we use. You type in Scheme code and it runs your program. You'll use it a lot, too. The third is called Emacs. Emacs is a text editor. It's kind of like Notepad or Simpletext or Word, but it's better for programming. For one thing, it color-codes your programs so you can see different parts easily. It also lets you write a Scheme program, edit it, save it, and then run it in STk. You can run one of these programs by typing the right name into xterm. Make sure you type the names in all lower-case letters. However, when you run a program in Unix, it locks your xterm window. If you type "mozilla," you will get a Mozilla window that will let you browse the web, but you can't use your xterm window anymore. There is an easy way around this. When you run Mozilla or Emacs, leave a space after the name and type "&" before you hit return.
h30 [16] ~/ucwise/> mozilla &
h30 [17] ~/ucwise > 
You can still use your xterm window. Important: STk does not open its own window. If you run STk on its own, it will use your xterm window. This is just how STk works. This means you can't put an "&" after STk. If you do, bad things will happen. Fortunately, you'll almost always run STk from inside of Emacs. You can probably figure out how to use Mozilla, and we'll show you how to use STk throughout the whole semester. The program you really need to worry about is Emacs. We'll spend the rest of this tutorial talking about it.
2.2 Starting Emacs(2 steps)
2.2.1 (Display page) What is Emacs?

Welcome to the world of Emacs!

You have already seen a little bit of emacs, and used it to write some scheme files. Emacs is actually one of the most common programs used by computer scientists to write programs, books, and even e-mail. Emacs isn't the friendliest of text editors, but it is quite powerful. This tutorial will take you through the basics of emacs.
2.2.2 (Display page) Entering Emacs from the Unix prompt

Let's get cracking!

Now that you have a gist of what emacs is about, we can go ahead and try to access it from our Unix prompt.

Before you can access emacs you will need to have an xterm window open on your screen. An xterm window is a window that has a prompt that looks something like:



Do note that this is an image of an xterm window via an SSH client, as many of the images are in the remainder of this tutorial, which will not look exactly like the xterm windows you will see on the computers you use in the lab. Nonetheless, the contents should look about the same. If you do not have an xterm window open you may open one up by either of the two methods

  1. right-clicking on your desktop and on the menu that appears and select 'xterm'
  2. through an xterm that is already open, type xterm& at the prompt.
Now at your command prompt, to open up Emacs type the command emacs&:

2.3 Emacs basics(5 steps)
2.3.1 (Display page) Take a look around

Take a look closer...

When you have successfully entered Emacs you will see a new window on your screen that looks like this:



Please note the menu bar that is circled in red--this will be helpful to you later.

The large space below this is called the buffer, where you will be doing all your text editing.

As you can see, some information already in the buffer tells you that Emacs does have its own tutorial provided for users. However, this tutorial should be sufficient to teach you what is necessary to navigate comfortably through the editor. If you would like a more in-depth understanding of Emacs' commands or more advanced commands, you may access their tutorial at your convenience. Now take note of the line at the bottom of your Emacs window--this is called the minibuffer which we'll talk about later in the tutorial. The line immediately above this will also be important to you later:
2.3.2 (Display page) Emacs has a lot of menu options

Easy Sch-measy!

Do you remember the menu bar at the top of your Emacs window? You can use your mouse and click on each of the different options, just like how you would in the Windows operating system; for example, if you look for the Tools option



and click on it, a drop-down menu will appear and display all the different commands available for your selection. By simply selecting the command with your mouse you can execute commands this way through Emacs. Emacs has a lot of menus, and they all have a lot of options. Fortunately, you don't need to know them all. Really, you'll only need to use the File, Edit, and Scheme menus. File menu
You can open, close, and save files (Emacs calls them "buffers"), as well as exit Emacs. Opening files doesn't work quite like it does in Word. We'll cover this in a minute. Edit menu
You can copy, paste, cut, undo, and search. Scheme menu
You won't see this menu until you have a Scheme file open. We'll do that in a few steps. Basically, this menu lets you send Scheme code you have written with Emacs to be interpreted by STk. This is really handy.
2.3.3 (Display page) Let's try creating a file

Create a new file

Now we have a lot of tools under our belt for use, let's try doing something constructive with them. As text editors are used primarily for saving, modifying and creating text, we can now create a file that we can access later. Within your Emacs window, go to the File menu and select "Open File." Now look at the bottom of the Emacs window. It should look like

This little are is called the minibuffer, and what it shows is the path, or the location, in which Emacs wants to start looking for the file. If you are in your ucwise directory, the minibuffer should say "Find file: ~/ucwise/" instead of what it says in the picture. That means Emacs is looking in the ucwise directory. If you save a file, it will go there. If your minibuffer just says "Find file: ~/" you are not in ucwise. Be sure to type "ucwise/" before you type a file name, assuming this file is for your lab. If you type in a file name that already exists, Emacs will open it. However, since there aren't any files in your ucwise folder, Emacs will create a new one and name it whatever you want. Let's create a file called favethings.txt in your ucwise directory. Your minibuffer should look like "Find file: ~/favethings.txt." We don't need to put it in the ucwise folder, since this is just an experiment. Upon hitting enter, look at the line immediately above the minibuffer line. By careful inspection, each one tells you the type of file you are editing, the name of the file you are editing, and where the file is located (or, its path.) In our case it will look like



You will notice that the type of file you are editing is a text file, but if you were to modify a Scheme file it will say Scheme instead. The minibuffer line, upon trying to find a file that does not already exist, will display a message specifying that you are creating a new file. Basically the minibuffer and the area right above it show you any editor commands that you may enter in, the result of executing a command (in this case, the result of trying to find and open a file), or any editor information. Once you have identified that all of this correctly is in your window, you may go to the empty space of the window and start typing. For this exercise, answer the following questions:

  1. What is your favorite color?
  2. What is your favorite ice cream flavor?
  3. Do you like going to bed late at night/early morning or earlier at night?
2.3.4 (Display page) Gone in the blink of an eye...

Saving is everything

One thing that all computer science students will learn inevitably at one time or another in their Berkeley career is to always save their files. Many students will tell you about their sad story of having a thousand line program on their screen one minute, and nothing but an error message the next. Unfortuantely, this is not something we can control. Professors usually do not empathsise with this mistake--so make a good habit of saving your work before you become another statistic. Go ahead and save your file in your directory (open the File menu and pick "Save (current buffer)"). And to make sure that you have saved and edited correctly, exit Emacs, open it up again and try retrieving your file.
2.3.5 (Display page) Learning more
There are many ways for you to learn more about emacs. Perhaps the easiest is to run the "Emacs tutorial" from the "Help" menu (although, this isn't a particularly pretty tutorial). There are many documents on the web that you might find helpful. Additionally, a reference sheet is included in your reader.
2.4 Programming with Emacs(3 steps)
2.4.1 (Display page) Create some Scheme files

Make a Scheme file

Congratulations. You have made it this far!

Now one more important point that should be mentioned: whenever you save your files in Emacs, be sure to specify the type of file that you are editing. Otherwise, Emacs will assume that it is a text file. To tell Emacs what kind of file you are creating/opening we add on what we call a file extension to the end of the file name. If you remember, the favethings.txt is specified to be a text file with the .txt file extension. For CS3, we identify Scheme files with the .scm extension.

With this in mind, let's try creating a Scheme file. Call it "firstfile.scm" and make sure you save it in your ucwise directory. Even though you probably don't know any Scheme yet, try typing some into Emacs. We'll help you. Type this:
(define (firstprogram x)
  (+ x 5))
You sould notice three things. First, there are a couple of different colors on the screen. As you get used to Scheme, these will help you see different parts of your code. Second, whenever your cursor is on or next to a parenthesis, the other parenthesis that goes with that one is highlighted. Scheme is full of parentheses, so this will be really helpful. Finally, Emacs automatically indented the second line. This also makes your code easier to follow.
2.4.2 (Display page) Say hello to STk

My friend, STk

This is a class on Scheme, not Emacs, so it's about time we show you how to actually run Scheme code. You need a Scheme interpreter, like STk. Fortunately, you can run STk from inside Emacs. There are two ways to do this. First, you could hold the Meta key (the diamond) on the keyboard and press the "s" key (we'll write this "M-s" for Meta-s). Second, you could go to the Scheme menu (it will only be there if you have a Scheme file open right now) and select "run Scheme in a split window." Either way, your Emacs window should now look something like



Now you have a regular STk interaction window! It is exactly the same as any other interaction window that you have used so far (i.e. the Listener). You can type expressions or functions into it, as well as highlight, copy and paste functions from the Emacs editor.

Let's say you have typed the following into the top buffer of Emacs:
(define (double x)
 (* x 2))

(define (square x)
  (* x x))
With STk running in the bottom of the window, put your cursor at the start of the definition for double and press M-p (hold the diamond key and press "p"). Emacs gives the definition of double to STk and moves your cursor to the next definition. Now Emacs really talks to STk. One final neat feature of STk is that it can load in whole Scheme files so you don't have to define everything every time. Try saving this file as "examples.scm." Go to STk and type (load "examples.scm"). Now you can use double and square as though you had typed them into STk.
2.4.3 (Display page) Conclusion

La fin

Well, if you have reached this step then you have successfully completed the UC-WISE version of the Emacs tutorial. With much practice and usage, you will grow to love (or possibly hate) Emacs, but it will definitely be useful to you in your Berkeley career if you so choose to continue with the lower division computer science courses. In fact, all course work in the cs61 series can be very conveniently done using Emacs, although you are not bound to only this editor.

If you would like to learn more of the [much] cooler commands that Emacs has to offer, please go through their tutorial that you can access through the menu bar under Help.

Thank you for participating in the CS3 Emacs tutorial and have a good day!
3 Get oriented to the UC-WISE system
(1 activity)
3.1 A brief introduction to UC-WISE(4 steps)
3.1.1 (Display page) Overview
The UC-WISE system that you'll use in CS 3 this semester lets us organize activities that you'll be doing inside and outside class. Notice the sidebar on the left; it organizes the activities that you'll do during class. Two important activities are "brainstorming" and online discussion. You'll get practice with this now.
3.1.2 (Brainstorm) Brainstorming
What's your favorite restaurant in Berkeley?
3.1.3 (Discussion Forum) Online discussion
Explain what's so good about the restaurant(s) you mentioned in the previous step, and comment on one of the opinions of your classmates.
3.1.4 (Display page) The "extra brain"
In the upper right part of the sidebar, there's an icon that's intended to resemble a brain. This is your "extra brain", where you can collect tips, things to remember, interesting programming examples, or anything else you'd like to keep track of in this course. Click on the brain icon and put a comment about a Berkeley restaurant you might want to try in it. Then click on the icon that looks like a diskette to save your brain entry.