Programming is not Algebra

A friend discovered a small interest in learning to program computers. She tried an online course but she didn’t get far before losing interest. She may have had other reasons for giving it up but by watching her and asking questions I did locate one significant hurdle: she was trying to read code through the lens of algebra.

I have seen this before. A student encounters a word, symbol, or pattern they recognize from some other context. Believing they have sufficient information to understand it in the new context, they push ahead. Soon their mental model fills with contradictions and the entire subject becomes intractable. This state of affairs causes people to quit their studies.

I’ve been there. Fortunately I learned the solution:

  1. Find what word or symbol was misunderstood.
  2. Learn the correct meaning.
  3. Re-study the material from the first occurrence.

The hardest part is completing the first step. This is especially true of students who already know a great deal and who are confident in their knowledge. Preconceptions hide themselves well but they surrender easily once found. Most people will be eager to complete the remaining steps after they have identified a real misunderstanding.

In this article I will expose some common preconceptions which can cause students of programming to quit. Teachers of beginner programming classes and writers of books would do well to treat these handicaps before setting students to code.

Remembering Algebra

The root of the problem is that programming uses many of the same symbols used by other disciplines. Some of the basic features of programming which are deceptively similar in algebra are variables, the equals sign =, functions, and lists of statements.

In elementary algebra a variable may refer to a quantity which is known or unknown. The context determines the type and meaning of each variable. It could be independent or dependent, variable or constant, free or bound, or even indeterminate.

