CS 1 Fall 2007

Lab 1: Invoking the Magic...err, Technology

Assigned: Monday, October 1, 2007
Due: Thursday, October 11, 2007, 02:00:00



Note: Before working this assignment, please read through Section 1.1.5 of Structure and Interpretation of Computer Programs.

Numbers in bold at the start of problems (e.g. [10]) are a crude estimate of the time (in minutes) the problem should take. If it seems like a problem is taking you disproportionately long to complete (e.g. if you are spending hours on a problem rated [10]), that is a hint that you might want to talk to a TA. These numbers assume that you have read the book and done the portions of the assignment that precede it.

Part A: Exercises

Please complete the following exercises before your recitation section on Thursday or Friday. Should you have any difficulties working these basic exercises, bring your questions to recitation.

Note: The exercises in Part A should be done worked offline (without using the Scheme interpreter). You will need to type up your results to submit them with your answers to the rest of the assignment.

  1. Write Scheme expressions to evaluate the following:
    1. [5] 2*9+62-8

    2. [5] (42*(6+7+(5-2)))2

  2. Walk through the substitutions involved in the evaluation of the following expressions. Show every step. Really. Every single step, no matter how trivial.

    1. [5]

      (+ 1 (/ (- 2 3) (* 4 5)))
      
    2. [10]

      (define (f x) 
        (+ (- 2 x) (* x (+ x 5))))
      (f 3)
      

      NOTE: Don't forget to evaluate the define as well as the function call!

    3. [10]

      (define (a x)
        (- 5 (/ x (+ x 1))))
      (define (b x)
        (* x x (a (+ x 3))))
      (b 11)
      

      NOTE: Again, don't forget to evaluate the defines as well as the function call.

  3. [20] What is wrong with these expressions?

    1. (+ + 3)
    2. (+ 3 4
    3. (3 + 4)
    4. (2)
    5. ((+ 2 3))
    6. (define b 3 4)
    7. (define c + 3 4)
    8. (define b (x) (+ x 1))

    In Part D, we'll ask you to type these "bugs" into the Scheme interpreter.


Part B: Book Exercises

There is no part B this week. Please make sure you have read the assigned sections. There will be assignments from the following sections in subsequent weeks.


Part C: Getting Started in the CS1 Computer Lab

One thing you should do immediately is to make sure you have an account on the CS cluster and begin to become familiar with the computers in the lab. This series of exercises is intended to guide you through this process.

The computers we are using for CS1 are in the CS lab (Jorgensen 154). TAs staff the lab Sunday through Wednesday to answer your questions and help you through difficulties (the first week they will be there from Tuesday to Friday).

  1. [10] Sign up for a computer account using the Account Request Form.

    If you have not already done so, do this immediately as it will take a bit of time to process your account.

  2. [30] Review the Unix handout. You may also want to go to the Tutorial session on Thursday, September 30 (9:00pm in Jorgensen 074).

  3. [15] Go to the lab and log in to a CS computer with your new account and password.

  4. [30] The Scheme interpreter we will be using for CS1 is called DrScheme (pronounced "doctor Scheme"). DrScheme also provides a full development environment for your Scheme programs. Start up DrScheme by typing "drscheme &" at the unix prompt; the DrScheme environment will come up in a new window. First DrScheme will ask you for your (human) language preference and then for a "language level", which means which version of the programming language Scheme to use. DrScheme supports several dialects of Scheme, but we'll only be using one. Select "PLT/Pretty Big (includes MrEd and Advanced)". Then the main DrScheme window will come up. WARNING: if you select the wrong language level, things WILL NOT work the way they're supposed do, so be careful. You can always change the language level later using the menus if this happens. You should not have to change the language level again (the preferences will be stored automatically when you exit DrScheme).

    Click on "help desk" in the "help" menu. This will bring up the help desk window, where all the documentation for DrScheme is found. Then click on "tour" in the window (under "Software"). This will bring up a hyperlinked page which will guide you through the DrScheme environment. Click on "contents"; this will give you a list of chapters. Read chapters 2 and 7 only (actually, you can read more if you want to, but you don't have to). Note that CS1 will not be using the textbook How to Design Programs which is referred to several times in the DrScheme documentation. Also note that the term "REPL" means "Read-Eval-Print Loop"; that's just another word for the interactive environment.

    Note that the files you turn in for CS1 must be plain-text (also known as ASCII) files (so, for example, Word documents are not acceptable --- your TA will return any such submissions unmarked and ask you to rewrite or convert your work to ASCII text). DrScheme will store your Scheme code as plain text files.

    CS1 will rely on email to contact students. After each assignment has been graded your TA will send you email commenting on your answers and code. If you did not master all the concepts on the lab, your TA will tell you what you need to rework in order to complete the lab.

  5. [5] If you do not plan to log in to the CS cluster and check your mail there regularly, make sure your mail is forwarded to wherever you do read mail regularly (e.g. your ITS account). Ask your TA how to set this up (it's very easy).


Part D: Working with the Interpreter

  1. [15] Type the following expressions into the DrScheme interactions window (the lower window) one at a time, then hit return. Make sure you understand why each expression gives the result it does. Then type all of the expressions into the definitions window (the upper window) and click on the Run button. Note that the results appear in the lower window in the order in which the expressions were encountered.

    1. (* 6 9)

    2. (* (+ 2 3) (+ 3 4))

    3. (define b 9)
      b
      
    4. (define (answer) 42)
      answer
      (answer)
      
    5. (define (f x)
        (+ 4 (* x 5) (* x x)))
      (f 2)
      (f 5)
      
  2. [20] Type the buggy expressions from A.3 into the interactions window of DrScheme. See how the interpreter responds to these "bugs".

  3. [60] Write a simple series of Scheme functions for temperature conversion:

    1. Write a Scheme procedure to take a Celsius temperature and return the corresponding Kelvin temperature.

      1. Make sure it works correctly.
      2. Test it on several conversions, including:

        • -273, -100, 0, 100, 300
      3. Write a routine to take in a Kelvin temperature and convert it to Fahrenheit.

      4. Compose (i.e. combine) your previous two routines to create a Celsius to Fahrenheit conversion routine.

      5. Test your composed routine and show that it works.

  4. [20] Become familiar with the load command and other ways to load files into DrScheme.

    1. Exit and restart DrScheme so that you are starting with a fresh environment.

    2. Put your two functions defined for the previous problem into a file temp_conversion.scm.

    3. Use the load command to read the contents of these files into DrScheme. Note that DrScheme starts relative to the directory in which it was started, so if you start DrScheme in your top level directory, you may have to load the file as e.g.:

      (load "cs1/lab1/temp_conversion.scm")
      
    4. Test that your functions, now loaded from a file, behave as they did before.

    5. Exit DrScheme and restart it. Now load the file using the File/Open menu command. This is an alternative to using load.

    6. Exit DrScheme and restart it with the filename as a command-line argument as follows:

      % drscheme temp_conversion.scm &
      

      where % is the unix prompt (yours may look different). This is yet another alternative to using the load command.

    In the future, you will often find it convenient to write your code in files and load them into the Scheme interpreter as you need them.


Finally, read the instructions on how to use the cs1man submission program to turn in your assignment, and then do so.

Every numbered exercise or problem in this assignment requires you to do something. The writeup you turn in should include your answer to every component with a few exceptions: