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

查看:42
本文介绍了用普通的 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)

我知道 #' 表示一个函数.所以我的问题是,这是某种约定还是推荐的做法?如果我不在 lambda 前面加上 #' 前缀,会不会有什么问题,它是否依赖于实现?

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 ...) 仅在某些地方被认为是 lambda 表达式,例如 function 形式或作为头部的函数调用.不评估 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 特殊运算符

FUNCTION 是一个特殊的运算符.它需要一个函数名或一个 lambda 表达式.因此,不会评估名称或 lambda 表达式.事实上,lambda 表达式 根本无法计算.在FUNCTION 中,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) 由 Common Lisp 的内置语法支持.请参阅 Lambda 表单.

((lambda (foo) foo) 10) is supported by built-in syntax for Common Lisp. See Lambda Forms.

令人困惑

这一切都合乎逻辑,但令人困惑.别担心,你并不孤单,但实际上这没什么大不了的.

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天全站免登陆