Gnu.Emacs.Lisp编程入门一书示例代码
最近两天看了Gnu.Emacs.Lisp编程入门一书,把书中的代码示例都写了出来,以及书中的练习题,供有需要的同志使用。
注意:分号后面内容是对应函数及表达式的结果。
:-)
;; Chapter 1: 列表处理 ; 在Lisp中,数据和程序都以同样的方式表示;也就是说,它们都是由空格分割、 ; 由括号括起来的单词、数字或者其他列表的列表。 ; 在一个列表中,列表的元素被称为“原子”,不可再被分割为更小的部分。列表 ; 中的原子是由空格一一分割的。 ; 双引号中的文本,不论是句子还是段落,都是一个单个原子,被称为串string ‘(this list includes "text between quotation marks.") ;(this list includes "text between quotation marks.") ; 列表中的空格数量是无关紧要的 ; 单引号,被称为一个引用。当单引号位于一个列表之前时,它告诉lisp不要对 ; 这个列表做任何操作,而仅仅是按其原样。 ; 但是,如果一个列表前没有引号,这个列表中的第一个符号就很特别了: ; 它是一条计算机要执行的命令(在Lisp中,这些命令被称作函数) (+ 2 2) ;4 ‘(this is a quoted list) ;(this is a quoted list) (this is a quoted list) ;Symbol‘s function definition is void: this ;; Lisp解释器 ; Lisp解释器对一个列表求值时它做些了什么:首先,它查看一下在列表前面是 ; 否有单引号。如果有,解释器就为我们给出这个列表。如果没有引号,解释器 ; 就查看列表的第一个元素,并判断它是否是一个函数定义。如果它确实是一个 ; 函数,则解释器执行函数定义中的指令。否则,解释器打印一个错误消息。 (+ 2 (+ 3 3)) ;8 ; 一个函数参量是函数后面的原子或者列表 kill-ring-max ;60 ;; Chapter 2: 求值实践 (buffer-name) "*scratch*" (buffer-file-name) "f:/Misc/elisp.org" (concat "abc" "def") "abcdef" (substring "The quick brown fox jumped." 16 19) "fox" (+ 2 fill-column) 72 ;; here is a problem (concat "The " (+ 2 fill-column) " red foxes.") (+) ;0 (*) ;1 (+ 3) ;3 (* 3) ;3 (+ 3 4 5) ;12 (* 3 4 5) ;60 ;(+ 2 ‘hello) (message "This message appears in the echo area!") "This message appears in the echo area!" (message "The name of this buffer is: %s." (buffer-name)) "The name of this buffer is: elisp.org." (message "The value of fill-column is %d." fill-column) "The value of fill-column is 70." (message "There are %d %s in the office!" (- fill-column 14) "pink elephants") "There are 56 pink elephants in the office!" (message "He saw %d %s" (- fill-column 34) (concat "red " (substring "The quick brown foxes jumped." 16 21) " leaping.")) "He saw 36 red foxes leaping." (set ‘flowers ‘(rose violet daisy buttercup)) flowers ; C-x C-e, aware the echo area->rose violet daisy buttercup ‘flowers ; aware the echo area->flowers (setq carnivores ‘(lion tiger leopard)) (setq trees ‘(pine fir oak maple) herbivorses ‘(gazelle antelope zebra)) ;; create an couner, increment by 1 (setq counter 0) (setq counter (+ counter 1)) ; press C-x C-e many times, will get many values counter ;; create an counter, increment by 2 (setq counter1 0) (setq counter1 (+ counter1 2)) ; press C-x C-e many times, will get many values (buffer-name) ;"elisp.el" ; only get the filename (buffer-file-name) ; "f:/Misc/elisp.el" ; get the filename and its path (current-buffer) ; #<buffer elisp.el> (other-buffer) ; #<buffer *Buffer List*> (switch-to-buffer (other-buffer)) (buffer-size) ; 1814 (point) (point-max) (point-min) ;; Chapter 3: 如何编写函数定义 ;; 当注册一个函数后,在函数的最后一个括号后,使用"C-x C-e"来使之生效。 ; Version 1 ;(defun multiply-by-seven (number) ; "Multiply NUMBER by seven." ; (* 7 number)) (multiply-by-seven 3) ; 21 ; Version 2 (defun multiply-by-seven (number) "Multiply NUMBER by seven." (interactive "p") (message "The result is %d" (* 7 number))) (multiply-by-seven 3) ; "The result is 21" (let ((zebra ‘stripes) (tiger ‘fierce)) (message "One kind of animal has %s and another is %s." zebra tiger)) ;"One kind of animal has stripes and another is fierce." (let ((birch 3) pine fir (oak ‘some)) (message "Here are %d variables with %s, %s, and %s value." birch pine fir oak)) ;"Here are 3 variables with nil, nil, and some value." (if (> 5 4) (message "5 is greater than 4!")) ;"5 is greater than 4!" ;; Version 1: type-of-animal (defun type-of-animal (characteristic) "Print message in echo area depending on CHARACTERISTIC. If the CHARACTERISTIC is the symbol ‘fierce‘, then warn of a tiger." (if (equal characteristic ‘fierce) (message "It‘s a tiger!"))) (type-of-animal ‘fierce) ; "It‘s a tiger!" (type-of-animal ‘zebra) ; nil (if (> 4 5) ; if-part (message "5 is greater than 4!") ; then-part (message "4 is not greater than 5!")) ; else-part ;; Version 2: type-of-animal (defun type-of-animal (characteristic) ; Second version "Print message in echo area depending on CHARACTERISTIC. If the CHARACTERISTIC is the symbol ‘fierce‘, then warn of a tiger; else say it‘s not fierce." (if (equal characteristic ‘fierce) (message "It‘s a tiger!") (message "It‘s not fierce!"))) (type-of-animal ‘fierce) ;"It‘s a tiger!" (type-of-animal ‘zebra) ;"It‘s not fierce!" (if 4 ‘true ‘false) ;true (if nil ‘true ‘false) ;false (> 5 4) ;t (> 4 5) ;nil (let ((foo (buffer-name)) (bar (buffer-size))) (message "This buffer is %s and has %d characters." foo bar)) ;"This buffer is elisp.el and has 3811 characters." (message "We are %d characters into this buffer." (- (point) (save-excursion (goto-char (point-min)) (point)))) (emacs-version) ; "GNU Emacs 24.4.1 (x86_64-w64-mingw32) of 2014-10-21 on KAEL" (substring (emacs-version) 10 12) ;"24" ;; Problem (if (string= (int-to-string 24) (substring (emacs-version) 10 12)) (message "This is version 24 Emacs") (message "This is not version 24 Emacs")) (defun is-larger-fill-column (number) "This function" (if (> number fill-column) (message "%d is larger than fill-column!" number) (message "%d is not greater than fill-column!" number))) ;; Exercise 1: (defun two-times (number) "multi number by 2" (message "multi %d by 2 is: %d" number (* 2 number))) (two-times 2) ;"multi 2 by 2 is: 4" (two-times 25) ;"multi 25 by 2 is: 50" (defun two-times (number) "multi number by 2" (interactive "p") (message "multi %d by 2 is: %d" number (* 2 number))) ;; Exercise 2: (is-larger-fill-column 23) ;"23 is not greater than fill-column!" (is-larger-fill-column 73) ;"73 is larger than fill-column!" ;; Chapter 4: 与缓冲区有关的函数 ; beginning-of-buffer (defun simplified-beginning-of-buffer () "Move point to the beginning of the buffer; leave mark at previous position." (interactive) (push-mark) (goto-char (point-min))) ;; Chapter 5: 更复杂的函数 ;; Chapter 6:变窄和增宽 ;; Chapter 7: 基本函数:car、cdr、cons ; cons -> construct ; car -> Contents of the Address part of the Register (寄存器地址的部分内容) ; car 返回列表的第一个元素, ; cdr -> Contents of the Decrement part of the Register (寄存器后部内容) ; cdr 返回第一个元素后的所有内容 (car ‘(rose violet daisy buttercup)) ; output: rose (cdr ‘(rose violet daisy buttercup)) ; output: (violet daisy buttercup) (car ‘((lion tiger cheetah) (gazelle antelope zebra) (whale dolphin seal))) ; output: (lion tiger cheetah) (cdr ‘((lion tiger cheetah) (gazelle antelope zebra) (whale dolphin seal))) ; output: ((gazelle antelope zebra) (whale dolphin seal)) (cons ‘pine ‘(fir oak maple)) ; output: (pine fir oak maple) (cons ‘buttercup ()) ; (buttercup) (cons ‘daisy ‘(buttercup)) ; (daisy buttercup) (cons ‘violet ‘(daisy buttercup)) ; (violet daisy buttercup) (cons ‘rose ‘(violet daisy buttercup)) ; (rose violet daisy buttercup) (length ‘(buttercup)) ; 1 (length ‘(daisy buttercup)) ; 2 (length (cons ‘violet ‘(daisy buttercup))) ; 3 (length ()) ; 0 (length ) ; (wrong-number-of-arguments length 0) (cdr ‘(pine fir oak maple)) ; (fir oak maple) (cdr ‘(fir oak maple)) ; (oak maple) (cdr ‘(oak maple)) ; (maple) (cdr ‘(maple)) ; nil (cdr ‘nil) ; nil (cdr ()) ; nil (cdr (cdr ‘(pine fir oak maple))) ; (oak maple) (nthcdr 2 ‘(pine fir oak maple)) ; (oak maple) (nthcdr 0 ‘(pine fir oak maple)) ; (pine fir oak maple) (nthcdr 1 ‘(pine fir oak maple)) ; (fir oak maple) (nthcdr 3 ‘(pine fir oak maple)) ; (maple) (nthcdr 4 ‘(pine fir oak maple)) ; nil (nthcdr 5 ‘(pine fir oak maple)) ; nil (setq animals ‘(giraffe antelope tiger lion)) ; (giraffe antelope tiger lion) animals ; (giraffe antelope tiger lion) (setcar animals ‘hippopotamus) ; hippopotamus animals ; (hippopotamus antelope tiger lion) (setq domesticated-animals ‘(horse cow sheep goat)) ; (horse cow sheep goat) domesticated-animals ; (horse cow sheep goat) (setcdr domesticated-animals ‘(cat dog)) ; (cat dog) domesticated-animals ; (horse cat dog) ;; Chapter 8: 剪切和存储文本 ;; Chapter 9: 列表是如何实现的 (setq bouquet ‘(rose violet buttercup)) ; (rose violet buttercup) (setq flowers (cdr bouquet)) ; (violet buttercup) (setq bouquet (cons ‘lilly bouquet)) bouquet (eq (cdr (cdr bouquet)) flowers) ;t (setq flowers ‘(violet buttercup)) ;(violet buttercup) (setq flowers (cons ‘liu flowers)) ;(liu violet buttercup) (setq flowers (cons ‘laven flowers)) ;(laven liu violet buttercup) (setq more-flowers flowers) more-flowers ;(laven liu violet buttercup) (setq myfish (car flowers)) ;laven more-flowers ;(laven liu violet buttercup) ;; Chapter 10: 找回文本 kill-ring-max ;60 (car kill-ring) ;#("(car kill-ring)" 0 15 (fontified t)) (car (nthcdr 0 kill-ring)) (car (nthcdr 1 kill-ring)) (car (nthcdr 2 kill-ring)) (car (nthcdr 3 kill-ring)) (car (nthcdr 4 kill-ring)) ;; Chapter 11: 循环和递归 (setq animals ‘(giraffe gazelle lion tiger)) animals (defun print-elements-of-list (list) "Print each elements of LIST on a line of its on." (while list (print (car list)) (setq list (cdr list)) ) ) (print-elements-of-list animals) ; ;giraffe ; ;gazelle ; ;lion ; ;tiger ;nil (defun triangle (number-of-rows) "Add up the number of pebbles in a triangle. The first row has one pebble, the second row two pebbles, the third row three pebbles, and so on. The argument is NUMBER-OF-ROW." (let ((total 0) (row-number 1)) (while (<= row-number number-of-rows) (setq total (+ total row-number)) (setq row-number (1+ row-number))) total)) (triangle 4) ;10 (triangle 7) ;28 ;; Chapter 12: 正则表达式查询
本文出自 “天道酬勤” 博客,请务必保留此出处http://lavenliu.blog.51cto.com/5060944/1614126
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。