- 论坛徽章:
- 0
|
这里头还是有个判断标准的,但我不清楚它在哪里。
又用 emacs lisp 试了一下。 在 elisp 中,lamba 是一个宏。代码为:
- (defmacro lambda (&rest cdr)
- "Return a lambda expression.
- A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
- self-quoting; the result of evaluating the lambda expression is the
- expression itself. The lambda expression may then be treated as a
- function, i.e., stored as the function value of a symbol, passed to
- `funcall' or `mapcar', etc.
- ARGS should take the same form as an argument list for a `defun'.
- DOCSTRING is an optional documentation string.
- If present, it should describe how to call the function.
- But documentation strings are usually not useful in nameless functions.
- INTERACTIVE should be a call to the function `interactive', which see.
- It may also be omitted.
- BODY should be a list of Lisp expressions.
- \(fn ARGS [DOCSTRING] [INTERACTIVE] BODY)"
- ;; Note that this definition should not use backquotes; subr.el should not
- ;; depend on backquote.el.
- (list 'function (cons 'lambda cdr)))
复制代码
注释真多呀,去掉后为:
- (defmacro lambda (&rest cdr)
- (list 'function (cons 'lambda cdr)))
复制代码
lambda 宏造出 lambda 表达式:一个以符号 'lambda 起头的表。参考注释中的:
A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
self-quoting; the result of evaluating the lambda expression is the
expression itself.
我发现 lambda 宏和表达式中的符号 lambda 似乎是不相干的。如果把 lambda 宏定义成其它东西,然后执行以下代码:
(funcall (list 'lambda '(x) x) 1)
得到 1.
仿照 lambda 宏的定义,我可以写一个自己的宏
- (defmacro make-lambda (&rest cdr)
- (list 'function (cons 'lambda cdr)))
复制代码
(funcall (make-lambda (x) x) 1) 得到 1。即使同时取消 lambda 宏的原有定义也不影响这个 make-lambda
但我定义出来的 make-lambda 并不能复制 lambda 的全部能力。首先 “self-quoting” 就做不到,
其次,make-lambda 出来的东西不能放在表的第一项。
((lambda (x) x) 1) 是合法的,但 ((make-lambda (x) x) 1) 就不合法。出错信息为:
- Register 1 contains the text:
- Debugger entered--Lisp error: (invalid-function (make-lambda (x) x))
- ((make-lambda (x) x) 1)
- eval(((make-lambda (x) x) 1))
- eval-last-sexp-1(nil)
- eval-last-sexp(nil)
- call-interactively(eval-last-sexp nil nil)
复制代码
似乎宏没有被展开。不明白为什么。如果 make-lambda 被展开,则结果与 lambda 宏被展开无异。
(macroexpand (lambda (x) x)) 和 (macroexpand (make-lambda (x) x)) 的值都是
- (function (lambda (x) x))
复制代码
另外,根据注释,上面的 function 换成 quote 也是可以的。
[ 本帖最后由 retuor 于 2009-2-18 13:39 编辑 ] |
|