编程语言与软件工程2024dl.acm.org
终极条件语法
The Ultimate Conditional Syntax
进阶 · 约 45 分钟读完前置知识:模式匹配、函数式编程基础、程序语言设计
导读
这篇论文研究如何用一种统一的条件语法取代传统的 if-then-else 与模式匹配分支,让多个布尔测试和模式解构可以串在同一条条件链里,并允许在分支前缀的任意位置拆分条件,从而更紧凑地表达程序意图。论文给出了该语法的设计与实现,并将其与现有语言中的条件/匹配结构作对比。
HN 关注点:HN 读者从语言设计角度讨论它:有人拿 C# 模式匹配、Elixir 的 with 和 lambda-zero 来对照,也有人质疑它相对传统模式匹配到底好在哪。
HN 评论精选
这几条评论里:支持 1 质疑 1 补充 2
支持C# 模式匹配几乎能做到,唯独缺少在任意位置拆分条件前缀;新语法一旦读懂就很有表达价值。
C# 的模式匹配已经很接近了,我认为除了论文最后一个例子(“在任意位置拆分条件前缀”)之外,C# 都能做到。这也是我转到 Rust 后怀念的东西之一。像这样:if foo(args) == 0 then "null" |> abs > 100 then "large" < 10 then "small" else "medium"。这段语法我在读论文时花了一会儿才解析明白,但我能想象日常代码里有很多地方用这种语法能更简洁地表达意图。
klauserc查看英文原文
C# pattern matching gets very close. I think C# can do everything except for their last example ("splitting conditional prefixes in arbitrary places"). One of the things I miss when switching to Rust. if foo(args) == 0 then "null" |> abs > 100 then "large" < 10 then "small" else "medium" That last syntax took me a while to parse in the paper, but I can imagine numerous places in our everyday code where such syntax would more concisely capture intent.
补充Elixir 的 with 已经提供类似的链式条件加模式匹配能力。
Elixir 早就有这个了——就是 with:with true <- is_email_address?(email), true <- String.length(code) === 6, %EmailConfirmation{} <- EmailConfirmations.get_email_confirmation(email), nil <- EmailAddresses.get_email_address(email), {:ok, user} <- Users.create_user(data), {:ok, email_address} <- EmailAddresses.create_email_address(user, email) do ... else ... end
huqedato查看英文原文
Elixir already has this - "with". https://www.openmymind.net/Elixirs-With-Statement/ E.g.: with true <- is_email_address?(email), true <- String.length(code) === 6, %EmailConfirmation{} <- EmailConfirmations.get_email_confirmation(email), nil <- EmailAddresses.get_email_address(email), {:ok, user} <- Users.create_user(data), {:ok, email_address} <- EmailAddresses.create_email_address(user, email) do ... else ... end
质疑质疑新语法相对传统模式匹配的实际优势。
我不明白它比传统模式匹配好在哪里。
mgaunard查看英文原文
I don't understand how it's better than traditional pattern matching.
补充与 lambda-zero 对比,指出后者每个条件只允许一次解构,而论文的语法更通用。
它看起来很像 lambda zero 的语法:def getNaturalName(tag, globals) if globals.lookup("0") is Just(Global(_, _, term)) if term is Numeral(_, type, _) return Just(Name(getTermTag(type))) error showSyntaxError("0 must be a numeral to use numerals", tag) return Void。不过这个“终极条件语法”更通用,因为 lambda zero 为了简化解析,每个条件只允许一次解构。
clark800查看英文原文
Looks very similar to lambda zero syntax (https://github.com/clark800/lambda-zero): def getNaturalName(tag, globals) if globals.lookup("0") is Just(Global(_, _, term)) if term is Numeral(_, type, _) return Just(Name(getTermTag(type))) error showSyntaxError("0 must be a numeral to use numerals", tag) return Void Though this ultimate conditional syntax is more general because lambda zero only allows one destructuring per conditional to simplify parsing.
相关资源
从这篇论文所有 HN 讨论帖的全部评论里整理出来的链接。
历次讨论
- 2024Hacker News 讨论帖145 分 · 48 评
同领域还值得读
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 分钟编辑精选