编程语言与软件工程2024dl.acm.org
C 语言中循环的常见误解
Misconceptions about loops in C
进阶 · 约 25 分钟读完前置知识:C 语言基础、控制流与 goto、编译器优化基础
导读
这篇论文讨论 C 语言中关于循环的常见误解,梳理循环的语义与控制流结构,并澄清一些容易被误读的细节。讨论涉及 `for`/`while` 等结构如何被理解、表示和优化。
HN 关注点:是因为它触及循环与尾递归、`goto`/`return` 以及编译器控制流优化这些基础但常被误解的话题。
HN 评论精选
这几条评论里:支持 1 补充 1 亲历 1
补充循环与尾递归可互转,C 缺少嵌套函数使尾递归写法不便。
我觉得很遗憾的是,大多数程序员都是先学 C 风格循环、后学尾递归。但这也是标准 C 不允许在函数内部定义函数这一问题的表现。循环总可以用尾递归表达;反过来也成立,但很多情况下并不直接,因为一个尾递归函数可能有多个退出点,而这些退出点必须合并成一个标志位才能放进循环。
diffxx查看英文原文
I think it's really unfortunate that most programmers are taught c style loops before tail recursion. But this is also a symptom of standard c's problem of not allowing one to define functions inside of functions. A loop can always be expressed with tail recursion. While the reverse is also true, there are many problems for which this is not straightforward given that a tail recursive function can have multiple exit points and these have to be coalesced into a single flag for the loop.
亲历把控制流拆成 `if`/`goto` 再用图算法重建循环,是编译器做循环优化的成熟思路。
在 80 年代,C 编译器识别 `for` 循环并对其优化是很常见的。我参加过斯坦福一个关于数据流分析的暑期课程,并利用了这一点。我的编译器会把所有 C 控制流结构拆成 `if` 和 `goto` 语句;然后优化器用图算法重建循环。这样,不管程序员写出多复杂的 `for`、`while`、`break`、`do-while`、`continue`、`switch` 或 `goto` 组合,优化器都能找出所有循环,识别循环变量、循环头等,然后进行优化。
WalterBright查看英文原文
Back in the 80's it was normal for C compilers to recognize `for` loops and do optimizations on them. I had attended a summer class at Stanford on Data Flow Analysis, and took advantage. My compiler would break all the C flow control constructs into `if` and `goto` statements. Then, the optimizer would reconstruct the loops using graph algorithms. Then, no matter what snarl of for, while, break, do-while, continue switch, or goto statements the programmer wrote, the optimizer could find all the loops, identify the loop variables, the loop header, etc. and then optimize it.
支持对编译器初学者有价值,但对资深编译器开发者不是新知识。
很高兴看到这篇整理——它应该成为编译器初学者的必读材料。但对这个领域有经验的人来说,这些内容并不新鲜。
pizlonator查看英文原文
It’s great to see this write up - should be mandatory reading for anyone getting started with compilers. But it’s not news for the experienced folks in the field.
历次讨论
- 2024Hacker News 讨论帖166 分 · 182 评
同领域还值得读
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 分钟编辑精选