已经消逝已久的对 lisp 的热情似乎又回来了,因为前些天摸索着居然写出了个 imgtag
出来,
于是重新开始看 elisp-intro
,做了两个第三章末尾的作业。以后做的作业阿笔记阿什么的,都贴这里。
The name of the cons function is not unreasonable: it is an abbreviation of the word `construct'. The origins of the names for car and cdr, on the other hand, are esoteric: car is an acronym from the phrase `Contents of the Address part of the Register'; and cdr (pronounced `could-er') is an acronym from the phrase `Contents of the Decrement part of the Register'. These phrases refer to specific pieces of hardware on the very early computer on which the original Lisp was developed. Besides being obsolete, the phrases have been completely irrelevant for more than 25 years to anyone thinking about Lisp. Nonetheless, although a few brave scholars have begun to use more reasonable names for these functions, the old terms are still in use. In particular, since the terms are used in the Emacs Lisp source code, we will use them in this introduction.
Two backslashes, `\\', are required before the parentheses and vertical bars: the first backslash quotes the following backslash in Emacs; and the second indicates that the following character, the parenthesis or the vertical bar, is special.
Bind some variables locally to particular values, and then evaluate the remaining arguments, returning the value of the last one. While binding the local variables, use the local values of variables bound earlier, if any.
- prog1 -> first - progn -> last
(defun leto-double (parameter) "a little function to double numbers" (interactive "p") (let ((number parameter) ) (message "%d's doubled value is %d" number (* number 2))))
(defun leto-check-with-fill-column (parameter) "compare the given number with fill-column" (interactive "p") (let ((number parameter) ) (if (< number fill-column) (message "compatible with current fill-column: %d" fill-column) (message "no kidding, that's far beyond what i can take."))))
(defun leto-simplified-end-of-buffer () "go to the end of current buffer." (interactive) (push-mark (point)) (goto-char (point-max)))
(defun leto-check-buffer (parameter) "check out whether the specified buffer exists or not." (interactive "BCheck it out: ") (let ((buf parameter)) (if (get-buffer buf) (message "buffer %s exists!" buf) (message "opps! buffer %s hasn't been created yet." buf)) ))
(defun leto-check-with-fill-column (&optional arg) "Compare the argument given with the value of fill-column. Use 56 as a default value is the argument is omitted. " (interactive "P") (let ((num (or (if arg ;; _if_ expression is essential, otherwise arg will ;; default to 1 if not assigned. (prefix-numeric-value arg)) 56) )) (if (< num fill-column) (message "%d is compatible" num) (message "no kidding."))))
(2008-09-11) Previous chapters are easy. Notice: the use of let is not necessary. See below as an example.
(defun check-fill-column (&optional num) "accepts a number and compare it with fill-column" (interactive "P") (or num (setq num 56)) (if (> num fill-column) (message "%d is greater than current fill-column value!" num) (message "%d is a little bit short." num)))
(defun leto-first-60 () "Return the beginning 60 characters of current buffer not regarding it is narrowed or not. " (interactive) (save-restriction (widen) (save-excursion (message "The first 60 characters of current buffer are: \n%s" (buffer-substring (goto-char (point-min)) 60) ;; _buffer-substring_ will use current buffer as the buffer being ;; processed. _substring_ need providing the major string arg. ) )))
(defun practise-fundamental () "shows the use of fundamental functions such as _cons_, _setcar_, _setcadr_. " (interactive) (message "cons birds: %s. \ncons birds to itself: \n%s. after setcar: %s, birds: %s. \nafter setcdr: %s, \nbirds(actually fish): %s. " (setq birds (cons 'crow (cons 'magpie (cons 'swallow (cons 'lark ())))) ) (cons birds birds) (setcar birds 'shark) birds (setcdr birds '(cyprinoid goldfish dorado)) birds))
(2008-09-11) Actually the printed final result won't be acurate. Emacs
calculate the list (cons
, setcar
, setcdr
, etc.) thoroughly before print
it. function practice-fundamental does not demonstrate the process.
(defun leto-search-forward (str) "search for a string and echo \"Found!\" if succeed." (interactive "sSearch: ") (when (search-forward str nil nil 1) (message "Found!") ))
(defun leto-nth-kill-ring-element (arg) "get the nth element of the kill ring. And if not provided, the value of n is default to 3." (interactive "p") ;; If the first element is nil, as it is here, information ;; about error signal is not stored by another function. (condition-case nil ;; Tells the lisp interpreter what to do when all goes well. (let ((depth (or arg 3)) ) (when (< depth (length kill-ring)) (message "%s" (car (nthcdr depth kill-ring)))) ) ;; Tells the interpreter what to do with an error. (message "Don't go that far plz.") ) )
;; Press C-x C-e at the end of each line orderly. (setq flowers '(violet buttercup)) (setq more-flowers (cons 'lily (cons 'rose flowers))) (setcar flowers 'fish) more-flowers
(defun leto-check-kill-ring-length () "show the maximum length of kill-ring. Display a message when kill-ring is full." (interactive) (message "capacity: %d. current: %d. \n%s." kill-ring-max (length kill-ring) (if (< (length kill-ring) kill-ring-max) "not full yet." "Ah... has been fed up already.")))
(defun leto-nth (arg) "constructed in _nthcdr_ and _car_ to return the nth element of a list." (interactive "p") (message "%s" (car (nthcdr (1- arg) kill-ring))))
(defun triangle-square-while (row-num) "" (let ((sum 0)) (while (> row-num 0) (setq sum (+ sum (* row-num row-num))) (setq row-num (1- row-num))) sum))
(defun foo (row-num) "" (let ((total 1)) (while (> row-num 0) (setq total (* total row-num)) (setq row-num (1- row-num))) total))
(defun triangle-square-recursive (row-num) "" (if (<= row-num 1) 1 (+ (* row-num row-num) (triangle-square-recursive (1- row-num)))))
(defun foo-recursive (row-num) "" (if (<= row-num 1) 1 (* row-num (foo-recursive (1- row-num)))))
N/A
(defun leto-search-blank-line () "Search forward for blank line" (interactive) (push-mark) (if (re-search-forward "\n\\([ \t]*\n\\)+" nil t) (message "blank line found.") (message "nah"))) ;;; bound function to F11 for test efficiency. (global-set-key (kbd "<f11>") 'leto-search-blank-line)
This is the author's key to this exercise.
(defun the-the () "Search forward for for a duplicated word." (interactive) (message "Searching for for duplicated words ...") (push-mark) ;; This regexp is not perfect ;; but is fairly good over all: (if (re-search-forward "\\b\\([^@ \n\t]+\\)[ \n\t]+\\1\\b" nil 'move) (message "Found duplicated word.") (message "End of buffer")))
Chapter 13 - Exercise
change the pattern in count-words-region function to [.,;:!?]
.
1. For using parameter
only will be okay, the let
form is not essential.
2. n. 混合物, 杂曲, 混成曲, 杂集, 大杂烩