Whitespace Programming Language: Features, Code & Examples

whitespace programming language

Have you ever opened a code file and wondered if it was empty? In the whitespace programming language, that is exactly what it’s all about. There is a code there but you’re not seeing it. All instructions consist of spaces, tabs and line breaks, and any other text is ignored and considered a comment. Weird, right?

It’s hilarious really and started out as a joke. However, the whitespace programming language is a truly real and functional language and it’s possible to write real programs in it. It’s one of those obscure tongues that makes you marvel at the meaning of “code.”

In this article I will explain about it, how it works, and its primary components. We’ll take a look at some examples of code, such as: Hello World and I’ll let you know which interpreters you can use to run your own programs.

What Is the Whitespace Programming Language? (Description)

So, what exactly is the whitespace programming language? In other words, it is a secret language with only 3 characters that are significant: space, tab, and linefeed (new line). It’s only those three that are the whole vocabulary. Anything else that you type (letters, numbers, symbols) is ignored altogether.

The cool part is that and the annoying part is that. Because the computer doesn’t read the text that is visible, it is possible to hide a working program in a normal looking document. Just put notes for yourself in the middle of your code and there are no breaks.

To remember the description of the whitespace programming language, it’s code you can’t see. Well, no one really uses it for actual projects, anyway. It’s more of a fun, puzzles and learning how languages actually work under the hood.

History and Origin of the Whitespace Programming Language

The whitespace programming language was developed by Edwin Brady and Chris Morris of the University of Durham, UK. The day they released it was April 1, 2003, April Fools’ Day, so that’s a pretty good gauge of how seriously they took the idea the first time they had it.

The inspiration was pretty simple. A lot of languages have lots of syntax visible, such as brackets, semicolons, keywords, etc. So, they turned it around and thought, “What if the visible stuff wasn’t even that important? The first one was even written in Haskell!

See also  PyTorch for Machine Learning: Unleashing the Power

It is compatible with other esoteric programming languages, such as Befunge, Malbolge and Brainf*ck. All of them were not practical. They’re designed to be weird and to demonstrate the minimal requirements of a language. 

Key Features of the Whitespace Programming Language

In this regard, the whitespace programming language is different from all other languages:

1. Only three characters count: Space, tab, and linefeed. So much of the syntax isn’t very complex, and that was the whole thing when you consider it.

2. Stack-based: A lot of work is done on the stack. You push numbers on, pop numbers off and perform math on the numbers on top.

3. Heap access: You have a heap in addition to a stack and you can store values there and retrieve them after. It’s convenient for anything of a size.

4. Imperative style: You give the computer instructions, one after another, in order. Nothing fancy there.

5. Turing complete: It can do anything a “normal” language can, strange as it may look. But it takes a lot more work.

6. Comments anywhere: Any text that’s visible is ignored, so you can drop notes anywhere in your code.

7. Binary numbers with a sign bit: The numbers are represented in binary with spaces and tabs, with the first character indicating whether the number is positive or negative.

Also Read: Want to explore languages that are actually worth your time? Check out our guide on the most useful programming languages to learn. 

How Whitespace Programming Language Code Works

All of the instructions in the whitespace programming language begin with a short prefix known as an IMP (Instruction Modification Parameter). It basically provides the computer information about the command that will follow. Next, a couple more characters whittle it down to the specific action.

Category IMP 
Stack manipulation Space 
Arithmetic Tab Space 
Heap access Tab Tab 
Flow control Linefeed 
Input/Output Tab Linefeed 

Numbers are represented in the binary system. The sign is the first character (space for positive, tab for negative) and space = 0, tab = 1. The number ends with a linefeed. Label statements and labels for jumps and loops are the same: a string of spaces and tabs followed by a linefeed.

In fact, nobody can read raw whitespace so people tend to include S (space), T (tab), and L (linefeed). If you push the number 5 onto the stack, this would be S S S T S T L.

See also  The Powerful Guide on Python Programming Module

A few tips that’ll save you some headaches:

  • Write in S/T/L and then convert.
  • Enable the “show whitespace” option in your editor.
  • Put visible comments to avoid losing your way.
  • Keep programs tiny. Seriously.

If you understand the pattern, the whitespace programming language is not so intimidating as it seems.

Whitespace Programming Language Hello World Example

It’s time for the old-fashioned. There’s one simple pattern in a whitespace programming language Hello World program: push ASCII code of character on the stack, print, repeat. When you’re done, end the program.

These are the three instructions you have to use (in S/T/L form):

  • Push a number: S S + sign + binary + L
  • Print a character: T L S S
  • End the program: L L L

Let’s construct the letter “H”. Its ASCII code is 72, which is 1001000 in binary. Space is 0 and Tab is 1, therefore it is:

