Teaching Kids Forth

Teaching Kids Forth / 教孩子们学 Forth

2026-04-14 · 11 min · #computing

Forth is uncommonly used to teach programming to kids. Scripting languages like Python and visual environments like Scratch are far more popular options. But when I got the opportunity to introduce middle and high school students to programming, I chose the stack-oriented concatenative language from 1970. I was ecstatic about this opportunity. Forth 很少被用于教孩子们编程。像 Python 这样的脚本语言和 Scratch 这样的可视化环境是更受欢迎的选择。但当我有机会向中学生介绍编程时,我选择了这门诞生于 1970 年的基于栈的连接式语言。我对这个机会感到非常兴奋。

I’m passionate about giving people just a bit more control over the potential locked up in the devices they carry with them every day. When people know more about computers in general and about making their own software in particular, whole worlds open up. This gave me the chance to pry the door to that world open for people who likely would have never discovered it otherwise. 我热衷于让人们对自己每天随身携带的设备中所蕴含的潜力拥有更多的掌控权。当人们对计算机有了更深入的了解,尤其是学会了如何制作自己的软件时,整个世界都会为他们敞开。这让我有机会为那些原本可能永远不会发现这个世界的人们撬开大门。

While I had taught people programming to individuals previously, this was my first experience teaching in a classroom environment. I went from knowing next to nothing about teaching to designing my own curriculum, including lesson plans, curriculum, and a digital learning environment. 虽然我之前曾一对一地教过别人编程,但这还是我第一次在课堂环境中教学。我从对教学一窍不通,到设计出自己的课程体系,包括教案、课程大纲和数字学习环境。

Why Forth? I could have chose to teach any programming language. I could have chosen Python or Scratch, but I wanted to explore a path less commonly chosen and maybe find some gems that are otherwise overlooked. Because of the limited amount of time I had to teach, and the fact that I wanted to teach more fundamental concepts, I wanted to avoid teaching syntax. 为什么选择 Forth?我本可以选择任何编程语言。我可以选择 Python 或 Scratch,但我想要探索一条不那么常见的路径,或许能发现一些被忽视的瑰宝。由于教学时间有限,且我希望教授更基础的概念,因此我想避免教授复杂的语法。

While Python makes sense as pseudocode to someone who understands what code is, it is still nearly incomprehensible to new learners, which I discovered in previous years teaching programming one-on-one. I decided I wanted to either teach Forth or Haskell. Forth was appealing because it’s a concatenative language. You write the instructions and it simply executes everything in order. As long as you understand what a stack is you can write Forth programs. 虽然对于理解代码的人来说,Python 看起来像伪代码,但对于初学者来说,它几乎是难以理解的,这是我在过去几年一对一教学中发现的。我决定要么教 Forth,要么教 Haskell。Forth 的吸引力在于它是一门连接式语言。你写下指令,它就会按顺序执行。只要你理解什么是栈,你就能编写 Forth 程序。

Haskell was appealing because it integrates well with how students are already taught about how to think about variables and functions in mathematics. I’ve seen too many new programmers get confused that in programming variables are boxes that can contain any value instead of the algebraic definitions they’ve learned in math class. Haskell 的吸引力在于它与学生在数学课上学习的变量和函数思维方式结合得很好。我见过太多新程序员感到困惑,因为在编程中,变量是可以包含任何值的“盒子”,而不是他们在数学课上学到的代数定义。

I ended up choosing Forth for two reasons. First, it was easier to integrate into a custom learning environment I could prepare for the students. Second, I figured procedural programming would be easier to conceptualize than functional programming for the in-class final demo. As a side benefit, the use of a semi-obscure language like Forth ensured that even students who did programming before would learn something from the experience without alienating new learners. 最终我选择了 Forth,原因有二。首先,它更容易集成到我为学生准备的自定义学习环境中。其次,我认为对于课堂上的最终演示来说,过程式编程比函数式编程更容易概念化。额外的好处是,使用像 Forth 这样相对冷门的语言,确保了即使是以前学过编程的学生也能从这次经历中学到东西,同时也不会让初学者感到疏远。

Designing the Curriculum / 设计课程

When designing the curriculum, I knew I would be pressed for time. I had 12 sessions, once per week, each one hour long. I also knew it wasn’t likely they were going to complete much homework if I had assigned it considering they had lives outside of my class. 在设计课程时,我知道时间会很紧。我有 12 节课,每周一次,每节课一小时。我也知道,考虑到学生们在课堂之外还有自己的生活,如果我布置作业,他们很可能无法完成太多。

