Staying Out of Trouble

As simple as it seems, there are a few things to keep in mind about let. The first is that the names defined in a let are exactly that: defined inside the let. Technically, let relies on lexical scope. In plain English this means the bindings created by let only exist inside the code that makes up the let body.

You can’t, for example, do this:

 ;; We can use title inside of the let.
 
 (​let​ [title ​"Let's Pretend This Never Happened"​]
  (println ​"The title is"​ title)
  (print-the-title))
 
 ;; But now we're outside of the let.
 
 (​defn​ print-the-title []
  (println ​"The title is"​ title)) ​; Boom!

Since title is only defined inside of the let, this shouldn’t come as a surprise. After all, we started this trip ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.