为什么是“纯"?称为“纯"的函数? [英] Why are "pure" functions called "pure"?

查看:20
本文介绍了为什么是“纯"?称为“纯"的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

纯函数是一个没有副作用的函数——它不能做任何类型的 I/O,也不能修改任何东西的状态——而且它是引用透明的——当使用相同的输入多次调用时,它总是给出相同的输出.

A pure function is one that has no side effects -- it cannot do any kind of I/O and it cannot modify the state of anything -- and it is referentially transparent -- when called multiple times with the same inputs, it always gives the same outputs.

为什么用纯"这个词来描述具有这些特性的函数?谁首先以这种方式使用纯"这个词,什么时候?还有其他词的意思大致相同吗?

Why is the word "pure" used to describe functions with those properties? Who first used the word "pure" in that way, and when? Are there other words that mean roughly the same thing?

推荐答案

为了回答您的第一个问题,数学函数通常被描述为纯"某些特定变量.例如:

To answer your first question, mathematical functions have often been described as "pure" in terms of some specified variables. e.g.:

第一项是x的纯函数,第二项是y

the first term is a pure function of x and the second term is a pure function of y

因此,我认为您不会发现真正的第一次"出现.

Because of this, I don't think you'll find a true "first" occurrence.

对于编程语言,稍微搜索一下就会发现 Ada 95 (pragma Pure)、高性能 Fortran (1993) (PURE) 和 VHDL-93 (pure) 都包含纯函数"的正式概念.

For programming languages, a little searching shows that Ada 95 (pragma Pure), High Performance Fortran (1993) (PURE) and VHDL-93 (pure) all contain formal notions of 'pure functions'.

Haskell (1990) 相当明显,但纯度并不明确.GCC 的 C 具有各种函数属性,用于不同级别的纯".

Haskell (1990) is fairly obvious, but purity isn't explicit. GCC's C has various function attributes for various differing levels of 'pure'.

几本书:C 编程语言的基本原理 (1990) 使用该术语,

A couple of books: Rationale for the C programming language (1990) uses the term, as does Programming Languages and their Definitions (1984). However, both apparently only use it once! Programming the IBM Personal Computer, Pascal (also 1984) uses the term, but it isn't clear from Google's restricted view whether or not the Pascal compiler had support for it. (I suspect not.)

有趣的一点是,Ada 的前身 Green,实际上有一个相当严格的函数"定义 - 甚至不允许分配内存.然而,这在成为 Ada 之前就被删除了,在那里函数可以有副作用(I/O 或全局变量),但不能修改它们的参数.

An interesting note is that Green, the predecessor to Ada, actually had a fairly strict 'function' definition - even memory allocation was disallowed. However, this was dropped before it became Ada, where functions can have side-effects (I/O or global variables), but can't modify their arguments.

C28-6571-3(第一本 PL/I 参考手册,在编译器之前编写)表明 PL/I 支持纯函数,形式为 REDUCIBLE (= pure) 属性,早在 1966 年- 首次发布编译器时.(这也回答了你的第三个问题.)

C28-6571-3 (the first PL/I reference manual, written before the compiler) shows that PL/I had support for pure functions, in the form of the REDUCIBLE (= pure) attribute, as far back as 1966 - when the compiler was first released. (This also answers your third question.)

最后一份文件特别指出,它包含 REDUCIBLE 作为自文件 C28-6571-2 以来的新变化.所以 REDUCIBLE,它可能是编程语言中形式纯函数的第一个化身,出现在 1966 年 1 月到 7 月之间.

This last document specifically notes that it includes REDUCIBLE as a new change since document C28-6571-2. So REDUCIBLE, which is possibly the first incarnation of formal pure functions in programming languages, appeared somewhere between January and July 1966.

更新:从这个意义上说,Google 网上论坛上最早的纯函数"实例来自 1988 年,这很容易将参考书的日期推迟.

Update: The earliest instance of "pure function" on Google Groups in this sense is from 1988, which easily postdates the book references.

这篇关于为什么是“纯"?称为“纯"的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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