The tangible learning outcome was for students to create their own art in the Forth environment. This could be anything from a simple drawing to an animation to a game. I wanted them to have something they could see and keep with them after the course had ended. It can be discouraging to learn a lot of technical details on a topic and feel like you had little to show for it. 具体的学习成果是让学生在 Forth 环境中创作自己的艺术作品。这可以是简单的绘图、动画或游戏。我希望他们在课程结束后能有一些看得见、留得住的东西。如果学习了大量的技术细节却感觉没什么成果,是会让人感到沮丧的。

It was important that the students could run the code they wrote on their own computers. It was doubly important that they didn’t have to download or install anything for it to work, since students could be using a variety of operating systems—some of which may not be capable of installing third party software. 重要的是学生能在自己的电脑上运行他们编写的代码。同样重要的是,他们不需要下载或安装任何东西就能运行,因为学生们可能使用各种不同的操作系统——其中一些可能无法安装第三方软件。

My first choice for our learning environment involved writing my own Forth from scratch in Rust and embedding it in our course website with WASM. Unfortunately, ironing out the bugs in my implementation took longer than I intended. So I switched gears and chose to use Nick Morgan’s Easy Forth as both our curriculum and Forth interpreter. For each lesson, I created slides and a lesson plan which I uploaded to an online portal which the students could access. This included a copy of Easy Forth. 我最初的学习环境方案是用 Rust 从零开始编写自己的 Forth,并通过 WASM 嵌入到我们的课程网站中。不幸的是,修复我实现中的 Bug 花费的时间比预期的要长。所以我改变了策略,选择使用 Nick Morgan 的 Easy Forth 作为我们的课程教材和 Forth 解释器。我为每节课制作了幻灯片和教案,并上传到学生可以访问的在线门户网站上。其中就包含了 Easy Forth 的副本。

Introducing Computing / 引入计算概念

It was then time to start going over the actual lessons with the students. I plugged my laptop into the large TV to display my slides and began the introductory lecture. We started by going over the history of computing and what a computer is. One of my favorite parts was showing them various devices and asking them if they were computers. They were initially perplexed when I showed them an abacus and told them it was a computer. 接下来是时候开始给学生上课了。我把笔记本电脑连接到大电视上展示幻灯片,并开始了入门讲座。我们首先回顾了计算的历史以及什么是计算机。我最喜欢的部分之一是向他们展示各种设备,并问他们这些是不是计算机。当我向他们展示算盘并告诉他们这是计算机时,他们起初感到困惑。

Our working definition was, “Anything that calculates numbers.” They soon realized that almost anything could be a computer, including themselves! This is a computer! After breaking their idea of what a computer could be and setting their expectations, I began to teach them about Forth in particular. I explained the places it’s used and its embedded applications. Once the fundamentals were down, the next step was to move to our curriculum and dive into Forth. 我们的工作定义是:“任何计算数字的东西。”他们很快意识到几乎任何东西都可以是计算机,包括他们自己!这就是一台计算机!在打破了他们对计算机的固有印象并设定了预期之后,我开始专门教他们 Forth。我解释了它的使用场景和嵌入式应用。一旦基础打好,下一步就是进入我们的课程,深入学习 Forth。

However, Easy Forth assumes its readers know at least one programming language and know about stack-based data structures. That was far from the case here. It was my job to bridge that gap. 然而,Easy Forth 假设读者至少了解一种编程语言,并且知道基于栈的数据结构。但在这里情况远非如此。我的工作就是填补这一空白。

What is a Stack? / 什么是栈?

Before even touching our computers, we had to begin to visualize the fundamental data structure underlying Forth: the data stack. Anyone with a rudimentary computer science education will understand what a data stack is and its operations, but normally that’s not the first thing introduced to new learners. So I used plates. 在接触电脑之前,我们必须先可视化 Forth 底层的基本数据结构:数据栈。任何受过基础计算机科学教育的人都会理解什么是数据栈及其操作,但这通常不是向初学者介绍的第一件事。所以我用了盘子来演示。

In Forth, when you type numbers surrounded by whitespace, they are added to the stack. For example, this adds 1, 2, and 3 to the data stack: 1 2 3. If you were to print these numbers, such as with ., the outputted text would be 3 2 1. This is because a stack is a Last-In First-Out data structure. You can either “push” an item onto a stack (put it on top), or pop an item off a… 在 Forth 中,当你输入被空格包围的数字时,它们会被添加到栈中。例如,这会将 1、2 和 3 添加到数据栈中:1 2 3。如果你要打印这些数字(例如使用 .),输出的文本将是 3 2 1。这是因为栈是一种“后进先出”(LIFO)的数据结构。你可以将一个项目“压入”(push)栈中(放在最上面),或者从栈中“弹出”(pop)一个项目……