S S S T S S T S S S L   (push 72)
T L S S                 (print it as “H”)

The first S S means “push”, the third S is the positive sign, T S S T S S S is the binary for 72, and L closes the number.

Now here’s the full “Hello” in S/T/L:

S S S T S S T S S S L   push 72 (H)
T L S S                 print
S S S T T S S T S T L   push 101 (e)
T L S S                 print
S S S T T S T T S S L   push 108 (l)
T L S S                 print
S S S T T S T T S S L   push 108 (l)
T L S S                 print
S S S T T S T T T T L   push 111 (o)
T L S S                 print
L L L                   end

Expected output: Hello

If you want to write the whole “Hello, World!”, you simply continue to add the ASCII codes of the characters. Two lines repeated and repeated and repeated — that’s the whitespace programming language for you — and it gets long.

To execute it, change the S, T, and L to the actual spaces, tabs, and line breaks, and then copy and paste it into an interpreter.

Whitespace Programming Language Interpreter: Tools to Run Code

An interpreter is simply a program that reads the code and executes it, line by line. It’s necessary because your computer cannot tell what to do with a file in which every line contains spaces and tabs. If it’s not translated into a whitespace programming language, your whitespace programming language code is just a blank page.

The good news is you’ve got options:

  • Online interpreters: The most convenient way to begin is to get an online interpreter. Try It Online (tio.run) is compatible with Whitespace, and there is a web-based Whitespace IDE to step through and watch the stack change. Paste, run, done.
  • The original Haskell interpreter: Edwin Brady wrote it, which is where things began.
  • Python, C, and JavaScript versions: There are many of these that have been developed in Python, C and JavaScript versions. You’ll find them by doing a quick Google search on GitHub.
See also  8 No-Brainer Programming Languages For Robotics For 2023

Here’s how to run your first program:

  1. Your code should be written in S/T/L first.
  2. Replace S, T, L with real spaces, tabs, and line breaks.
  3. Copy and paste it in the interpreter.
  4. Try run and see

One tip: use “show whitespace” in your editor. Some editors will edit tabs to spaces without being told, and this will silently destroy everything.

Advantages and Limitations of the Whitespace Programming Language

Come on guys, no one chooses this language for a serious project. There are some actually good benefits to it, and some obvious drawbacks.

Advantages

  • Great for learning: You have to build all of this from scratch using a stack and simple jumps, so you begin to see how the programs really work under the hood.
  • Fun and creative: It’s a good brain puzzle. Feeling like writing a small program is a small victory..
  • A new way to see syntax: It demonstrates how little it takes for a language to work, and that’s quite an eye opener.

Limitations

  • Unreadable: Cannot read and comprehend code. Not even a little.
  • Hard to debug: One missing space or a tab your editor didn’t notice and you’re left with no idea what went wrong.
  • Not practical: No true library support, it’s too slow to write anything useful. Real projects are out of the question.

Is Whitespace a Good Language for Learning Programming Concepts?

In a nutshell, it’s yes, but not as a first language. If you are a novice programmer, begin with a language such as Python. If you have white space just before you learn, you will just get frustrated.

Once you understand the fundamentals though, it’s a cool way to check out under the hood. You are not given a variables or loops to work with. Pushes values onto a stack, pops them off and uses labels and jumps to construct your own loops. Well, it’s pretty much like assembling but you get a feel for low-level thinking.

The difficulty is debugging. If you click on the wrong tab, you’ll end up only seeing the invisible characters. So don’t use it as a learning path, it’s a fun side project, just for the brain to stretch.

Final Thoughts

That’s the short version of whitespace programming language. It’s weird, a bit goofy and, come to think of it, sort of fun when you master it. Now you’ve learned how it works, what its features are, and how to write a Hello World with spaces, tabs and line breaks only.

Do you intend to use it in your job? Probably not. Playing with whitespace is a good way to work your brain, and understand the essence of code. Use an interpreter to do an example of Hello World and see if you can make some changes to it yourself.

But if you find yourself in a coding dilemma, or just need more programming guides, look at the other resources on Statanalytica. We’re here to help!

FAQs

1. Is Whitespace a real programming language?

Yes, it is it and it is working! It was designed to be a joke in 2003, but programs can be written and executed in it by using an interpreter.

2. Is Whitespace Turing complete?

Yes, it is. Although it is composed of only spaces, tabs, and line breaks, it is capable of calculating anything that a normal language can. Just requires more work.

3. How do I run Whitespace code?

An interpreter is required. You can copy and paste it into an online one such as tio.run or use a Python, C or JavaScript version on your own computer.

4. Who created Whitespace?

It is developed by Edwin Brady and Chris Morris, from the University of Durham. It was their April Fools’ Day release on April 1, 2003.

Leave a Comment

Your email address will not be published. Required fields are marked *