# 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:

Programming
not like algebra
Program
a sequence of instructions to be executed in order
Variable
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 xii-xiv 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.

Next Post

1. #### csabill

/  July 20, 2012

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. #### Thomas Kluyver

/  July 20, 2012

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. #### Ori Livneh (@scatterblot)

/  July 20, 2012

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.
10
2> B = 20.
20
3> A = B.
** exception error: no match of right hand side value 20
4>

4. #### jmhowitt m howitt

/  July 20, 2012

Tried Erlang?

• #### Andy Skelton

/  July 20, 2012

I am an avid user of Erlang. It would not be my language of choice for introducing students to programming.

5. #### √

/  July 20, 2012

“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.

• #### Andy Skelton

/  July 20, 2012

This post is about teaching beginners. Concurrency is an advanced topic, not for beginners.

6. #### mech

/  July 20, 2012

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.).

7. #### André

/  July 20, 2012

“Programming
not like algebra”

Programming is like math

“Program
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.

8. #### Jake Grimley

/  July 20, 2012

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.

9. #### Tony Morris

/  July 20, 2012

Programming most definitely is algebra. Suggesting otherwise implies you understand neither. Let me know when you’d like to fix that.

10. #### iijiij (@iijiij)

/  July 20, 2012

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?” http://www.iijiij.com/?p=14060
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).

• #### Andy Skelton

/  July 20, 2012

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?”

11. #### encryptio

/  July 20, 2012

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: https://encryptio.com/posts/2012-07-20-re-programming-is-not-algebra/

12. #### Benjamin Dean

/  July 20, 2012

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.

13. #### Jed Wesley-Smith

/  July 20, 2012

Even grandmothers know this is wrong. http://www.mrspeaker.net/2011/11/28/immutable-nanna/

/  October 17, 2012

This post throws some light on why most programmers suck. If you think programming is not like math and you consider yourself a proficient programmer, then you’re in the same boat as the beginners you mentioned. Programming and math are practically identical but you don’t see it because you use imperative languages and your code reeks of assignment statements. Go learn lambda calculus, Haskell, ML, Lisp, or some other functional language and then you will understand. Until then you’re still a beginner.

• #### Andy Skelton

/  October 22, 2012

The gist of the post: programming is not like the algebra that most people are familiar with: the elementary algebra they learned as teenagers. Was it TL;DR for you?

We agree that programming is actually like math, especially in lambda calculus. However, as this post is about deficiencies in how beginner topics are taught it is directed at people for whom a little differentiation would help.

So while your technical assertion is correct, this is not the place to post it. Furthermore, your condescending and combative tone is inappropriate. Normally I would move such comments into the trash. Yours was allowed solely as an example of the kind of comment which should be discouraged.

/  November 4, 2012

I find your response hard to believe. If my comment was indeed inappropriate, then you would have ignored it completely and that would be the end of it. But I understand how rhetoric works so that is why I used the tone that I did. In any case, I disagree with you that this is not the place to post these comments–it most certainly is. It’s no different than you as a teacher, or a tutor, stopping to correct a student right then and there if you overheard something that you knew was not correct. Please read Tony Morris’ comment above.

• #### Andy Skelton

/  November 5, 2012

Beginners in all fields are given to simplify in order to gain understanding. It is not incorrect for them to think and solve problems without knowing what complexities lurk at higher levels.

A high-school physics teacher is not wrong to present practice problems in which gravity is a uniform field with all “down” vectors parallel. His students lack the math to integrate the converging vectors. So their study of parabolas is good even though the teacher knows that nature produces much more complex curves.

You would be wrong to enter his classroom and cross out his chalk marks, preaching graduate-level theory to beginning students. They need to approach the subject on a gradient. That is the point of the post. It is not to say that programming is not related to math. You and Tony Morris both made this mistake: you assumed that the term “algebra” always refers to higher maths when I clearly marked it as “elementary algebra” (the stuff of middle and high schools) early in the post.

16. #### Tony Morris

/  November 5, 2012

You’re kidding yourself mate. You are justifying your total absence of understanding of both programming and algebra with superficial qualifications. It makes me cringe.

The most difficult kind of beginner is the one who doesn’t realise it. I suspect you are that.

• #### Andy Skelton

/  November 5, 2012

I do not claim to be expert in programming or algebra or rhetoric. But I use these things every day and I did post something worthwhile about all three. Your comments suggest that you possess superior knowledge (I can’t argue that) but you and JL have done nothing worthwhile here.

Since this is the WWW you can post links to pages explaining your position. That would be worthwhile. Otherwise your comments here will only testify to your poor social skills.