[CS Dept., U Chicago] Courses


CMSC 16100: Honors Introduction to Computer Programming I (Autumn 2003)

Homework

Assignment #0: Use DrScheme to run a small program



[23 Sep 2003] I am updating this assignment from 2002 to 2003. Details may change. [MOD]

Assignment #0 is due at 11:59 PM on Friday, 3 October 2003. Submit your work according to the general instructions.

This assignment is designed to exercise you in the annoying logistical details that you need to handle while programming. There is no interesting programming problem for you to solve. It is very important that you are not distracted by too many logistical details in the subsequent assignments, which will present you with interesting conceptual problems to solve. So, try to shake out your methods for working on this trivial assignment.

I've described much of what you need to do for Assignment #0 in highly annoying detail. I will never do that again in this class. After this assignment, I will describe your work clearly enough that you can figure out what you need to do, possibly after asking some questions. In order to succeed in the later work of the course, you must use Assignment #0 to become fluent in the basic operations of programming and exploring with DrScheme. If you fumble around a lot about which buttons to push when, you will not be able to concentrate on the interesting problem-solving content of the real assignments.

You only hand in a small part of the work in this assignment. But do it all.

These instructions describe how to perform assignment #0 on one of the CS Department's GNU/Linux computers. You may use DrScheme on another computer if you like (e.g. the CS Department's Macintosh computers). In that case, it's a good idea to make sure that you have the same version of DrScheme (it was version 205 last time I checked). And you will have to figure out any differences in the methods of working. In particular, you will have to transfer your files to the CS Department's Linux/Unix file system in order to hand them in. I will not have time to figure out problems due to the use of different computers.

1. Start DrScheme and select your language

  1. Log in to a Computer Science Department Linux computer. Open an interactive terminal/shell window. Use the "mkdir" command to create a directory in which to work on CMSC 16100 homework, with a subdirectory named "Asst0" for this assignment. Use the "cd" command to move into that directory, and the "ls" command to notice that there's nothing in it yet. If you don't know how to do all this stuff in Linux, get help from a friend, a TA, or a consultant in the CS instructional computing labs in Regenstein.

  2. Start DrScheme by typing "drscheme &" to your interactive terminal/shell window (the "&" causes DrScheme to run in the background, allowing you to type further commands if you need to).

  3. As DrScheme starts, accept the default choices of "English" and "Beginning Student".

  4. Select "About DrScheme" from the "Help" menu, and verify that you are using version 205. Take the "Tour" if you like, but it is designed more for beginning instructors than for students.

  5. Select "Choose Languages" from the "Language" menu. Notice that DrScheme starts up with the "Beginning Student" language. Upgrade yourself to "Beginning Student with List Abbreviations".

    Select the "Show Details" button, and notice that there are a number of choices having to do with input and output "syntax" (that is, notation).

    Don't worry about the meaning of the language choices now, but remember their presence for later consideration. They affect When you discuss and compare work with other DrScheme users, make sure you notice whether you are using the same language choices.

2. Evaluate arithmetic expressions in the interaction window

  1. The DrScheme user interface shows two windows in which you may type. The top one is called the "definitions window", and the bottom one is called the "interaction window". Point to the bar between the windows until you get a diamond-shaped pointer, press the left mouse button, and move the bar up to make the interaction window bigger.

    When you type in these windows, the presence or absence of space between two typed characters may make a big difference, but the amount of space makes no difference, even when it takes you to a new line of text. In the jargon of Scheme, and many other programming languages, a sequence of one or more blank spaces, tabs, and/or line breaks is called "whitespace".

    Type "(+ 2 3)" in the interactions window, and hit the enter key to evaluate that expression. You should see the result "5", but also a yellow-highlighted warning: "WARNING: Interactions window is out of sync with the definitions window. Click Execute." In this case, it really means that the interactions window has not incorporated the change from Beginning Student to Beginning Student with List Abbreviations.

    Select the "Execute" button. Notice that the interactions window now confirms your use of DrScheme version 205 and the Beginning Student with List Abbreviations language. But you appear to have lost your expression adding 2 and 3. Not so. Type "Escape p" to recover your expression, and "Enter" to evaluate and display the result "5" without the warning.

  2. Evaluate lots of variations on "(+ 2 3)", such as

    to get an idea of the effects of parentheses, white space, order of tokens. Evaluation of some of these expressions is erroneous. Avoid retyping everything by using "Escape p" to recover old expressions and then editing them.

