Laboratory 1

Laboratory 1


Location

The laboratory in Computer and Space Sciences room 3330 has been reserved for our class from 9:30am until 11am on Thursday, September 7.


Goals

This laboratory has two goals:
  1. Providing practice using the LISP environment on the AITS system
  2. Exercising some of the concepts we've been learning in class
It is being organized as an in-laboratory experience rather than a homework assignment so that students have the opportunity to experience the "social" aspect of programming work, and so that an assistant is readily available to answer questions that come up as students get up to speed on the system.


Logging in

The lab is a WAM lab. You should log in using your WAM logname and password.


Connecting to AITS

  1. You should choose one of the machines on the AITS Unix cluster to log in to. The possible machines are
              frost.umd.edu
              holmes.umd.edu
              koko.umd.edu
              maigret.umd.edu
              marlowe.umd.edu
              marple.umd.edu
              morse.umd.edu
              poirot.umd.edu
              tracy.umd.edu
              wolfe.umd.edu
       

  2. Use telnet to connect to one of the above machines. This is generally available under the "Internet" menu, or you can select "START", "Run...", and type "telnet holmes.umd.edu" or whatever machine name you choose.

  3. You may wish to execute telnet twice to open two different windows, one of which, for example, can be used for editing while the other is used for running your LISP interpreter.


The Exercise: Getting Started

Some friends of mine are in a Girl Scout troop. As you may know, every year the Girl Scouts sell cookies in order to raise money. This particular troop had five days in which to sell cookies, and each of the six girls in the troop did her best. (One of them, Ellen, had the flu and didn't sell any at all.) The following global definition creates a symbol called +sales+ and binds to it a table showing how the girls did on each of the five days:
(defconstant +sales+
  '((ann     4 2 3 0 4)
    (betsy   1 4 0 0 0)
    (carol   0 1 2 3 4)
    (dawn    1 3 5 1 0)
    (ellen   0 0 0 0 0)
    (fran    8 1 7 8 5))
  "Data on Girl Scout cookie sales over five-day sale period")
Notice that +sales+ is a list containing six elements, and each element is itself a list containing information about one of the girls.

Two of the girls, Ann and Dawn, are sisters, and they're very competitive. So competitive that they've decided someone needs to write some LISP code to determine who sold more cookies this year. Being good programmers, they've decided to organize the task in a very general way, so that it's easy to get and compare information about any of the girls' sales. To make things easier, they've gotten their mom to write the first function they need, which takes a list of numbers and sums it up. She wrote:

(defun sum (numlist)
  "Adds up the numbers on the list"
  (if (null numlist)
      0.0
      (+ (first numlist) (sum (rest numlist)))))

Rather than typing in +sales+ and the definition of the sum function, you should copy a file that contains them into your local directory by doing the following to use the "ftp" program on the AITS machine:

Now that you've got file lab1.lisp, you should bring it up in a text editor: your job is going to be to write the rest of the LISP code the girls need. To work most efficiently, you'll want to be able to easily go back and forth between your text editor and a Unix window where you'll be running LISP. Here are a couple of ways to do this.

  1. If you've brought up two telnet windows, you should be able to execute
             emacs lab1.lisp
          
    in one of them. The result should be a window for the emacs editor. You should also still be able to work in the other telnet window, so you can execute
             lisp
          
    there to start lisp. Now you should just be able to go from window to window. (Usually you click on the title bar of the window to select it, but that varies from system to system.)

  2. If you know emacs fairly well, you can start emacs, use ctl-x 2 to create a split screen, and then use meta-x shell on one half of the screen. Then you can run lisp in one half, and edit your file in the other half, using ctl-x o to go back and forth between the two. If you do this, you can resize your window to make it tall.


The Exercise: Doing the Work

Loading the code you've been given

By this point, you should have LISP running. The first thing to do is load up the information from file lab1.lisp. In LISP (not in the Unix shell) you can do this by evaluating the following:
      (load "lab1")
This is just the same as if you'd just typed all of lab1.lisp into the LISP environment. Type
      (describe '+sales+)
to see information about that symbol.

Function: AVERAGE

Before anything else, add a comment to the top of lab1.lisp identifying yourself, such as:
      ;;;;;;;;;;;;;;;;;;;;
      ;;; My Name Here
      ;;; Ling689A Lab 1
      ;;; Today's date
      ;;;;;;;;;;;;;;;;;;;;

Now you're ready to do some programming. First, define a function called average. It should take a single argument, which you may name anything you like. That argument will be a list. The value returned by the 'average' function should be the average of the numbers on the list; that is, you're going to take the sum of the numbers on the list and divide it by how many numbers were on the list.

If you're having difficulty getting started, draw a boxes-and-arrows diagram using Touretzky's notation. What are the pieces you need? What are the inputs and outputs for each piece? How can you connect these? If you're helping someone who's stuck (this is a note for the assistant and/or any students who have easily solved this), what I'd like you to focus on first is helping them get a correct boxes-and-arrows diagram written, then on the LISP notation for the sub-pieces, and then on writing the whole function definition.

At any point in time, you can save lab1.lisp, go back to LISP, and re-execute

  (load "lab1")
This will re-load the file with the new changes. Note that you may get errors when you re-load the file. That's ok. The best thing to do is to read the error message, quit out of the debugger, and go back to the editor to fix the file. Then save your changes, go back to LISP, etc. This is called the edit-debug cycle. (Get used to it...)

If your edited LISP code works correctly, you can then test the function, e.g. by evaluating:

  (average '(1 2 3 4))
If you've done your Touretzky reading, you'll realize we're putting the quote symbol in front of the list to make sure that there's no attempt to evaluate 1 as a function, with 2, 3, and 4 as its arguments!