Curriculum Builder Portal 2/11/10 9:26 AM total day span:"99" . 1 Get oriented to the UC-WISE system 2009-8-27 ~ 2009-8-28 (1 activity) 1.1 A brief introduction to UC-WISE(4 steps) 1.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. 1.1.2 (Brainstorm) Brainstorming What's your favorite restaurant in Berkeley? 1.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. 1.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. 2 Communicating with the Scheme interpreter 2009-8-27 ~ 2009-8-28 (7 activities) 2.1 Start "conversing" with the Scheme interpreter.(3 steps) 2.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. 2.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. 2.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. 2.2 Have some complicated "conversations".(7 steps) 2.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. English Scheme 3 + 5 (+ 3 5) 3 – 5 (– 3 5) Adding or multiplying more than two numbers can be done in a sort of shorthand: English Scheme 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. 2.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. 2.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. http://fall09.ucwise.org/builder/builderPortal.php?BUILDER_menu=curriculumSummary Page 1 of 104 Curriculum Builder Portal 2/11/10 9:26 AM 3 + (7-2) 3 + 4 * 5 (3 + 4) * 5 2.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. 2.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. 2.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 in conventional arithmetic notation. 2.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). 2.3 Make your own procedures!(10 steps) 2.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. 2.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: ... 2.3.3 (Display page) Put the interesting errors in your notebook. Put a note in your notebook about the errors that are most likely for you to make and the error messages that result. 2.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.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages104 Page
-
File Size-