CS 2360 - Assignment 1

Homework Assignment 1


CS 2360
Fall 1998
Homework Assignment 1
Due no later than 8:00am, Monday, October 5, 1998
 
It is with a bit of sadness that we welcome you, the last generation
of LISPlings.  But there's no time for remorse as we now present
your first programming assignment.  It's worth a total of 180 points.  

If you were to look through various LISP books, you might find many
solutions to the problems below.  In fact, all of them have been solved 
by CS2360 students from previous quarters because we've been using 
variations of this assignment for years.  So it's going to be relatively 
easy to find solutions to all these problems if you want to.  But knowing 
where to find these answers isn't going to help you in the weeks to come; if 
you can't solve most of these problems on your own now, you're going to 
encounter great difficulty with future homework assignments.  So please, do 
yourself a very big favor and solve as many as you can on your own and don't 
go looking up the answers in textbooks or elsewhere.

For all these problems, 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.  That means you should worry about modularity
and abstraction where appropriate, and you should employ meaningful 
function and parameter names.  Don't forget to document your code
with appropriate comments, and it wouldn't hurt to use indentation
to make your code look pretty (which in turn makes for happier and
more generous graders).  You may use only the following predefined LISP 
functions in constructing your solutions to the these problems: 
DEFUN, LIST, SQRT, *, +, /, and -.  

1)  Let's start with something real easy.  Construct a function called
    CUBE-IT which takes one argument, a number, as input and returns the
    cube of that number.
 
    example:
 
    ? (cube-it 3)
    27
    ? (cube-it 1.414)
    2.8271459439999993
    ?
 
 
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)  In baseball, a batter's slugging percentage is calculated by
    dividing the number of total bases of all safe hits by the total
    times at bat.  The number of total bases is calculated by awarding
    one base for a single, two bases for a double, three bases for a
    triple, and four bases for a home run.
 
    Construct a function called SLUGGING which takes five arguments as
    input.  The first argument is a number representing the batter's
    times at bat.  The second argument is a number representing the
    total number of hits (i.e., singles, doubles, triples, and home 
    runs).  The third argument is a number representing the number of 
    doubles hit by the batter.  The fourth argument is a number 
    representing the number of triples hit by the batter.  The fifth 
    argument is a number representing the number of home runs hit
    by the batter.  This function returns a number which is the
    batter's slugging percentage.
 
    Example:
 
    ? (slugging 585 158 15 4 40)
    0.5145299145299145
 
 
6)  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
 
 
7)  Define a function called CIRCLE-STUFF which takes one argument, a
    number representing the radius of a circle, and returns a list
    of three numbers representing the radius, the area, and the
    circumference of the circle.
 
    example:
 
    ? (circle-stuff 4)
    (4 50.26548245743669 25.132741228718345)
 
 
8)  The length of the hypotenuse of a right triangle is calculated by
    computing the square root of the sum of the squares of the lengths
    of the two sides of the triangle which meet at a right angle.  
    Define a function called HYPOTENUSE which takes two arguments.  The
    two arguments are numbers which represent the lengths of the two
    sides of a right triangle which meet at a right angle.  The function
    returns a number representing the length of the hypotenuse of the
    the right triangle.
 
    examples:
 
    ? (hypotenuse 3 4)
    5
 
    ? (hypotenuse 9 16)
    18.35755975068582
 
    ? (hypotenuse 1 1)
    1.4142135623730951
 
 
9)  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: September 29, 1998