CodePlexProject Hosting for Open Source Software

x = 12 y = x + 3

Both the number 12, and the sequence x + 3 are expressions. Technically, lines "x = 12" and "y = x + 3" are also expressions. These are expressions that are comprised of sub expressions. At the end of the previous example, we considered a case where one variable is defined in terms of another variable, but that variable was not itself defined at the time. The sample below is an example of one such case:

y = 3 * x

The variable y is defined without x first being defined. This is allowable in NeturalMath because y is not actually being defined as the value of three times x, but is instead defined as the expression three times x. When the actual value of y is required by the system, it first checks to see if x has been defined. If it has not, it will throw out and error. If the value has been defined in a later statement, then the computed value will be returned. This may be a very difficult concept to initially grasp. It is best represented in the example below:

y = x + 2 print y // returns an error x = 2 print y // returns 4 x = 5 print y // returns 7

When the value for x has not been defined, trying to return the value of y will result in an error because we are trying to evaluate the value of something which does not exist. As soon as we set x equal to the value of two, we can return the correct value of y. This does not mean that y is actually assigned the value of four, however. What this means is that y is still defined as an expression representing the value of x plus the number two. Whenever the value of x changes, the result value of y changes as well.

y = x + 3 print y // returns an error because x is undefined def y // returns the x + 3

The def command outputs the expression that y was assigned rather than the result of evaluating that expression. Try experimenting with the samples below:

def 3 x = 3 def x y = x + 12 def y def z

x = 2 + 2 def x

The def statement returns that x is equal to four, not two plus two. This is because NeturalMath realizes that two plus two can be expressed more simplely as the number four rather than an addition of two numbers. NeturalMath inherently understands that constants and variables of the same type may be combined following typical mathematical rules to create a more compact expression that yields the same net result. For example:

y = x + 3 * x + 4 def y // (4 * x) + 4

y = y +2 // invalid assignment, throws an error

The next section on symbolic notation explains some ways to work around this issue.

A further look into expressions and how they work, as well as how to manipulate expressions directly is provided in the next section.

Part 6: Symbolic Manipulation

Last edited Nov 27, 2010 at 2:40 PM by zanethorn, version 7