Comp 210 Lab 5: Structures

Table of Contents

  • Structures & Mutual Recursion
  • Running a Java program
  • Emacs Intro
  • Mutual Recursion

    Suppose a dog consists of a color, size, and temperment (all of which are symbols). How do you
    1. Tell Scheme about this data definition?
    2. Have Scheme create a couple of dogs?
    The point is that the above two items are different tasks. define-structure doesn't actually create any dogs, it just creates constructor, accessor, and prediates for the dog type.

    Fealty in the Dark Ages

    Consider the following data definition, which describes the structure of a medieval kingdom:
    <regent> := (general aide)
       where general is a <knight>, and aide is a <serf>.
    
    <knight> :=   (private salary)
                | (officer fellow squire servant salary)
       where fellow is a <knight>, and squire and servant are <serf>s.
       salary is a number of potatoes.
    
    <serf> :=   (simpleton potatoes)
              | (idiot)
              | (spy potatoes rebel)
       where potatoes is a number, and rebel is a <regent>.
    
    1. Express the above data structures in Scheme.
    2. Create one of each of the above types, except for serf-spy. (Use define to give them names for later testing.)
    3. Create templates for them. To save typing, you can crib the template for knights.
    4. Write a function that counts the number of potatoes grown in a kingdom. (The function should take a regent as its argument and return a number.)
    5. Optional: If you like, write a function that counts the net potatoes in a kingdom, after salaries are paid. Or, the number of people in a kingdom. Or the number of regents.
    6. Cut-and-paste some further subjects, use them to create a serf-spy, and then test your code.

    Any questions on anything that might be on the exam?

    Running a Java program

    Today's lecture previewed some Java code. Though we won't be writing Java code for a while yet, if you want to experiment and play around, here's what you need to do in order to run a Java program.

    First, create a text file containing the source code; the file needs to end with the ".java" suffix.

    Running a Java program is a two-step process. First you must translate--compile--the program text to a format more readily understood. The program javac (``Java compiler'') takes a .java file, and creates ``byte code'' files, with suffix .class, for each class. Then, to run the procedure main for a particular class Foo, you say java Foo.

    An example is worth a thousand words:

           owl% cd ~/comp210
           owl% mkdir lab05
           owl% cd lab05
    
           owl% cp ~comp210/Labs/lab05/*.java .
           owl% ls
           owl% javac List.java
           owl% ls
           owl% javac Main.java
           owl% ls
           owl% java Main
    

    Emacs Intro

    Emacs is a text editor. We'll be using it later in the semester, and are easing you into it now. Go back to your home directory, and start emacs by either selecting it from the "editors" menu when you right-click on the background of your console, or typing emacs & from the UNIX prompt. (If you ever want to run emacs from a text-only (non-X) terminal, just type emacs without the ampersand.)

    Once emacs is started, select the a built-in tutorial file via the ``Help'' menu. It's one of those things you can dabble at; we'll let you do this on your own time. In the long run you'll find the control-keys more useful for moving around than the arrow keys.

    Some common movement commands are repeated here. You can try them out immediately on the TUTORIAL file in emacs.
    Movement
    keystroke meaning
    C-b go backward a character
    C-f go forward a character
    M-b go backward a word
    M-f go forward a word
    C-a go to the start of the line
    C-e go to the end of the line
    C-p go to previous line
    C-n go to next line
    C-v go down one screen
    M-v go up one screen
    If you want a 2-page summary of these and other emacs commands, you can print out the
    emacs reference card.
    Editing
    C-d delete next character
    C-k kill (cut) from cursor to end of line
    C-y yank (paste) back the most recently-killed text
    C-_ undo


    Back to Comp 210 Home