Common Lisp Programming Preliminary---environment construction and external Lisp code run

Source: Internet
Author: User
Tags setf

1 Introduction

Lisp is the second oldest language in the world, and the first ancient language is FORTRAN. The founder of the Lisp language is John McCarthy, a pioneer in the field of artificial intelligence. Since its inception, the language has gone through more than 50 years and is a very vigorous language. The development and use of the language is inseparable from the two universities of CMU and MIT, which have been the designated language of the university's computer courses for a very long time.

At the beginning of the creation of the language, in addition to the founders, there are some other famous university scholars have also been involved in the development and perfection of Lisp language. So there are a number of Lisp dialect, which is very inconvenient to transplant the language. To this end, in 1981, the experts and scholars of the multi-Lisp, Common Lisp (CL), meaning "Common Lisp language", aimed at all Lisp users to follow the common standards, and facilitate industrial applications. CL absorbs the advantages of the previous Lisp dialect and adds an object-oriented feature that makes it the most widely used in the Lisp language. From 1981 ANSI absorbed CL as standard, 1989 ANSI updated CL standard, this is called CL 1989, and maintain the previous CL standard compatibility. For standards that are not specified, allow the compiler to play their own manufacturers. Since then, CL has entered a stable stage of development, and you will find that the CL code in some AI textbooks at the end of the 1980 century is still working.

The CL language, including other dialects of Lisp, is not widely used, but it cannot be concluded that it is a language that will die. In fact, CL is full of vitality and vitality, and currently there are large projects built using CL. The textbook "Artificial Intelligence: Theory and Practice" 1 is a 2003 edition of the photocopy, the artificial intelligence algorithm is used in the CL language; Works 2
"Common lisp:a gentle Introduction to symbolic computation" in 2013, a new corrected electronic version. Lisp has a lot of good characteristics, such as garbage Collection, dynamic type, functional programming, and so on, many modern languages have borrowed some of the features of the Lisp language, in which Lisp sets a model for other languages. It can be said that any programmer who wants to pursue excellence should learn and use the Lisp language. Once you have learned the language, you will find that other languages have become so easy for you, when you are on top of Tarzan.

Another Lisp dialect that is still in use today is scheme, and the textbook "Structure and interpretation of computer Programs" 3 is based on the language to explain the relevant algorithms. Scheme is mainly a kind of teaching, theoretically perfect language, but the application of industrialization is not enough, in large-scale projects without CL widely used.

This article will explain the environment of CL, the use of the IDE, writing a simple interactive program, and the code files. Lisp imports into the IDE and runs the content.

2 Environment Construction

There are many different environments in which you can run the CL program, which explains a Lispbox operating environment that you could call an IDE. Lispbox integrates Emacs with CL's interpreter to become a CL development environment, and it automatically imports some packages (packages) to facilitate programmer programming. You can download Lispbox from here, the website provides the Windows, OS X, Linux three different operating systems under the Lispbox, you can according to your own needs, choose one.

3 Use of IDE Lispbox

This article explains the use of Lispbox in the Windows 7 system as an example. Once downloaded, you can simply unzip it into your folder and no installation process is required. Lispbox is a pure green software.

3.1 Start Lispbox

Under the home folder, double-click Lispbox.bat to start.

3.2 Changing the current default directory

Perhaps your Lisp source code is not in the current directory, which requires you to change the current working directory in Lispbox. This practice is very common, in other software, such as MATLAB,R, etc. also learn how to change the directory. If you have an Emacs editor, you will naturally not have to read this section.

If I have written a lisp program Add-title.lisp stored in the Lispbox home directory My-study-lisp folder, then the method to change to the My-study-lisp folder is:

    1. Enter a comma after the command prompt cl-user>;
    2. Enter the CD at the bottom, such as:
    3. Enter a carriage return after the CD, enter My-study-lisp after the path that appears, that is, complete our task, after success such as:
4 CL Programming Preliminary

In CL there is a term called REPL (Read, Evaluate, Print, Loop), which is equivalent to interactive programming in Python, entering a command that immediately displays the result of the command execution and supports rapid development.

4.1 An example of a simple interactive program

CL's language syntax is simple, is to write some content in the (), the way the prefix is written. For example, such as:

The procedure is to ask for the square root of 2.

4.2 Import the source file. Lisp program to Lispbox

We can't run our program interactively every time, and when our program is stable, it's natural to deposit it into a file so that it doesn't have to be re-entered every time. The suffix of this file is usually lisp. Next, we explain how to run the program stored in a Lisp file through Lispbox.

If, my source program file Add-title.lisp content is (from "Common lisp:a gentle Introduction to Symbolic Computation" 4):

(defun titledp (name)(member ( First name) '( Mr Ms Miss Mrs   )))(setf male-first-names '(John Kim Richard Fred George)(setf female-first-names '(Jane Mary Wanda Barbara Kim)) (defun  malep  (  name  )   (and          (member  name Male-first-names)   (not    (member  name Female-first-names) ) ) ) (defun femalep (name)( and (member name Female-first-names)  ( not (member name male-first-names          ))))(DefunGive-title(name)  "Returns a name with a appropriate title on the front."  (Cond ((TITLEDP name) name)    (Malep (First  name )) (cons ' Mr Name )    (Femalep (First  name) ) (cons ' Ms name))     (t (append '(Mr or MS) name))))

We use the following command to enter after the Cl-user (load "add-title.lisp") , such as:

There are some warning messages in the import, do not worry, they do not affect the execution of subsequent programs. Below we will use the Add-title program, as follows:

The program will automatically precede the name with a salutation, if there is a courtesy, then no longer add, in line with our expectations.

5 concluding remarks

CL is a very challenging language for programmers, and it takes considerable effort to learn it well. But it is very fun, and many AI books of some intelligent programs to CL as the programming language, so, to understand these books, some of the practical books in the algorithm, you must first use CL language. Of course, you can also use the C + + language to implement the algorithm in the Book of Artificial intelligence, but I think the final implementation code will be more verbose, no CL seems straightforward and easy to understand, closer to people's thinking. Or, the C + + implementation of code is closer to the computer.

    1. Dean (Dean, T.) Wait. Artificial Intelligence: Theory and practice. Beijing: Electronic Industry Press, 2003. ?
    2. David S. Touretzky. Common lisp:a Gentle Introduction to symbolic computation. Benjamin/cummings Publishing Company, Inc., 1990. ?
    3. Harold Abelson, Gerald Jay Sussman, and Julie Sussman. Structure and interpretation of computer Programs. The MIT Press, Cambride, 1996. ?
    4. David S. Touretzky. Common lisp:a Gentle Introduction to symbolic computation. Benjamin/cummings Publishing Company, Inc., 1990. ?

Common Lisp Programming Preliminary---environment construction and external Lisp code run

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.