用通用Lisp编写Lambda表达式 [英] Writing lambda expressions in common lisp

查看:289
本文介绍了用通用Lisp编写Lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读Paul Graham撰写的ANSI Common Lisp,并且对编写lambda表达式有疑问。

I am currently reading ANSI Common Lisp by Paul Graham, and I have a question about writing lambda expressions.

我们是否需要为lambda表达式加上#’前缀?如果我在REPL中写这样的东西,它将很好用

Do we need to prefix a lambda expression with #'?. If I write something like this in REPL, it will work fine

> ((lambda (x) (+ x 1)) 1)
  2

所以此

> (mapcar (lambda (x) (+ x x)) '(1 2 3 4))
  (2 4 6 8)

我知道#'表示一个函数。所以我的问题是,这是某种惯例还是推荐做法?如果我不给lambdas加上#'前缀,这会实现吗?

I understand that #' denotes a function. So my question is, is it some sort of convention or recommended practice? Can anything go wrong if I don't prefix lambdas with #', is it implementation dependent?

推荐答案

LAMBDA表达式

(lambda ...)仅在某些地方(例如 function 形式或作为函数调用的头)才被视为 lambda表达式。不会评估Lambda表达式。

(lambda ...) is considered to be a lambda expression only in certain places, like the function form or as the head of a function call. Lambda expressions are not evaluated.

(function              ; special operator FUNCTION
  (lambda () 'foobar)) ; <- this is a lambda expression


(                    ; a function call
 (lambda (foo) foo)  ; <- this is a lambda expression
 'bar                ; argument
)

但是这里(lambda ... )是宏形式,而不是lambda表达式:

But here (lambda ...) is a macro form and not a lambda expression:

(funcall             ; calling a function via funcall
 (lambda (foo) foo)  ; this is not a lambda expressions, but the macro lambda
                     ;  as all arguments to FUNCALL it will be
                     ;    macro expanded and evaluated
                     ;  it expands to (function (lambda (foo) foo))
 'bar)               ; argument

LAMBDA宏

LAMBDA 是一个宏。它将(lambda ...)扩展为(function(lambda ...)) #'(lambda ...))

LAMBDA is a macro. It expands (lambda ...) to (function (lambda ...)), which is the equivalent of #'(lambda ...)).

CL-USER > (macroexpand '(lambda (foo) foo))
(FUNCTION (LAMBDA (FOO) FOO))

该宏为您节省了一些写/读操作,仅此而已。在Common Lisp(CLtL1)的第一个版本中,没有 LAMBDA 宏。它是后来添加的,现在是 ANSI Common Lisp 的一部分,

The macro saves you a bit of writing/reading, that's all. In the first version of Common Lisp (CLtL1) there was no LAMBDA macro. It has been added later and is now a part of ANSI Common Lisp,

FUNCTION特殊运算符

功能是特殊运算符。它需要一个函数名或一个 lambda表达式。因此,不会评估名称或 lambda表达式。实际上, lambda表达式根本无法求值。在功能内, lambda表达式不是不是宏格式,因此不是再次扩大。 FUNCTION 的目的是返回相应的函数对象,该函数对象由名称或 lambda表达式表示。它返回函数对象作为值。使用这种特殊的运算符,可以从全局函数和词法函数访问函数对象。

FUNCTION is a special operator. It expects a function name or a lambda expression. Thus the name or the lambda expression are not evaluated. In fact lambda expressions can't be evaluated at all. Inside FUNCTION, the lambda expression is not a macro form and thus will not be expanded again. The purpose of FUNCTION is to return the corresponding function object which is denoted by the name or by the lambda expression. It returns the function object as a value. With this special operator one can access the function object from global functions and lexical functions.

FUNCTION 运算符是必需的在Common Lisp中,因为它具有用于值,函数和其他一些东西的独立命名空间。它是所谓的 Lisp-2 或什至 Lisp-n ,具有两个或多个命名空间。

The FUNCTION operator is necessary in Common Lisp, because it has separate namespaces for values, functions and a few other things. It as a so-called Lisp-2 or even Lisp-n, with two or more namespaces.

Lambda表达式以函数形式出现在函数位置

(((lambda(foo)foo)10)。参见 Lambda表单

令人困惑

这是合乎逻辑的,但令人困惑。不必担心您并不孤单,但实际上这没什么大不了的。

This is all logical, but confusing. Don't worry you are not alone, but in practice it's not a big deal.

这篇关于用通用Lisp编写Lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