23 Aug 2023 - pinaki
The following is a combination of a lecture and also my view on Computer Programming. I am no expert so take this lecture with a grain of salt. If you find it useful, feel free to steal it and share (with or without attribution). I don’t want any hindrance to knowledge just because someone forgot to cite the source.
In this lecture, we shall revisit computer programming. This lecture is relevant for those who already has learned some basic programming language like C, C++, Python etc. Also, if you haven’t learned any computer programming, then read it as a fun story, but don’t believe everything I say. I am no expert, and these are just my view of computer programming. So let’s dig in.
First, we should be clear, when to use a computer and when to simply use a mathematical formula. If you want to add two small numbers, maybe a computer is not required – you can just do it in your head or on a piece of paper. A computer comes in when we have to add several (say 1000) large numbers. But we also need to ask – can we add any number in a computer? How many numbers can I add using a computer? 10, 100, 1000 … maybe \(10^{6}\) (that is 1,000,000)? Yes, modern computer allow you to do that. But even modern computers would fail if you try to add \(1,000,000,000,000\) numbers.
In my opinion, this perspective is important – before we start thinking about writing computer programs, we should have a clear picture of:
What is a computer?
Wikipedia (the repository of collective human knowledge) gives a nice description – A computer is a machine that can be programmed to carry out sequences of arithmetic or logical operations (computation) automatically.
Now my ‘‘hand-wavy’’ version – A computer can be an imaginary or mathematical tool, a person, a wodden box with multiple columns and slidable beads (the abacus), a vaccum tube that can control the flow of electric current, a silicon+germanium based transistor that can change the voltage of current flowing through a wire. As you see, we kept evolving computers and going from abacus, to humans, to transistors, and hopefully to quantum physics – we get some added benefits/features and some limitations.
Okay, so there are a lot of variation and each type of computer has some advantages and some disadvantages – but then what is the common factor among all of them? From the wikipedia definition – can be programmed to carry out sequence of operations
.
In other words,
computer
– be it a person or a box – to do some operation reliably. E.g., you can rely on your friend to correctly add two numbers for you.computer
to do some specific operation (say add two or multiply two numbers) whenever you feel like – this is what we mean by programmable
.Given that you have a human friend (who is a computer
) – your first job is to identify – what can your friend do reliably
and quickly
?
To understand the significance of capabilites and how it affect computer programming, we consider two friends – Dr. X can only do additions whereas Dr. Y can do additions and multiplications.
Since, Dr. X can only do addition, we create Rules as:
ADD a + b
means Dr. X will add a + bASSIGN c = ...
means Dr. X will assign the value in ...
to c
Since Dr. Y can do both addition and multiplication, we create Rules as:
ADD a + b
means Dr. Y will add a + bMULTIPLY a * b
means Dr. Y will multiply a * bASSIGN c = ...
means Dr. Y will assign the value in ...
to c
(ps: I skipped some things for simplicity, e.g., how I store the variables a, b, c; the fact that I used algebriac notation, etc. But let’s keep things simple for now.)
Note:
First, identify the capabilities of your computer.
Second, using those capabilities, define the best set of rules. Try to keep these rules short and simply so that everyone can use your computer.
Suppose, I want to use your computer for some problem that I want to solve. The first thing I should do is – Get the list of Rules from you
.
Now that I have your computer and the list of rules – I will use it to solve a problem – track how much money I have in my bank, in particular, how much money I earn and how much money I spend.
Here is the list of records:
Day-1:
My bank balance is £0.
I earned £100 as salary.
I bought 1 potato price £7.
Day-2:
I bought 10 potatoes each price £5.
I earned £30 as salary.
Day-3:
I bought 3 potatoes each price £6.
What is my final bank balance after 3 days?
If I had to track the record for one day, I would do it myself. But tracking money for 3 days is tedious and repetative – so, I can teach or program
my friend a computer (Dr. X or Dr. Y) how to do it.
Note, we already highlighted a limitation of computer programming
– since you teach your computer what to do – first, you need to know how to keep track of money (i.e., addition when you get salary and substraction when you buy potatoes).
This is easy for buying potatoes or getting salary – but there are problems where:
undecidable problems
(click here for a list) like the famous Halting Problem, Hilbert’s Entscheidungsproblem, etc. I will later write a lecture/blog on these interesting undecidable problems, or better, you can learn about these problem from experts like Erik D. Demaine.Anyways, back to our money tracking problem
Given that Dr. X can only do addition and we know the Rules of how to ask Dr. X to do an addition for us, we can write a program
as:
ASSIGN bank_balance = 0
// Day-1
ASSIGN bank_balance = ADD bank_balance + 100
ASSIGN bank_balance = ADD bank_balance - 7
// Day-2
ASSIGN bank_balance = ADD bank_balance -5
ASSIGN bank_balance = ADD bank_balance - 5
ASSIGN bank_balance = ADD bank_balance - 5
ASSIGN bank_balance = ADD bank_balance - 5
ASSIGN bank_balance = ADD bank_balance - 5
ASSIGN bank_balance = ADD bank_balance - 5
ASSIGN bank_balance = ADD bank_balance - 5
ASSIGN bank_balance = ADD bank_balance - 5
ASSIGN bank_balance = ADD bank_balance - 5
ASSIGN bank_balance = ADD bank_balance - 5
ASSIGN bank_balance = ADD bank_balance + 30
// Day-3
ASSIGN bank_balance = ADD bank_balance - 6
ASSIGN bank_balance = ADD bank_balance - 6
ASSIGN bank_balance = ADD bank_balance - 6
Now that was a lengthy program. At least, we know that our computer can do these additons reliably and all we had to say is a sequence of ADD a + b
and ASSIGN c = ...
.
Using a more powerful Dr. Y that allows addition and multiplication, we can write a smaller program (and hopefully less number of operation to make it fast) as:
ASSIGN bank_balance = 0
// Day-1
ASSIGN bank_balance = ADD bank_balance + 100
ASSIGN bank_balance = ADD bank_balance - 7
// Day-2
ASSIGN total_price = MULTIPLY 10 * 5
ASSIGN bank_balance = ADD bank_balance - total_price
ASSIGN bank_balance = ADD bank_balance + 30
// Day-3
ASSIGN total_price = MULTIPLY 3 * 6
ASSIGN bank_balance = ADD bank_balance - total_price
The main takeaway from this lecture is – before you write Computer Programs – you need to be clear about the following two things:
ASSIGN
and store information inside a magnetic tape. A modern GPU-like computer can add \(1,000,000,000,000\) numbers and do mathematical operations (additions, multiplications) much much faster but is not good for non-mathematical operations like ASSIGN
.Rules
defined by the people who created that computer. If you know these rules properly, you will have an idea of all the problems that I can solve using this computer.The objective of Rules
are to be short and simply but allows maximum use of the computer’s capabilities. If you feel that some rules are unnecessarily complicated, then you can ask the people who created the computer to modify the rules.
Programming languages like C, C++, and Python – they are basically different Rules
to use the same computer.
Then, you should ask – if everyone wants to utilise the same computer – why not have a common set of Rules because, at the end, we just want to use the modern computers and its capabilities?
You see, our modern computers are very general – that is – using the same computer you can play video games whereas I can calculate the precise trajectory of a rocket. We have different needs but our computers are the same. Programming language like C, C++, Python basically provide these customised set of Rules
instead of forcing everyone to use the same set of Rules.
If tomorrow, your needs change (say you want your computer to solve a problem that helps you go to Mars so you need more MULTIPLY
and less ADD
), you might come up with a new set of Rules
to make you job easier and we shall call it a new programming language.
Hope this helps. The reason I wrote this lecture is – I think we should teach some basics of ‘‘what is a computer’’ before teaching ‘‘how to program a computer’’ using C, C++, Python. We should also teach – what is easy, what is hard, and what can never be solved using our current computers.