Program 3
CS 320
HOW TO COMPILE:
Unix based compiler : Scheme –load p3.scm
WHAT TO PROGRAM:
This p3.scm file already defines some lists with which you can experiment;
all you need to do is add the definition of four functions:
- evens
- oddrev
- middle
- endsmatch
Each of these functions should take exactly one parameter; the parameter must be a list. If your function was given the wrong number of parameters, we will let the interpreter complain about it. However, if the function is given the wrong type of parameter (i.e., not a list), the function should return a friendly error message (including a newline) beginning with 'USAGE:', followed by an indication of the correct invocation. For example, "USAGE: (endsmatch [list])" Here is what each function should do [assuming it is given an acceptable parameter]: EVENS: (evens lst) should return a new list, formed from the even-numbered elements taken from lst. That is, (evens '(a b c d e f g)) should return: (b d f) (evens (LIST 's 't 'u 'v 'w 'x 'y 'z)) should return: (t v x z) (evens '(f (a b) c (d e d) (b a) f) ) should return: ((a b) (d e d) f) Both (evens '()) and (evens '(a)) should return the empty list, etc.
ODDREV: (oddrev lst) should return a new list, formed from the odd-numbered elements taken from lst, but in the reverse of their original order. That is, (oddrev '(a b c d e f g)) should return: (g e c a) (oddrev (LIST 's 't 'u 'v 'w 'x 'y 'z)) should return: (y w u s) (oddrev '((h i) (j k) l (m n))) should return: (l (h i)) (oddrev '()) should return the empty list, etc.
MIDDLE: (middle lst) should return a one-element list consisting of just the middle element of lst [or return the empty list if there were an even number of elements in lst]. That is, (middle '(a b c d e f g)) should return: (d) (middle '(s t u v w x y z)) should return the empty list. (middle '((a b) c (d e d) c (b a)) ) should return: ((d e d)) Note that ((d e d)) is a list containing the thing that was in the middle, which happened to itself be a list.
ENDSMATCH: (endsmatch lst) should return #t if the first element in the list is the same as the last element in the list, and return #f otherwise. That is, (endsmatch '(s t u v w x y z)) should return: #f (endsmatch (LIST 'j 'k 'l 'm 'n 'o 'j)) should return: #t Both (endsmatch '()) and (endsmatch '(a)) should return #t, etc. You can receive full credit for your endsmatch function as long as it works for all lists that are made up entirely of atoms. For a little extra credit, you can try to get it to behave correctly when you feed it complex lists such as: (endsmatch '((a b) c (d e d) c (a b)) ) The above list should result in #t; the two lists below should produce #f. (endsmatch '((a b) c (d e d) c (b a)) ) (endsmatch '((y z) y) ) Whether you are trying for extra credit or not, you do NOT have to worry about what endsmatch should do with things like: (endsmatch '(abc) ) [I only intend to feed 'endsmatch' single-character atoms, not strings.]
If you forget a closing parenthesis, your functions will not work (you'll get a 'premature EOF' error). It's critical that you use an editor that will match up parentheses, since there are so many of them in LISP-like languages. In vi, for example, if you navigate your cursor over a parenthesis and then type the '%' character, the vi cursor will jump to the matching parenthesis. If the cursor doesn't move, then you know you have a mismatch. (In vi, '%' causes the same trick to happen with {} and [], too.)
GRADING CONSIDERATIONS:
The most important thing to remember is that you must provide me with a functional programming solution in order to get any credit. Even though the variant of Scheme on rohan allows constants to be redefined (an example of this is shown in ~cs320/schemetutorial.scm), you must NOT store values; therefore, all four of your functions must be defined recursively [Chapter 15 of Sebesta shows many such 'legal' scheme functions of this type]. It is OK to define 'helper' functions if you find that convenient (for example, facthelper was a helper function for factorial on page 679). To start scheme and get it to pay attention to your definitions, you would do: scheme --load p3.scm ...and then type commands at it (much like you did with the APL interpreter). [scheme, like most UNIX utilities, exits when it sees EOF, so typing a Control-D is the graceful way to quit scheme.] Your program will be checked against some predefined lists (you'll find these are already DEFINEd in ~cs320/p3.scm for you) as well as some new lists that the autograder will make up. The autograder will periodically conduct tests on your ~/Three/p3.scm file and mail you feedback. However, it won't attempt to do anything unless you have [at least] defined the 'evens' function.
So, put a syntactically correct definition of the evens function into your ~/Three/p3.scm file early on [it doesn't have to work correctly; it merely has to exist and be 'legal' scheme code], or else you won't really have much of a clue as to what sort of tests I will be running. Refer to the documentation guidelines advertised in our first C program; most of those suggestions are applicable here (no extraneous prompts; explain your algorithm; point out any failures to meet the specs; give credit when EXTRA CREDIT: The file called ~/Three/GradeEarly (note the odd capitalization) is intended to signal that you have completed the project a week early, and wish to take advantage of the extra credit reward. The contents of the file are immaterial; the autograder will only test for the existence of this file. If the file has been created by the 120% deadline, your project will be picked up for grading at that time (so no further changes to your code or documentation will be possible). Your score will be augmented by 20% (for example, if you would have normally received 80% of the points, you will instead qualify for 96% of the points possible). Use the best HTML, CSS, JavaScript resources to get the perfect code in your web projects