Lisp 中的 lambda 函数如何工作?
我在Land of Lisp一书中读到,lambda函数是唯一的内置函数。然而我不太明白这是怎么可能的,因为我认为你至少需要一个用于加法的命令,一个用于比较数字的命令,一个用于将数据从一个变量移动到另一个变量的命令。我想知道是否有人可以向我解释 lisp 是如何做到这一点的。我不是数学家,所以如果可能的话,您能否在没有大量复杂数学的情况下解释它?
I read in the book Land of Lisp that the lambda function is the only built-in function. However I don't really understand how that is possible because I thought you would at least need one command for addition, one for comparing numbers, and one for moving data from one variable to another. I was wondering if someone could explain to me how lisp does it. I'm not a mathematician so if it is possible could you also explain it without a whole lot of complex math?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“Land of Lisp”在这里所说的并不是 lambda 是唯一的 Lisp 原语,而是(根据 Alonzo Church 的 lambda 演算,Lisp 有理论基础)人们可以实现 Lisp 的其余部分与 lambda ,因为 lambda 演算相当于通用图灵机。
对于大多数实际应用,
lambda
用于定义匿名函数。What 'Land of Lisp' is saying here is not that
lambda
is the only Lisp primitive, but rather that (according to Alonzo Church's lambda calculus, which Lisp has theoretical underpinnings) one could implement the rest of Lisp withlambda
, as the lambda calculus is equivalent to a Universal Turing Machine.For most practical applications,
lambda
is used to define anonymous functions.这是理论和实际编程语言之间的区别。
Lisp 从 Lambda 演算中汲取了思想,但没有实现它。 lambda 演算描述了使用函数进行计算的系统。了解 Lambda 演算很有用,但当您使用 Lisp 时,您不会使用纯 Lambda 演算进行编程。
作为一种编程语言,Lisp 拥有各种数据类型和操作(数字、字符串、字符、cons 单元格、符号、函数……)。
将其与图灵机和编程语言 C 之类的东西进行比较。
That's a difference between theory and a real programming language.
Lisp took ideas from Lambda Calculus, but does not implement it. The lambda calculus describes a system to do calculation using functions. It is useful to understand Lambda Calculus, but you won't program in pure Lambda Calculus when you use Lisp.
As a programming language, Lisp has all kinds of data types and operations for those (numbers, strings, characters, cons cells, symbols, functions, ...).
Compare that to Turing Machines and something like the programming language C.
你在这里混淆了一些事情。
lambda
不是一个函数。它是 Lisp 语言中内置的一个结构。任何实用的 Lisp 都会有很多内置函数;它至少需要
car
和cdr
来挑选列表,并且一些原始算术函数不能根据其他函数来定义。(*) 此外,“非功能” Lisp 的某些部分(例如setf
)需要一些原语。[*] 你可以用 Lisp 做教会算术,
但是你不能漂亮- 由于 Lisp 的类型系统而打印结果,但是否可以正确打印结果取决于 Lisp 变体。You're confusing some things here.
lambda
is not a function. It's a construct built into the Lisp language.Any practical Lisp will have lots of built-in functions; it needs at least
car
andcdr
to pick lists apart and some primitive arithmetic functions cannot be defined in terms of other functions.(*) Also, the "non-functional" parts of Lisp such assetf
need some primitives.[*] You can do Church arithmetic in Lisp,
but then you can't pretty-print the results due to Lisp's type systembut whether you can properly print the result depends on the Lisp variant.