15-150:函数式编程原理
15-150: Principles of Functional Programming

原始链接: https://brandonspark.github.io/150/

X 教授于 2023 年在卡内基梅隆大学担任函数式编程课程 15-150 的暑期讲师期间,教授了等价、绑定、归纳、递归、树、排序、多态性、高阶函数、组合子、延续等原理-传递风格、异常、正则表达式、结构和签名、函子、红黑树、惰性编程、命令式编程、编译器理论和程序分析。 他强调函数式编程的纪律性、面向类型、安全第一的方法优于传统的命令式方法。 该课程旨在帮助计算机科学专业的学生使用渐近分析、组合器组合和结构证明等技术更深入地思考性能数学、并行处理、模块化设计和程序验证。 通过学习这些技能,学生可能会发现他们对计算机科学的看法发生了转变,并更好地理解现代软件开发实践背后的理论基础。

Brandon Sparks 在最近与 Zulip 上托管的 Purple Room 社区服务器成员的聊天中提出了一个有趣的挑战。 他向其他程序员提出挑战,“使用任何编程语言编写一个程序,接受用户输入(在本例中为数字)并将该数据永久存储在内存中”,似乎主张避免通常被视为“无功能”的技术。 以下是我接受他的邀请参加此练习的解释和理由。 首先,我想澄清的是,虽然布兰登的主张建议永久存储信息,但这并不一定意味着使用命令式编程范例。 是的,在涉及内存分配的编程练习中,数据的永久存储存在技术含义。 然而,在具有声明式表达风格的函数式范例中,例如使用 SQL 或 DSL 的函数式变体(如 Dart 的 dart-json_schema)进行数据库集成,可以在纯函数式编程范例中模拟更改状态的行为。 该技术被称为“持久数据结构”范例。 通过打破我们对存储为全局内存或其他持久内存的可变变量的依赖和耦合,并将计算计算与强制更新分开,我们可以规避并有效地忽略功能主义排除永久存储的传统观点。 然而,在实践中,虽然有些人可能反对使用纯声明性方法通过持久数据结构来管理状态,但我相信使用命令式技术和纯功能性技术的组合(即采用混合模型)会产生最佳结果。 然而,暂时遵循布兰登的提议,让我们假设目标确实是在纯函数范式中编写一个程序,该范式包含与使用命令变量在整个程序执行过程中持续维护状态相关的行为和行为后果,而不是 而不是利用堆栈帧在动态作用域系统中临时分配易失性资源。 虽然我们确实必须牺牲函数纯度来追求这样的功能,但这并不意味着使用纯函数式编程技术对于涉及处理可变、外部化和共享资源的应用程序没有吸引力。 在基本层面上,人们可能会认为通过纯函数式编程技术实现持久数据结构会增加该过程的复杂性和细微差别。 从历史的角度来看,早期的函数式编程先驱拥护纯函数式编程
相关文章

原文

I had the pleasure of serving as the summer instructor for 15-150, the introduction functional programming class for computer science students at Carnegie Mellon, in the Summer 2023 semester.

This course typically serves as the second or third course in the traditional computer science undergraduate sequence, a privilege which not many other universities get to enjoy, as functional programming is often considered a niche topic.

Despite this, I (and CMU) believe this to be of the utmost importance. A disciplined, type-oriented, safety-first view of programming can be of utmost benefit to burgeoning computer science scholars, and I have often heard feedback from students that it is has a transformative view on their perspective of computer science in general.

To that end, I have made my lecture materials from my iteration of the course available for free on the Internet. Please feel free to use this knowledge in any way that you see fit, and I hope that it aids you in your future endeavors.

"Welcome to the rest of your life!"

"Equivalences save lives."

"Recursion is the bread and butter of doing anything in a functional language."

"Proving things about pretty much any kind of data you can imagine."

"Trees are the most fundamental data structure."

"A step is not always a step. We care about analyzing performance mathematically."

"What do we do when we have infinitely many processors?"

"Code reuse at different types, without breaking any of our safety properties."

"We can take in functions and also return functions, like any other value."

"You shouldn't have to wait on something unrelated when you can do work right now."

"CPS is the difference between writing instructions now, or remembering them later."

"Exceptions make mathematical thinking harder."

"Regular expressions are one of the most practical things you can learn."

"SML has one of the most sophisticated module systems of any programming language."

"Functors will show us how we can write code that depends upon modules."

"Red-black trees are a very good application of modules for protecting invariants."

"With sequences, we can keep immutability and also have the benefits of arrays."

"With laziness, only pay for for the computations you want to use."

"We invented immutability and purity, but we do not serve them. It's OK to be a little mutable."

"A long time ago, programming languages did not exist."

"We are solving an unsolvable problem."

"You can never go back, because you are forever a functional programmer."

联系我们 contact @ memedata.com