Hello, world!

Turning words into numbers

Jack Holland
Understanding computer science
4 min readFeb 11, 2014

--

This is an ongoing series. Please check out the collection for the rest of the articles.

A couple of posts ago, I introduced the idea of digital transmission: converting a message into a sequence of digits like 0 and 1. This post, I’ll show you how to apply this principle to expand our favorite programming language, Cake. Right now, Cake is very limited because it has just three types of data:

  1. Numbers like 0, 1, 5.843, -10, and 101.0
  2. Booleans: true and false
  3. Arrays like [5, 10], [true], [ ], and [-1, 4, 15]

Now, using the ideas from the last post, we’ll add one more type:

  • Strings like “cat”, “Mount Olympus”, “Hi!”, and “”

Don’t be put off by the strange name — strings are just words with a more formal definition. Specifically, a string is an array of characters. So “boat” is represented as [‘b’, ‘o’, ‘a’, ‘t’]. That’s cumbersome to write, so instead, there’s shorthand: “boat”. These two forms are equivalent in Cake; one is just easier to read and write than the other.

You might have noticed that I skipped over something. “boat” in array form, [‘b’, ‘o’, ‘a’, ‘t’], uses some new syntax; we’ve never written ‘b’ or ‘o’ or any other letter enclosed in apostrophes before (note that ‘ is not the same as “). What are these specially marked letters? As the definition of a string implies, they’re characters. This is where the discussion of digital transmission comes in: each letter, digit, and symbol is uniquely linked to a sequence of digits. For instance, ‘A’ links to 1000001 , ‘B’ links to 1000010, ‘!’ links to 100001 , etc. Every letter, digit, and symbol commonly used in English gets its own sequence of digits, just like in Morse code. Based on ideas in the last post, we also know that any sequence of 0s and 1s is just a number in disguise (specifically, a base 2 number). So ‘A’ links to 1000001, which in base 10 is the number 65. ‘B’ links to 1000010, which in base 10 is 66.

There are different ways to map characters to numbers — we could make ‘A’ map to 100 or 1000 — but Cake uses a very prevalent encoding called ASCII. As suggested above, when you enter ‘A’ in Cake, Cake sees this as 65. So ‘A’ + 1 is 66, which just so happens to be linked to ‘B’. Each character has its own unique number to which it links.

Going a step further, when you enter a number in Cake, such as 65, deep down in the bowels of the computer, that number is represented in base 2: 1000001. Everything in Cake is secretly a number, and every one of those numbers is secretly a sequence of 0s and 1s.

As an aside, “sequence of 0s and 1s” is just another way of saying “binary number”. “bin-” is the prefix for 2 and “-ary” means pertaining to bases (among other things). From now on, I’ll use the term “binary number” or “base 2 number”.

Coming back to the original goal, let’s define strings with our new understanding: a string is an array of characters; a character is a letter, digit, or symbol enclosed in apostrophes; Cake automatically converts any character it sees into its corresponding number; thus, a string is just an array of numbers that, when properly interpreted, corresponds to a word.

Don’t forget, while strings are arrays, they are not usually written as such because they’re incredibly annoying to read and write that way. Cake and virtually all other programming languages that support strings support an easier way to write them: write the string like a regular word and enclose it in quotation marks. For example, “cheese” is convenient shorthand for [‘c’, ‘h’, ‘e’, ‘e’, ‘s’, ‘e’].

We are now ready to observe a most ancient and august tradition: writing a “Hello, world!” program. It’s common practice for beginners to write a simple program that prints “Hello, world!” (or “Hello world” or “hello world” — you get the point). It’s also used as a proof-of-concept for new languages. Think of it as the computer introducing itself to the world it’s a part of. It’s a great way to get your feet wet when learning a new language and now that we’ve covered strings, I think it’s time to write a “hello world” program in Cake:

A “hello world” program in Cake

Well, that was easy.

A toast-writing machine demonstrating its abilities

To be clear, the above program simply returns the value “Hello, world!”. Since Cake isn’t a real language, there’s no actual screen or other output device to view our results, and so we won’t bother making some bogus “print” instruction that doesn’t do anything. When we transition to using a real language, we’ll write a “hello world” program that actually prints the message to the screen. Until then, enjoy the simplicity and concision of Cake’s version.

Next up: strings, strings, and more strings! We’ve barely gotten started with them. After that, we’ll transition into a broad explanation of how electronic computers work and then how to use a terminal to manipulate the computer. Eventually, we’ll actually learn a real programming language. Whether all this will take 10 posts or 100, I’m not sure, but we’ll get there! As for the more long term plan, we’ve got quite a lot of basics to cover.

--

--