The nature of LISP

Source: Internet
Author: User
Document directory
  • Review XML
  • Review ant
  • Getting closer to lisp
  • Hello, LISP
  • Lisp macro

Http://www.cnblogs.com/Leap-abead/articles/762180.html

I admire it very much. The patience of the author can write such a long article, although some of them are too arrogant.

I also admire it. the imagination and profound understanding of the technical nature of the author can equate obscure lisp syntax with XML.

 

Review XML

XML is a standardized syntax. It expresses any hirearchical data in a format suitable for reading. It is actually a tree structure.

Any data that can be expressed in a tree can also be expressed in XML, and vice versa.

Source codeIt is also used after resolutionTree StructureAny compiler will parse the source code into an abstract syntax tree. Such a representation is appropriate because the source code is hierarchical:
A function contains parameters and code blocks. The code quickly contains expressions and statements, and statements include variables and operators.

 

So the conclusion is:Any source code and XML can be converted to each other.. We can use XML as a general storage method for source code. In fact, we can generate a complete set of programming languages that use the unified syntax, write the converter, and convert existing code into XML format. If you really adopt
In this way, compilers in various languages do not need to write their own Syntax Parsing. They can directly use XML Syntax Parsing to directly generate an abstract syntax tree.

<define-function return-type="int" name="add">    <arguments>        <argument type="int">arg1</argument>        <argument type="int">arg2</argument>    </arguments>    <body>        <return>            <add value1="arg1" value2="arg2" />        </return>    </body></define>

The above XML and the following code can be converted to each other...

int add(int arg1, int arg2){    return arg1+arg2;}

 

This example also provides an intuitive and simple explanation of the concepts that were previously considered difficult to understand.Code is also dataAnd always.

 

Review ant

How does ant work? The principle is very simple. Ant handed over an XML file containing constructor commands to a Java program to parse each element, which is much simpler than I said.

A simple XML command will cause the Java class with the same name to be loaded and execute its code.

<copy todir="../new/dir">    <fileset dir="src_dir" /></copy>

Why are the advantages of using XML instead of Java code to describe the construction of commands?

Why don't I write a set of Java classes and provide APIs to meet basic tasks (copy directories, compile, and so on), and then directly call the code in Java? This can still ensure portability and scalability.

In terms of semantics structuring, XML elasticity is beyond the reach of Java.

To put it bluntly, XML can be used to modify the schema at will. You want to add a tag, such as copy, which is very simple. for Java, it is very difficult to add a keyword, because the protocol and schema of Java are fixed.

So if you use Java to write this simple copy, it will not look so good, because you cannot create a copy command, as shown below:

copy("../new/dir");{    fileset("src_dir");}
You can't write and execute it like this. You must write it like this and implement it using the existing schema of Java...
CopyTask copy = new CopyTask();Fileset fileset = new Fileset();fileset.setDir("src_dir");copy.setToDir("../new/dir");copy.setFileset(fileset);copy.excute();

For complex operators, the benefits of doing so are obvious. For example, you can use a specific operator to check out source code, compile files, perform unit tests, and send emails.

For specific questions, such as constructing software projects, the use of these operators can greatly reduce the number of less code.Increase code clarity and reusability.

Explanatory XML can easily achieve this goal. XML is a simple data file that stores hierarchical data. In Java, the hierarchy is fixed (you will soon see that the lisp situation is completely different ), we cannot achieve this goal. Maybe this is exactly where ant is successful.

Java cannot perform metaprogramming, that is, it cannot implement DSL (domain-specific language), that is, it cannot expand schema. So ant uses XML (ant is a project-compiled DSL) to create DSL.

Ant needs to parse XML and match the XML tag with the implemented Java function. Although it can also be implemented, it is a little troublesome.
Imagine if Java itself supports metaprogramming, this problem is not quite simple...

 

Getting closer to lisp

As mentioned above, Java itself cannot be extended, and ant xml tag is constantly extended through Java implementation, which is quite troublesome.

Can the ant xml tag itself be used to implement tag extension?We first implement a bunch of core ant-XML tags, and then we can expand other new tags based on these tags. Of course this is acceptable.

This idea is cool, but not practical.

The reason is thatXML is cumbersome.. This problem is not big for data, but if the code is cumbersome, simply typing is enough to offset its benefits.

 

To solve this problem, we shouldSimplified writing. Note: XML is just a way to express hierarchical data.

We do not need to use angle brackets to get the serialization result of the tree. We can use other formats. One of these formats (which is exactly what lisp uses) is calledS expression.

