Learn/Clojure/2. Values and names
Lesson 2 of 8
Sign in

Clojure, lesson 2

Values and names

Programs work with values: numbers, text, true or false. To refer to a value by name, Clojure gives you two tools: def for names the whole file can see, and let for names that only live inside one expression.

def: top-level names

(def city "Lyon")
(def population 522000)
(def rainy? true)

(println city population)      ; Lyon 522000

def creates a var, a named slot at the top level of your namespace. Names are written in kebab-case (words joined by dashes), and names of true/false values often end with ?. Clojure allows many characters in names that other languages don't: rainy?, total-price, ->celsius.

Basic types

Clojure works out types at run time, so you never declare them:

KindExamplesNotes
integer42, -764-bit whole numbers
decimal3.14, 2.064-bit floating point
ratio1/3exact fractions
string"hello"text in double quotes
character\a, \spacea single character
booleantrue, false
nilnil"no value"
keyword:red, :sizea name that stands for itself, used as a label

str turns anything into a string and joins the results, and count tells you how long a string is:

Values never change

In Clojure, values are immutable. The number 3, the string "Lyon" and, as you will see later, whole lists and maps cannot be modified. Functions that look like they change something actually return a new value. This sounds restrictive, but it means a value you are holding can never be changed behind your back by some other part of the program.

def can technically be repeated to point a name somewhere else, but that is meant for experimenting, not for normal program logic. Treat each def as a constant: define it once, near the top of the file.

let: local names

Most names in real code are local. let takes a vector of name/value pairs, followed by a body that can use them:

Each name can use the ones defined before it (area uses width and height). The names exist only inside the let's parentheses. The value of the whole let is the value of its last form, because in Clojure every form returns a value.

If you let a name that already exists, the new one shadows the old one inside the body. Nothing is overwritten: outside the let, the old meaning is still there.

Further reading

  • Learn Clojure: Syntax
  • Vars and the Global Environment
  • Special Forms (def, let)

2 exercises to try what you just read.

Exercise 1

Trip budget

A road trip costs fuel-per-day for each of the days, plus a one-off snacks cost. The starter already defines these. Add a def called total with the full cost and print it in this form:

The program reads no input.

Press Ctrl Enter or Check to test your code.

Exercise 2

Name tag

The starter reads a first name and a last name, one per line, into a let. Print the name in "last, first" form, and on the next line the total number of letters in both names. Use str to join strings and count to measure them.

Sample input:

Sample output:

Press Ctrl Enter or Check to test your code.

(str "Apples: " 3)          ; "Apples: 3"
(count "Clojure")           ; 7
(let [width 8
      height 5
      area (* width height)]
  (println "Area:" area))
Total: 300
Ada
Lovelace
Lovelace, Ada
Letters: 11