CS 2360
Winter 1997
Homework Assignment 1
Due no later than 8:00am, Tuesday, January 21, 1997
Welcome all you budding young LISP hackers. It's time to get your feet
wet, so here's your first 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. Write your code so that other people can understand it easily.
You may use only the following predefined LISP functions in constructing
your own functions: DEFUN, FIRST, REST, CONS, LIST, *, +, /, and -.
And since we briefly mentioned APPEND in class, you can use that one
too. In fact, it will be really handy in at least one of the problems
below. Here are the problems:
1) 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. 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
2) 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
3) Construct a function called MY-FOURTH which returns the fourth
element of a list.
Examples:
? (my-fourth '(a b c d))
D
? (my-fourth '(a b c d e))
D
? (my-fourth '(a b c))
NIL
We'll explain the NIL thing on Thursday.
4) Construct a function called SWAP which exchanges the first
and fourth elements of any list containing four or more elements.
Feel free to use the MY-FOURTH function you created above.
(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 d))
(D B C A)
? (swap '(a b c d e))
(D B C A E)
5) Construct a function called ROTATE-LEFT which, when passed
a list of arbitrary length, returns the original list
with the first element moved to the end of the list, (thus
effectively moving all other elements to the left by one position).
Examples:
? (rotate-left '(a b c d))
(B C D A)
? (rotate-left '(a b c d e))
(B C D E A)
? (rotate-left '(a))
(A)
6) 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)
7) 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 1997 by Kurt Eiselt. All rights reserved.
Last revised: January 14, 1997