The above code is translated into the s expression, which is indeed greatly simplified.

(copy     (todir "../new/dir")    (fileset (dir "src_dir")))

The s expression is easier to use. When I read the s expression (LISP) for the first time, is brackets annoying? Now that we understand the truth behind it, it becomes much easier at once. At least, it is much better than XML. Writing code with the s expression is not only practical, but also pleasant.

The s expression is actually a nested list, as shown below:

(Copy (todir "../New/DIR") (fileset (DIR "src_dir ")))

I hope you will not be confused, becauseNested list and tree are actually the same thing. The literal meaning of lisp is table processing (List ProcessingIn fact, it can also be called tree processing, which is no different from processing XML nodes.

The conclusion is that lisp can be simple schema extension and meta-programming like XML to facilitate DSL opening.

At the same time, lisp is executable code like Java, so it does not need to parse XML like ant, and then execute it in Java. LISP meta-programming can be directly executed.

In addition, LISP uses the list (s expression). Although it is essentially the same as the XML expression, it is much more concise and more suitable for coding programmers.

Hello, LISP

So far, what we know about lisp can be summarized as one sentence:LISP is an executable XML with better syntax.

What isList)? You may have heard many related statements. List, in a word, is to use the s expression to represent data blocks like XML. List is enclosed by a pair of parentheses. Elements in list are separated by spaces. List can be nested.

When the lisp system encounters such a table, what it does is very similar to what ant processes XML data, that is, trying to execute them. In fact, the lisp source code is a specific table, just like the ant source code is a specific XML.

The sequence of running the lisp table is as follows. The first element of the table is treated as a function, and other elements are treated as function parameters. If a parameter is also a table, evaluate the value of the table according to the same principle, and pass the result to the original function as the parameter. This isBasic Principles.

(* 3 4); Calculate 3 by 4

In the preceding example, all tables are processed as code.How to process a table as dataWhat about it?

In lisp, we addPrefix'To represent data.

(Set test' (1 2); the value of test is a two-element table (set test (1 2); error, 1 is not a function

 

Lisp macro

Lisp uses macro (macro) for metaprogramming. For example,

Suppose we want to write a management program for the task table and store the task table data in a group of files. When the program starts, the data is read from the file and displayed to the user.

In other languages (such as Java), what should I do with this task? We will parse the XML file, get the task table data, write code to traverse the XML tree, convert it to the Java data structure, and then display the data to the user.

What should we do if we use lisp?

    <todo name = "housework">        <item priority = "high">Clean the hose</item>        <item priority = "medium">Wash the dishes</item>        <item priority = "medium">Buy more soap</item>    </todo>

First, in LISP, we can use a more concise s expression.

   (todo "housework"        (item (priority high) "Clean the house")        (item (priority medium) "Wash the dishes")        (item (priority medium) "Buy more soap"))
For lisp, it is very easy to read and parse the s expression directly, because it can be expressed by the s expression for lisp regardless of code and data.
But I understand that this layer does not understand the true meaning of data as code...
 
We can use macros to do more surprising things. Yes, we can make the data itself executable.
Macros work in a similar way as functions. The main difference is that,Macro parameters do not have any value at the time of Substitution.
(Macro-Name (+ 4 5 ))
So here I will introduce the macro of C in the above sections, which is very similar, but the difference is that the macro of C is not C, and the function is very weak.
Process-oriented programming is also based on functions on the surface, but it is essentially different from lisp.

For (item (priority high) "clean the house"), although it is a piece of data, in LISP, we can also regard it as a piece of code, which is equivalent to item (priority, note)

That is, as long as we implement the item, we can directly execute this data, without additional code.

 (defmacro item (priority note)        `(block             (print stdout tab "Prority: " ~(head (tail priority)) endl)            (print stdout tab "Note: " ~note endl endl)))

As I mentioned above, this is similar to ant's use of Java to implement the tag in XML. For example, copy is no different, but it is very convenient to use LISP to extend lisp. unlike ant, C can only use a simple pre-compiled language.

We have created a very small and limited language to print task tables. This language is used only to solve problems in specific fields.Dsls (specific domain language, or specialized domain language)

So for the same piece of data, we only need to load different DSL to have completely different interpretations and logic...

Use DSL to solve problems, simplify the program, make it easy to maintain, and be flexible.

In Java, we can use classes to handle problems. The difference between the two methods is that lisp makes us abstract at a higher level and we are no longer limited by the Language parser itself, compare the differences between the construction scripts written directly in the Java Library and the construction scripts written with ant.

Can I say,LISP is a concise and executable XML

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.