As section 2.2 of the textbook explains, a programmer can have Scheme
construct a new procedure, not equivalent to any of the built-in ones, by
writing a lambda-expression. For instance, Scheme recognizes
the expression
(lambda (n) (* n n))
as a description of a procedure that, when called, squares its argument. Like any other value, this procedure can be given a name by means of a definition:
(define square
(lambda (n)
(* n n)))
Such a procedure can be called, just as if it were a built-in procedure:
> (square 12) 144
One foot is equal to 761/2500 meters (exactly). Write a procedure named
feet->meters that takes one argument, a real number
representing a length measured in feet, and returns the number that
represents the same length as measured in meters. Use this procedure to
determine the number of meters in a mile (5280 feet).
The volume of a sphere of radius r is 4/3 times pi times
r3. Write a procedure named sphere-volume
that takes as its argument the radius of a sphere (in, say, centimeters)
are turns its volume (in cubic centimeters). Use this procedure to compute
the volume of a softball (radius: eight centimeters).
Define a procedure snoc (``cons backwards'') that
takes two arguments, of which the second should be a list.
snoc should return a list just like its second argument,
except that the first argument has been added at the right end:
> (snoc 'alpha '(beta gamma delta))
(beta gamma delta alpha)
> (snoc 1 (list 2 3 4 5 6))
(2 3 4 5 6 1)
> (snoc "first" '())
("first")
Hint: There are two ways to define this procedure. One uses calls to
reverse and cons; the other uses calls to
append and list.
Define a predicate bigger? that takes two real numbers as
arguments and returns #t if the absolute value of the first is
greater than the absolute value of the second, #f if it is
not. Call your procedure twice -- once with arguments that ensure that
the value of the procedure call is #t, once with arguments
that make the value #f.
Suppose that an object is falling freely near the surface of the earth, under circumstances that make it possible to ignore air resistance and other messy complications. The distance d that the object falls is related to the time t it takes to fall that distance by the equation
d = gt2/2
where g is the constant acceleration due to gravity, 9.8 meters per second squared.
Define a procedure distance-fallen that takes one argument,
which should be a non-negative real number representing the duration of the
fall of an object (in seconds), and returns the distance that the object
falls (in meters). Use your procedure to determine how far an object would
fall in ten seconds.
Extending the previous exercise, define a procedure
falling-time that takes one argument, a non-negative real
number representing the distance fallen by an object (in meters), and
returns the duration of the fall (in seconds). Use this procedure and the
feet->meters procedure defined in exercise 1 to determine
how long it would take an object to fall to the ground from the top of the
World Trade Center in New York (a distance of 1368 feet).
For additional exercises of this sort, see Exercises 2.1 through 2.5 on pages 39 and 40 of the textbook. If you have time, try them.
This document is available on the World Wide Web as
http://www.math.grin.edu/~stone/courses/scheme/procedure-definitions.html
created September 2, 1997
last revised May 27, 1998