Chapter 3: Giving Names to Expressions
In the previous chapter, you have learned to compose nested expressions in computer programming language.
In this chapter, you are going to learn how to name expressions.
But before that, you need to understand why it is important to have the ability to name expressions. One reason is that without naming expressions, computer programming can rapidly become a mess.
When you name expressions, computer programming is more organized.
The problem: Without naming expressions, it’s sometimes a mess
Look at this expression
(+ (* (+ 3 4) 7) (* (+ 3 4) 9) (* (+ 3 4) 5))
Do you see that the expression (+ 3 4)
is repeated three times?
This repetition makes the expression very hard to understand.
The solution
The solution to this mess is give a name to the expression (+ 3 4)
and to use its name instead of the expression itself.
In order to name name expressions, we use the def
operation. Like this
(def a (+ 3 4))
(For the moment, don’t pay attention to #'cljs.user/a
.)
And using a
instead of (+ 3 4)
our expression becomes simpler:
(+ (* a 7) (* a 9) (* a 5))
How to use def
def
is the operation
that tells the computer to give a name
to an expression
. (The word def
is a shorthand for define
).
In order to use def
, you need to compose an expression in the same way that you did with +
and *
in the previous chapters: with the 3 steps of an expression that we introduced in chapter 1:
-
First, you need to tell the computer that you want it to execute something. For that you use the parenthesis:
()
. -
Then, you need to tell the computer what
operation
you want it to execute: in our case, the operation isdef
. -
Finally, you need to tell the computer what are the details of the
operands
fordef
operation: the first operand is thename
you want to give to the expression and the second operand is theexpression
you want to name.
Combining all of that, we get:
(def a (+ 3 4))
Can you see the 3 parts of the expression?
()
def
- first operand:
a
, second operand(+ 3 4)
Congratulations! This is the first time you gave a name to an expression.
Now, try to use different names: for instance, you could replace a
by b
, c
or your firstname.
You can also replace (+ 3 4)
by another expression: (* 6 7)
or (+ (* 7 2) (* 2 9))
.
Using names
Once you give a name to an expression, you can use it everywhere.
Here, we define my-favourite-number
to be 18
:
(def my-favourite-number 18)
And now, we add 10
to my-favourite-number
:
(+ my-favourite-number 10)
Back to the complicated expression
Now, we can re-write the complicated expression we started with: (+ (* (+ 3 4) 7) (* (+ 3 4) 9) (* (+ 3 4) 5))
:
(def my-number (+ 3 4))
(+ (* my-number 7) (* my-number 9) (* my-number 5))
Do you agree that it looks much simpler?
Of course, it gives exactly the same result as the original expression:
(+ (* (+ 3 4) 7) (* (+ 3 4) 9) (* (+ 3 4) 5))
Exercises
If you are having difficulties with one exercise, read again the details of the 3 steps of an expression.
A. Calculate (4 + 7 + 8)*3 + (4 + 7 + 8)*7 + (4 + 7 + 8)*9
()
You should get 361
.
B. Calculate (2*3 + 4)*3 + (2*3 + 4)*7 + (2*3 + 4)*9
()
You should get 190
.
C. Calculate 2*3 + 4*5
. Then multiply the result by 4 + 5
.
()
You should get: 234
.
C. Calculate 2*3 + 4*5
. Then multiply the result by 4 + 5
. Then multiply the result by 19
.
()
You should get: 4446
.
Send us a screenshot with your programs to viebel@gmail.com.