Functional Python Programming in Introductory Computer Science Courses

AI4SE Other SE
2025年12月03日
函数式编程范式历史悠久,起源于λ演算。近几十年来,像Haskell这样的纯函数式语言因其不可变的数据结构等诸多函数式特性,被证明在开发健壮软件方面极为有效。使用不可变数据结构的优势同样可以在Python等非函数式语言中实现。多年来,许多非函数式语言也引入了不可变数据结构以及推导式和lambda表达式,使得以纯粹的函数式风格进行编程成为可能。本文提出了一种在编程入门课程中的“最佳实践”方法:要求学生在Python的一个纯函数式子集中学习并完成编程作业。通过这种方式,学生能够掌握函数式编程的核心概念,例如数据不可变性、无副作用的纯函数以及无状态编程。我们定义了Python的一个函数式子集,并通过若干小型示例来说明这一最佳实践的具体应用。我们坚信,计算机相关专业的学生有必要熟悉纯函数式编程,并主张这种理念完全可以在以Python为教学语言的编程入门课程中加以传授。
The functional programming paradigm has a long and storied history, with its beginnings in the Lambda Calculus. In recent decades, pure functional languages such as Haskell have been shown to be highly effective in producing robust software due to immutable data structures, among other functional features. The advantages of programming with immutable data structures can also be had in non-functional languages such as Python. Over the years, non-functional languages have introduced immutable data structures as well as comprehension and lambda expressions, and it is possible to program in a purely functional style in them. In this paper, we present a ``best practice'' idea in introductory programming classes that forces students to learn and complete programming assignments in a purely functional subset of Python. By doing so, the student can learn functional ideas such as immutability, pure functions with no side effects, and stateless programming. We define a functional subset of Python and illustrate the best practice using small examples. We strongly feel that students in computing need familiarity with pure functional programming and argue that this can be taught in introductory programming courses that use Python.
许愿