# Creating Temporary Variables

This article covers creating temporary variables—an advanced Clojure topic. If you are not familiar with Clojure, we recommend reading some of our other materials first. If you have no experience with Clojure, our Clojure Rules 101 article is a good place to start.

### Introduction

LoanPro provides many useful loan variables that you can use. However, at times, some of your computation rules will become very complex and will use the same computed value or expression repeatedly. Copying and pasting these expressions may work, but it can make a rule hard to read.

For example, suppose we needed to use the expression (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4)) several times throughout a computation rule. We could copy and paste the expression, but our computation rule could end up looking something like this:

(- 8 (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4)) (* 2 4 (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4))) (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4)))

That is difficult to read, to say the least.

Now, imagine that each number in the repeated expression represented a different loan variable. Our computation rule would get even longer very quickly. It would be much easier if we could use a single character as a variable of some sort to represent our repeated expression; that is, if we could do something like this:

Let x = (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4))

That long, scary computation rule from above would look like this instead:

(- 8 x (* 2 4 x) x)

Now, the computation rule is much easier to read for us humans, and we can easily see what’s going on. Fortunately, Clojure allows us to do this by using the LET operator which can define complex expressions once and then repeatedly use them throughout our computation rule.

### How to Use the LET Operator

Using the LET operator is a bit different than using other Clojure operators. Below is the basic syntax:

(let[var1 value var2 value] (expression))

Let’s look at the syntax in sections. We’ll start with the very first part — the let operator. This tells the system that we want to create a variable.

Now, let’s look at the next section — [var1 value var2 value]. This section tells the system what variables we want to create and what values we want to give them. For example, say we want to set the variable “x” to equal 5, we would write [x 5]. If we also wanted to add the variable “y” and set it to 10, we could simply write [x 5 y 10].

Finally, let’s look at the last part of the LET operator syntax — (expression). This is the Clojure expression that we want to be able to use the variables for. Outside of this expression, the variables don’t exist, so if you set x and y in the LET command, you can only use x and y in this expression.

### Examples

Now let’s apply the LET operator to the expression and computation rule we used at the beginning of this article. We wanted to let x (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4)) in the computation rule (- 8 x (* 2 4 x) x)). Using Clojure, we would set it up like the following:

(let[x (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4))] (- 8 x (* 2 4 x) x))

As a reminder, without the LET operator, the same computation rule looks like this:

(- 8 (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4)) (* 2 4 (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4))) (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4)))

As you can see, the computation rule is a lot smaller and easier to read when we use the LET operator. However, please take note that you cannot use variables outside of the given expression. For example, the following won’t work because we’re using the last “x” outside of the LET operator’s expression:

(+ (let[x (+ 8 (* 5 9 (/ 4 3) (+ 4 8)) 19 (- 9 4))] (- 8 x (* 2 4 x) x)) x 9 19)

The LET operator’s expression defined above is (- 8 x (* 2 4 x) x)). The x in x 9 19 is outside that expression, so the last x is invalid and the rule wont work. Here is an example of using LoanPro variables in a computation rule with the LET operator:

(let[x (- status-amount-due status-due-fees)] (/ x 2))

Here, the variable x is set to the difference between the amount due on the loan and the due fees. In the expression, we divide this number by 2.