[Zz] Why is the lisp language so advanced?

Source: Internet
Author: User

Http://www.cnbeta.com/articles/124369.htm

1. If we take the popularProgramming LanguageIn this order: Java, Perl, Python, and Ruby. You will find that the language that comes behind is more like lisp. Python imitates lisp, and even imitates what many lisp hackers think is a wrong design function. As for Ruby, if you go back to 1975, you claim that it is a lisp statement, and no one will oppose it. The current development of programming languages has just caught up with the level of Lisp in 1958.

II,

In 1958, John McCarthy designed the lisp language. In my opinion, the latest programming language just realized his vision in 1958.

How is this possible? Isn't the development of computer technology ever changing? How can the technology in 1958 surpass today's level?

Let me tell you why.

This is because John McCarthy was not planning to design lisp into a programming language, at least not in our current sense. His original intention was to make a theoretical calculation and define the Turing machine in a more concise way.

So why is the programming language of the 1950s s not outdated yet? Simply put, this language is essentially not a technology, but a mathematics. Mathematics is not outdated. Instead of associating the lisp language with the hardware in 1950s, you should integrate it with quicksort)AlgorithmAnalogy. This algorithm was proposed in 1960 and is still the fastest generic sorting method.

III,

The Fortran language emerged in the 1950s s and has been used since now. It represents a completely different direction of language design. Lisp was inadvertently developed from pure theory to programming language, while Fortran was designed as a programming language from the very beginning. However, today we regard lisp as a high-level language, while Fortran as a rather low-level language.

In 1956, when Fortran was born, it was called Fortran I, which is very different from today's Fortran language. Fortran I is actually an assembly language plus mathematics. In some aspects, it is not as powerful as today's assembly language. For example, it does not support sub-databases.Program, Only the branch jump structure (Branch ).

LISP and FORTRAN represent the development of programming languages. The former is based on mathematics, and the latter is based on hardware architecture. Since then, the two directions have been moving closer to each other. When lisp was designed, it was very powerful. In the next two decades, it improved its running speed. The so-called mainstream languages use faster running speed as the starting point of design, and then use more than 40 years to become more powerful step by step.

Today, the most advanced mainstream language is just close to the level of lisp. Although it is very close, it is still not as powerful as lisp.

IV,

When the lisp language was born, it contained nine new ideas. Some of them are used to today, while others have just appeared in other advanced languages. So far, there are two other types that are unique to lisp. According to the degree of public acceptance, these nine ideas are:

1. Condition structure (that is, the "if-then-Else" structure ). Now everyone thinks this is taken for granted, but Fortran I does not have this structure. It only has a goto structure based on the underlying machine commands.

2. A function is also a data type. In lisp, functions, like integers or strings, are also data types. It has its own literal representation, which can be stored in variables and passed as parameters. All functions of a data type are available.

3. recursion. LISP is the first advanced language that supports recursive functions.

4. Dynamic types of variables. In the lisp language, all variables are actually pointers, and the values are of different types, but the variables themselves do not. Copying variables is equivalent to copying pointers, rather than copying the data they point.

5. Garbage collection mechanism.

6. The program is composed of expressions. A lisp program is a set of expression blocks. Each expression returns a value. This is completely different from Fortran and most later languages. Their programs are composed of expressions and statements (statement.

Distinguishing expressions and statements is natural in fortran I, because it does not support nested statements. Therefore, if you need to use a mathematical formula to calculate a value, only this value is returned using an expression. No other syntax structure is available, because otherwise the value cannot be processed.

Later, the new programming language supported block structures, which of course did not exist. However, it is too late, and the distinction between expressions and statements is deep-rooted. It spreads from FORTRAN to the ALGOL language, and then to the successor languages of the two.

7. symbol type. A symbol is actually a pointer pointing to a string stored in a hash table. Therefore, to compare whether two symbols are equal, you only need to check whether their pointers are the same. You do not need to compare them one by one.

8.CodeA tree representation (notation) consisting of symbols and constants ).

9. The entire language is available at any time. Lisp does not really distinguish between the read period, compilation period, and runtime period. You can compile or run the Code during the read period, read or run the Code during the compilation period, and read or compile the Code during the runtime.

Run the Code during the read period so that you can re-adjust the (reprogram) lisp syntax. Running the Code during the compilation period is the basis of the work of the lisp macro; compiling and translating the Code during the runtime, this allows LISP to act as an extension language in programs such as Emacs. It reads code at runtime so that programs can communicate with each other using the S-expression, the emergence of XML format has recently led to the re-release of this concept.

V,

When the LISP Language emerged, its idea was quite different from that of other programming languages. The latter's design philosophy is mainly determined by the hardware in the later 1950s S. As time passes, popular programming languages are constantly updated, and language design ideas are gradually moving closer to lisp.

