(define pi 3.1415926535) (define r 5) (define r-squared (* r r)) (define r-cubed (* r r-squared)) (define pi-r-cubed (* pi r-cubed)) (define volume (* 4/3 pi-r-cubed))
Check (by hand or with a calculator) that this sequence produces the correct result.
In the last expression, why are no parentheses used around the number 4/3?
Does the computation still come out correctly if parentheses are used around 4/3? Why or why not?
To select material from Netscape, move the cursor to the beginning of a section and push down the left mouse button. Then, holding the button down, move the mouse to the end of the section. (The entire section now should be highlighted.) When the desired section is highlighted, stop pressing on the left mouse button -- the section should stay highlighted. Now move the mouse to where you want to paste the material, and click the middle mouse button.
For practice, select and paste the above sequence of define statements into Scheme within the dtterm window.
(define volume (* 4/3 3.1415926535 5 5 5))Check the values Scheme returns for each of the following expressions:
(* 2) (* 2 2) (* 2 2 2) (* 2 2 2 2) (* 2 2 2 2 2)What happens if you do not supply any operands?
(*)Hypothesize why you get this result.
(quote pi)(quote pi) may be abbreviated 'pi . Try typing this at the keyboard as well.
Often, it is convenient to move this window to the right the computer screen. This is accomplished by moving the mouse to the labeled bar emacs:... at the top of the XEmacs window. Press the left mouse button on this labeled bar, and move the mouse (keeping the left button depressed). The XEmacs window will follow your mouse movements. When the XEmacs window is where you want it, release the left mouse button.
While XEmacs is an extremely powerful editor, many common capabilities are highlighted with buttons and menus at the top of window. These menus are analogous to most word processing packages, and thus are not discussed here. Ask the instructor as questions arise. (If something particularly strange seems to be happening, type <Ctrl/g> to stop the processing of a command.)
Type the following Scheme definitions into the XEmacs file.
(define pi 3.141596535) (define q 'quarts) (define a (sqrt 2))As you are typing, note that when you type a right parenthesis, XEmacs shows you which left parenthesis it matches. This will be particularly helpful when typing longer Scheme programs.
Save the file by clicking on the save button at the top of the XEmacs window. XEmacs then will open a new window, asking you to give a file name for the program. For example, to save your work in a file first-test.ss, you could type this name into the new file-naming window and hit the return key. File first-test.ss now is ready for use within Scheme.
Move the mouse to the dtterm window and type scheme to begin running the Scheme environment. Within Scheme, you can use the definitions from a file with the load procedure. Here, you should type
(load "first-test.ss")More generally, load allows you to specify any file by placing the file name in double quotes.
Check that the definitions from the file work as expected by typing
pi q a
Modify first-test.ss so that it contains a typographical error. (Remember to save the file by clicking on the save button.) What happens when you try to load this version of the file into scheme?
(+ 2 3)in the file and load it into scheme. Describe what happens.
(define r (+ 2 3)) (define s '(+ 2 3)) (define t (quote (+ 2 3))) (define u ''(+ 2 3)) (define v (quote (quote (+ 2 3))))Load the revised file into Scheme and check the definitions for r, s, t, u, and v. Explain the results that you observe.
(lambda (x) (* x x))This expression indicates that the input parameter is x and the result of the function will be computed as (* x x).
((lambda (x) (* x x)) 1) ((lambda (x) (* x x)) 3) ((lambda (x) (* x x))-2)What happens if you try to apply the function to all three values at once?
((lambda (x) (* x x)) 1 3 -2)In Scheme, a lambda expression is called a procedure.
(define f (lambda (x) (* x x))) (f 1) (f 3) (f -2)
Your activity at a workstation may be recorded in a file using the following steps.
submit first.session[This indicates that what follows will be recorded in a file called first.session.]
cat first-test.ss
print first.session
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/153.sp00/lab-procedures.html