More first-class functions, emacs basics,
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...)])))
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?''.
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.
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.
keystroke | meaning |
---|---|
Backspace | delete previous character |
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 |
keystroke | meaning |
---|---|
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 o | other-window |
C-x 0 | delete-window (not the associated file) |
C-x C-c | quit 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.
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''.