Ideology 1 to ideology 5 has been widely accepted. Ideology 6 has already appeared in mainstream programming languages. Idea 7 has been implemented in Python, but it does not seem to have a dedicated syntax.

Thought 8 may be the most interesting. It and IDEA 9 become part of the lisp language only by chance, because they are not part of John McCarthy's original ideas and are added by his student Steve Russell. These made lisp seem odd, but also became the most unique feature of this language. The odd form of lisp is not because its syntax is odd, but because it has no syntax at all, the program is expressed directly in the form of a parsing tree. In other languages, this form is generated only after parsing in the background, but lisp uses it as the expression form directly. It consists of the List, and the list is the basic data structure of lisp.

Using a language's own data structure to express the language proves to be very powerful. Thought 8 and thought 9 mean that you can write a program that can be programmed by yourself. This may sound weird, but it is not common for lisp. The most common practice is to use macros.

The term "macro" in lisp is different from that in other languages. Lisp macros are all-encompassing. They may be a scaling form of a certain expression or a compiler in a new language. If you want to really understand the lisp language or broaden your programming horizons, you must learn macros.

As far as I know, macros (defined in The LISP Language) are still unique to lisp. One reason is that to use macros, you probably have to make your language look as ancient as lisp. Another possible reason is that if you want to add this NLP weapon to your language, you cannot claim that you have invented a new language, but you can only say that you have invented a new dialect of lisp.

I told it as a joke, but this is the case. If you create a new language with functions such as car, CDR, cons, quote, Cond, atom, and EQ, you can also write functions as a list representation, then, based on these, you can completely export all the other parts of the lisp language. In fact, The LISP Language is defined in this way. John McCarthy designed the language to make this derivation possible.

VI,

Even if lisp really represents the current direction that mainstream programming languages are constantly approaching, does that mean you should use it to program?

If you use a language that is less powerful, how much do you suffer? Isn't it a wise choice to choose from the most advanced technology? If so many people use mainstream programming languages, doesn't that show their merits?

On the other hand, it doesn't matter which programming language you choose. Many projects can be completed in different languages. In general, the more demanding the project, the more powerful programming languages can play a role. However, numerous projects do not have strict restrictions. Most programming tasks may only need to write small programs and then use the glue language to connect these small programs. You can use programming languages you are familiar with, or use languages with the most powerful function libraries for specific projects to write these applets. If you only need to transmit data between Windows applications, Visual Basic can achieve the same purpose.

So what are the advantages of Lisp programming?

VII,

The more powerful the programming capability of a language, the shorter the program to be written (not the number of characters, but an independent syntax unit ).

The number of codes is very important, because the time required to develop a program depends mainly on the length of the program. If the code written in one language is three times longer than the code written in another language, it means that it takes three times more time to develop it. And even if you hire more people, it will not help reduce development time, because when the team size exceeds a certain threshold, increasing the number of people will only cause a net loss. Fred Brooks described this phenomenon in his famous book "The Mythical man-month". What I have seen proves his theory.

How short can a program be in The LISP Language? Taking the comparison between lisp and C as an example, most of the statements I have heard are that C code is 7 to 10 times the length of lisp. But most recently, there is an article about ita software in the new impact ect magazine.Article"A line of Lisp code is equivalent to 20 lines of C code", because this article references the president of ITA, so I think this number comes from the Programming Practice of ITA. If so, we can believe this sentence. The Ita software not only uses the lisp language, but also uses C and C ++ in large quantities at the same time, so this is their experience.

Based on the figure above, if you compete with ITA and you develop software using C language, the development speed of ITA will be 20 times faster than that of you. If you need to implement a function within one year, it only takes less than three weeks. Conversely, if a new feature has been developed for three months, it takes five years to complete.

Do you know? The comparison above only takes into account the best situation. When we only compare the number of codes, the implication is that we can develop the same software if we use a language with weak functionality. But in fact, there is a limit to what programmers can do in a certain language. If you want to use a low-level language to solve a difficult problem, you will be faced with a situation that is extremely complicated or even unclear.

So when I say that if you are competing with ITA and you have made something in five years, it takes only three months for ITA to complete it with the help of the lisp language, in the five years that I have been talking about, everything went well, there were no mistakes, and there were no major troubles. In fact, according to the actual situation of most companies, projects planned to be completed within five years may never be completed.

I admit, the above example is Taiji. Ita seems to have a group of very smart hackers, and C language is a very low-level language. However, in a highly competitive market, even if the development speed is only two or three times different, it is enough to keep you behind.

Appendix: programming capability

To explain the differences in programming capabilities, consider the following questions. We need to write a function that can generate the accumulators. That is, this function accepts the N parameter and returns the other function. The latter accepts the I parameter and then returns the N increment (increment) the value after I.

Common LISP is written as follows:

(Defun Foo (N) (lambda (I) (incf n I )))

Ruby is written almost identical:

