GNU.EMACS.LISP Programming Primer Sample Code

Source: Internet
Author: User

In the last two days, I read the GNU.EMACS.LISP programming primer, wrote the code examples in the book, and the exercises in the book for the needy comrades to use.

Note: The content behind the semicolon is the result of the corresponding function and expression.

:-)

;;  Chapter 1:  list processing;  in Lisp, both the data and the program are represented in the same way, that is, they are separated by spaces,;  by parentheses, and lists of words, numbers, or other lists.   In a list, the elements of the list are called "atoms" and can no longer be split into smaller parts. The atoms in the list;  are separated by a space of one by one.;  the text in double quotes, whether sentence or paragraph, is a single atom, called String ' (this list includes  "text  between quotation marks. ");( this list includes  "Text between quotation marks."); The number of spaces in the   list is insignificant;  single quotation marks, which are referred to as a reference. When the single quotation mark precedes a list, it tells Lisp not to do anything about the;  list, but just as it is.;  However, if there is no quotation mark in front of a list, the first symbol in the list is very special:;  It is a command to be executed by a computer (in Lisp, these commands are called functions) (+&NBSP;2&NBSP;2); 4 ' (this is a quoted list);(This is  a quoted list) (this is a quoted list); Symbol ' s function definition is void: this;; &NBSP;LISP interpreter; What does the &NBSP;LISP interpreter do when it evaluates a list: first, it looks at the front of the list is;  no single quotes. If so, the interpreter gives us this list. Without the quotation marks, the interpreter;  the first element of the list and determines whether it is a function definition. If it is indeed a;  function, the interpreter executes the instruction in the function definition. Otherwise, the interpreter prints an error message. (+ 2  (+ 3 3));8;  A function parameter is the atom behind the functionor list kill-ring-max;60;;  Chapter 2:  Evaluation Practice (Buffer-name) "*scratch*" (buffer-file-name) "f:/misc/elisp.org" (concat  "abc")   "Def") "ABCdef" (substring  "the quick brown fox jumped.") &NBSP;16&NBSP;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 valuescounter;;  create an counter, increment by 2 (setq counter1 0) (setq counter1   (+&NBSP;COUNTER1&NBSP;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:  How to Writefunction definition;;   When a function is registered, after the last parenthesis of the function, use "C-X&NBSP;C-E" to make it effective.; 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&NBSP;ARE&NBSP;%D&NBSP;CHARACTERS&NBsp;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&NBSP;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:  buffer-related functions;  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:  more complex functions;;  chapter 6: narrowing and widening;;  Chapter 7:  basic functions: Car, CDR, cons; cons -> construct; car ->  contents of the address part of the register  (partial contents of the Register address);  car   Returns the first element of a list,; cdr -> contents of the decrement part of  the register  (post-register content); cdr  returns all content after the first element (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)) &NBSP;;&NBsp;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)   ; hippopotamusanimals ;  (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:  cut and store text;; How the  Chapter 9:  list is implemented (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)); lavenmore-flowers; (Laven liu violet buttercup);  Chapter 10:  Retrieve text 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:  loops and recursion (SETQ&NBSP;ANIMALS&NBSP; ' (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) ; ten (triangle 7); 28;;  Chapter 12:  Regular Expression queries


This article is from the "Tiandaochouqin" blog, make sure to keep this source http://lavenliu.blog.51cto.com/5060944/1614126

GNU.EMACS.LISP Programming Primer Sample Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.