Equations are similarly diverse in algebra. They are used express:

  • laws: a + b = b + a
  • functions: f(x, y) = 2x + 5y
  • and systems that may or may not have solutions:
    \left\{ {x+y=1\atop 0x+0y=2}\right.

Most adults who attempt to learn programming will have taken an elementary algebra class where they learned to substitute letters for numbers and to follow strict rules for manipulating equations. If they try to understand code through the lens of algebra they are in for a headache.

Programming is not like Algebra

Code can look like algebra problems. This is especially common in beginner-level examples. While a technological polyglot can see familiar symbols in fixed-width fonts and mentally execute the code, a person new to the field might attempt to understand it by applying the most similar subject in their repertoire: algebra. If you are teaching or writing about programming for beginners you should learn to anticipate and identify this mistake.

An exercise in role playing will illustrate this point. If you are a coder, try to forget what you know and read the following code as if its only parallel in your memory is your algebra lessons.

Sample Code

a = 10;
b = 20;
a = b;

// What are the values of a and b?

“Um… 10 and 20… but then how can a be equal to b? Programming is not for me.”

This is an actual response from my beginner friend. She had not progressed with her online course so I showed her the example above (from Programming Sheep) as a diagnostic.

Instead of a program, she saw three statements of equality and assumed they were a system of simultaneous equations. These preconceptions could have been handled by a simple introductory lesson defining the words “program” and “variable” and the symbol “=”.

The Simple Things

People who have been coding for a long time forget that their variables and equals signs are incompatible with algebra. My friend might have passed the sheep test if her “beginner course” had taught her only these basic definitions:

not like algebra
a sequence of instructions to be executed in order
a name which can be assigned a value
Assignment (a = b)
an instruction to assign to the variable a the value of b

Instead the course focused on instant gratification through immediately visible results. Lesson one launched a code editor and made the student call functions to display a message on the screen. She complied but did not understand. And with that, she was gone.

Goodbye, Students

Hello World is no basis for teaching programming to beginners! By assuming the student knows what programming is and how assignment works, the instructor relies on the student’s intuitive ability to induce the basic definitions for themselves. That way lies failure for many students because they are handicapped by earlier studies such as algebra.

It could be argued that an initially steep learning curve is good for the discipline because it filters out people who are not sufficiently clever or driven. I would counter that any teacher who intentionally leaves these pitfalls in place is not sufficiently clever or driven.

Sadly, academia and industry accept a high drop-out rate as if it were a natural consequence of the subject matter or some natural characteristic of the failing student. This acceptance is wrong. The drop-out rate is just as much a consequence of the teaching. Teachers who fail that many students have failed themselves.

Making it Better

Maybe intuition was how most professionals got over these first hurdles. Because we were never taught the basics, we don’t know how to teach them. Worse, we don’t even know that they must be taught.

If you write or teach a beginning programming course, or anything course, please invest some time into defining the basic terms. And if you find your students failing, please try to find out what word or symbol they did not correctly understand.

Cues from Knuth

In The Art of Computer Programming, Volume 1, Donald Knuth presents a most effective way to teach the meaning of programs, variables, assignment and control structures. On pages xiixiv the reader is instructed to study TAOCP by faithfully executing a program. The program is presented as both a flow chart and a list of written instructions:

3. Set N equal to 1.
4. Begin reading chapter N. …
16. Increase N by one. …
17. If N is less than 12, go back to step 4.
18. Congratulations. Now try to get your friends to purchase a copy of Volume 1 and to start reading it. Also, go back to step 3.

The student is made to emulate a computer even before the first chapter can begin. Expert programmers would agree that one of their most indispensable traits is their ability to mentally trace the execution of their programs–to emulate a computer. It makes sense to cultivate that ability as early as possible. If you are planning a lesson in programming for beginners, you could hardly do better than to follow Knuth’s example.

15 thoughts on “Programming is not Algebra”

  1. Well… this is the whole argument behind FP. To emulate a computer in my head…??? While it is a useful skill… is that a productive use of my time?

    And… assignment is complicated.

  2. I remember having that moment – it was when I saw (in a QBASIC program), “A = A + 1”. It really threw me – how could A be the same as A+1? Luckily, my Dad could explain it, and after that it was all clear.

  3. Erlang R15B01 (erts-5.9.1) [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]

    Eshell V5.9.1 (abort with ^G)
    1> A = 10.
    2> B = 20.
    3> A = B.
    ** exception error: no match of right hand side value 20

  4. “Program
    a sequence of instructions to be executed in order”

    This misconception is the root behind probably half of all concurrent-related bugs ever. Huge mistake to define it like that.

  5. This is only true for imperative programming languages. If someone has a math background but no programming background, it makes much more sense to start with a functional language (Haskell, F# etc.).

  6. “Programming
    not like algebra”

    Programming is like math

    a sequence of instructions to be executed in order”

    A Program is an algorithm which is a mathematical concept. Alan Turing wrote a chess program using nothing more than paper. Therefore, programming is math.

  7. Is this nothing more than a confusion about the assignment operator (typically =) and the evaluation operator typically (==) ? I don’t think it’s quite true that programming is not algebra, and I don’t think programming is ‘a sequence of instructions’ I think it’s just that the syntax is different.

  8. So, I think this is the sort of problem (in the context of of encountering ‘the misunderstanding of what you are being taught by an online/automated/self-administered teaching resource) that would potentially be helpfully addressed by a ‘more discursive teaching process’ that I describe in an article I wrote recently called “Why are we so bad at teaching struggling learners online?”
    The idea is that ‘question-asking should be woven into the teaching/learning process’ in a sort of 2001 HAL-like ‘Dave, I just noticed that you..’. questions which are aimed at attempting to determine if the learner has misunderstood something.
    An issue which arises is whether the question asking and answering process that this requirement seems to demand is beyond our current AI capabilities and how to go about the job of ‘bring humans into the conversation’ (as an ‘AI substitute’) if AI isn’t up to tackling it (yet).

    1. Another valuable aspect of the student-teacher relationship is human observation. A very good teacher can often detect trouble before the student knows she’s in it. “I saw you frowning at your book. What were you reading just then? Did you find something confusing?”

  9. The same problem can come across in the opposite direction – *when programming is exactly algebra*. Coming from a strong, but only procedural background, I spent 6 months learning Haskell.

    For the first month I thought I was doing okay, even though it was difficult. For the next two months I slowly figured out what was wrong with my understanding, and the month after that I couldn’t code well at all because I was rethinking all my knowledge from the ground up. I had headaches after relatively short periods of coding because I was doing that; I was rethinking literally everything from how scalar values work to the chaining of large functions in an organized way.

    As a sidenote, that experience is the most intensive and useful coding experience of my life, second only to my first understanding of mutation of variables when I was using REALbasic and the ensuing flood of experiments, 9 years earlier.

    I also have many thoughts on the teaching/learning of programming with students, which I wrote up as a response post because it got too big:

  10. for some time, exactly the way that students in some of the top CS programs in (at least) the US was to introduce them to programming with SICP, and to present the substitution model of evaluation for Scheme. Which is, more or less, algebra. Personally, I found this to be a fantastic, though admittedly difficult introduction to programming.

  11. One of the first things we ever did in an introductory computer class was to assign functions to every person in the class (I got to be an adder) and then “play computer.” Indeed, stepping through programs is probably the best way to start to get an idea about what programming is. Your story shows how important it can be to have a good teacher whenever you start to learn a new subject.

    On the other hand, have you ever programmed in Prolog? Or created a font with METAFONT? Programming and algebra may be more closely related than you think 🙂 Programmers who have only dealt with procedural languages are often just as lost as your friend when they are confronted with purely functional or declarative languages. Your friend would probably have done great if she started with Prolog!

Comments are closed.