More first-class functions, emacs basics,


More first-class functions

Review: In the last lab and lecture, we looked at functions like fold and make-list-fun, respectively. We won't write these on the board again in lab, but go ahead and look them over here:

   ;; list-of-alpha-fun : list-of-alpha -> ?? [template]
   ;;
   ;(define list-of-alpha-fun
   ;    (lambda (loa)
   ;        (cond [(empty? loa) ...]
   ;              [else         ...(first loa)
   ;                            ...(list-of-alpha-fun (rest loa))
   ;                            ...)])))

   ;; From lab.
   ;; fold : list-of-alpha beta (alpha beta -> beta) -> beta
   ;;
   (define fold
      (lambda (loa base combinef)
         (cond [(empty? loa) base]
               [else         (combinef (first loa)
                                       (fold (rest loa) base combinef))])))

   ;; From lecture.
   ;; make-list-fun : beta (alpha beta -> beta) -> (list-of-alpha -> beta)
   (define make-list-fun
      (lambda (base combinef)
         (local [(define answer-fun
                    (lambda (loa)
                       (cond [(empty? loa) base]
                             [else         (combinef (first loa)
                                                     (answer-fun (rest loa)))])))]
               answer-fun)))
These two functions are very similar -- make-list-fun is just the curried version of fold. Look at the following examples, think about how they work. (Your labby will ask you some questions, to make sure you know why sum1 and sum2 are both lambda-values.)
   ; Example functions to compute sum of a list-of-numbers.
   (define sum1 (lambda (lon) (fold lon 0 +)))
   (define sum2 (make-list-fun 0 +))

   ; Examples of *applying* those functions.
   (define sample-lon (list 3 76 123 435 -2))
   (sum1 sample-lon)
   (sum2 sample-lon)
   (fold sample-lon 0 +)
   ((make-list-fun 0 +) sample-lon)
Each is essentially the template list-of-alpha-fun with the blanks replaced with appropriate placeholders, wrapped as a function of those placeholders.

To do: Today's task is to do the same thing for accumulator-style. Start with the template, which we're already familiar with:

   ;; list-of-alpha-fun-acc: list-of-alpha ?? -> ?? [template]
   ;;
   ;(define list-of-alpha-fun-acc
   ;   (lambda (loa accum)
   ;      (cond [(empty? loa) accum]
   ;            [else         (list-of-alpha-fun-acc (rest loa)
   ;                                                 ...(first loa)...accum...)])))
  1. Write functions fold-acc and
  2. make-list-fun-acc. They should look a lot like the functions above.
  3. Now, use these functions to write sum-acc (the accumulator version of sum).
  4. Write my-reverse: list-of-alpha --> list-of-alpha using these functions. Do not use append to write my-reverse.

    If this stumps you, always make the abstract concrete: first try writing an accumulator version of my-reverse directly, without make-list-fun-acc. The design recipe still guides you to an answer! If you get stuck ask your labby...be sure to ask about the particular stpe of the design recipe you are stuck on: not ``Hey labby, what next?'', but something like ``Kind labby, what is the data-extraction step in this case?''.

  5. If you finish these examples early, here's one more extra exercise.)


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 similar to Scheme). 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.

You can move the cursor using the mouse (move the mouse and then click), using the arrow keys, or using keyboard commands. In the long run, you'll find the commands more useful because you don't have to move your hands as much. 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

Many options are available both from the pull-down menus and from keyboard commands. To start with, just use the menus. In the long run, you'll find the control-keys more useful for common operations.

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

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. You'll notice in Scheme-mode that it does some of the same formatting things that DrScheme does.

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, or to see different parts of the same file.

  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 menu. 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 Space, 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.

If you want a 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. If you need emacs help, either type ``M-x help'', or ``M-x doctor''.