Table of Contents
回复内容:
Home Backend Development Python Tutorial 离散数学中的闭包和计算机语言中的闭包有联系吗?

离散数学中的闭包和计算机语言中的闭包有联系吗?

Jun 06, 2016 pm 04:24 PM

回复内容:

根据Wikipedia条目(en.wikipedia.org/wiki/C#History_and_etymology),尤其是注解4、5、6,可知计算机语言中的clourse用法是P.J. Landin发明的,意思其实就是closed expression。即lambda表达式中本来open的bindings(即free variables)被close后的结果。

没有证据表明Landin创造的这个术语与数学中的closure概念有直接的关系。@Ivony 的说法我认为只是源于两者用了相同的词汇,所以必然与close这个单词的意思相关而已。 没有半点关系,SICP里第二章注释6:

The use of the word ``closure'' here comes from abstract algebra, where a set of elements is said to be closed under an operation if applying the operation to elements in the set produces an element that is again an element of the set. The Lisp community also (unfortunately) uses the word ``closure'' to describe a totally unrelated concept: A closure is an implementation technique for representing procedures with free variables. 有联系,虽然联系并不明显。

朱兆龙童鞋搞混了闭包和闭合:
在离散数学(具体的说是抽象代数)里,如果对一个集合中的每个元素执行某个运算操作,得到的结果还是这个集合的元素,那么就说该集合在这个运算操作下构成闭包。例如,整数集合在减法运算下构成闭包;但是自然数在减法运算下不构成闭包。
如果一个集合中的每个元素执行某个运算还是这个集合的元素,那么说该集合在这个运算下闭合

闭包的定义是,包含指定集合的满足在某个运算下闭合最小集合


说白了,闭包就是,添加最少的元素,使得集合在某个运算下闭合。抽象之,闭包是指添加东西使得某种性质成立

那么程序设计语言里面的闭包又是什么呢?函数使用函数外的变量,如若使用的函数外的变量不是全局变量,将自动创建闭包,令内部函数所使用的变量与函数一同存在,使得该函数有效。

也就是说闭包就是自动为这个函数添加一个隐形的参数,其值为使用的外部变量,并确保其与函数一同存在,使得函数有效。 简单的说,这两个概念几乎没有联系(也许有,但是我没有发现)。我简单的解释一下两个闭包在两个领域中的含义:
1,在离散数学(具体的说是抽象代数)里,如果对一个集合中的每个元素执行某个运算操作,得到的结果还是这个集合的元素,那么就说该集合在这个运算操作下构成闭包。例如,整数集合在减法运算下构成闭包;但是自然数在减法运算下不构成闭包。
2,在编程语言里,也称为词法闭包或者函数闭包,它表示的是一个函数,以及一个定义这个函数时的环境(环境里记录了非本地变量的值)。例如(横线是为了对齐):
def counter():
----x = 0
----def increment(y):
--------nonlocal x
--------x += y
--------print(x)
----return increment
这个例子用的是python 3.X的语法,一直觉得python的闭包的实现不美观,不如lisp、ruby等。
参考自:
1,en.wikipedia.org/wiki/C
2,en.wikipedia.org/wiki/C 关于离散数学中的闭包

1.@Ivony的说法和我学校采用的课本的说法是一样的,“包含指定集合的满足在某个运算下闭合的最小集合”,即闭包是满足特定条件的集合。

2.另一种说法来自维基,即如果集合A在某种操作作用下的结果仍然属于集合A,那么就认为集合A在这个操作下拥有闭包属性。也可以说,集合A在这个操作下是闭合的。

在第一种说法里,闭包和闭合不是同一含义。而在第二种说法里闭包和闭合仅仅是名词和形容词的区别。

关于程序语言中的闭包

又叫词法闭包,或者函数闭包,指的是程序语言中的特性,具备该特性的程序语言能将函数和函数所处的环境当做整体使用。有时也把这个整体叫做闭包。

建议以后把闭包和函数闭包区别开来使用,否则会产生误解。 我不想提这个问题,只是,离散说的闭包和函数闭包是一样的,可以认为是等价命题
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python vs. C  : Exploring Performance and Efficiency Python vs. C : Exploring Performance and Efficiency Apr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Which is part of the Python standard library: lists or arrays? Which is part of the Python standard library: lists or arrays? Apr 27, 2025 am 12:03 AM

Pythonlistsarepartofthestandardlibrary,whilearraysarenot.Listsarebuilt-in,versatile,andusedforstoringcollections,whereasarraysareprovidedbythearraymoduleandlesscommonlyusedduetolimitedfunctionality.

Learning Python: Is 2 Hours of Daily Study Sufficient? Learning Python: Is 2 Hours of Daily Study Sufficient? Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

See all articles