UserDefined Procedures
UserDefined Procedures
Summary: This lab provides practice with simple userdefined
procedures.
In previous labs, we have seen that Scheme provides a variety of capabilities
that are already defined as procedures. For example, the procedures
car, cdr, and cons are builtin procedures for
list processing. In addition, Scheme allows a programmer to
construct a new procedure 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 builtin procedure:
> (square 12)
144

Write a Scheme procedure (add2 a) that returns the sum a+2.

Define a procedure f(x) = x²  3x + 2 and evaluate it for
x = 0, 1, 2, 3, 4 .

Write a procedure (quadraticroot a b c) that finds one root of a
quadratic equation
ax² + bx + c = 0 using the quadratic
formula. Use it to find a root of the above equation.
Test your procedure by computing
(quadraticroot 1 5 6)
(quadraticroot 2 10 12)
(quadraticroot 1 4 4).
In
each case, use algebra to check your answers.
What are (quadraticroot 1 0 1) and (quadraticroot 1 0
2)?
[Would these two examples work in other programming languages
that you know?]

Write a procedure swap that interchanges the first two elements on a
list, leaving the rest of the list unchanged. Thus, (swap '(a b c d
e)) should return (b a c d e). In this problem, assume that
the list given to swap has at least two elements; do not worry about
the possibility that swap might be applied to numbers, empty lists,
or lists with only one element.

The volume of a sphere of radius r is 4/3 times pi times r3. Write a
procedure named spherevolume that takes as its argument the radius of a
sphere (in, say, centimeters) and returns 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.

More Practice: As you have time, work on the following exercises.

Given a list of 3 elements (e.g., (a b c)), write a procedure that
creaste a new list with the original first element moved to the end (e.g.,
(b c a)).

Write a procedure at computes the distance from a point in space to the
origin.
Work to be turned in:
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/151.fa00/labprocedures.html
created January 22, 1997
last revised September 1, 2000