1 Communicating with the Scheme interpreter | (7 activities) |
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 / ESSIAt 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. |
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. |
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. |
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.
|
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.
|
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 |
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. |
The Simply Scheme textbook describes a visual way of viewing Scheme procedures. Click here to get a detailed explanation of this representation. |
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. |
Figure out what Scheme evaluates a subtraction with more than two inputs, for example, (- 7 2 3 8). |
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:
|
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: ... |
Put a note in your "Extra Brain" about the errors that are most likely for you to make and the error messages that result. |
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. |
Earlier, we discussed rules for evaluating Scheme expressions involving + and *. Here is how evaluation of a call to a user-defined procedure works.
(square (+ 2 1))
|
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. |
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 discount-price. This procedure is given two arguments:
expression value (discount-price 1000.0 .25) 750.0 (discount-price 10.0 .40) 6.0Type your procedure into the Scheme interpreter, then test it by evaluating the two expressions above. |
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. |
Summary:Explanation of why "send region" seems to not work if the parenthesis are incorrect. |
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: xis produced. What was the symbol that was changed, and how did you figure out what it was? |
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. |
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) ) |
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. |
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. |
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 |
The following terms were discussed today:
|
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!
|
Exercises
How to submitYou 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:
(three of the following five): |
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) |
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. |
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. |
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:
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." |
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). |
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. |
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. |
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
|
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: |
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 optionand 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. |
Create a new fileNow 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 likeThis 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:
|
Saving is everythingOne 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. |
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. |
Make a Scheme fileCongratulations. 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. |
My friend, STkThis 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 likeNow 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. |
La finWell, 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) |
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. |
What's your favorite restaurant in Berkeley? |
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. |
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. |