3. Evaluate different types of expressions in the interaction window

  1. Evaluate

    Think about the nature of lists in Scheme as suggested by these examples. (If you have seen another form of LISP or Scheme in the past, you will notice that DrScheme's presentation of list values is a bit different from the one you saw before. But the values themselves are the same.)

  2. Think about the variety of types of Scheme values in the examples above. Numbers, booleans, lists, symbols, and strings will be important to us as the quarter progresses. Evaluate "(list? (list 1 2 3))", "(list? empty)", and "(list? "(1 2 3)")" to get an idea what lists are (and what they are not).

    Try lots of other examples, using the operations "number?", "boolean?", "symbol?", "string?", and "char?". Figure out the types of lots of values such as "123", "'123", "(list "1" "2" "3")", "abc", "'abc", "(list "a" "b" "c")". Most of these make a lot of logical sense after you assimilate them, but there are some subtle quirks, such as the different effects of single- or double-quoting "123" vs. "abc". It's not important to understand the whole mess right now---just get a sense of the scope of the problem, and we'll treat each type more carefully later.

4. Define a new function in the definitions window

  1. Enter the following text in the definitions window (the top window):
    
    ; rotate-list-left is a function that rotates the elements
    ; of a list to the left, moving the first element to the
    ; last position.
    
    (define (rotate-list-left listin)
      (reverse (cons (first listin) (reverse (rest listin)))))
    
    This is a tiny Scheme program, defining the function rotate-list-left. Select the "Check Syntax" button. If you typed it all in correctly, you will see a nicely color-coded display of the text. I don't recommend learning the meanings of the colors (I still haven't done so), but I think their presence makes the program easier to read.

    If you mistyped something, you will probably see an error message in a small window above the definitions window, and a portion of the program will be highlighted in pink. DrScheme finds it likely that you mistyped something in the pink region. If you're lucky, the region is small. If you're unlucky, it contains the whole program.

    Even if you typed everything correctly on your first try (or if you were clever enough to cut and paste instead of typing), introduce some errors deliberately, notice the message and highlighting, and then correct your errors. Omit parentheses, and add extra ones. Misspell one of the instances of "listin", perhaps as "litsin". Each time you change the program, select "Check Syntax" again.

    With a syntactically correct program, click next to a parenthesis, to place the cursor there. Notice the grey highlighting. Place the pointer over the first instance of the symbol listin and notice the blue arrows. Think about what they might be telling you. This is some of the really nice stuff in DrScheme, and there's a bit more of it to discover.

    If you are really unlucky, there will be no error message, but the program will define a different function from the one that I intended.

  2. With a seemingly correct program (one that passes "Check Syntax" with no error message), evaluate "(rotate-list-left (list 1 2 3 4)) in the interaction window. Oops! We forgot to select the "Execute" button. DrScheme warns us that the two windows are out of sync, and tells us that "rotate-list-left" is an "undefined identifier". Select the "Execute" button. The nice coloring disappears, which annoys me a lot, so I select "Check Syntax" again immediately. Your expression to evaluate also disappears from the interaction window, but you can get it back with "Escape p". When you finally see it, the result of this example should give you the basic idea of what rotate-list-left is intended to do.

    Try to think of some subtle points about the behavior of rotate-list-left, and evaluate other expressions to illuminate those subtle points. If you must do something on your own already, figure out how to define rotate-list-right in the same style. It's only slightly more complicated

  3. Add a comment (lines starting with ";") to the top of the definitions window with your name displayed prominently, as specified in the general instructions.

  4. Select the "Save" button. A new window will pop up, with a text entry field at the bottom, with some stuff already typed in. Add "/rotate-list-left.scm" to the end of that stuff, and select the "OK" button. Back in your Linux terminal/shell window, type "ls", and notice the new file named "rotate-list-left.scm". That file contains only the contents of the definitions window. Get in the habit of selecting "Save" after every change to the definitions. You only have to enter a file name the first time.

5. Create and execute a test suite

  1. From the "File" menu, select "New Test Suite". A new window will pop up. Select the "New Test" button. In the "Call" region, enter "(rotate-list-left (list 1))", and in the "Expected" window enter "(list 1)". Select the "Execute" button on the test suite window (not the one on the definitions window). "(list 1)" will appear in the "Actual" region, and a big green check mark to indicate that the program passed the test.

  2. Add the following additional tests, using the "New Test" button each time:

    By this point, you'd better figure out how to cut and paste, instead of retyping all of these expressions. You also need to figure out how to expand the test suite window, so you can see the whole list of 5 test cases. Select the "Execute" button to see our program pass the tests. Think about why I chose those particular tests.

  3. I left out one very important test. Add it:

    Notice that my program fails this test, not by producing a wrong result, but by failing to produce any result. Think about why empty is arguably the right result, and why my program doesn't get it.

    To see how DrScheme displays more conventional failures of tests with a red "X", change one of the "Expected" entries, but fix it again after you've seen the result.

    In your further work in the class, every function that you define in a program must be tested in a test suite. Most of the time, your programs will fail their tests, and you will work to correct them. We will discuss how to craft good test suites later.

    Test suites are another really nice feature of DrScheme. But, they have some shortcomings. For example, if one test case fails to produce any result (such as (rotate-list-left empty) above), no further test cases are executed. That's why I saved the empty test for last. There is also no way to incorporate tests that are intended to fail in this way, even though that is sometimes the desired result. Later on, we may switch to a more carefully crafted testing procedure that doesn't look as nice.

    Warning: DrScheme tests the last saved version of your program. If you have changed the program in your definitions window, but did not yet save it, you will test the previous version. I think this is the wrong behavior, and a later version of DrScheme will correct it. For now, remember to save before executing a test suite.

  4. Use the "File" menu on the test suite window to save your test suite with the name "rotate-list-left.tst.scm".

6. Check out the Scheme documentation

  1. Select "Help Desk on the "Help" menu. Find the Revised^5 Report on the Algorithmic Language Scheme (you have to hunt down a few levels in the help materials). This is the definitive reference for the Scheme programming language, but not for the DrScheme interface. It is rather hard to read. But in order to program effectively, you must be able to dig precise information out of manuals like this, even though you may not understand all of the surrounding information.

  2. Find the list of trigonometric functions in standard Scheme.

7. Hand in your definitions and test suite, and notice how to continue work

Hand in your definitions and test suite according to the general instructions. In this assignment, you just type


~odonnell/Teaching/Utilities/handin 0 rotate-list-left.scm rotate-list-left.tst.scm
To avoid typing the path prefix "~odonnell/Teaching/Utilities/", see the section on PATH in the general instructions. This is a good time to read about the handin command.


Valid HTML 4.0!


Last modified: Sun Sep 28 22:50:52 CDT 2003