编程语言与软件工程2016cse.chalmers.se编辑精选
函数式编程为何重要
Why Functional Programming Matters (1984)
进阶 · 约 50 分钟读完前置知识:函数式编程基础、递归与高阶函数、惰性求值概念
导读
这篇论文讨论函数式编程为何重要,核心不是罗列语法技巧,而是论证函数式语言提供的两种“粘合剂”——高阶函数/函数组合与惰性求值——能显著提升程序模块化。作者认为,问题的分解方式取决于解的组合方式,语言若提供新的组合手段,就能改善概念层面的模块化。
HN 关注点:这篇经典论文对函数式编程模块化优势的论证,以及惰性求值、纯函数在实际工程中的代价与适用边界。
后来怎么样
这篇论文成为函数式编程推广与教学的经典论证,影响了后续函数式语言设计与教学;高阶函数和函数组合被现代语言广泛吸收,但把惰性求值作为默认策略的主张后来因空间与 IO 问题受到明显反思。
HN 评论精选
这几条评论里:质疑 2 补充 1
质疑批评只展示有利例子,指出函数式编程在状态、性能、底层场景的弱点,但认可纯函数对模块化与正确性的价值。
很多为函数式编程辩护的文章会列出一长串很能说明问题的例子。几乎任何语言都能这样做,而且这很有误导性,因为这种做法常用来劝初学者学另一门语言:“看这个刻意构造的例子在 <语言 X> 里写起来多简单!”要真正用好一门语言或范式,尤其有用的是知道它的弱点。函数式编程在处理大量状态的程序里表现不好,在对性能或内存要求高的程序里表现不好,在需要底层操作的程序里表现不好。这些是严重的缺点,我希望这类入门文章更经常强调它们。尽管如此,我认为函数式编程是写出更简单程序的必备能力,也认为程序员应尽可能写函数(即只依赖其参数的方法),出于模块化和正确性。我认为非纯函数式语言可以解决这些缺点,新语言在这个方向大有可为。
kutkloon7查看英文原文
A lot of pleas for functional programming give a long list of convincing examples that work well with functional programming. You can do this with almost any language, and this is also quite deceptional, as this is often done to convince beginners to learn another language; "look at how simple it is to program this contrived example in <language X>!". In order to be able to use a programming language or paradigm well, it is especially useful to know what its weaknesses are. Functional programming does not work well in programs which handle a lot of states. It does not work well in programs which have high requirements for performance or memory. It does not work well for programs which have to do low-level stuff. These are serious disadvantages, and I would like to see them highlighted more often in this introductory-style articles. Nevertheless, I think functional programming is a must to be able to write simpler programs, and I think programmers should write functions (i.e. methods that only depend on their arguments) whenever possible, for modularity and correctness reasons. I think the disadvantages can be solved by non-pure functional languages, and that there is a lot to gain for new programming languages in this area.
质疑以 Haskell 社区实例质疑惰性求值作为默认策略。
现在 /r/haskell 的头条链接[1]就是有人抱怨,程序因为惰性与 IO 之间微妙的交互而耗尽空间。我觉得可以稳妥地说,我们已经试过把惰性作为默认求值策略,并学到这是错误的默认,因为它会严重破坏空间行为和 IO。
cousin_it查看英文原文
The top link on /r/haskell right now [1] is someone complaining that their program runs out of space due to a subtle interaction between laziness and IO. I think it's safe to say that we have tried laziness as the default and have learned that it's the wrong default, because it plays havoc with space and with IO. [1] https://www.reddit.com/r/haskell/comments/5h6emf/haskell_run_out_of_memory/
补充强调论文核心是“粘合剂”决定模块化,但认为函数式编程只提供两种,仍需更多架构连接件。
对我来说,这是全文最重要的部分(第2页底部):“人们划分原问题的方式,直接取决于人们把解粘合起来的方式。因此,要提升人在概念上模块化问题的能力,语言就必须提供新的粘合方式。”函数式编程之所以好,是因为它提供了两种(新的)粘合剂:函数组合与惰性求值。即便你接受这一点(我完全接受函数组合,对惰性求值没那么热情),我也会说它只提供了两种新粘合剂。我们需要很多种粘合剂,也就是很多架构连接件;这意味着需要能定义和变换架构连接件的语言手段。http://objective.st
mpweiher查看英文原文
To me, this is the most important part of the text (p2, bottom): "The ways in which one can divide up the original problem dep end directly on the ways in which one can glue solutions together Therefore to increase ones ability to mo dularise a problem conceptually one must provide new kinds of glue in the programming language." Functional programming is great because it provides two (new) kinds of glue: function composition and lazy evaluation. Even if you accept that (I certainly do for function composition, less enthusiastic about lazy evaluation), I would say that it only provides two new kinds of glue. We need lots of kinds of glue, in other words, lots of architectural connectors. And that means linguistic means of defining and varying architectural connectors. http://objective.st
相关资源
从这篇论文所有 HN 讨论帖的全部评论里整理出来的链接。
历次讨论
- 2016Hacker News 讨论帖181 分 · 140 评
同领域还值得读
Google 的软件工程(2017)
Software Engineering at Google (2017)
介绍了 Google 的软件工程实践,包括其单一代码库(monorepo)、几乎全部开发在主干进行而非分支、代码审查、构建系统 Blaze、协议缓冲区等工具与流程,并讨论了频繁重…
编程语言与软件工程2017 · 1,127 分 · 465 评 · 引用 15入门 · 约 45 分钟编辑精选Peter Naur:编程作为理论构建 (1985)
Peter Naur – Programming as Theory Building (1985)
Peter Naur 1985 年的经典论文《Programming as Theory Building》提出:编程的本质不是写代码或文档,而是在程序员头脑中构建对程序及其运行…
编程语言与软件工程2016 · 629 分 · 74 评入门 · 约 45 分钟编辑精选SciPy 1.0:Python 科学计算的基础算法
SciPy 1.0: fundamental algorithms for scientific computing in Python
这篇发表于 Nature Methods 的论文介绍了 SciPy 1.0——Python 科学计算生态的基石库,涵盖其核心算法模块(优化、积分、插值、线性代数等)、软件架构与社…
编程语言与软件工程2020 · 387 分 · 98 评入门 · 约 45 分钟编辑精选Lisp 的历史(1979)
History of Lisp (1979)
这是 Lisp 发明者 John McCarthy 于 1979 年为 HOPL(编程语言史)会议撰写的回顾文章,讲述 Lisp 的起源、设计动机与早期演进,文中也涉及 if 表…
编程语言与软件工程2018 · 352 分 · 44 评入门 · 约 45 分钟编辑精选使用 NumPy 进行数组编程
Array Programming with NumPy
这篇 Nature 文章介绍 NumPy 的数组编程模型及其在科学计算中的核心作用:以 ndarray 为中心,通过向量化操作让 Python 高效处理多维数组。
编程语言与软件工程2020 · 318 分 · 134 评入门 · 约 40 分钟编辑精选