Quick Start to Common LISP

Source: Internet
Author: User

 

LISP is the split force in the software field. On the one hand, LISP enthusiasts swear that lisp is faster, more neat, and more powerful than other languages in the software field, while opponents argue that, unreliable execution and insufficient library support make it difficult for developers to write any real software. In fact, they all have their own principles.

The first version of lisp was launched about 50 years ago, making it the oldest version still in use like FORTRANProgramming Language. It can be proved that it has the largest list of features (they will), and it is also the first language that we all consider as a standard language feature today, such as garbage collection, recursion, functions as objects, and even common if-then-else clauses. At the same time, it is also considered to be a good teaching language: MIT use solutions, LISP derivatives, teach them introductory programming classes.

We will learn the most powerful and project-ready version of LISP: Common LISP to make it run normally and understand some lisp applications.

Why choose LISP?

    • CodeThere is no difference with data-in LISP, the code is just a list of function objects.Source codeThere is no difference between it and the data source, allowing LISP to present it internally to the compiler, annotator, andProgramMember. This allows you to easily read and evaluate code locally, and even allow you to use macros.
    • Macro-define and redefine any language element. Do you not like the running mode of conditions, loops, or functions? Well, you can define your own annotations. If you execute a specific task multiple times in the code, isn't it more convenient to add that feature to the language? Using lisp, You can implement the above functions.
    • Speed-although in many cases Common LISP is not as fast as C or ocaml, it performs well in a series of tests, especially when executing a short program. With some basic compiler knowledge, you can compile the source list and code for large numbers, which is superior to other languages in terms of execution speed and memory usage.
    • Simplicity-all the functions of Lisp are based on some basic concepts-once you understand those concepts, you will be able to handle almost any problem. Lisp programmers often boast that with just a few hundred lines of code, you can execute a lisp annotator in almost any language (such as C ++ or Haskell.
    • Flexible-write code in any way you like. Do you prefer functional programming methods? No problem! Do you want to program it completely repeatedly? Write a few quick macros to complete the task. You can write programs in the most convenient and efficient way, and these programs can run well.

Okay, all right! I purchased LISPHow can I install it?

This is a tough issue. Similar to Python or C #, Common Lisp has no standard execution-the language is defined by a specification rather than execution. Common Lisp does not have the advantage of C language either: it is a dominant execution or popular execution on every platform. Each version should execute the above standards, but some details should be handled by the compiler or annotator, which makes every execution slightly different.

You can use several options-in this article, I use clisp, which runs well on Windows, Linux, and MAC (PPC only. If you use intel Mac, you must use other commands, such as Allegro Common Lisp or sbcl. For the simple example in this quick start, it doesn't matter which execution you use.

Lispbox can be used to quickly install the Common Lisp system. lispbox provides you with a Common Lisp execution, emacs, and slime -- Emacs advanced lisp integration mode. Many lisp programmers will tell you, it is the only method to use lisp. If you are not an Emacs user (I am also the same as you), don't worry. It is not a necessary condition, but makes it easier to write Common LISP programs.

The installation process varies with the platform. In Windows, you can only run the installer. Most linux package managers provide installation packages. Select an execution and follow the installation instructions. Open repl (Interactive prompt) and return here. Let's continue later.

Repl

Repl stands for "Read-evaluate-print-loop", which simply represents an interactive prompt of the annotator. You can output some simple lisp code from this. If you use another annotator prompt, you can use the prompt as a calculator to output some basic mathematical expressions-but it cannot run properly. Entering 5*2 in clisp does not return any meaningful results:

[1]> 5*2

5

[2]>

5

[3]>

2

Lisp does not run in that way. operators such as "+" are not between numbers, but before numbers, just as if they are function names. Therefore, if you want to use repl as a calculator, you must enter:

[1]> (* 5 2)

10

[2]> (+ 1 2 3 4)

10

[3]> (+ (x 5 2) (X 10 3) (/100 4 ))

65

For you, understanding this usage may be more difficult, but it has some advantages: it is easy for the compiler to parse, it is the same for all functions and operators, it allows you to add as many independent variables as possible to the function-for example, in the second example above, you can expand the number of Addons at will, so that the addition function is exactly the same as the total function.

In addition, you will notice that the function name is in parentheses, and not in parentheses like many other languages. This indicates that you want to write (function independent variable) instead of the function (independent variable ).

Each lisp expression returns a value, and a function always returns the result of the last expression-even nil, the equivalent of null in Java or C ++. Therefore, it is quite simple to display "Hello World" in LISP:

[3]> "Hello World"

"Hello World"

If you want to print some content on the screen and return other content, you should use the print function:

[4]> (print "Hello World ")

"Hello World"

"Hello World"

This string is displayed twice. One is the print result and the other is the result returned by the function.

Lisp indicates list processor. Almost all contents in LISP exist in the form of list, so sometimes you must process the list. Defining a list is very easy:

[5]> (List 1 2 3 4 5)

(1 2 3 4 5)

[6]> '(1 2 3 4 5)

(1 2 3 4 5)

The second definition method is reference. In addition to defining a simple list, it also has more functions, but we have to go through another articleArticle.

Control Process

Lisp has all the standard control flow methods. Defining a simple loop that repeats a value is quite easy:

[7]> (dotimes (I 10) (print I) 0

1

2

3

4

5

6

7

8

9

Nil

Likewise, it is easy to repeat a list:

[8]> (dolist (I '(0 1 2 3 4 5 6 7 8 9) (print I ))

0

1

2

3

4

5

6

7

8

9

Nil

The above two functions are special versions of the do function, just like using the while and for functions in other languages. It consists of three parts: Definition of cyclic change, termination condition, and statement subject:

[9]> (do (I 0 (+ 1 I) (> I 10) (print I ))

0

1

2

3

4

5

6

7

8

9

10

Nil

In this example, the change is defined as (I 0 (+ 1 I) and the variable I is 0, and call the function (+ 1 0) in each loop ). The termination condition is (> I 10), indicating that the function stops when I is greater than 10. The value of I is printed in the final body part.

There are also conditional functions in lisp. The most basic conditional function is the IF function:

[10]> (if (> 10 20) (print "hello") (print "world "))

"World"

"World"

The IF function consists of three parts: condition, then statement, and else statement. If the condition is true, the then statement is executed; otherwise, the else statement is executed.

You may have noticed that we have only used a single statement so far-but what if you need to connect several statements together? In lisp, to connect several statements, you need to use the special control flow function progn:

[11]> (progn (print "hello") (print "world "))

"Hello"

"World"

"World"

For example, the preceding example allows you to use several statements in conditional functions and loops.

Well, the above content is enough for you to get to know the Common LISP Language: through what you know, you can compile some micro programs to test the lisp language. Please pay close attention to the next article in this series. We will introduce the unique list processing mechanism of lisp.

Responsible editor: dedong

View international sources of this Article

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.