http://blog.9cbs.net/windoze/archive/2004/08/01/57796.aspx
A episode we talked about "functions", in fact, this concept has been learned in junior high school mathematics, and a function is nothing more than the corresponding relationship of the argument to the value, the same in Lisp.
The function in the LISP is defined in the previous section (fast answering: Who remembers please raise your hand), use the following form to describe a function in the LISP:
(Lambda (P1 P2 ... PN) E)
Among them, PI is atom, referred to as parameters in the function, E is an expression, which is a function body.
The way to call a function is as follows:
((Lambda (P1 P2 ... PN) E) A1 A2 ... AN)
Among them, AI is expressive, according to our conventions, called inform.
The calling process of the entire function is as follows: Each expression AI (inactive) first evaluates, and then evaluates these first paragraphs, and the final result is the return value of the entire expression.
If an expression of the first element is an atom, it is not a basic operator (that is, the 7 we have mentioned), such as:
(f A1 a2 ... an)
And the value of f is a function (lambda (p1 p2 ... pn) e), the above expression is equivalent to
((Lambda (P1 P2 ... PN) E) A1 A2 ... AN)
After reading this paragraph, you may have a little dizziness, go to the window to do a few deep breath, then come back to do this, see what the result of this expression is?
((Lambda (f) (f '(B C))) (Lambda (X) (CONS' A X)))
If you got out, then continue to see it down, otherwise you will read more than a few words, and enter the above practice to a text editor that can automatically match the braces.
Here I plan to insert a few sentences, there may be many people have already seen this lambda, but it is not too might be in the LISP (if such words, you should not use this "entry", isn't it?) In Python, in Python, the Python brochure is just a stroke. He probably said: "Use the lambda word because it is similar to a syntax structure in the Lisp language." Ok, we now Let's take a look at the true origin of Lambda's allusions.
The word Lambda comes from the Lambda calculation theory. What is lambda? For the present, this concept is just "function", but for the age of Lambda calculation theory, it is a revolutionary innovation. The Lambda calculation theory is too complicated, and as an introduction to LISP, it is discussed that it has been completely deviated from the subject, but another concept of it proposed - high order function - is important in Lisp language The status, even can be said to be the main reason for Lisp.
As the "high-order derivative" is the "derivative of the derivative", the so-called high-order function is actually a "function of the function" (high teacher, forgive me). That is, a function itself is used as an argument of another function ("Functor" in modern C is actually an implementation of high-order functions in C ). The appearance of the high-order function, enhances the "function" to the status of the programming language to a "first-class citizen", you can operate a function like any basic data type, transform it, pass, how to toss. Let's go back to the topic and continue to discuss the functions in the LISP. We can see that, so far, our functions have no names, the function can have no name, that is, the anonymous function is another major feature of Lisp, Lisp can let The programmer peels the data and name, which is a luxury that is not allowed to enjoy many other programming languages.
The function does not have a problem, that is, you can't call itself in the function (OK, I know there is Y combination, but this is a introduction article), so Lisp provides a form that allows you to use one Identifier to reference functions:
(Label F (lambda (p1 p2 ... pn) e))
This expression is equivalent to the previous simple lambda expression, but all f appearing in E will be replaced with the entire Lambda expression, which is recursive.
At the same time, LISP provides it with a simple form:
(Defun f (p1 p2 ... pn) e)
You can start writing your first useful Lisp program, what do you plan to write? (No matter what, as long as hello world is good)
Next episode, we will give some common functions.
Getting started by LISP (1)
Getting started by LISP (2)
Getting started by LISP (4)
Getting started by LISP (5)
LISP Getting Started (postscript)
Note: This episode is too much, but unfortunately, the ability of my long-awaiting is too bad. Some prefaces don't write, if you have any opinions and suggestions, I will change it. .
Published on April 01, 2004 11:43 AM
comment
#
Reply: Getting started by Lisp (3)
2004-09-27 6:59 PM
D1Y2J3
((Lambda (f) (f '(B C))) (Lambda (X) (CONS' A X)))
Single single statement explains in CLISP-2.33.1 and Corman Lisp 2.5
The prompt is as follows:
;; Warning: unused variable f in anonymous function
;; An error opcurred in function #
;; error: The function f is undefined
;; Entering Corman Lisp Debug Loop.
;;; Use: c followed by an option to exit. Type: Help for Help.
;; Restart Options:
;; 1 Abort to Top Level.