Crossing boundaries: The beauty of Lisp

Source: Internet
Author: User
Tags mathematical functions types of functions

Lisp has long been seen as one of the great programming languages. The following frenzy sparked by its long process of development (nearly 50 years) shows that this is an extraordinary language. Mit,lisp plays a pivotal role in the curriculum of all programmers. Entrepreneurs like Paul Graham use Lisp's remarkable productivity as the driving force behind the success of their careers. But to their followers ' chagrin, Lisp has never been a mainstream programming language. As a Java™ programmer, if you spend a little time studying Lisp, the forgotten city of gold, there are many techniques that can improve the way you encode.

I recently finished the marathon for the first time and I found that running is more valuable than I expected. I ran 26.2 miles, and by that step I began to think of it as a very useful simple activity for the body. Some languages give me similar feelings, such as Smalltalk and Lisp. For Smalltalk, it is the object that triggers a similar sensation, and everything in Smalltalk is in the process of object and message passing. For Lisp, this important step is simpler. This language is made up entirely of lists. But don't be fooled by this simple illusion. This 48-Year-old language is incredibly powerful and flexible, which is beyond the reach of the Java language.

When I first worked with Lisp, I was a student at school, but this time it didn't go well. Because I desperately wanted to put Lisp into familiar procedural paradigms, rather than working in Lisp's functional structure. Although Lisp is not a strictly functional language (because of some features that do not conform to the strictest definition of terms), many of the idioms and features of Lisp have a strong functional style. Since then, I have learned to use lists and functional programming.

This period of crossing the border will regain the lost wealth. I'll take you through the basic structure of Lisp and quickly expand it. You'll learn about Lambda expressions, recursion, and macros. This simple wizard will give you an idea of the efficiency and flexibility of Lisp.

Entry

This article uses the GNU GCL, which is free to download for many operating systems. But with a little modification, you can use any version of Common Lisp. See resources for a detailed description of the available Lisp version.

Like learning most other languages, the best way to learn Lisp is to practice. Open your interpreter and encode it with me. Lisp is basically a compiled language that you can easily program with by typing a command directly.

List language

Basically, Lisp is a language about lists. Everything in Lisp (from data to the code that makes up the application) is a list. Each list is made up of atoms and lists. The number is the atom. Typing a number simply returns the number as the result:

Listing 1. Simple atoms>1
1
>a
Error: The variable A is unbound.

If you type a letter, the interpreter will make an error, as shown in Listing 1. Letters are variables, so you must first assign them to them before you use them. If you want to refer to a letter or word instead of a variable, enclose it in quotation marks. Add a single quotation mark before a variable to tell LISP to delay the evaluation of a subsequent list or atom, as shown in Listing 2:

Listing 2. Deferred evaluation and reference>"a"
"a"
>'a
A

Please note that Lisp capitalizes a on a. Lisp assumes that you want to use a as a symbol because it does not have braces. The assignment is discussed later, but let the list do the task first. Simply put, the Lisp list is a sequence of atoms with parentheses and separated by spaces. Try typing a list as shown in Listing 3. This list is invalid unless you precede the list with '.

Listing 3. Type a simple list>(1 2 3)
Error: 1 is invalid as a function.
>'(1 2 3)
(1 2 3)

Unless you add ' in front of the list ', Lisp will evaluate each list as if it were evaluated for a function. The first atom is an operator, and the remaining atoms in the list are parameters. Lisp has a number of primitive functions, which, as you would expect, include many mathematical functions, such as +, *, and sqrt. (+ 1 2 3) returns 6, (* 1 2 3 4) returns 12.

There are two types of functions for manipulating lists: constructors and selection functions. constructor to build the list and select the function decomposition list. The core selection function is the one and rest. The first selection function returns the initial atom of the list, and the rest selection function returns the entire list except the first atom. Listing 4 shows the two selection functions:

Listing 4. Basic Lisp function> (first '(lions tigers bears))
LIONS
> (rest '(lions tigers bears))
(TIGERS BEARS)

All two select functions get the entire list, returning the main fragment of the list. Later, you'll see how recursion can take advantage of these selection functions.

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.