CS 2360
Winter 1998
Homework Assignment 1
Due no later than 8:00am, Monday, January 19, 1998
Greetings young LISPlings! Here's your very first graded homework
assignment in CS 2360. There will be many more, so program early
and often. As with all of your assignments, this one will be worth
4% of your final grade. 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. Write your code so that other people can
understand it easily. Among other things, this means you must
document your code. And using appropriate indentation will help
keep your graders happy. You may use only the following predefined LISP
functions in constructing your own functions: DEFUN, FIRST, REST, CONS,
LIST, SQRT, *, +, /, and -. 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 second
argument would be 5 and the third argument would be 10. If the
person is exactly 6 feet tall, the second argument would be 6 and
the third 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 according
to NBC. 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) When converting between temperatures in degrees Fahrenheit and
degrees Celsius, it is useful to note that -40 degrees Fahrenheit
equal -40 degrees Celsius. This observation makes for the following
symmetric conversion formulas:
C = (F + 40) * 5/9 - 40
F = (C + 40) * 9/5 - 40
Construct conversion functions, F-TO-C and C-TO-F, using these
formulas. Each function takes exactly one argument. The F-TO-C
function takes a number representing a temperature in Fahrenheit and
returns a number representing the same temperature in Celsius. The
C-TO-F function takes a number representing a temperature in
Celsius and returns a number representing the same temperature in
Fahrenheit.
Examples:
? (f-to-c 32)
0
? (f-to-c 98.6)
37.0
? (c-to-f 100)
212
8) Construct a function called SWAP which exchanges the first
and third elements of any list containing three or more elements.
(You can assume the list will have the correct length when
passed; you don't have to do error checking.)
Examples:
?(swap '(a b c))
(C B A)
? (swap '(a b c d e))
(C B A D E)
9) Construct a function called ADDPAIRS which takes two lists of
three numbers as its arguments, and returns a single list containing
the sum of the corresponding numbers from each list.
Example:
? (addpairs '(2 4 6) '(4 5 6))
(6 9 12)
10) Finish the QUADRATIC function we started in class. Add the
SQRT-OF-DISCRIMINANT function that we didn't construct. And so as
not to cause any problems, change the name of DENOMINATOR to
MY-DENOMINATOR, since the DENOMINATOR function already exists
in LISP. 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 1998 by Kurt Eiselt. All rights reserved.
Last revised: January 13, 1998