Forth for R

3 minute read

Published:

I’ve always enjoyed learning about different programming languages. Different languages come with different specialties, paradigms, and constraints. As said in a recent talk at Strange Loop, the languages we know affect how we think about and approach problems.

This effect is especially pronounced in languages with significantly different paradigms. For example, while there are certainly differences between C and C++, their core features are very similar. In contrast, a Haskell specialist will likely have a much different approach to problem solving than a C++ veteran. It’s these languages that introduce totally new ways to write code that really interest me, which brings me to Forth.

Go Forth and code!

If you’ve never heard of Forth, it’s an interesting little language. Like Python, Forth is an interpreted language with a REPL and no enforcement of variable types. Like C, Forth gives you unfettered direct access to your system’s memory, and like Java, it compiles your code on the fly to optimize runtime. Oh, and did I mention it doesn’t have variables? (sort of)

Forth was also released prior to C in 1970. Since then, it has powered several spaceflight missions, the Open Firmware boot system, and many other projects. It also happens to double as an operating system, and is one of the most lightweight languages out there. A complete Forth system (OS, compiler, interpreter, and space for user data) could fit in memory on an 8-bit system. That’s less than 64KB!

Programming languages are typically classified by their features. For example, you’re probably heard of object-oriented programming languages, like Java or Python. Forth falls into a unique little realm of programming languages called “stack-oriented” languages. This is because everything Forth does revolves around a single stack—all your values get pushed to a single stack, and all functions work on that same stack.

Cool! Who cares?

Hopefully at this point, I’ve given a sufficiently good exposition of Forth to make you at least moderately interested in it. I mentioned earlier that using different programming languages helps change how your approach problems. However, one of the largest barriers for me to work in new languages is the entry point. Working in a new language is often frustrating, and experienced programmers usually have a workflow they’re used to for their normal languages.

That’s what I sought to address with the froth package. Rather than making you go out and figure out how to download, install, and start working in Forth, froth lets you work directly in a Forth-like environment from R. You can use all the aspects of R you’re used to (RStudio, R objects, the R profiler, etc.) but still try out working in Forth!

How do I get started?

You can download the froth package from CRAN with

install.packages("froth")

From there, you can enter a Forth environment by just running froth(), and you can quit the environment by typing exit or quit.

What can I do with this?

I’ve shown you how to start a Forth environment in R, but not actually what to do with it. Like I mentioned, Forth is a complete programming language with its own syntax distinct from R. My implementation of froth doesn’t have all the features of full Forth systems, but it has enough to get you started.

The go-to resource for Forth is the Starting Forth textbook. To help you get started, I’ve adapted this book into a tutorial for getting started with froth, which is available on the the package website. I’ll leave you with the go-to first program for any new programming language:

> library(froth)
> froth()
fr> ." Hello, world!\n"
Hello, world!
ok.

Good luck in your Forth journey!