Homework #1: Intro
Out: Monday, January 14th, Due: Thursday, January 17th, 11:00pm


Administrative

The purpose of this homework is to familiarize yourself with the various tools that will be used throughout the course, and to get a feeling of basic programming in Racket. Note that the submission system will not allow you to submit your code if it does not follow certain requirements. For example, you will not be able to submit code that doesn’t have the required definitions, or doesn’t bind them to appropriate value types, or code that contains lines that are longer than 79 characters. In addition, in this submission as well as future homeworks, you are required to have tests that cover your whole code, otherwise your submission will be penalized severely (you’ll be asked to confirm it).

In this particular homework, the server will perform additional tests over your code, which will require you to come up with correct solutions to be able to submit. This means that you will generally be graded on contracts and purpose statements, other comments, style, test quality, etc. Correctness will play a very small role here, since everyone is expected to be able to solve these questions.

The first thing you will need to do is to download and install Racket and then the course plugin. When this is done (and you restart DrRacket), you will be able to register for homework submission, as described in the Software Section. (If you work in the lab, Racket should already be installed.)
Shortly after you install the plugin and register, you will be added to the course piazza group, which will allow you to post the required test message (see below). Note: do not email requests to be subscribed to the piazza group, it will be done after you register with the handin server.

You might want to consult How to Design Programs and the Class Notes before writing your code.

For this problem set, you are required to set the language level to “Intermediate Student”. This will allow you to use the Stepper to debug your code, and more importantly: learn how Racket evaluates it.

This homework is for individual work and submission.

Submitted code should have comments that describe the function and its type, as well as enough test cases for complete coverage (DrRacket indicates covered expressions with colors for covered and uncovered source code, unless your code is completely covered). Your tests should have the following form: (equal? <expected> <expression>), except for boolean functions (predicates) where they should be either <expression> or (not <expression>).
Important: Your tests should cover your whole code, otherwise the server will heavily penalize your submission. You should not have any uncovered expressions after you hit “Run” — it should stay at the same color, indicating complete coverage. Furthermore, the server will run its own tests over your code, which means that you will not be able to submit code that does not work. Reminder: this means that most of the focus of this homework is put on the contract and purpose statements, good style (indentation, comments, etc), and good tests.

General note: do not duplicate code! If there is an expression that is used in multiple places, then you should use let.


Questions

  1. Once you’re subscribed to the course piazza group, you will see a test post for this homework. Post a followup note to this. Make sure that it actually appears, otherwise you will not get the credit for posting.
    Reminder: the piazza group is https://piazza.com/class/cs4400, see also the Piazza Group Section. Note that you will not be able to post on the piazza group until you are subscribed to it, and you will be subscribed to it only after you installed the course plugin and created an account — so make sure you do that first. Once you do this, you will get a notification when you’re on the piazza group. Again, do not try to subscribe to it by yourself.
  2. Define a near? function that consumes three integers and determines whether they are “near” each other, where near means that they are at most within an interval of 2. For example, (near? 1 2 3) would return #t because all of the numbers are within the 1–2 interval. Note that the numbers might not be sorted, and they might not be unique. (Hint: you can use the min and max functions.) Here is another example, written in a form of a test that you can use:
    (near? 1 -1 1)
    Don’t forget to write a proper contract, a purpose statement, and sufficient tests that cover the whole code and also verify corner cases.
    Note that for this homework, test cases are simple toplevel expressions that should all evaluate to #t.
  3. Remember that lists are defined inductively as either: A “Listof T” would be similar, except that it will use T instead of “any”.
    With this in mind, define a count-xs function that consumes a list of symbols and returns the number of occurrences of the symbol x in the list.
  4. Define an ascending? function that determines whether a list of numbers is sorted in ascending order. This means that each element is smaller than or equal to the next one in the list. Again, make sure that you write a proper contract, purpose statement, and tests.
  5. Finally, implement a zip2 function that does the following: You can assume here too that the input is always valid.
    For example, here is a use of this function, in a test form (note that we’re now using equal? to compare a nested list structure):
    (equal? (list (list 1 'a) (list 2 'b) (list 3 'c))
            (zip2 (list 1 2 3) (list 'a 'b 'c)))
    (This can be used as a test, and it will probably even cover your code completely, but you need more tests. Specifically, you need to test corner cases.)
    Make sure that you write a correct contract!
  6. In the definitions window, define my-picture — bind it to the number next to your picture in the pictures that were taken in class or the one from the university records. If you appear in several pictures, choose the one that looks best and/or the one you like most, and write a comment with the numbers of the other pictures that you found yourself in. (The pictures have numbers where your name will appear, so bear in mind that it’s better to have something with a little space below.)
    Please make sure that you are recognizable — some of these pictures are very old or barely visible, and some pictures from the university records are ancient. If there is no picture for you, or no picture that is good enough to recognize you, then please email me a good recent picture of yourself. No tiny images, please (they’ll be scaled down anyway). Once you do that, you will be added to the list of pictures and you will have a number to use. (As long as there is no picture, you can use 0, and say that you don’t appear there in a comment.)
  7. Define minutes-spent as the number of minutes you spent on your homework. Please specify a reasonable estimate here and in future homeworks, since these values help in determining homework weights.