********************************************************************** File ~/../public/A0/README Homework Assignment 0 CSE 150, UCSD Fall 2002 Due: Thursday, October 10th, 3:30PM (NOTE: Changed due date...) (NOTE: Special due date time so we can discuss it in class!) ********************************************************************** >>> SUMMARY: This assignment will acquaint you with the basics of defining functions that manipulate lists in LISP. You DO NOT need to worry about error checking for this assignment. >>> RULES: In writing your function definitions, you may use any built-in LISP function, special form, or macro introduced in Chapters 1 through 4 in the Winston and Horn text, except for SET, SETF, and SETQ. Also, you may not use any iteration constructs such as DO, DOTIMES, DOLIST, or LOOP --- so you will be writing recursive function definitions without assignment statements: real functional programming! HOWEVER, it is often useful to use SETQ when TESTING your code. E.g., USER: (setq testlist '(1.5 2.1 -4.2)) (1.5 2.1 -4.2) USER: testlist (1.5 2.1 -4.2) USER: (LCEIL testlist) ; Note NO QUOTES! (2 3 -4) This saves a lot of typing. However, NO SETQ's SHOULD APPEAR IN YOUR HOMEWORK FILE!!! ** NOTE ** Make sure to reread the homework policy from the course description handout. Specifically, read the sections entitled "Rules for collaboration on Programming Assignments", "Style rules for LISP programs" and "Turning in programs." >>> ASSIGNMENT: This assignment has 5 parts, described below. Each part involves defining a function (or functions) with a specified name. (You may find it useful to define some other functions too, if you wish.) All these function definitions should be put in a file named "hw0.cl" so that when that file is loaded into LISP, evaluating calls to the functions will work as required. Make sure the file name and function names are spelled correctly; your assignment will be graded in part by a computer program, and computer programs don't handle misspellings very well. Make sure your programs are commented in acceptable LISP style! Your file "hw0.cl" must be submitted using the "turnin" utility no later than the due time deadline specified above. Once you have done a "prep cs150f", just type "turnin hw0.cl" to submit your solution file. ---------------------------------------------------------------------- Part 1. You've been assigned as a TA for a new class that is being taught. To simplify the grading procedure you've decided to write some functions to help you out. Since you want to minimize the amount of work you have to do, you've decided to only give As and Fs in the class. Everything above 50% is an A and everything less than or equal to 50% is an F. Define a function named GRADE. GRADE takes one argument, a floating point number (between 0 and 1, inclusive), and returns 'A' if the argument is greater than .5 and 'F' if the argument is less than or equal to .5. For example: USER: (GRADE .9) A USER: (GRADE .45) F ---------------------------------------------------------------------- Part 2. Define a function named SUM-SQUARED. SUM-SQUARED takes a single argument, a list of integers and returns the sum of the squares of the integers of the list. For example: USER: (SUM-SQUARED '(1 2 3)) 14 USER: (SUM-SQUARED '(1 2 2)) 9 USER: (SUM-SQUARED '(3)) 9 (HINT: You'll need to use recursion for this problem) ---------------------------------------------------------------------- Part 3. Define a function FIRST-LIST. FIRST-LIST takes one argument, a list of lists and returns a list consisting of the first element from each of the lists. To clarify, input is of the form: (list1 list2 list3 ... listN) Where list1 ...listN are lists. For each list, if the list is non-NIL the first element is added to the list of values to be returned. Otherwise, if the list is NIL nothing is added. For example: USER: (FIRST-LIST '((a b) (c d))) (a c) USER: (FIRST-LIST '((1 2 3) (4 5) (6))) (1 4 6) USER: (FIRST-LIST '((a b c d) (e))) (a e) USER: (FIRST-LIST '((a b c) ())) (a) USER: (FIRST-LIST '((a b c) (d e) () (f g h i))) (a d f) USER: (FIRST-LIST '(((a b) (c d)) (e f) (g (h)))) ((a b) e g) ---------------------------------------------------------------------- Part 4. You like how your initial grading program worked out from Par 1, however, you've decided that the function lacks a little... functionality. So, you've decided to add a few new features. a. You want to be able to have the grading program take in both the students score and the total possible for that assignment and then spit out the grade. As before, if the percentage is greater than 50 an 'A' should be returned and if the percentage is less than or equal to 50 an 'F' should be returned. Define a function GRADE2. GRADE2 has two arguments. The first argument is the students score and the second argument is the total point possible on that assignment (you can assume that the both arguments are greater than zero and that the second argument is greater than or equal to the first). For example: :(GRADE2 50 50) A :(GRADE2 45 100) F :(GRADE2 90 100) A b. You're happy with how things are progressing, but you realize that the students in you class actually have names. So, you decide to write one more grading function that also takes the name of the student and associates the grade with the name. Define a function GRADE3. GRADE3 has three arguments. The first is the student's name, the second is the student's score and the third is the total possible for that assignment. The function returns a two item list with the first item being the students name and the second item being the students grade. For example: :(GRADE3 'Dave 45 50) (DAVE A) :(GRADE3 'Gary 100 100) (GARY A) :(GRADE3 'Homer 15 100) (HOMER F) c. You've decided to make one last addition to your grading function. You want to be able to get the grades for the whole class with just one function call. Using GRADE3, define a function GET-GRADES. GET-GRADES has two arguments. The first argument is a list of two item lists with the first item being the student's name and the second item being the student's score. The second argument is the total possible for that assignment. The function returns a list containing two item lists with the student name and the students grade ('A' or 'F'). (YOU MUST USE THE GRADE3 FUNCTION). For example: :(GET-GRADES '((Dave 90) (Gary 100) (Homer 15)) 100) ((DAVE A) (GARY A) (HOMER F)) ---------------------------------------------------------------------- Part 5. Define a function named DELETE-MEMBER. DELETE-MEMBER has two arguments. The first may be an atom or a list. The second is always a list. DELETE-MEMBER returns a list with ALL occurences of the first argument removed from the second argument. For example: USER: (DELETE-MEMBER 4 '(1 2 3 4)) (1 2 3) USER: (DELETE-MEMBER 1 '(1 2 1 2)) (2 2) USER: (DELETE-MEMBER 4 '(1 2 3)) (1 2 3) USER: (DELETE-MEMBER '(a b) '((a b) 1 2 (c d))) (1 2 (c d)) ----------------------------------------------------------------------