Setup: Computers cannot speak English because they live in a much different environment than normal humans. In order to understand why programming languages work the way they do we must have some intuition about how that environment works. In this applet we solve the very simple problem of finding the largest number in a list of numbers in four ways starting from how a human would solve it and ending with how a computer would solve it.
A human without help in a human environment: Open the menu with the title "Choose a case" and select "Human in a human environment". There are 16 numbers at the top of the applet. There is no help in finding the largest. But you do not need help. I have asking many early students what goes through their minds when they solve this problem. The answer is typically nothing - they can just look and see what the number is.
A human without help in a computer environment: Open the menu again and select "Human in a computer environment". Now you get to solve the problem with a view that is similar to the view a computer gets - that is, all computer actions happen one step at a time and on one object at a time (more or less and for most problems). Now only one number shows up. To see the next number click on "Next". But then only the next number shows up. You can never go back (Ok this is slightly artificial - a computer would not go back because it is smart enough not to have to go back). Keep clicking on next to see all the numbers. In this environment something needs to go through your mind if you are going to solve this problem, especially if you have to deal with 1000s of numbers, as a computer would. What is it? Probably, you are thinking that you need a sheet of paper and a pencil with an eraser. You write the first number on the paper with the pencil. As you click "Next" you check the current number you are looking at with the number on the paper. If the current number is greater, you erase the number on the paper and write the current number in its place. Can a computer do this?
A human with help in a computer environment: Instead of paper, the computer maintains variables. What would be written on the paper in the human world is the value of the variable in the computer world. The eraser changes what is on the paper in the human world. Correspondingly, the = operator changes the value of a variable, erasing forever its previous value (unless it was copied somewhere). Open the menu again and select "Human in computer environment w/ help". The setup is the same as before except that the computer offers a variable for you to set a value. The name of the variable is help and its value is shown in the textfield on the right with the label ": help". You can set the value of help from the only other thing you know in this world - the value of the current number. That value is shown in the textfield near the center and is labeled "current :". There is a button, called "Current to Help", which is between the current number and the help value. Clicking this button causes the value of the current number to be written as the value of help. If this is done properly, the value of help will be the answer after the last number on the right becomes current. Is that all the help the computer can give?
A computer in a computer environment: No. A computer gives much more help than that. A computer offers the ability to compare the numbers using an if expression and the ability to iterate through all the list numbers using a for. The actions that the computer takes, on its own (up to being programmed, that is) are shown as they happen in the last presentation which can be accessed by opening the menu and selecting "Computer in a computer environment". Two clicks on "Next" are need for each step - you can read the action the computer is taking in the long textfield in the lower right portion of the applet.
Express what a computer is doing in English: The computer performs the following operations numerous times:
A concise English description of the actions taken by the computer might look like this:
1. Input a list of numbers. 2. Let current hold the first number in the list. 3. Let held also hold the first number in the list. 4. Repeat the following until all numbers have been considered: 5. Set the value of current to the next number in the list 6. If the value of current is greater than that of help 7. then replace help's value with that of current. 8. The answer is the value of help.
Translation to Matlab code: by line number
End result: an additional check on the length of the list is made at the very beginning to prevent a crash if the length of the list is 0.
lst = input('list of numbers> '); if length(lst) > 0 current = lst(1); help = current; for i=2:length(lst) current = lst(i); if current > help help = current; end end fprintf('%d\n',help); end