Simulating Car Dynamics with a Computer Program: Part II
The first thing we need to do is create a data structure that contains the mathematical state of the car at any time. This data structure is a block of computer memory. As simulated time progresses, mathematical operations performed on the data structure simulate the physics. We create a new instance of this data structure by typing the following on the computer keyboard at the Scheme prompt:
(new-race-car)
This is an example of an expression. The expression includes the parentheses. When it is typed in, it is evaluated immediately. When we say that Scheme is an interactive programming language, we mean that it evaluates expressions immediately. Later on, I show how we define this expression. It is by defining such expressions that we write our simulation program.
Everything in Scheme is an expression (that's why Scheme is simple). Every expression has a value. The value of the expression above is the new data structure itself. We need to give the new data structure a name so we can refer to it in later expressions:
(define car-161 (new-race-car))
This expression illustrates two Scheme features. The first is that expressions can contain sub-expressions inside them. The inside expressions are called nested. Scheme figures out which expressions are nested by counting parentheses. It is partly by nesting expressions that we build up the complexity needed to simulate racing. The second feature is the use of the special Scheme word define. This causes the immediately following word to become a stand-in synonym for the value just after. The technical name for such a stand-in synonym is variable. Thus, the expression car-161, wherever it appears after the define expression, is a synonym for the data structure created by the nested expression (new-race-car).