<commit_before><ol class="breadcrumb">
  <li><a href="/">Home</a></li>
  <li><a href="/book/">Book</a></li>
  <li><a href="/book/1-0-0-overview/">Part One: Grokking Lisp</a></li>
  <li class="active">Namespaces, Symbols, Packages, and Systems</li>
</ol>

## Chapter 1.12

# Namespaces, Symbols, Packages, and Systems

> "Every law that was ever written opened up a new way to graft."
> <footer><NAME>, <em>Red Planet</em></footer>

As briefly introduced in previous chapters, Common Lisp is a Lisp-2: that is to say, within any given package, you can use the same symbol to refer to both a function and a variable, if you like, because functions and variables have separate namespaces.  The namespaces for functions and variables are managed by the symbol object itself, which exist within a package.

Obviously, scope is important too.  A symbol inside a function body may not have the same definition as in the top-level.  This is by design---and whenever you use a symbol, it is important to remember dynamic and lexical scope.  Not all enclosing forms introduce a new lexical scope like `defun` does.

So, as you can see, what a symbol happens to mean is highly contextual---and for the most part, the Lisp reader is very good at figuring out which definition of a symbol you happen to mean in any given context.  When you want to stray from the norm, Lisp has tools to help you be more specific.

We will also cover some basic aspects of packaging your Lisp code for the wild, and working with ASDF and the Quicklisp package manager built on top of it.

## Exercise 1.12.1

**Dynamic and Lexical Scope, Revisited**

All symbols in Common Lisp are namespaced in packages, and within a namespace, symbols are also scoped.  *Scope* refers to how the Lisp reader looks up symbol definitions in the current environment.

There are two flavours of scope---dynamic and lexical.  Lexical scope is tightly integrated with the structure of Lisp code and the lambda calculus from which it descends; it is *spatial* to the establishing form.  Dynamic scope is special, because it has indefinite scope and dynamic extent, which is to say that dynamically scoped symbols are not lexically contained to their establishing form and they live in the dynamic environment.

Remember that Lexical Scope *shadows* Dynamic scope, as well as surrounding lexical scopes.  Unless you specify otherwise, the innermost binding of a symbol is the one that is found by the Lisp reader.

```lisp
(defparameter one #x01)

(let ((one 1))
  (let ((one 1.0))
    (let ((one "one"))
      (format t "~a~%" one))))
```

## Exercise 1.12.2

**Implicit Lexical Scope**

Many forms in Common Lisp introduce an *implicit* lexical scope.  That is, you do not have explicitly call a `let` or `flet` to introduce a new nested scope; they provide a means for binding and assignment that is more intuitive for their particular use-case.  You have already seen a few of these forms, like the `DEFUN` macro.  You will also find implicit lexical scoping in `BLOCK` forms, and destructuring macros such as `DESTRUCTURING-BIND`, to name a few.

```lisp<commit_msg>extend exercise code on scope, ch 1.12: namespaces<commit_after><ol class="breadcrumb">
  <li><a href="/">Home</a></li>
  <li><a href="/book/">Book</a></li>
  <li><a href="/book/1-0-0-overview/">Part One: Grokking Lisp</a></li>
  <li class="active">Namespaces, Symbols, Packages, and Systems</li>
</ol>

## Chapter 1.12

# Namespaces, Symbols, Packages, and Systems

> "Every law that was ever written opened up a new way to graft."
> <footer><NAME>, <em>Red Planet</em></footer>

As briefly introduced in previous chapters, Common Lisp is a Lisp-2: that is to say, within any given package, you can use the same symbol to refer to both a function and a variable, if you like, because functions and variables have separate namespaces.  The namespaces for functions and variables are managed by the symbol object itself, which exist within a package.

Obviously, scope is important too.  A symbol inside a function body may not have the same definition as in the top-level.  This is by design---and whenever you use a symbol, it is important to remember dynamic and lexical scope.  Not all enclosing forms introduce a new lexical scope like `defun` does.

So, as you can see, what a symbol happens to mean is highly contextual---and for the most part, the Lisp reader is very good at figuring out which definition of a symbol you happen to mean in any given context.  When you want to stray from the norm, Lisp has tools to help you be more specific.

We will also cover some basic aspects of packaging your Lisp code for the wild, and working with ASDF and the Quicklisp package manager built on top of it.

## Exercise 1.12.1

**Dynamic and Lexical Scope, Revisited**

All symbols in Common Lisp are namespaced in packages, and within a namespace, symbols are also scoped.  *Scope* refers to how the Lisp reader looks up symbol definitions in the current environment.

There are two flavours of scope---dynamic and lexical.  Lexical scope is tightly integrated with the structure of Lisp code and the lambda calculus from which it descends; it is *spatial* to the establishing form.  Dynamic scope is special, because it has indefinite scope and dynamic extent, which is to say that dynamically scoped symbols are not lexically contained to their establishing form and they live in the dynamic environment.

Remember that Lexical Scope *shadows* Dynamic scope, as well as surrounding lexical scopes.  Unless you specify otherwise, the innermost binding of a symbol is the one that is found by the Lisp reader.

```lisp
(defparameter one #x01)

(let ((one 1))
  (let ((one 1.0))
    (let ((one "one"))
      (format t "~a~%" one))
    (format t "~a~%" one))
  (format t "~a~%" one))

(format t "#x~X~%" one)
```

## Exercise 1.12.2

**Implicit Lexical Scope**

Many forms in Common Lisp introduce an *implicit* lexical scope.  That is, you do not have explicitly call a `let` or `flet` to introduce a new nested scope; they provide a means for binding and assignment that is more intuitive for their particular use-case.  You have already seen a few of these forms, like the `DEFUN` macro.  You will also find implicit lexical scoping in `BLOCK` forms, and destructuring macros such as `DESTRUCTURING-BIND`, to name a few.

```lisp
{'fraction_non_alphanumeric': 0.07700787401574803, 'fraction_numerical': 0.006771653543307087, 'mean_word_length': 4.2057377049180324, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3187329', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1693, 'n_words': 980}