Lesson 4. Arithmetic expressions
In order to get familiar with the Clojure syntax, we will learn how to deal with numbers in Clojure.
In the current Lesson, we will explore how to write arithmetic operations in Clojure. By arithmetic operations, we mean addition, subtraction, multiplication and division of numbers. We will discover how the Clojure syntax for arithmetic operations differs from the usual mathematical syntax. This will allow your mind to start getting used to the fact that in Clojure every compound expression is wrapped with parenthesis.
After completing this lesson, you will be able to:
- Understand simple arithmetic expressions in Clojure
- Write simple arithmetic expressions in Clojure
- Understand nested arithmetic expressions in Clojure
- Write nested arithmetic expressions in Clojure
4.1 Expressions and forms
In many programming languages, we have functions, operators and statements.
In Clojure, we have only expressions: every part of the language is an expression.
There are two kinds of expressions:
- primitive expressions like numbers, booleans and strings
- compound expressions like function calls, if expressions and variable definitions
Compound expressions are always wrapped into parenthesis. Compound expressions are also called forms. The name of the form is given by the first element of the expression.
For instance (+ 1 2) is a “+ form” and (def my-variable 10) is a “def form”.