This article is part of a series describing a port of the samples from Practical Common Lisp (PCL) to Clojure. You will probably want to read the intro first.
This article covers Chapter 10, Numbers, Characters, and Strings.
Because Clojure is Java under the covers, you can always use Java's support for numbers, characters, and strings. The Java interop syntax is clean and simple, so it is idiomatic to call Java directly, rather than write wrappers to make code look more Lisp-like. A few examples follow:
In Clojure, as in most Lisps, numbers are numbers. They don't do irritating things like overflow:
Also, integer division is exact:
Under the covers, Clojure's numeric representation switches Java types as necessary to do the right thing.
Clojure does a good job of balancing the purity of math (Lisp) and the practical reality of efficient representation (Java's primitives). But you still have to know your way around. Some things are wrapped in Lisp, and some things aren't. For example, numbers support the mathematical comparison operators, but Strings use Java's compareTo
:
For example:
If you aren't sure whether there is a Lispy wrapper for some functionality, you can check the API docs, or just try it in the REPL.
For numbers, characters, and strings, Clojure provides some of the trappings a Lisp programmer would expect, e.g. exact integer division. But under the covers, it's all Java. If you don't find what you need in the Clojure API, drop to Java using the interop syntax.