Emacs LISP Language

Source: Internet
Author: User
ArticleDirectory
    • History of LISP
    • Table Processing
    • Atom
    • Execution Program
    • Evaluate sub-tables
    • Variable
    • Parameters
    • Assign values to variables
    • Function
    • Special form of "Let"
    • Special form of "if"
    • Basic functions
    • List Implementation
    • Loop and Recursion
From:

Http://www.neu.edu.cn/cxsj/online/C1/Lisp%E8%AF%AD%E8%A8%80.htm

Emacs LISP Language an introduction to programming in Emacs LISP

In Emacs, press "C-H I" and enter "M, emacs lisp Intro. I picked out the basic part and translated it.

History of LISP

The LISP Language was first developed by the Massachusetts Institute of Technology (MIT) at the end of 1950s to study artificial intelligence. The strength of the lisp language makes it show its advantages in other aspects, such as compiling and editing commands and integrating environments. GNU Emacs lisp was developed mainly by maclisp, which was written by MIT in 1960s. It inherits Common LISP to some extent, while Common Lisp became a standard in 1980s. However, emacs lisp is much simpler than common lisp.

Table Processing

Lisp stands for list processing, that is, table processing.Programming LanguageIt is used to process the list consisting of parentheses (I .e. "(" and. Parentheses mark the boundary of the table. Sometimes an underscore (') is added before a list, or a single quotation mark ). List is the basis of the lisp language. In lisp, a list looks like this: "'(Rose VILET Daisy Buttercup )". You can also write the following form:

 
'(Rose
Viotlet
Daisy
Buttercup)

This looks more familiar. The elements in this table are the names of the four flowers: Rose, viotlet, Daisy, and Buttercup. The list can also contain numbers, such as "'(1 2 3. In lisp, data andProgramThey are represented in the same way, that is, they are all composed of words, mathematics, or other tables, separated by spaces in the middle, and parentheses are added on both sides. Because a program looks like data, it can be easily used as data elsewhere. This is a very powerful feature of the lisp language. Sentences with semicolons (;) and periods (.) in parentheses are not a list. The following is a list of other formats:

 
'(This list has (a list inside of it ))

The elements of this table include this, list, has, and a sub-table (a list inside of it ). This sub-table consists of elements a, list, inside, of, and it.

Atom

In lisp, words are called "atoms" because atoms are inseparable. A list can be composed of one or more atoms, or a list without any atoms, such as "()" or "nil ", it is called "empty table ". An empty table can be viewed as either an atom or a list. Both atoms and lists are called symbol expressions ". In addition, text marked with double quotes (or even sentences or paragraphs) is also an atom, such:

 
'(This list includes des "text between quotation marks .")

This form of atom is also called "text between quotation marks.

Execution Program

A table in lisp is an executable program. If you run it (in LISP terminology, it is "evaluate"), the computer will execute one of the following three conditions: first, nothing, just return the List itself, the second is to return the error message, and the third is to use the first symbol in the list as a command for some operations. If "'" is added before a list, the first case is executed when the computer processes the list. If there is no "'" before the list, the first item in the table has special significance, it is a command (in LISP, these commands are called functions ). For example:

 
(+ 2 3)

The result of this list execution (in terms of "evaluate the List") is 5, because the first item in the table is "+", indicating the sum of the remaining elements in the table. If you want to run the command in Emacs, you only need to move the cursor to the right bracket and then execute "C-X c-e". In the echo area, the result "5" is displayed ". This is the process of delivering commands to a program (known as the "lisp Interpreter" in Emacs.

Evaluate sub-tables

If you want to evaluate a list that contains another sub-table, the external list can evaluate the value returned by the internal sub-table. This also explains why Internal expressions are evaluated first, because these return values need to be used by external expressions. For example:

(+ 2 (+ 3 4 ))

Run the previous list and you can get result 9 in the echo area, because the lisp interpreter evaluates the internal expression "(+ 3 4)" first, and obtains 7, the external expression "(+ 2 7)" is evaluated to 9.

Variable

In Emacs lisp, a symbol can refer to a value or a function definition. There is a difference between the two. function definition is a set of commands that a computer needs to execute, while a value is different from other things, such as a number or a name. A symbolic value can be any lisp expression, also known as a variable ". A symbol can also refer to a value and a function definition at the same time.

Parameters

Parameter refers to the information that needs to be submitted to the function. Different functions require different numbers of parameters, and some functions do not even need parameters. For example:

 
(Concat "ABC" "def ")

In the list, the "Concat" function connects two or more strings to form a string. The parameter contained in this function is a string, and the list is evaluated as "abcdef ". Some functions such as "Concat", "+", or "*" can have any number of parameters. For example, there is no parameter (that is, 0 parameters ):

 
(+)
(*)

Evaluate the two expressions and obtain 0 and 1 respectively. Like this, there is also the "message" function, which is used to send messages to users, such:

 
(Message "My name is % s and I am % d years old." "Xiaodong Xu" 25)

Calculate "My name is Xiaodong Xu and I am 25 years old." For the above table .". "% S" in double quotation marks "is not directly displayed, but is searched for parameters following the string, it evaluates the second parameter "Xiaodong Xu" and displays its value at the location of "% s. Similarly, "% d" is replaced by the third integer parameter 25.

Assign values to variables

There are several ways to assign values to variables. One is to use the "set" or "setq" function, and the other is to use the "let" function. In this process, the number of operations is to "bind" a value to a variable. The "set" function can be assigned as follows:

 
(Set 'flowers' (Rose viletdaisy Buttercup ))

When you evaluate the value in the preceding table, the "(Rose viletting Daisy Buttercup)" list will be displayed in the echo area, which is the value returned by the "set" function. As an incidental effect, the symbol "Flowers" is bound to this list, that is, the variable "Flowers" is assigned a value to this list. In addition, this process also illustrates a incidental effect of the lisp interpreter, that is, the value assignment, which can be the main effect we expect. In practical applications, the first parameter of almost every value assignment statement must be appended with an underscore ('). This is a common case, set and the "'" before the first parameter constitute a specific form of "setq", such:

 
(Setq carnivores '(lion tiger leopard ))

This follows

 
(Set 'carnivores' (lion tiger leopard ))

It is equivalent.

"Setq" can also be used to assign different values to different variables. The first parameter is assigned the value of the second parameter, and the third parameter is assigned the value of the fourth parameter, similarly, if you want to assign a group of "Trees" to the "Trees" symbol and assign a group of "herbivores" to the "herbivores" symbol, you can do this:

 
(Setq trees '(pine fir oak maple)
Herbivores '(gazelle antelope zebra ))

Here, the word "assign value" is used. Another way of thinking is "setq", which points to this list. The latter is very common.

Function

Except for some "Basic" functions written in C, all functions are defined based on other functions. When defining a function, you can use other functions in the function body, some of which are written in Emacs lisp and some are basic functions written in C. The purpose of writing in C is to make GNU Emacs easier to run on any computer with C language conditions. However, there is no need to distinguish between functions written in C and those written in Emacs lisp. A function consists of five parts: 1. The name of the function symbol. 2. List of parameters to be passed to the function. If there is no parameter, it is an empty table "()". 3. Documentation used to describe functions (technically optional, but it is strongly recommended to write ). 4. The expression that allows the function to interact with people (that is, the function name can be added with "M-X", or the function can be called with an appropriate key or key combination). Optional. 5.Code, That is, the function body.

The function definition can be written as the following template:

 
(Defun function-Name (arguments ...)
"Optional-documentation ..."
(Interactive argument-passing-Info); optional
Body ...)

For example, define a function multiplied by 7:

 
(Defun multiply-by-seven (number)
"Multiply number by seven ."
(* 7 number ))

After this table is evaluated, we load the "Multiply-by-seven" function into Emacs. Execute the following statement:

 
(Multiply-by-seven 3)

Expected result 21 is displayed in the echo area.

Special form of "Let"

The "let" expression is a special form of the lisp language and is used in most function definitions. "Let" is used to prevent confusion. It creates a "local variable" name that overwrites all variables with the same name outside the "let" expression. The local variables created by the "let" expression are retained only within the "let" expression and have no effect on the external. "Let" can create multiple variables at a time and create an initial value for each variable. The initial value can be a specific value or null ("nil "). After "let" creates a variable, the code in the "let" body is executed and the value of the last expression is returned. The "let" expression is a list composed of three parts: The first part is the symbol "let", and the second part is a list called "Variable list ", each element can be a symbol or a binary list (the first element is a symbol), and the third part is the "let" expression body. The following template can be used to describe the "let" expression:

 
(Let varlist body ...)

If the Variable list is composed of a list of binary groups (which is common in this case), the "let" expression template can be written as follows:

 
(Let (variable value)
(Variable value)
...)
Body ...)

The following is an expression for creating and initializing two variables "ZEBRA" and "Tiger". The "let" expression is a list of calls to the "message" function.

 
(Let (zebra 'stripes)
(Tiger 'fierce ))
(Message "one kind of animal has % s and another is % S ."
Zebra Tiger ))
Special form of "if"

In addition to "defun" and "let", there are also conditions "if ". This form is used by computers for judgment. The basic meaning behind "if" is: "If an expression is true, it is evaluated ". If the value is false, the expression is not evaluated. The judgment and execution part is the second and third parts of the "if" list, while the first element is "if ". However, the judgment part of the "if" expression is often referred to as the "If part", while the execution part is often referred to as the "then part ". The "if" expression can also contain the third optional parameter, namely the "else part", to determine whether the expression is false. In this case, the then part is not evaluated, and the else part is evaluated. The "if" expression can be written as the following template:

 
(If true-or-False-test
Action-to-carry-out-if-the-test-returns-true
Action-to-carry-out-if-the-test-returns-false)

For example,

 
(If (> 4 5); If-part
(Message "5 is greater than 4! "); Then-part
(Message "4 is not greater than 5! "); Else-part

If it is false, "nil" is returned ". It is worth noting that "nil" has two meanings in Emacs LISP: one stands for empty tables, the other stands for false, and any non-null value is considered true.

Basic functions

In lisp, "car", "CDR", and "cons" are basic functions. The "cons" function is used to build the list, and the "car" and "CDR" functions are used to split the list. The car of a table is the first item of the table, such:

 
(Car '(Rose viletdaisy Buttercup ))

Run this expression to get "Rose ". Similarly, the CDR of a table is the remainder of the table, that is, the "CDR" function returns the part after the first item of the table. For example

 
(CDR '(Rose VILET Daisy Buttercup ))

Run the command to obtain "(viletting Daisy Buttercup )". Like "car", "CDR" does not modify or delete elements from the list, but only returns a value. This is a very important feature. The "cons" function is opposite to "car" and "CDR". For example, "cons" can form a quad-element table from a three-element table:

 
(Cons 'pine' (FIR oak maple ))

After execution, the result "(pine fir oak maple)" is obtained in the echo area )". "Cons" generates a new list. The element "pine" in this list is followed by the element "(FIR oak maple)" in the original list )". It can be said that "cons" puts a new element at the beginning of a table, or presses it above a table to generate a new list without changing the value of the original list. The "length" function can obtain the number of elements in a table, for example:

 
(Length (cons 'viole' (Daisy Buttercup )))

Because (cons 'viole' (Daisy Buttercup) returns a three-element list, result 3 is displayed in the preceding table. The length of an empty table is 0. The "nthcdr" function is related to the "CDR" function. What it does is to repeatedly execute "CDR" for a list multiple times. For example:

 
(Nthcdr 2' (pine fir oak maple ))

The result "(oak maple)" is displayed )". The "nth" function returns the nth element of a list, starting from 0, for example:

 
(Nth 1' ("one" "two" "three "))

The result "two" is displayed ". "Setcar" and "setcdr" functions are similar to "car" and "CDR" functions, but they change the values of the original list.

List Implementation

In lisp, atoms are recorded directly. If they are not directly recorded, they are theoretically direct. Let alone a table. It consists of a pointer pair sequence. In this sequence, the first pointer of each pointer pair points to an atom or another list, the second Pointer Points to another pointer pair, or to an empty symbol ("nil ", used to mark the end of the list ). A pointer itself is the address of the content it points to, so a table is a sequence of addresses. For example, the implementation of the "(Rose viotlet Buttercup)" list can be expressed in a simple way:

 
__________________
| ___ | --> Nil
|
|
--> Rose --> Violet --> buttercup

When a variable is assigned to a list by a function such as "setq", it stores the address of the first rectangle. Therefore

 
(Setq bouquet '(Rose viletbuttercup ))

The result can be expressed as follows:

 
Bouquet
|
| __________________
--> | ___ | --> Nil
|
|
--> Rose --> Violet --> buttercup

Based on the previous concepts, a single symbol can be regarded as a drawer, expressed:

Chest of drawers contents of drawers

_ O0o0o __
/\
---------------------
| Directions to | [map to]
| Symbol name | bouquet
|
+ --------------------- +
| Directions to |
| Symbol definition | [none]
|
+ --------------------- +
| Directions to | [map to]
| Variable value | (Rose viletbuttercup)
|
+ --------------------- +
| Directions to |
| Property list | [not described here]
|
+ --------------------- +
|/\ |
Loop and Recursion

Emacs lisp has two main methods to repeatedly evaluate values: one is to use a "while" loop, and the other is to use "recursion ". The "while" expression has the following forms:

Simple Form

 
(While true-or-False-test
Body ...)

Null table judgment cycle

 
(While test-whether-list-is-empty
Body...
SET-LIST-TO-CDR-OF-LIST)

Incremental counting cycle

 
Set-count-to-Initial-Value
(While (<count desired-number); true-or-False-test
Body...
(Setq count (1 + count); incrementer

Reduction counting cycle

 
Set-count-to-Initial-Value
(While (> counter 0); true-or-False-test
Body...
(Setq counter (1-counter); decrementer

Recursive functions include the code that the lisp interpreter uses to call its own functions, but the parameters of these functions are slightly different. In terms of terms, there are different "entities ".

A simple recursive function is like:

(Defun name-of-recursive-function (argument-list)
"Documentation ..."
(If do-again-test
Body...
(Name-of-recursive-Function
NEXT-STEP-EXEXAMPLESSION )))

Another form is to use the "cond" function:

 
(Cond
(First-true-or-False-test first-consequent)
(Second-true-or-False-test second-consequent)
(Third-true-or-False-test third-consequent)
...)

If the first judgment condition returns NULL (false), the first judgment statement is skipped, and then the second judgment condition is evaluated, and so on. If a condition returns true, the statement is evaluated.

Updated: 2006-01 1 Index Edited by stid

Based on GNU/Linux | powered by | best view with

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.