# How Computation Rules Work

### General

This article will go over the basics of how computation rules work and why they look the way they do. This article assumes you already know how to create a basic computation rule. If you don’t know how to create a computation rule, please read the article Creating your First Computation Rule.

#### The Core of Computation Rules

All computation rules are written in a language called Lisp. Lisp is a basic language that tells the computer what to do and in what order. In this case, it tells the computer what math operations to do and in which order to do them.

For example, suppose we have the equation 2 – 5 * 32 / 5 – 4 + 68 * 41.02 / 42 + 1. We don’t really want to try to figure this problem out by ourselves, instead we want the computer to solve it for us. However, the computer needs to know which operations we want to do first, which ones second, and so forth, otherwise we’re going to get the wrong answer.

For example, take the segment 2 – 5 * 32 / 5 – 4. Using the order of operations, we get -34. However, suppose the computer doesn’t know about the order of operations. In this case, it acts as a basic calculator performing the operations in the order that it receives them. The computer would then evaluate it as follows:

2 – 5 = -3

-3 * 32 = -96

-96 / 5 = -19.2

-19.2 – 4 = -23.2

In this case, the computer will return -23.2, which we know is not the right answer. So, we need a way to tell the computer how we want it to perform math so it will return -34 instead of -23.2. That is why we use Lisp.

#### Format of Computation Rules

Lisp wraps everything in parentheses that needs to be evaluated. For example, in the Creating your First Computation Rule tutorial we created the rule (+ 2 3). This rule wraps the statement inside of parentheses. The other thing about Lisp is that the operation comes first before the operands. That is why instead of writing (2 + 3) we write (+ 2 3). The main advantage of this is that you can list as many operands as you want and only have to write the operator once.

For example, traditionally if we wanted to sum up 1, 1, 2, 3, and 5 we would write 1 + 1 + 2 + 3 + 5 which requires a lot of typing. With Lisp you write (+ 1 1 2 3 5) which is a lot shorter and requires a lot less typing. This becomes especially useful the more numbers you want to perform the same operation on.

Additionally, each operand can be the result of an operation. For example, lets say we wanted the sum of 2*3 and 4*5. Traditionally we would write something like (2 * 3) + (4 * 5). Here, the result of the product of 2 and 3 is added to the result of the product of 4 and 5; these are designated by parentheses. In Lisp we would write (+ (* 2 3) (* 4 5)) to get the same result. Notice how we have multiple layers of parentheses. Each set of parentheses is an operation that will evaluate to a value that we can then use in another operation. The inner-most set(s) of parentheses will always evaluate first. In the example (+ (* 2 3) (* 4 5)) the operations (* 2 3) and (* 4 5) will both evaluate first. The computer will then take those values and place the values where the operations used to be, so we’ll have the operation (+ 6 20) which in turn gives us 26.

With Lisp we can have as many sets of parentheses inside each other as we want, and the computer is guaranteed to run the equation in the same order every time. This means that we can tell the computer how we want to evaluate an equation and we can get the same result every time.

#### Putting it all together

Let’s go back to the example of the equation 2 – 5 * 32 / 5 – 4 + 68 * 41.02 / 42 + 1 which we wanted the computer to solve for us. Let’s turn this into Lisp so that the computer will always run the order of operations.

The first thing is to break it into different sections using parentheses for each operation so that it will run in the correct order. When we’re done with that, we just place parentheses around everything. With the above equation, we could write it as follows:

((2 – ((5 * 32) / 5) – 4) + ((68 * 41.02) / 42) + 1)

If you notice, there is a lack of parentheses around operands that share the same operation, such as (2 – ((5 * 32) / 5) – 4). It could have been written as ((2 – ((5 * 32) / 5)) – 4), but if you remember in Lisp we can have as many operands share the same operator as we want, so when we convert it to Lisp we don’t need the extra parentheses.

Once we have an equation split into sections of parentheses, we now just need to move the operators to the start of the proper set of parentheses. We also remove duplicate operators. So, for ((5 * 32) / 5) we move the “*” operator to just before the first 5 and we move the “/” operator right after the first opening parentheses, so we get (/ (* 5 32) 5). When we do this with the whole equation, we get:

(+ (- 2 (/ (* 5 32) 5) 4) (/ (* 68 41.02) 42) 1)

Now, what if we want to convert Lisp to traditional syntax? We start off by doing the steps in reverse. So, let’s say we have the Lisp equation (* (- 4 (/ (+ 3 2) 1) 2) (/ (- 16 10) 12) 4). First, we take the operators and move them between each operand in the corresponding set of parentheses. So for the segment (/ (+ 3 2) 1) we would get ((3 + 2) / 1). If we apply this principle to the whole equation we would get:

((4 – ((3 + 2) / 1) – 2) * ((16 – 10) / 12) * 4)

This equation follows all of the correct rules for traditional formulas and doesn’t need to be changed at all past this. However, if you wish you can remove some sets of parentheses to improve readability provided it still follows the order of operations. In this example, we can only remove the outermost set of parentheses, giving us

(4 – ((3 + 2) / 1) – 2) * ((16 – 10) / 12) * 4

#### Practice

For those who wish to practice doing Lisp, here are some problems that you can practice. The answers are given in white text, so to see the answer just highlight the entire line or the entire page by pressing Ctrl + A.

Convert to Lisp from the traditional format

(5x + 4 + 9y) * 34 / (3 + 2 – 1) – 4 Answer: (- (* (+ 5x 4 9y) (/ 34 (- (+ 3 2) 1))) 4)

5 – 8 – 4 + (3 * 4 * 2 – 1) Answer: (+ (- 5 8 4) (- (* 3 4 2) 1))

(9 * (4 + 5 + 5) ) / 12 / 16 + 23 – 89 * 3.2 Answer: (- (+ (/ (* 9 (+ 4 5 5)) 12 16) 23) (* 89 3.2))

Convert to the traditional format from Lisp

(* 3 4 5 (+ 2 4 1) (- 3 4 (+ 1 1)) (- 2 3)) Answer: 3 * 4 * 5 * (2 + 4 + 1) * (3 – 4 – (1 + 1)) * (2 – 3)

(+ 8 (* 4 5 3) (- 23 30) (/ 30 3)) Answer: 8 + (4 * 5 * 3) + (23 – 30) + (30 / 3) or 8 + 4 * 5 * 3 + 23 – 30 + 30 / 3

(+ (/ 4 5 6 ) (* 3 4 ) (- 4 3 4)) Answer: ((4 / 5 / 6 ) + (3 * 4 ) + (4 – 3 – 4)) or 4 / 5 / 6 + 3 * 4 + 4 – 3 – 4