- 論壇徽章:
- 0
|
這里頭還是有個(gè)判斷標(biāo)準(zhǔn)的,但我不清楚它在哪里。
又用 emacs lisp 試了一下。 在 elisp 中,lamba 是一個(gè)宏。代碼為:
- (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)))
復(fù)制代碼
注釋真多呀,去掉后為:
- (defmacro lambda (&rest cdr)
- (list 'function (cons 'lambda cdr)))
復(fù)制代碼
lambda 宏造出 lambda 表達(dá)式:一個(gè)以符號(hào) '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.
我發(fā)現(xiàn) lambda 宏和表達(dá)式中的符號(hào) lambda 似乎是不相干的。如果把 lambda 宏定義成其它東西,然后執(zhí)行以下代碼:
(funcall (list 'lambda '(x) x) 1)
得到 1.
仿照 lambda 宏的定義,我可以寫一個(gè)自己的宏
- (defmacro make-lambda (&rest cdr)
- (list 'function (cons 'lambda cdr)))
復(fù)制代碼
(funcall (make-lambda (x) x) 1) 得到 1。即使同時(shí)取消 lambda 宏的原有定義也不影響這個(gè) make-lambda
但我定義出來的 make-lambda 并不能復(fù)制 lambda 的全部能力。首先 “self-quoting” 就做不到,
其次,make-lambda 出來的東西不能放在表的第一項(xiàng)。
((lambda (x) x) 1) 是合法的,但 ((make-lambda (x) x) 1) 就不合法。出錯(cuò)信息為:
- 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)
復(fù)制代碼
似乎宏沒有被展開。不明白為什么。如果 make-lambda 被展開,則結(jié)果與 lambda 宏被展開無異。
(macroexpand (lambda (x) x)) 和 (macroexpand (make-lambda (x) x)) 的值都是
- (function (lambda (x) x))
復(fù)制代碼
另外,根據(jù)注釋,上面的 function 換成 quote 也是可以的。
[ 本帖最后由 retuor 于 2009-2-18 13:39 編輯 ] |
|