Def Foo (n) Lambda {| I | n + = I} End

The write rules for Perl 5 are:

Sub Foo {My ($ n) =@_; sub {$ n + = Shift }}

This has more syntax elements than lisp and Ruby versions, because in Perl, You have to extract parameters manually.

The syntax of Smalltalk is a little longer than that of Lisp and Ruby:

Foo: N | S: = n. ^ [: I | S: = S + I.]

In smalltalk, the local variable (lexical variable) is valid, but you cannot assign values to a parameter. Therefore, you have to set a new variable to accept the accumulated value.

The javascript syntax is a little longer than that of Lisp and Ruby. Because JavaScript still distinguishes between statements and expressions, You need to specify the return statement to return a value:

Function Foo (n) {return function (I) {return N + = I }}

(Practically speaking, Perl also retains the differences between statements and expressions, but uses a typical Perl method for processing, so that you can omit return .)

If you want to change the version of Lisp/Ruby/perl/Smalltalk/JavaScript to Python, you will encounter some restrictions. Because Python does not fully support local variables, you have to create a data structure to accept N values. And although Python does support function data, there is no literal representation (literal representation) that can generate a function (unless the function body has only one expression ), so you need to create a name function and return it. The final statement is as follows:

Def Foo (n): S = [N] def bar (I): s [0] + = I return s [0] Return bar

Python users can reasonably question why it cannot be written as follows:

Def Foo (n): Return Lambda I: Return N + = I

Or:

Def Foo (n): Lambda I: N + = I

I guess python will support this method one day. (If you don't want to wait until Python gradually evolves to be more like lisp, you can always directly ......)

In an object-oriented programming language, you can simulate a closure to a limited extent (that is, a function that can reference the variables defined by the Code containing this function ). You define a class with a method and an attribute to replace all variables in the closed scope. This is similar to letting programmers perform code analysis on their own. Originally, this should be done by a compiler that supports local scopes. If there are multiple functions pointing to the same variable at the same time, then this method will become invalid, but in this simple example, It is enough.

Python experts also agreed that this is a good solution to this problem, written as follows:

Def Foo (n): Class Acc: def _ init _ (self, S): Self. S = s def Inc (self, I): Self. S + = I return self. s return ACC (n ). INC

Or

Class FOO: def _ init _ (self, n): Self. N = n def _ call _ (self, I): Self. N + = I return self. n

I added this section because I want to avoid a python enthusiast saying that I misunderstand this language. However, in my opinion, these two methods seem to be more complex than the first version. You are actually doing the same thing, but you just draw an independent region and save the accumulators function. The difference is that the function is saved in an attribute of the object rather than in the list). Using these special internal attribute names (especially _ call _) does not look like a common solution, but more like a cracking.

In the contest between Perl and python, Python hackers seem to think that python is more elegant than Perl, but this example shows that programming capabilities ultimately determine elegance. Perl is easier to write (with fewer syntax elements), although its syntax is a bit ugly.

How about other languages? As mentioned above, Fortran, C, C ++, Java, and Visual Basic cannot be used. Ken Anderson said that Java can only write an approximate solution:

Public interface inttoint {public int call (int I );}

Public static inttoint Foo (final int N) {return New inttoint () {int S = N; Public int call (int I) {S = S + I; return s ;}};}

This method does not meet the question requirements, because it is only valid for integers.

Of course, I said that using other languages cannot solve this problem. This sentence is not completely correct. All of these languages are turing equivalent, which means strictly speaking that you can use any of them to write any program. So how can we achieve this? In this small example, you can use these less powerful languages to write a lisp interpreter.

This may sound like a joke, but it is widely used in large programming projects to varying degrees. Therefore, some people name it Greenspun's Tenth Rule ):

"After any C or FORTRAN program is complex to a certain extent, it will contain a Common Lisp implementation that is temporarily developed, with only half of the functions, not fully compliant with specifications, bug everywhere, and slow running. "

If you want to solve a difficult problem, the key is not whether the language you are using is powerful, but several factors play a role at the same time (a) using a powerful language (B) write a de facto interpreter for this puzzle, or (c) the human-handed compiler that turns itself into this puzzle. In the python example, this processing method has started to appear. We actually write our own code to simulate the compiler's function of implementing local variables.

This practice is not only common, but also systematic. For example, in the world of object-oriented programming, we hear the word pattern in many ways, and I think those patterns are actually factors (c ), that is, the human flesh compiler. When I found the pattern in my own program, I thought it would indicate that something went wrong. The program form should only reflect the problems it wants to solve. Any other form in the Code is a letter (at least for me) indicating that I am not abstract enough to the problem, and I am often reminded, if you are doing something manually, you should write code and implement it automatically through macro extension.

Translator: Ruan Yifeng

Original English:Revenge of the Nerds

(Excerpt from the upcoming Chinese Translation of hacker and painter)

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.