函数式编程
函数式编程,或称函数程序设计、泛函编程(英语:Functional programming),是一种编程范型,它将电脑运算视为函数运算,并且避免使用程序状态以及可变对象。
简介
[编辑]在函数式编程中,函数是头等对象即头等函数,这意味着一个函数,既可以作为其它函数的输入参数值,也可以从函数中返回值,被修改或者被分配给一个变量。λ演算是这种范型最重要的基础,λ演算的函数可以接受函数作为输入参数和输出返回值。
比起指令式编程,函数式编程更加强调程序执行的结果而非执行的过程,倡导利用若干简单的执行单元让计算结果不断渐进,逐层推导复杂的运算,而不是设计一个复杂的执行过程。
历史
[编辑]阿隆佐·邱奇在1930年代开发的λ演算[1],是建造自函数应用的一种计算形式系统。在1937年,艾伦·图灵证明了λ演算和图灵机是等价的计算模型[2],展示了λ演算是图灵完备性的。λ演算形成了所有函数式编程语言的基础。另一种等价的理论公式化是组合子逻辑,它由Moses Schönfinkel和哈斯凯尔·柯里在1920年代和1930年代开发[3]。
邱奇后来又开发了简单类型λ演算,它通过向所有的项指定一个类型而扩展了λ演算。[4]这个系统形成了静态类型函数式编程的基础。
于20世纪50年代后期,John McCarthy在麻省理工学院,开发了早期的函数式语言LISP,运行在大型IBM主机(IBM700/7000系列)上[5]。LISP的函数定义借鉴了邱奇的λ表示法[6],并扩展了标签构造来允许递归函数[7]。最开始的LISP是多范型语言,并且随着新的范型的发展,越来越多的编程风格得到了支持。后来发展出来的方言比如Scheme、Clojure,和分支语言比如Dylan等,试图围绕一个清晰的函数式核心,来得出简化和理性化的LISP,而Common Lisp旨在保留并更新它所替代的各种更早先LISP方言的那些范型特征。[8]
而于1956年发明的IPL语言,一般被认为是第一个基于计算机的函数式编程语言。[9] 它是一种用于操纵符号列表的汇编式语言。它有一个生成器的概念,相当于一个接受函数作为参数的函数,并且,由于它是汇编级语言,代码可以是数据,因此IPL可以被视为具有高阶函数。但是,它在很大程度上依赖于改变列表的结构和类似的指令式编程特征。
在1960年代早期,Kenneth E. Iverson开发了APL语言,在他1962年出版的《A Programming Language》一书中对其有所介绍。[10]APL对John Backus的FP语言施加了巨大的影响。在20世纪90年代早期,Iverson和Roger Hui创造了J语言。在20世纪90年代中期,以前曾与Iverson合作过的Arthur Whitney创建了K语言,后者在金融行业中与其派生出来的Q语言一起被商业化使用。
1977年John Backus在他的图灵奖颁奖演讲《编程可以从冯·诺依曼式风格中解放出来吗?一种函数式风格及其程序代数》中,展示了他提出的FP[11]。他将函数式编程定义为通过“组合形式”以分层方式构建,允许“程序代数”; 在现代语言中,这意味着函数式程序应遵循复合性原理。Backus的论文推广了函数式编程的研究,虽然它强调的是函数级编程而不是现在所说的λ演算风格。
1973年爱丁堡大学的Robin Milner发明了ML语言,它的语法受到了ISWIM的启发。同年,David Turner在圣安德鲁斯大学开发了SASL语言,它基于了ISWIM的应用式子集[12]。在1976年,Turner重新设计并重新实现它为惰性求值语言[13]。在20世纪70年代的爱丁堡,Rod Burstall和John Darlington开发了NPL语言。[14] NPL基于Kleene的递归方程,并在他们的程序转换工作中首次引入。[15] 然后Rod Burstall、David MacQueen和Don Sannella结合了来自ML的多态类型检查,从NPL派生出了Hope语言。[16]ML最终发展成几种语言,其中最常见的是OCaml和Standard ML。
在1970年代,Guy L. Steele和Gerald Jay Sussman开发了Scheme,如有影响力的“λ论文集”和经典的1985年教科书《计算机程序的构造和解释》中所描述的那样。Scheme是使用词法作用域和尾调用优化的第一个Lisp方言,将函数式编程的影响力提升到更广泛的范围,让更多的编程语言社区接触到它们。
在1980年代,佩尔·马丁-洛夫开发了直觉类型论(也称为构造类型论),它将函数式编程与表现为依赖类型的数学证明联系起来。这导致了交互式定理证明的新方法的产生,并影响了后续的函数式编程语言的发展。
在1985年David Turner开发的惰性求值函数式语言Miranda出现,它采用了来自ML与Hope语言的概念,作为他先前所设计的SASL和KRC语言的后继者。Miranda对后来的Haskell有很强的影响,由于它当时是专有软件,所以Haskell社区于1987年开始达成共识,以形成函数式编程研究的开放标准,对标准的实现自1990年以来一直在进行中。
最近,它在基于CSG几何框架构建的OpenSCAD语言的参数CAD中得到了应用,虽然在重新赋值上的限制(所有值都被当作常量),导致了不熟悉函数式编程的用户混淆。[17]
应用
[编辑]工业
[编辑]函数式编程长期以来在学术界流行,但几乎没有工业应用。造成这种局面的主因是函数式编程常被认为严重耗费CPU和存储器资源[18] ,这是由于在早期实现函数式编程语言时并没有考虑过效率问题,而且面向函数式编程特性,如保证参照透明性等,要求独特的数据结构和算法。[19]
然而,最近几种函数式编程语言已经在商业或工业系统中使用[20],例如:
- Erlang,它由瑞典公司爱立信在20世纪80年代后期开发,最初用于实现容错电信系统。此后,它已在Nortel、Facebook、Électricité de France和WhatsApp等公司作为流行语言建立一系列应用程序。[21][22]
- Scheme,它被用作早期Apple Macintosh计算机上的几个应用程序的基础,并且最近已应用于诸如训练模拟软件和望远镜控制等方向。
- OCaml,它于20世纪90年代中期推出,已经在金融分析,驱动程序验证,工业机器人编程和嵌入式软件静态分析等领域得到了商业应用。
- Haskell,它虽然最初是作为一种研究语言,也已被一系列公司应用于航空航天系统,硬件设计和网络编程等领域。
其他在工业中使用的函数式编程语言包括多范型的Scala[23]、F#,还有Wolfram语言、Common Lisp、Standard ML和Clojure等。
教育
[编辑]教育方面,函数式编程一直受到了很大的重视,很多学校使用函数式编程来教授算法和几何的相关概念[24]。
典型的函数式编程语言
[编辑]纯函数式编程语言通常不允许直接使用程序状态以及可变对象,典型语言有:Miranda、Haskell和Idris等。
非纯函数式编程语言可按类型系统分为两类:
- 静态类型语言:ML家族的Standard ML、OCaml、F#,还有Scala、Typed Racket[25]等。
- 动态类型语言:Lisp家族的Scheme、Common Lisp、Clojure、Racket,还有LOGO、Erlang、Wolfram语言和R等。
参考文献
[编辑]- ^ Alonzo Church. A set of postulates for the foundation of logic (PDF). Annals of Mathematics. Series 2. 1932, 33 (2): 346–366 [2022-12-19]. JSTOR 1968337. doi:10.2307/1968337. (原始内容存档 (PDF)于2022-12-19).
Alonzo Church. The Calculi of Lambda-Conversion. Annals of Mathematics studies, no. 6. Lithoprinted. Princeton University Press, Princeton. 1941 [2021-09-24]. (原始内容存档于2022-05-19). - ^ Turing, A. M. Computability and λ-definability (PDF). The Journal of Symbolic Logic (Cambridge University Press). 1937, 2 (4): 153–163 [2021-09-24]. JSTOR 2268280. doi:10.2307/2268280. (原始内容 (PDF)存档于2021-09-24).
- ^ Haskell Brooks Curry; Robert Feys. Combinatory Logic. North-Holland Publishing Company. 1958 [2022-12-19]. (原始内容存档于2022-12-19).
- ^ Church, A. A Formulation of the Simple Theory of Types (PDF). Journal of Symbolic Logic. 1940, 5 (2): 56–68 [2021-09-24]. JSTOR 2266170. doi:10.2307/2266170. (原始内容 (PDF)存档于2021-05-07).
- ^
McCarthy, John. History of Lisp (PDF). Artificial Intelligence Laboratory, Stanford University. 12 February 1979 [2021-09-23]. (原始内容 (PDF)存档于2020-11-07).
There were two motivations for developing a language for the IBM 704. First, IBM was generously establishing a New England Computation Center at M.I.T. …… Second, IBM was undertaking to develop a program for proving theorems in plane geometry (based on an idea of Marvin Minsky’s), ……. ……
In connection with IBM’s plane geometry project, Nathaniel Rochester and Herbert Gelernter (on the advice of McCarthy) decided to implement a list processing language within FORTRAN, ……. This work was undertaken by Herbert Gelernter and Carl Gerberich at IBM and led to FLPL, standing for FORTRAN List Processing Language. ……
I spent the summer of 1958 at the IBM Information Research Department at the invitation of Nathaniel Rochester and chose differentiating algebraic expressions as a sample problem. It led to the following innovations beyond FLPL:
a. Writing recursive function definitions using conditional expressions. …… b. The maplist function that forms a list of applications of a functional argument to the elements of a list. …… c. To use functions as arguments, one needs a notation for functions, and it seemed natural to use the λ-notation of Church (1941). I didn’t understand the rest of his book, so I wasn’t tempted to try to implement his more general mechanism for defining functions. Church used higher order functionals instead of using conditional expressions. Conditional expressions are much more readily implemented on computers. d. The recursive definition of differentiation made no provision for erasure of abandoned list structure. ……
The implementation of LISP began in Fall 1958. …… The programs to be hand-compiled were written in an informal notation called M-expressions intended to resemble FORTRAN as much as possible. - ^ John McCarthy. History of Lisp (PDF). Artificial Intelligence Laboratory, Stanford University. 12 February 1979 [2021-09-23]. (原始内容存档 (PDF)于2020-11-07).
To use functions as arguments, one needs a notation for functions, and it seemed natural to use the λ-notation of Church (1941). I didn’t understand the rest of his book, so I wasn’t tempted to try to implement his more general mechanism for defining functions. Church used higher order functionals instead of using conditional expressions. Conditional expressions are much more readily implemented on computers.
David Turner. Some History of Functional Programming Languages (PDF). [2021-10-25]. (原始内容 (PDF)存档于2020-04-15).LISP was not based on the lambda calculus, despite using the word “LAMBDA” to denote functions. At the time he invented LISP, McCarthy was aware of (Church 1941) but had not studied it. The theoretical model behind LISP was Kleene’s theory of first order recursive functions. (McCarthy made these statements, or very similar ones, in a contribution from the floor at the 1982 ACM symposium on LISP and functional programming in Pittsburgh. No written version of this exists, as far as know.)
- ^
John McCarthy. Recursive functions of symbolic expressions and their computation by machine, Part I. (PDF). Communications of the ACM (ACM New York, NY, USA). 1960, 3 (4): 184–195 [2021-02-24]. doi:10.1145/367177.367199. (原始内容 (PDF)存档于2021-02-19).
John McCarthy, Paul W. Abrahams, Daniel J. Edwards, Timothy P. Hart, Michael I. Levin. LISP 1.5 Programmer's Manual (PDF) 2nd. MIT Press. 1985 [1962] [2021-09-23]. ISBN 0-262-13011-4. (原始内容 (PDF)存档于2021-03-02).A function can be simply a name. In this case its meaning must be previously understood. A function may be defined by using the lambda notation and establishing a correspondence between the arguments and the variables used in a form. If the function is recursive, it must be given a name by using a label. ……
When a symbol stands for a function, the situation is similar to that in which a symbol stands for an argument. When a function is recursive, it must be given a name. This is done by means of the formLABEL
, which pairs the name with the function definition on the a-list. The name is then bound to the function definition, just as a variable is bound to its value.
In actual practice,LABEL
is seldom used. It is usually more convenient to attach the name to the definition in a uniform manner. This is done by putting on the property list of the name, the symbolEXPR
followed by the function definition. The pseudo-functiondefine
used at the beginning of this section accomplishes this. Whenapply
interprets a function represented by an atomic symbol, it searches the p-list of the atomic symbol before searching the current a-list. Thus adefine
will override aLABEL
. - ^ Guy L. Steele; Richard P. Gabriel. The Evolution of Lisp (PDF). In ACM/SIGPLAN Second History of Programming Languages. February 1996: 233–330 [2019-05-12]. ISBN 978-0-201-89502-5. doi:10.1145/234286.1057818. (原始内容存档 (PDF)于2020-11-12).
- ^ The memoir of Herbert A. Simon (1991), Models of My Life pp.189-190 ISBN 0-465-04640-1 claims that he, Al Newell, and Cliff Shaw are "...commonly adjudged to be the parents of [the] artificial intelligence [field]," for writing Logic Theorist, a program that proved theorems from Principia Mathematica automatically. To accomplish this, they had to invent a language and a paradigm that, viewed retrospectively, embeds functional programming.
- ^ Kenneth E. Iverson. A Programming Language. Wiley. 1962 [2021-09-24]. (原始内容存档于2019-04-01).
- ^ Can Programming Be Liberated from the von Neumann Style? A Functional Style and Its Algebra of Programs (PDF). [2019-05-12]. (原始内容存档 (PDF)于2020-11-08).
- ^ Turner, D.A. An Implementation of SASL. University of St. Andrews, Department of Computer Science Technical Report.
- ^ D.A. Turner. A New Implementation Technique for Applicative Languages (PDF). [2021-09-24]. (原始内容 (PDF)存档于2021-09-06).
- ^ R.M. Burstall. Design considerations for a functional programming language. Invited paper, Proc. Infotech State of the Art Conf. "The Software Revolution", Copenhagen, 45–57 (1977)
- ^ R.M. Burstall, J. Darlington. A transformation system for developing recursive programs. Journal of the Association for Computing Machinery: 24(1):44–67. 1977 [2021-09-24]. (原始内容存档于2020-01-28).
- ^ Rod Burstall, D.B. MacQueen, D.T. Sannella. Hope: An Experimental Applicative Language (PDF). 1980 [2021-09-24]. (原始内容 (PDF)存档于2022-01-28). Conference Record of the 1980 LISP Conference, Stanford University, pp. 136-143.
- ^ Make discovering assign() easier!. OpenSCAD. [2019-05-12]. (原始内容存档于2020-09-28).
- ^ Larry C. Paulson. ML for the Working Programmer. Cambridge University Press. 28 June 1996 [10 February 2013]. ISBN 978-0-521-56543-1. (原始内容存档于2020-04-09).
- ^ Odersky, Martin; Spoon, Lex; Venners, Bill. Programming in Scala: A Comprehensive Step-by-step Guide 2nd. Artima. December 13, 2010: 883/852 [2019-05-12]. ISBN 978-0-9815316-4-9. (原始内容存档于2016-04-30).
- ^ Ray, Baishakhi; Posnett, Daryl; Devanbu, Premkumar; Filkov, Vladimir. A large-scale study of programming languages and code quality in GitHub. Communications of the ACM. 2017-09-25, 60 (10): 92. doi:10.1145/3126905 (英语).
- ^ Piro, Christopher. Functional Programming at Facebook. CUFP 2009. 2009 [2009-08-29]. (原始内容存档于2009-10-17).
- ^ 1 million is so 2011 (页面存档备份,存于互联网档案馆) // WhatsApp blog, 2012-01-06: "the last important piece of our infrastracture is Erlang"
- ^ Momtahan, Lee. Scala at EDF Trading: Implementing a Domain-Specific Language for Derivative Pricing with Scala. CUFP 2009. 2009 [2009-08-29]. (原始内容存档于2009-10-17).
- ^ Emmanuel Schanzer of Bootstrap在TWiT.tv上的节目《Triangulation》的采访(英文)
- ^ Typed Racket (页面存档备份,存于互联网档案馆)
外部链接
[编辑]
|
|
|