Does It Know Five

What happens if we try evaluate something else than a number or a fraction? Say, something resembling a word?

user=> five
Syntax error compiling at (REPL:0:0).
Unable to resolve symbol: five in this context

Now that's interesting. We tried five and Clojure reports a problem. It doesn't know five. It hasn't seen its definition so it can't tell us what does it mean. Same goes for six or chocolate.

Notice that Clojure refers to five as a symbol. The cool thing is, we can teach it new symbols. We can make it learn new vocabulary. Specifically, we define new things using def.

We use def in a similar way to how we did with + or -. We start with the operation, that is with def, and follow with two arguments: the symbol and the value for which the symbol will stand.

user=> (def five 5)

So we typed in (def five 5) and got back #'user/five. What the response says is that the user introduced a new definition. From now on, we can use five in all following interactions with the REPL.

user=> five
user=> (+ five 3)

Clojure doesn't restrict us here. We could for example redefine + to stand for -. It's not a good idea, though.

user=> (+ 3 2)
user=> (def + -)
WARNING: + already refers to: #'clojure.core/+
in namespace: user, being replaced by: #'user/+
user=> (+ 3 2)

Notice that Clojure warns us when we change the meaning of + and replace it with something else. Every other programmer would expect + to perform addition. Turning it into something else will be confusing and Clojure lets us know about it. Making our code understandable to our fellow programmers should always be our top priority.