Comp 210 Lab 5: Mutual recursion; Emacs basics

Mutual Recursion, Emacs basics


Mutual Recursion

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>.

Write the appropriate uses of define-structure.

List the functions that these uses of define-structure make.

Create all the program templates for functions of one input on these data types.

Write a function that counts the number of potatoes grown in a kingdom. (Simpletons generate potatoes, idiots are too stupid to raise potatoes, spies steal potatoes. Knights and regents are too important to farm themselves.) The function should take a regent as its argument and return a number.

Create test data for this function.

With either answer, be sure to pick each serf's number of potatoes well. E.g., it's not very helpful if each serf generates/steals zero potatoes: you can't tell if you're adding all the numbers up properly. E.g., if you pick the numbers 1, 10, 100, ..., you'd be able to check not only whether your sum is correct, but if it isn't, you should be able to tell which number wasn't being added correctly.

Write a function that checks if a kingdom has any spies working against it. The function should take a regent as its argument and return a boolean.

Create test data for this function.

This example requires a lot of test data to be fully complete. (In the real world, understanding all of the relevant test cases is very important for checking software products. This is part of the area of Quality Assurance, or QA.)


Emacs basics

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 root window, or typing emacs from the UNIX prompt.

There are several other text editors available. We recommend Emacs because it is the main standard in the Unix world. Also, it is the most flexible since, for better or worse, it has everything (including the kitchen sink!) in it (e.g., a mail reader, a newsreader, a web browser, games, and a fully programmable command language). Because of this flexibility, it isn't always the easiest editor to use.

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 mouse or 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
M-< go to the start of the file
M-> go to the end of the file
Editing (cf. Edit menu)
keystrokemeaning
Backspacedelete previous character
C-ddelete next character
C-kkill (cut) from cursor to end of line
C-yyank (paste) back the most recently-killed text
C-_undo

Emacs has the concept of "modes". For instance, when editing a file ending in ".ss", it is in Scheme-mode, and will treat certain command (such as indent) a little differently than if you are in (say) C mode or text mode.

An emacs window is a "view" into a file which you are editing. It can be handy to have different windows so you can deal with several files at once.

  1. Split an existing window by typing C-x 2 (or choose Split-Window from the Files menu). You now have two different windows looking at the same file. Type a bit -- you'll see the change happening in both windows.
  2. Go to the other window by typing C-x o (or use the menu command).
  3. In this bottom window, let's open your homework file. Type C-x C-f if you don't want to select "Open File..." from the command window. The cursor will be on the bottom line, called the minibuffer. Note that you can use editing commands like C-a in the minibuffer. Also, if you type the first part of the file name and press Tab, emacs will complete the filename (as much as it can).
  4. You should now see your homework assignment in the lower half of the screen. It is easy to transer text between the bottom and the top using cut-and-paste. You can also kill text in one window (with C-k), move to the ohter window (which is C-x o, or the menu) and yank (C-y) the text back there.
Files, Buffers, Windows (cf. Files menu)
keystrokemeaning
C-x C-f find file (space bar completes name)
C-x C-s save file
C-x b switch buffer (show other file)
C-x 2 split-window
C-x oother-window
C-x 0delete-window (not the associated file)
C-x C-cquit emacs

If you want a 2-page summary of these and other emacs commands, you can print out the emacs reference card. There is also a handout about Emacs available in Mudd.