Computing Primitives

The science-fiction writer Arthur C. Clarke once wrote that “Any sufficiently advanced technology is indistinguishable from magic.” It makes sense then, that computers seem like magic to people. They are very advanced, and operate at speeds we can’t comprehend. But, as we learned in the last chapter, computers are just machines.

Just as a baker needs to follow a recipe when making something new, a computer needs a list of instructions to complete a task. We saw one such list of instructions - or algorithms in the last chapter when we doubled a binary number. At their core, every computer has an instruction set. Each of these instructions are very low-level, and may be different depending on whether the chip is an Intel, AMD, Arm, etc. We don’t think about writing algorithms at such a low-level, as that would be far too time consuming and error-prone. Instead, we write algorithms taking into account abstractions, which are higher-level processes that all computers can perform.

The basic abstractions for writing algorithms are:

  • Input
  • Output
  • Computation (sometimes called Processing)
  • Selection (also known as Branching)
  • Looping (often referred to as Iteration)

We will define each of these in the sections that follow. Notice that we aren’t talking about a specific programming language here, but simply defining at a higher level what operations all computers can perform. We will get to individual languages and their syntax later.

Input

Computers are machines, and all machines serve a function. The function of computers is to process data into information. You might think that data and information are the same thing, but in computing we see them differently. Data is unprocessed, and we can’t easily make choices by perceiving it. For instance, a list of 1,000 prices of apartments in New York City doesn’t tell us much. Information though, is actionable. This means it can help us make decisions. If we have a computer compute statistics about those apartment prices such as the mean, median, standard deviation, etc. then we can decide whether a particular apartment is a good deal or not. Knowing this, let’s update our definition of what a computer is. From here on, we define a computer as “a machine that processes data into information”.

Input is the process by which we give data to the computer. It may come in from a keyboard, a disk, a microphone, an Internet connection, or any number of other sources. Regardless, all computers need to be able to accept input.

Output

Computers output information. That information may be statistics about an apartment, but it could also be three dimensional audio, haptic feedback on a game controller, or an indicator on your car that you are low on gas. All computers provide some form of output.

Computation/Processing

All computers perform calculations. At their lowest-levels computers are simply performing math. While different computers may have different operations in their instruction sets, all of them should be able to perform computations. Sometimes we may want to store results from input, output, or computation in memory for use later. We still consider that storing (or retrieving the value later) to be computation.

In the last chapter, we talked about the Turing Machine, and how it would have an “infinitely long tape”. We noted that each “cell” on the tape could be given a number. Even though computers don’t use tape divided into cells, they do refer to each spot in memory with a number. We call those numbers addressess, because they tell the computer how to find that particular location of memory later. Computers often have billions of addresses, and humans aren’t that great at remembering lots of numbers. Instead, we typically refer to locations with names. For instance, we might call a particular value stored at some location as studentName, or id_number, or even just x. We call these variables. The next chapter will be all about variables. For now, just try to understand that a variable is a location in the computers memory that we store some data and give a temporary name to.

Selection/Branching

While operating on data, computers need to make choices about which code to run. For instance, a user might select to save a file they have been working on, or to send the file to a printer. Depending on which set of keys the user pressed, the computer needs to run the appropriate code to perform that function. Another example could be if a sensor on your thermostat at home registered that the temperature in your home went below you set level. In that case, the computer could decide to turn on your heat.

When writing algorithms, we think about all of the choices that the computer can make to solve a particular task, and we write code to handle all of those situations.

Looping/Iteration

One of the main reasons humans make machines is to reliably perform repetitive tasks for us. Computing is the same. Very often, we want to repeat a part of an algorithm in order to solve a larger problem. Perhaps we have a list of files, and we want the computer to delete each of them one at a time. Or, we may want an algorithm to load and play music files one after another. Being able to have the computer loop saves us a great deal of time.

Putting it all together

By putting these primitive abstractions together, we can come up with algorithms that we can reuse to solve problems. For instance, let’s say we wanted to find the largest value in a list of numbers. It might look something like this:

  • Get all of the values into a list. [Input]
  • Create a variable called biggest. [Computation]
  • Set biggest equal to the first value in our list. [Computation]
  • Begin looping over all the elements of the list, one at a time. [Looping]
    • If the current value is bigger than biggest, run the statement following this marked True. Otherwise, run the statement marked False. [Selection]
      • True: Change biggest to be equal to the current element. [Computation]
      • False: Continue the loop at the next value of the list. [Computation]
  • Print out the value of biggest. [Output]