CS 2360 Summer 1998 Homework Assignment 1 Due no later than 8:00am, Tuesday, July 7, 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. This assignment comes in two parts. The first part is just to get your feet wet without worrying about recursion, while the second part adds the mystery and excitement of functions that call themselves. 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). Let the LISP hacking begin! Turnin Proceedures will be posted to this newsgroup in a couple of days... ***IMPORTANT*** You may use ONLY the following predefined LISP functions in constructing your solutions to the nine problems in this assignment: DEFUN, FIRST, REST, CONS, LIST, ABS, SQRT, *, +, /, and -. 1) Let's start with an easy one. 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 2) 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) 3) Construct a function called ROTATE-LEFT which, when passed a list of four elements, 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 '(b d e nil)) (D E NIL B) ? (rotate-left '(1 2 3 4)) (2 3 4 1) 4) 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 ? 5) 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 ? 6) 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 ? 7) 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) ? 8) 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) 9) Time travel is a great science-fiction concept. Unfortunately, there's some good reasons why it can't happen, at least the way it's depicted in _Terminator_. One counter-argument is that if you travel in time, you'll also need to travel in space; otherwise, you'll arrive in open space, since the Earth will have continued moving. So, why is this a problem? Let's see. The Earth's solar system sits about two-thirds of the way out from the center of Milky Way galaxy and revolves around the center of it. The speed the Earth is traveling is approximately 250 kilometer / second. Suppose you have a time machine which will transport you any amount of time in only one second. That means, you can travel 200 years into the past, but the trip will only take one second. Sounds good. But, remember, we also need to shift in space to catch up with the Earth. So, in that one second of travel, we need to travel whatever distance the Earth will have moved in the time span we are traversing. For example, suppose we want to go 10 seconds into future. We make the trip in one second. In 10 seconds, the Earth will have moved 2500 kilometers, so we need to travel 2500 kilometers in our one second of travel. But, we cannot travel faster than the speed of light, which is 300,000 kilometers per second. So, if we try to travel a distance in time such that the Earth will have moved more than 300,000 kilometers, then we'd have to break the speed of light to do that. Which is impossible, so we cannot time travel at those spans of time. How does this fit into LISP? Well, we want a function which we tell us what percentage of the speed of light we will need to travel for a given time span. We'll give the function a time span in days, hours, minutes. (CHRONOS 0 0 1) means we are traveling one minute into the future. The Earth will have moved 15,000 kilometers (250 km / sec times 60 seconds = 15,000 kilometers). So, we need to travel 15,000 km / sec, which is 5 percent the speed of light (15,000 km / sec divided by 300,000 km / sec = 0.05 times 100 = 5 percent). That is, (CHRONOS 0 0 1) --> 5 If we travel one day into the future, we get (CHRONOS 1 0 0) --> 7200 OOPS. Looks like we will not be traveling a day into the future, since we need to go faster than the speed of light. To travel into the past, we simply specify a negative value. (CHRONOS 0 0 -1) --> 5 Notice that the answer will still be positive! NO ERROR CHECKING IS NEEDED. THE RESULT IS UNDEFINED IF NON-NUMBERS ARE ENTERED. THE days, hours, and minutes PARAMETERS NEED NOT MAKE TOTAL SENSE. FOR EXAMPLE, (CHRONOS -2 1000 32.12) MEANS WE WANT TO TRAVEL 2 days INTO THE PAST, 1000 hours INTO THE FUTURE, AND 32.12 minutes INTO THE FUTURE. JUST ASSUME THIS IS ALL ADDTIVE (2 days is 48 hours, so we are asking to travel 952 hours and 32.12 minutes into the future). Please, ask Christina (chrisb@cc) questions if this problem seems excessively complex. Good Luck!!!