CS 2360
Spring 1996
Homework Assignment 1
Due no later than 8:00am, Monday, April 8, 1996
Here's your first LISP programming assignment. It's worth a total of 140
points. Make sure that you adhere to the constraints of the functional
programming paradigm (e.g., access only information passed as
parameters, return one value, no side effects, etc.). Furthermore,
remember that you're programming for people, not for the benefit of the
computer. If you have working code, but we have to struggle to figure
out how your code does what it does, you won't get many points. If your
code doesn't work, but we think we can debug it easily, you'll get
partial credit. If it works, and it communicates to us how it works,
you'll do great.
Here are the problems:
1) Let's start with something real easy. Construct a function called
SQR which takes one argument, a number, as input and returns the
square of that number.
examples:
? (sqr 3)
9
? (sqr 1.414)
1.9993959999999997
?
2) Let's get slightly more complicated. Define a function called
HEIGHT-IN-INCHES which takes two arguments as input. These
arguments represent a person's height in feet and inches as follows:
if, for example, the person is 5 feet 10 inches tall, the first
argument would be 5 and the second argument would be 10. If the
person is exactly 6 feet tall, the first argument would be 6 and
the second argument would be 0. This function returns one number
which represents the person's height in inches.
example:
? (height-in-inches 5 10)
70
?
3) Feeling a tad pudgy? According to NBC's "Dateline" (which, of
course, is where we should all be getting our medical advice) you
can determine if you're overweight by plugging the appropriate
values into this formula:
704 * your weight in pounds
INDEX = -------------------------------
2
(your height in inches)
If the value of INDEX is 25 or less, you're in great shape says
Jane Pauley. If the value of INDEX is 30 or more, you're at
significant risk for heart disease and other weight-related
problems.
Define a function called INDEX which takes three arguments as input.
The first argument is a number representing a person's weight in
pounds. The second and third arguments represent a person's height
as described in problem 2. The function returns the value indicated
by the formula above. Make sure that the value returned by the
function is expressed as a decimal fraction.
examples:
? (index 190 6 0)
25.80246913580247
? (index 190 5 10)
27.29795918367347
?
4) If we use a little algebra, we can rewrite the formula above to
give us something we can use to compute our ideal weight. Assuming
that an index of 25 is optimal, we can then find the ideal weight
as follows:
2
25 * (your height in inches)
IDEAL-WEIGHT = ---------------------------------
704
Define a function called IDEAL-WEIGHT which takes two arguments
as input. These two arguments represent the person's height in
feet and inches as described earlier in problem 2. The function
returns a numeric value representing the person's ideal weight
as calculated by the re-engineered formula just a few lines above.
examples:
? (ideal-weight 5 10)
174.0056818181818
? (ideal-weight 6 2)
194.46022727272728
?
5) Now construct a function called FANCY-IDEAL-WEIGHT which takes three
arguments as input. The first two represent the person's height in
feet and inches, as we've done so many times now. The third
argument is a symbol representing the person's name. The function
returns a list which can be interpreted as a more meaningful
message. The examples below show exactly what we want here. (You
don't need any input/output stuff here; just use what you've learned
so far in class.)
examples:
? (fancy-ideal-weight 5 10 'kurt)
(THE IDEAL WEIGHT FOR KURT IS 174.0056818181818 POUNDS)
? (fancy-ideal-weight 6 2 'john)
(THE IDEAL WEIGHT FOR JOHN IS 194.46022727272728 POUNDS)
?
6) Construct two more functions, called GET-NAME and GET-WEIGHT. Each
function takes exactly one argument, which is a list like that
returned by FANCY-IDEAL-WEIGHT. GET-NAME returns the name contained
in that list (i.e., the fifth element), and GET-WEIGHT returns
the weight (i.e., the seventh element).
examples:
? (get-name '(the ideal weight for kurt is 174.0056818181818 pounds))
KURT
? (get-weight (fancy-ideal-weight 6 2 'john))
194.46022727272728
?
7) Finish the QUADRATIC function we started in class. Add the
DISCRIMINANT function that we didn't construct, and change the
name of DENOMINATOR. Prove to yourself, and to us, that the
solution works by getting it to run on some Common LISP system.
Show us the results.
examples:
? (quadratic 2 0 2)
(#c(0.0 1.0) #c(0.0 -1.0))
? (quadratic 2 2 0)
(0.0 -1.0)
Copyright 1996 by Kurt Eiselt. All rights reserved.
Last revised: April 3, 1996