|CSC 153||Grinnell College||Spring, 2005|
|Computer Science Fundamentals|
Consider the following sequence of definitions computes the volume of a sphere of radius 5:
(define pi 3.14159265358979) (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))
Highlight this code in your browser by moving the mouse to its start, and holding down the left mouse button while moving the mouse to its end. After the section is highlighted, release the left mouse button.
Now start Scheme in a terminal window, and paste this highlighted material into Scheme by moving the mouse to the terminal window and clicking the middle mouse button.
Describe the results of this work.
Retrieve the value computed by typing volume into Scheme.
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?
(define volume (* 4/3 3.14159265358979 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.
Follow a similar pattern using addition + rather than multiplication.
(quote pi)(quote pi) may be abbreviated 'pi . Try typing this at the keyboard as well.
Creating New Scheme Files: Clicking with the left mouse button on the icon of gnu (African antelope) at the bottom of the screen to open an Emacs window.
Type the following Scheme definitions into the Emacs file.
(define pi 3.14159265358979) (define q 'quarts) (define a (sqrt 2))
As you are typing a right parenthesis, Emacs has the capability to show you which left parenthesis it matches. If Emacs does not do this for you automatically, bring your mouse to the Options menu at the top of the Emacs window, and select the option for Paren Match Highlighting. This will be particularly helpful when typing longer Scheme programs.
Save the file by clicking on the save button at the top of the Emacs window. Emacs 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 terminal 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)) 1) ((lambda (x) (* x x)) 3) ((lambda (x) (* x x))-2)What happens if you try to apply the procedure 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)
script first.session[This indicates that what follows will be recorded in a file called first.session.]
This document is available on the World Wide Web as
created 22 January 1977
last revised 23 January 2006
|For more information, please contact Henry M. Walker at email@example.com.|