emacs.d/clones/llthw.common-lisp.dev/1-12-0-namespaces.html
2022-08-24 19:36:32 +02:00

2351 lines
78 KiB
HTML

<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Namespaces, Symbols, Packages, and Systems · Learn Lisp The Hard Way</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<meta name="author" content=""the Phoeron" Colin J.E. Lupton">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-hints/plugin-hints.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-folding-chapters/folding-chapters.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="1-13-0-simple-web-app.html" />
<link rel="prev" href="1-11-0-text-adventure.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="header">LEARN LISP THE HARD WAY</li>
<li class="chapter " data-level="1.1" data-path="./">
<a href="index.html">
Second Draft (in-progress)
</a>
</li>
<li class="chapter " data-level="1.2" data-path="CHANGELOG.html">
<a href="CHANGELOG.html">
CHANGELOG
</a>
</li>
<li class="chapter " data-level="1.3" data-path="TODO.html">
<a href="TODO.html">
TODO
</a>
</li>
<li class="header">PREFACE</li>
<li class="chapter " data-level="2.1" data-path="preface.html">
<a href="preface.html">
TANSTAAFL
</a>
</li>
<li class="chapter " data-level="2.2" data-path="preface-part-two.html">
<a href="preface-part-two.html">
The Hard Way is Easier
</a>
</li>
<li class="chapter " data-level="2.3" data-path="preface-part-three.html">
<a href="preface-part-three.html">
Who Is This Book For?
</a>
</li>
<li class="chapter " data-level="2.4" data-path="introduction.html">
<a href="introduction.html">
Lisp: A Future History
</a>
</li>
<li class="chapter " data-level="2.5" data-path="acknowledgements.html">
<a href="acknowledgements.html">
Acknowledgements
</a>
</li>
<li class="header">PART ONE</li>
<li class="chapter " data-level="3.1" data-path="1-0-0-overview.html">
<a href="1-0-0-overview.html">
Grokking Lisp
</a>
<ul class="articles">
<li class="chapter " data-level="3.1.1" data-path="1-01-00-lisp-bootcamp.html">
<a href="1-01-00-lisp-bootcamp.html">
Common Lisp Bootcamp
</a>
<ul class="articles">
<li class="chapter " data-level="3.1.1.1" data-path="1-01-01-syntax-overview.html">
<a href="1-01-01-syntax-overview.html">
Syntax Overview in 5 Minutes
</a>
</li>
<li class="chapter " data-level="3.1.1.2" data-path="1-01-02-repl.html">
<a href="1-01-02-repl.html">
The REPL
</a>
</li>
<li class="chapter " data-level="3.1.1.3" data-path="1-01-03-expressions.html">
<a href="1-01-03-expressions.html">
Expressions, Parentheses, and Return Values
</a>
</li>
<li class="chapter " data-level="3.1.1.4" data-path="1-01-04-lists-cons-cells.html">
<a href="1-01-04-lists-cons-cells.html">
Lists, Cons-Cells, and Memory
</a>
</li>
<li class="chapter " data-level="3.1.1.5" data-path="1-01-05-symbols.html">
<a href="1-01-05-symbols.html">
Symbols and Namespaces
</a>
</li>
<li class="chapter " data-level="3.1.1.6" data-path="1-01-06-prefix-notation.html">
<a href="1-01-06-prefix-notation.html">
Prefix Notation
</a>
</li>
<li class="chapter " data-level="3.1.1.7" data-path="1-01-07-style-guide.html">
<a href="1-01-07-style-guide.html">
Common Lisp Style Guide
</a>
</li>
<li class="chapter " data-level="3.1.1.8" data-path="1-01-08-configuration.html">
<a href="1-01-08-configuration.html">
Configuring Your Development Environment
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.1.2" data-path="1-02-00-input-output.html">
<a href="1-02-00-input-output.html">
Printing, Streams, and Strings
</a>
<ul class="articles">
<li class="chapter " data-level="3.1.2.1" data-path="1-02-01-strings.html">
<a href="1-02-01-strings.html">
Strings
</a>
</li>
<li class="chapter " data-level="3.1.2.2" data-path="1-02-02-more-strings.html">
<a href="1-02-02-more-strings.html">
More Strings
</a>
</li>
<li class="chapter " data-level="3.1.2.3" data-path="1-02-03-unicode.html">
<a href="1-02-03-unicode.html">
Unicode and Strings
</a>
</li>
<li class="chapter " data-level="3.1.2.4" data-path="1-02-04-chars.html">
<a href="1-02-04-chars.html">
Characters
</a>
</li>
<li class="chapter " data-level="3.1.2.5" data-path="1-02-05-more-chars.html">
<a href="1-02-05-more-chars.html">
More Characters
</a>
</li>
<li class="chapter " data-level="3.1.2.6" data-path="1-02-06-char-codes.html">
<a href="1-02-06-char-codes.html">
Character Codes
</a>
</li>
<li class="chapter " data-level="3.1.2.7" data-path="1-02-07-strings-from-chars.html">
<a href="1-02-07-strings-from-chars.html">
Strings from Chars
</a>
</li>
<li class="chapter " data-level="3.1.2.8" data-path="1-02-08-printing.html">
<a href="1-02-08-printing.html">
Printing
</a>
</li>
<li class="chapter " data-level="3.1.2.9" data-path="1-02-09-more-printing.html">
<a href="1-02-09-more-printing.html">
More Printing
</a>
</li>
<li class="chapter " data-level="3.1.2.10" data-path="1-02-10-prin1.html">
<a href="1-02-10-prin1.html">
Printing With prin1
</a>
</li>
<li class="chapter " data-level="3.1.2.11" data-path="1-02-11-princ.html">
<a href="1-02-11-princ.html">
Printing With princ
</a>
</li>
<li class="chapter " data-level="3.1.2.12" data-path="1-02-12-format.html">
<a href="1-02-12-format.html">
A Brief Introduction to Format
</a>
</li>
<li class="chapter " data-level="3.1.2.13" data-path="1-02-13-more-format.html">
<a href="1-02-13-more-format.html">
A Little Bit More on Format
</a>
</li>
<li class="chapter " data-level="3.1.2.14" data-path="1-02-14-pathnames.html">
<a href="1-02-14-pathnames.html">
Pathnames
</a>
</li>
<li class="chapter " data-level="3.1.2.15" data-path="1-02-15-streams.html">
<a href="1-02-15-streams.html">
Streams
</a>
</li>
<li class="chapter " data-level="3.1.2.16" data-path="1-02-16-file-streams.html">
<a href="1-02-16-file-streams.html">
File Streams
</a>
</li>
<li class="chapter " data-level="3.1.2.17" data-path="1-02-17-binary-streams.html">
<a href="1-02-17-binary-streams.html">
Binary Streams
</a>
</li>
<li class="chapter " data-level="3.1.2.18" data-path="1-02-18-prompting-users.html">
<a href="1-02-18-prompting-users.html">
Prompting Users
</a>
</li>
<li class="chapter " data-level="3.1.2.19" data-path="1-02-19-pretty-printing.html">
<a href="1-02-19-pretty-printing.html">
Pretty-Printing
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.1.3" data-path="1-03-0-getting-input-from-users.html">
<a href="1-03-0-getting-input-from-users.html">
Extra Credit: Getting Input from Users
</a>
</li>
<li class="chapter " data-level="3.1.4" data-path="1-04-0-lists.html">
<a href="1-04-0-lists.html">
Lists and List-Operations
</a>
</li>
<li class="chapter " data-level="3.1.5" data-path="1-05-0-lookups-trees.html">
<a href="1-05-0-lookups-trees.html">
Extra Credit: Look-up Lists and Trees
</a>
</li>
<li class="chapter " data-level="3.1.6" data-path="1-06-0-math.html">
<a href="1-06-0-math.html">
Numbers and Math
</a>
<ul class="articles">
<li class="chapter " data-level="3.1.6.1" data-path="1-06-01-integers.html">
<a href="1-06-01-integers.html">
Integers
</a>
</li>
<li class="chapter " data-level="3.1.6.2" data-path="1-06-02-more-integers.html">
<a href="1-06-02-more-integers.html">
More Integers
</a>
</li>
<li class="chapter " data-level="3.1.6.3" data-path="1-06-03-hexadecimal-notation.html">
<a href="1-06-03-hexadecimal-notation.html">
Hexadecimal Integer Notation
</a>
</li>
<li class="chapter " data-level="3.1.6.4" data-path="1-06-04-octal-notation.html">
<a href="1-06-04-octal-notation.html">
Octal Integer Notation
</a>
</li>
<li class="chapter " data-level="3.1.6.5" data-path="1-06-05-binary-notation.html">
<a href="1-06-05-binary-notation.html">
Binary Integer Notation
</a>
</li>
<li class="chapter " data-level="3.1.6.6" data-path="1-06-06-ratios.html">
<a href="1-06-06-ratios.html">
Ratios and Rational Numbers
</a>
</li>
<li class="chapter " data-level="3.1.6.7" data-path="1-06-07-floating-point.html">
<a href="1-06-07-floating-point.html">
Floating-point Numbers
</a>
</li>
<li class="chapter " data-level="3.1.6.8" data-path="1-06-08-constants.html">
<a href="1-06-08-constants.html">
Numeric Constants
</a>
</li>
<li class="chapter " data-level="3.1.6.9" data-path="1-06-09-complex-numbers.html">
<a href="1-06-09-complex-numbers.html">
Complex Numbers
</a>
</li>
<li class="chapter " data-level="3.1.6.10" data-path="1-06-10-arithmetic.html">
<a href="1-06-10-arithmetic.html">
Arithmetic
</a>
</li>
<li class="chapter " data-level="3.1.6.11" data-path="1-06-11-more-arithmetic.html">
<a href="1-06-11-more-arithmetic.html">
More Arithmetic
</a>
</li>
<li class="chapter " data-level="3.1.6.12" data-path="1-06-12-even-more-arithmetic.html">
<a href="1-06-12-even-more-arithmetic.html">
Even More Arithmetic
</a>
</li>
<li class="chapter " data-level="3.1.6.13" data-path="1-06-13-exponents.html">
<a href="1-06-13-exponents.html">
Exponents
</a>
</li>
<li class="chapter " data-level="3.1.6.14" data-path="1-06-14-logarithms.html">
<a href="1-06-14-logarithms.html">
Logarithms
</a>
</li>
<li class="chapter " data-level="3.1.6.15" data-path="1-06-15-trigonometry.html">
<a href="1-06-15-trigonometry.html">
Trigonometry
</a>
</li>
<li class="chapter " data-level="3.1.6.16" data-path="1-06-16-psuedorandom-numbers.html">
<a href="1-06-16-psuedorandom-numbers.html">
Pseudo-Random Numbers
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.1.7" data-path="1-07-0-arrays.html">
<a href="1-07-0-arrays.html">
Extra Credit: Arrays and Vectors
</a>
</li>
<li class="chapter " data-level="3.1.8" data-path="1-08-0-variables.html">
<a href="1-08-0-variables.html">
Variables, Parameters, and Constants
</a>
</li>
<li class="chapter " data-level="3.1.9" data-path="1-09-0-closures.html">
<a href="1-09-0-closures.html">
Extra Credit: Closures
</a>
</li>
<li class="chapter " data-level="3.1.10" data-path="1-10-0-functions.html">
<a href="1-10-0-functions.html">
Functions and Macros
</a>
</li>
<li class="chapter " data-level="3.1.11" data-path="1-11-0-text-adventure.html">
<a href="1-11-0-text-adventure.html">
Extra Credit: A Simple Text Adventure
</a>
</li>
<li class="chapter active" data-level="3.1.12" data-path="1-12-0-namespaces.html">
<a href="1-12-0-namespaces.html">
Namespaces, Symbols, Packages, and Systems
</a>
</li>
<li class="chapter " data-level="3.1.13" data-path="1-13-0-simple-web-app.html">
<a href="1-13-0-simple-web-app.html">
Extra Credit: A Simple Web Application
</a>
</li>
<li class="chapter " data-level="3.1.14" data-path="1-14-0-conditionals.html">
<a href="1-14-0-conditionals.html">
Conditionals
</a>
</li>
<li class="chapter " data-level="3.1.15" data-path="1-15-0-command-line-utility.html">
<a href="1-15-0-command-line-utility.html">
Extra Credit: Command-Line Utilities
</a>
</li>
<li class="chapter " data-level="3.1.16" data-path="1-16-0-map-loop.html">
<a href="1-16-0-map-loop.html">
Mapping and Looping
</a>
</li>
<li class="chapter " data-level="3.1.17" data-path="1-17-0-iterate.html">
<a href="1-17-0-iterate.html">
Extra Credit: Revisiting Loops with Iterate
</a>
</li>
<li class="chapter " data-level="3.1.18" data-path="1-18-0-format.html">
<a href="1-18-0-format.html">
Format Strings
</a>
</li>
<li class="chapter " data-level="3.1.19" data-path="1-19-0-dsl.html">
<a href="1-19-0-dsl.html">
Extra Credit: Domain Specific Languages
</a>
</li>
<li class="chapter " data-level="3.1.20" data-path="1-20-0-review.html">
<a href="1-20-0-review.html">
Part One in Review
</a>
</li>
</ul>
</li>
<li class="header">PART TWO</li>
<li class="chapter " data-level="4.1" data-path="2-0-0-overview.html">
<a href="2-0-0-overview.html">
The Suffusion of Blue
</a>
<ul class="articles">
<li class="chapter " data-level="4.1.1" data-path="2-01-0-programming-paradigms.html">
<a href="2-01-0-programming-paradigms.html">
Programming Paradigms
</a>
</li>
<li class="chapter " data-level="4.1.2" data-path="2-02-0-regex.html">
<a href="2-02-0-regex.html">
Extra Credit: Regular Expressions
</a>
</li>
<li class="chapter " data-level="4.1.3" data-path="2-03-0-objects-control.html">
<a href="2-03-0-objects-control.html">
Objects and Control Structures
</a>
</li>
<li class="chapter " data-level="4.1.4" data-path="2-04-0-data-persistence.html">
<a href="2-04-0-data-persistence.html">
Extra Credit: Persistence and Databases
</a>
</li>
<li class="chapter " data-level="4.1.5" data-path="2-05-0-extended-types.html">
<a href="2-05-0-extended-types.html">
Extended Types
</a>
</li>
<li class="chapter " data-level="4.1.6" data-path="2-06-0-threads-memos-parallel.html">
<a href="2-06-0-threads-memos-parallel.html">
Extra Credit: Concurrency and Memoization
</a>
</li>
<li class="chapter " data-level="4.1.7" data-path="2-07-0-logic-and-more-math.html">
<a href="2-07-0-logic-and-more-math.html">
Logic and Advanced Math
</a>
</li>
<li class="chapter " data-level="4.1.8" data-path="2-08-0-number-theory.html">
<a href="2-08-0-number-theory.html">
Extra Credit: Number Theory
</a>
</li>
<li class="chapter " data-level="4.1.9" data-path="2-09-0-binary-octets-bits.html">
<a href="2-09-0-binary-octets-bits.html">
Binary Streams, Octet-Vectors, and Bit-Vectors
</a>
</li>
<li class="chapter " data-level="4.1.10" data-path="2-10-0-improved-text-adventure-engine.html">
<a href="2-10-0-improved-text-adventure-engine.html">
Extra Credit: An Improved Text Adventure Engine
</a>
</li>
<li class="chapter " data-level="4.1.11" data-path="2-11-0-conditions.html">
<a href="2-11-0-conditions.html">
Conditions and Error Handling
</a>
</li>
<li class="chapter " data-level="4.1.12" data-path="2-12-0-2d-game.html">
<a href="2-12-0-2d-game.html">
Extra Credit: Write a 2D Game
</a>
</li>
<li class="chapter " data-level="4.1.13" data-path="2-13-0-compiler.html">
<a href="2-13-0-compiler.html">
The Compiler
</a>
</li>
<li class="chapter " data-level="4.1.14" data-path="2-14-0-tree-shaker.html">
<a href="2-14-0-tree-shaker.html">
Extra Credit: Write a Tree-Shaker
</a>
</li>
<li class="chapter " data-level="4.1.15" data-path="2-15-0-docs-and-inspection.html">
<a href="2-15-0-docs-and-inspection.html">
Documentation and Inspection
</a>
</li>
<li class="chapter " data-level="4.1.16" data-path="2-16-0-foreign-libs.html">
<a href="2-16-0-foreign-libs.html">
Extra Credit: Foreign Libraries in Lisp
</a>
</li>
<li class="chapter " data-level="4.1.17" data-path="2-17-0-debugging-testing.html">
<a href="2-17-0-debugging-testing.html">
Debugging and Unit Testing
</a>
</li>
<li class="chapter " data-level="4.1.18" data-path="2-18-0-ffi.html">
<a href="2-18-0-ffi.html">
Extra Credit: Write a Foreign Function Interface
</a>
</li>
<li class="chapter " data-level="4.1.19" data-path="2-19-0-essential-libs.html">
<a href="2-19-0-essential-libs.html">
Essential Lisp Libraries
</a>
</li>
<li class="chapter " data-level="4.1.20" data-path="2-20-0-packaging-libs.html">
<a href="2-20-0-packaging-libs.html">
Extra Credit: Packaging Lisp Libraries
</a>
</li>
<li class="chapter " data-level="4.1.21" data-path="2-21-0-review.html">
<a href="2-21-0-review.html">
Detailed Syntax Review
</a>
</li>
</ul>
</li>
<li class="header">PART THREE</li>
<li class="chapter " data-level="5.1" data-path="3-00-00-overview.html">
<a href="3-00-00-overview.html">
Lisp So(u)rcery
</a>
<ul class="articles">
<li class="chapter " data-level="5.1.1" data-path="3-01-00-web-apps.html">
<a href="3-01-00-web-apps.html">
Real-world Web Apps
</a>
</li>
<li class="chapter " data-level="5.1.2" data-path="3-02-00-typesetting.html">
<a href="3-02-00-typesetting.html">
Typesetting
</a>
</li>
<li class="chapter " data-level="5.1.3" data-path="3-03-00-mobile.html">
<a href="3-03-00-mobile.html">
Native Mobile Applications
</a>
</li>
<li class="chapter " data-level="5.1.4" data-path="3-04-00-gui.html">
<a href="3-04-00-gui.html">
Cross-platform Desktop Applications
</a>
</li>
<li class="chapter " data-level="5.1.5" data-path="3-05-00-system-utils.html">
<a href="3-05-00-system-utils.html">
Drivers, Daemons, and System-Utilities
</a>
</li>
<li class="chapter " data-level="5.1.6" data-path="3-06-00-reverse-engineering.html">
<a href="3-06-00-reverse-engineering.html">
Reverse Engineering
</a>
</li>
<li class="chapter " data-level="5.1.7" data-path="3-07-00-graphics.html">
<a href="3-07-00-graphics.html">
Graphics Rendering
</a>
</li>
<li class="chapter " data-level="5.1.8" data-path="3-08-00-gaming.html">
<a href="3-08-00-gaming.html">
OpenGL, SDL, and 3D Game Development
</a>
</li>
<li class="chapter " data-level="5.1.9" data-path="3-09-00-audio.html">
<a href="3-09-00-audio.html">
Audio Generation and Manipulation
</a>
</li>
<li class="chapter " data-level="5.1.10" data-path="3-10-00-data.html">
<a href="3-10-00-data.html">
Data Aggregation and Analysis
</a>
</li>
<li class="chapter " data-level="5.1.11" data-path="3-11-00-cryptosec.html">
<a href="3-11-00-cryptosec.html">
Cryptography and Security
</a>
</li>
<li class="chapter " data-level="5.1.12" data-path="3-12-00-fintech.html">
<a href="3-12-00-fintech.html">
Financial Software and Crypto-Currencies
</a>
</li>
<li class="chapter " data-level="5.1.13" data-path="3-13-00-scientific-computing.html">
<a href="3-13-00-scientific-computing.html">
Scientific Computing
</a>
</li>
<li class="chapter " data-level="5.1.14" data-path="3-14-00-computational-physics.html">
<a href="3-14-00-computational-physics.html">
Computational Physics
</a>
</li>
<li class="chapter " data-level="5.1.15" data-path="3-15-00-quantum-computing.html">
<a href="3-15-00-quantum-computing.html">
Quantum Computing
</a>
</li>
<li class="chapter " data-level="5.1.16" data-path="3-16-00-nlp.html">
<a href="3-16-00-nlp.html">
Natural Language Processing
</a>
</li>
<li class="chapter " data-level="5.1.17" data-path="3-17-00-ai.html">
<a href="3-17-00-ai.html">
Artificial Intelligence
</a>
</li>
<li class="chapter " data-level="5.1.18" data-path="3-18-00-robotics.html">
<a href="3-18-00-robotics.html">
Robotics
</a>
</li>
<li class="chapter " data-level="5.1.19" data-path="3-19-00-space-tech.html">
<a href="3-19-00-space-tech.html">
Space Tech
</a>
</li>
<li class="chapter " data-level="5.1.20" data-path="3-20-00-neurotech.html">
<a href="3-20-00-neurotech.html">
Neuroscience and Thought-Controlled Computing
</a>
</li>
<li class="chapter " data-level="5.1.21" data-path="3-21-00-lispos.html">
<a href="3-21-00-lispos.html">
A Simple LispOS
</a>
</li>
<li class="chapter " data-level="5.1.22" data-path="3-22-00-lisp-machine.html">
<a href="3-22-00-lisp-machine.html">
Build Your Own Lisp Machine
</a>
</li>
<li class="chapter " data-level="5.1.23" data-path="3-23-00-gov-mil.html">
<a href="3-23-00-gov-mil.html">
Government and Military Grade Systems
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="index.html" >Namespaces, Symbols, Packages, and Systems</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="chapter-112">Chapter 1.12</h1>
<h2 id="namespaces-symbols-packages-and-systems">Namespaces, Symbols, Packages, and Systems</h2>
<blockquote>
<p>&quot;Every law that was ever written opened up a new way to graft.&quot;</p>
<footer>Robert A. Heinlein, <em>Red Planet</em></footer>
</blockquote>
<p>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.</p>
<p>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 <code>defun</code> does.</p>
<p>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.</p>
<p>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.</p>
<h3 id="exercise-1121">Exercise 1.12.1</h3>
<p><strong>Dynamic and Lexical Scope, Revisited</strong></p>
<p>All symbols in Common Lisp are namespaced in packages, and within a namespace, symbols are also scoped. <em>Scope</em> refers to how the Lisp reader looks up symbol definitions in the current environment.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">(<span class="hljs-name">defparameter</span> one <span class="hljs-number">#x01</span>)
(<span class="hljs-name">let</span> ((<span class="hljs-name">one</span> <span class="hljs-number">1</span>))
(<span class="hljs-name">let</span> ((<span class="hljs-name">one</span> <span class="hljs-number">1.0</span>))
(<span class="hljs-name">let</span> ((<span class="hljs-name">one</span> <span class="hljs-string">&quot;one&quot;</span>))
(<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;~a~%&quot;</span> one))
(<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;~a~%&quot;</span> one))
(<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;~a~%&quot;</span> one))
(<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;#x~X~%&quot;</span> one)
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">* (<span class="hljs-name">defparameter</span> one <span class="hljs-number">#x01</span>)
ONE
* (<span class="hljs-name">let</span> ((<span class="hljs-name">one</span> <span class="hljs-number">1</span>))
(<span class="hljs-name">let</span> ((<span class="hljs-name">one</span> <span class="hljs-number">1.0</span>))
(<span class="hljs-name">let</span> ((<span class="hljs-name">one</span> <span class="hljs-string">&quot;one&quot;</span>))
(<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;~a~%&quot;</span> one))
(<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;~a~%&quot;</span> one))
(<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;~a~%&quot;</span> one))
one
<span class="hljs-number">1.0</span>
<span class="hljs-number">1</span>
NIL
* (<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;#x~2,&apos;0,,X~%&quot;</span> one)
<span class="hljs-number">#x01</span>
NIL
</code></pre>
<p>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 <em>spatial</em> 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.</p>
<p>Remember that Lexical Scope <em>shadows</em> 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.</p>
<h3 id="exercise-1122">Exercise 1.12.2</h3>
<p><strong>Implicit Lexical Scope</strong></p>
<p>Many forms in Common Lisp introduce an <em>implicit</em> lexical scope. That is, you do not have explicitly call a <code>let</code> or <code>flet</code> 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 <code>DEFUN</code> macro. You will also find implicit lexical scoping in <code>BLOCK</code> forms, and destructuring macros such as <code>DESTRUCTURING-BIND</code>, to name a few.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">(<span class="hljs-name">block</span> ta-da!
(<span class="hljs-name">cond</span> ((<span class="hljs-name">=</span> (<span class="hljs-name">+</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span>) <span class="hljs-number">2</span>)
(<span class="hljs-name">return-from</span> ta-da! <span class="hljs-literal">t</span>))
((<span class="hljs-name">=</span> (<span class="hljs-name">+</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span>) <span class="hljs-number">3</span>)
(<span class="hljs-name">return-from</span> ta-da! <span class="hljs-literal">nil</span>))
(<span class="hljs-name">t</span>
(<span class="hljs-name">return-from</span> ta-da! <span class="hljs-string">&quot;Wait, what?&quot;</span>))))
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">(<span class="hljs-name">block</span> ta-da!
(<span class="hljs-name">cond</span> ((<span class="hljs-name">=</span> (<span class="hljs-name">+</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span>) <span class="hljs-number">2</span>)
(<span class="hljs-name">return-from</span> ta-da! <span class="hljs-literal">t</span>))
((<span class="hljs-name">=</span> (<span class="hljs-name">+</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span>) <span class="hljs-number">3</span>)
(<span class="hljs-name">return-from</span> ta-da! <span class="hljs-literal">nil</span>))
(<span class="hljs-name">t</span>
(<span class="hljs-name">return-from</span> ta-da! <span class="hljs-string">&quot;Wait, what?&quot;</span>))))
T
</code></pre>
<h3 id="exercise-1123">Exercise 1.12.3</h3>
<p><strong>Function and Variable Namespaces, Revisited</strong></p>
<p>Common Lisp has seprate namespaces for Functions and Variables. This means that you can define a function and variable within the same dynamic or lexical scope that share the same symbol name, and Lisp will generally figure out on its own which you mean by its context---<em>i.e.</em>, where in an S-Expression the symbol appears.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">(<span class="hljs-name">defparameter</span> nonce <span class="hljs-number">1.4</span>)
(<span class="hljs-name">defun</span> nonce (<span class="hljs-name">n</span>)
(<span class="hljs-name">*</span> (<span class="hljs-name">random</span> <span class="hljs-number">128</span>) n))
(<span class="hljs-name">inspect</span> &apos;nonce)
&gt; <span class="hljs-number">2</span>
&gt; u
&gt; <span class="hljs-number">3</span>
&gt; <span class="hljs-number">0</span>
&gt; u
&gt; <span class="hljs-number">1</span>
&gt; q
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<p>You can inspect any object in Lisp. When you inspect a symbol, you get its symbol table in the form of a menu with a new prompt, <code>&gt;</code>. This allows you to choose an entry in the table by number, and see more information about it. You can return to the Common Lisp REPL by entering <code>q</code>, or navigate back up a level with <code>u</code>. When you&apos;ve defined both a variable and a function with the same name, you can then see them both in the symbol table and inspect every object that they consist of.</p>
<pre><code class="lang-lisp">(defparameter nonce 1.4)
NONCE
(defun nonce (n)
(* (random 128) n))
NONCE
(inspect &apos;nonce)
The object is a SYMBOL.
0. Name: &quot;NONCE&quot;
1. Package: #&lt;PACKAGE &quot;COMMON-LISP-USER&quot;&gt;
2. Value: 1.4
3. Function: #&lt;FUNCTION NONCE&gt;
4. Plist: NIL
&gt; 2
The object is an ATOM:
1.4
&gt; u
...
&gt; 3
The object is a FUNCTION named NONCE.
0. Lambda-list: (N)
1. Ftype: (FUNCTION (T) (VALUES NUMBER &amp;OPTIONAL))
&gt; 0
The object is a CONS.
0. CAR: N
1. CDR: NIL
&gt; u
...
&gt; 1
The object is a proper list of length 3.
0. 0: FUNCTION
1. 1: (T)
2. 2: (VALUES NUMBER &amp;OPTIONAL)
&gt; q
</code></pre>
<h3 id="exercise-1124">Exercise 1.12.4</h3>
<p><strong>First-Class Functions</strong></p>
<p>As mentioned in the previous exercise, Lisp will assume that when a symbol appears in the operator position of a list, it refers to a function, and when it appears in the lambda list, it refers to the variable value. But Common Lisp supports first-class functions, so naturally there must be a way, in parameter position, for the Lisp reader to recognize when you mean the function namespace in parameter position.</p>
<p>This is the reader macro <code>#&apos;</code>. Prepend it to a symbol, and the Lisp reader will know to treat that parameter as a first-class function instead of as a variable.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">(<span class="hljs-name">mapcar</span> #&apos;nonce &apos;(<span class="hljs-number">128</span> <span class="hljs-number">123</span> <span class="hljs-number">118</span> <span class="hljs-number">113</span> <span class="hljs-number">108</span> <span class="hljs-number">103</span> <span class="hljs-number">98</span> <span class="hljs-number">93</span>))
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">* (<span class="hljs-name">mapcar</span> #&apos;nonce &apos;(<span class="hljs-number">128</span> <span class="hljs-number">123</span> <span class="hljs-number">118</span> <span class="hljs-number">113</span> <span class="hljs-number">108</span> <span class="hljs-number">103</span> <span class="hljs-number">98</span> <span class="hljs-number">93</span>))
(<span class="hljs-number">13184</span> <span class="hljs-number">12300</span> <span class="hljs-number">10502</span> <span class="hljs-number">13786</span> <span class="hljs-number">3564</span> <span class="hljs-number">1751</span> <span class="hljs-number">4998</span> <span class="hljs-number">5115</span>)
</code></pre>
<p>First-class functions are very different than a function application that happens to be in parameter position. Lisp code is evaluated innermost-form first, so a form in source-code can almost always be replaced by its return value---and as far as Lisp is concerned, it is. This is known in Comp-Sci parlance as &quot;pass-by-value&quot; or &quot;call-by-value&quot;, which is to say that a form is actually evaluated to a result, and the result itself is passed to the parent form and copied---anything done to that value has no effect on the original. For a function application, this doesn&apos;t mean much because the evaluation usually <em>computes</em> this original value in the first place---but for first-class function objects and variables, this is quite important.</p>
<h3 id="exercise-1125">Exercise 1.12.5</h3>
<p><strong>Symbol Names</strong></p>
<p>Symbol names are stored as case-sensitive strings in the symbol table. Typically, the Lisp reader upcases symbol-names when a symbol is created unless forced to do otherwise, such as with the <code>INTERN</code> function.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">(<span class="hljs-name">symbol-name</span> &apos;pi)
(<span class="hljs-name">intern</span> <span class="hljs-string">&quot;myCamelCaseSymbol&quot;</span>)
(<span class="hljs-name">symbol-name</span> &apos;|myCamelCaseSymbol|)
(<span class="hljs-name">symbol-name</span> &apos;cl-user::|myCamelCaseSymbol|)
(<span class="hljs-name">export</span> &apos;|myCamelCaseSymbol|)
(<span class="hljs-name">symbol-name</span> &apos;cl-user:|myCamelCaseSymbol|)
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">(symbol-name &apos;pi)
&quot;PI&quot;
(intern &quot;myCamelCaseSymbol&quot;)
|myCamelCaseSymbol|
NIL
(symbol-name &apos;|myCamelCaseSymbol|)
&quot;myCamelCaseSymbol&quot;
;; this symbol hasn&apos;t been exported yet, so we have to refer to it using package-internal namespacing
(symbol-name &apos;cl-user::|myCamelCaseSymbol|)
&quot;myCamelCaseSymbol&quot;
(export &apos;|myCamelCaseSymbol|)
T
;; now that we&apos;ve exported |myCamelCaseSymbol|, we can call it with package-external namespacing and import it into other packages
(symbol-name &apos;cl-user:|myCamelCaseSymbol|)
&quot;myCamelCaseSymbol&quot;
</code></pre>
<p>Symbols exist within packages, so their full name is prefixed with their package. When your working package is the same namespace as where the symbol was interned, you can omit the package prefix---but it is still there.</p>
<h3 id="exercise-1126">Exercise 1.12.6</h3>
<p><strong>Packages</strong></p>
<p>In addition to the separate Function and Variable namespaces offered by Common Lisp, your code can be organized into <em>packages</em>. Packages allow you to design an interface to your code-base under a common name, separating the functions, variables, and other units of code from the Common Lisp language and from other libraries; as a result, they can help eliminate symbol collision as well.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">(defpackage my-new-package)
(in-package :my-new-package)
(cl:defparameter *hello-world*
(cl:format cl:nil &quot;Hello ~A!&quot; &apos;multiverse))
(cl:export &apos;*hello-world*)
(cl:in-package :cl-user)
my-new-package:*hello-world*
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">
(defpackage my-new-package)
#&lt;PACKAGE &quot;MY-NEW-PACKAGE&quot;&gt;
(in-package :my-new-package)
#&lt;COMMON-LISP:PACKAGE &quot;MY-NEW-PACKAGE&quot;&gt;
(cl:defparameter *hello-world*
(cl:format cl:nil &quot;Hello ~A!&quot; &apos;multiverse))
*HELLO-WORLD*
(cl:export &apos;*hello-world*)
T
(cl:in-package :cl-user)
#&lt;PACKAGE &quot;COMMON-LISP-USER&quot;&gt;
my-new-package:*hello-world*
&quot;Hello MULTIVERSE!&quot;
</code></pre>
<p>Note how in your new package, you have to explicitly namespace-resolve symbols from the Common Lisp package now in order to use them. This can be both useful and irritating, so we will introduce how to use another package&apos;s exported symbols as if they were local symbols in a few exercises.</p>
<h3 id="exercise-1127">Exercise 1.12.7</h3>
<p><strong>More Packages: Nicknames</strong></p>
<p>You can give your packages shorter, alternative nicknames which can be used for namespace-resolution on symbols, instead of having to type the full package name every single time. You have already seen this in the previous exercise, where you had to refer to Common Lisp functions and variables prefixed with <code>cl</code>. This is a package nickname for the <code>COMMON-LISP</code> package.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">(defpackage my-new-package
(:nicknames :mnp :newpack))
(in-package :my-new-package)
(cl:defparameter *hello-world*
(cl:format cl:nil &quot;Hello ~A!&quot; &apos;multiverse))
(cl:in-package :cl-user)
my-new-package::*hello-world*
mnp::*hello-world*
newpack::*hello-world*
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<p>You define a package&apos;s nicknames inside the <code>defpackage</code> macro. You can have as many nicknames as you want; but like package names themselves, they have to be unique.</p>
<pre><code class="lang-lisp">(defpackage my-new-package
(:nicknames :mnp :newpack))
#&lt;PACKAGE &quot;MY-NEW-PACKAGE&quot;&gt;
(in-package :my-new-package)
#&lt;COMMON-LISP:PACKAGE &quot;MY-NEW-PACKAGE&quot;&gt;
(cl:defparameter *hello-world*
(cl:format cl:nil &quot;Hello ~A!&quot; &apos;multiverse))
*HELLO-WORLD*
(cl:in-package :cl-user)
#&lt;PACKAGE &quot;COMMON-LISP-USER&quot;&gt;
my-new-package::*hello-world*
&quot;Hello MULTIVERSE!&quot;
mnp::*hello-world*
&quot;Hello MULTIVERSE!&quot;
newpack::*hello-world*
&quot;Hello MULTIVERSE!&quot;
</code></pre>
<h3 id="exercise-1128">Exercise 1.12.8</h3>
<p><strong>More Packages: Exporting Symbols</strong></p>
<p>Technically, once a package and all its source code has been loaded into your Lisp image, you can call any unit of code within it by using the full symbol, <code>&lt;package-name&gt;::&lt;symbol-name&gt;</code>; but it is more convenient to export an interface to your users, which they can then <em>import</em> into their packages individually, or <em>use</em> your entire library&apos;s API.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">(defpackage my-new-package
(:nicknames :mnp :newpack)
(:export #:*hello-world*))
(in-package :my-new-package)
(cl:defparameter *hello-world*
(cl:format cl:nil &quot;Hello ~A!&quot; &apos;multiverse))
(cl:in-package :cl-user)
my-new-package:*hello-world*
mnp:*hello-world*
newpack:*hello-world*
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">(defpackage my-new-package
(:nicknames :mnp :newpack)
(:export #:*hello-world*))
#&lt;PACKAGE &quot;MY-NEW-PACKAGE&quot;&gt;
(in-package :my-new-package)
#&lt;COMMON-LISP:PACKAGE &quot;MY-NEW-PACKAGE&quot;&gt;
(cl:defparameter *hello-world*
(cl:format cl:nil &quot;Hello ~A!&quot; &apos;multiverse))
*HELLO-WORLD*
(cl:in-package :cl-user)
#&lt;PACKAGE &quot;COMMON-LISP-USER&quot;&gt;
my-new-package:*hello-world*
&quot;Hello MULTIVERSE!&quot;
mnp:*hello-world*
&quot;Hello MULTIVERSE!&quot;
newpack:*hello-world*
&quot;Hello MULTIVERSE!&quot;
</code></pre>
<p>You can export your defined units of code either with the <code>export</code> form, or specifying symbols to export in your package definition; if you try to export the same symbol twice, once from the package definition and again with <code>export</code> form, you&apos;ll trigger a condition. Generally speaking it&apos;s considered best practice to export all your symbols from the package definition form, so that your library&apos;s API is clear and together in one place.</p>
<h3 id="exercise-1129">Exercise 1.12.9</h3>
<p><strong>More Packages: Using Other Packages</strong></p>
<p>When you <em>use</em> a package in your package, you are telling Lisp to add all the exported symbols of that package to your current package-internal namespace, so that you can call any unit of code locally as if you defined it yourself in your current package.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">(defpackage my-new-package
(:nicknames :mnp :newpack)
(:use :cl :cl-user)
(:export #:*hello-world*))
(in-package :my-new-package)
(defparameter *hello-world*
(format nil &quot;Hello ~A!&quot; &apos;multiverse))
(in-package :cl-user)
my-new-package:*hello-world*
mnp:*hello-world*
newpack:*hello-world*
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">(defpackage my-new-package
(:nicknames :mnp :newpack)
(:use :cl :cl-user)
(:export #:*hello-world*))
(in-package :my-new-package)
(defparameter *hello-world*
(format nil &quot;Hello ~A!&quot; &apos;multiverse))
(in-package :cl-user)
my-new-package:*hello-world*
mnp:*hello-world*
newpack:*hello-world*
</code></pre>
<p>The first thing you should notice is that now you no longer have to refer to symbols from the Common Lisp language proper using their package namespace from within your new package. You will (almost) always want to, at minimum, <em>use</em> the <code>COMMON-LISP</code> package in your packages; and since you will be working a lot in the REPL from your new package, you will also want to <em>use</em> the <code>COMMON-LISP-USER</code> package as well, since in addition to the full Common Lisp language, it includes all the utilities that your Lisp implementation includes for a more complete experience.</p>
<p>While you develop your library, it can be useful to call out units of code from other packages and libraries explicitly, using their full package-namespaced symbol, so that there is no confusion on your dev team as to what code is being called---if you find that you&apos;re relying heavily enough on a library that typing out its package nickname before each symbol becomes tedious, annoying, and a source of existential woe, that is a good indicator the library should be <em>used</em>, and your full dev team made deeply familiar with its interface.</p>
<p>Once development is complete, you will want to remove the <code>COMMON-LISP-USER</code> package from your packages before release unless you explicitly need it.</p>
<h3 id="exercise-11210">Exercise 1.12.10</h3>
<p><strong>More Packages: Importing Specific Symbols</strong></p>
<p>You won&apos;t always need to <em>use</em> a full library; more often than not, you will only need to import a handful of specific symbols. This is another advantage of typing out the full symbol, package namespace included, while developing your library---you can search through your source code for references to a given package, and make a judgment call as to whether you need to use it or only import the symbols you need. This helps to avoid cluttering your namespace, among other things.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">
</code></pre>
<h3 id="exercise-11211">Exercise 1.12.11</h3>
<p><strong>More Packages: Shadowing-Imports</strong></p>
<p>Sometimes, you will come across naming collisions between libraries you need to use, which at first glance may seem to force you to manually import, symbol by symbol, everything from a library except the offender. That&apos;s not very efficient, however, so Lisp has you covered---you can <em>shadow-import</em>, which lets you tell Lisp exactly which symbol from which package you want to favour in the case of a naming collision.</p>
<h4 id="in-the-repl">In the REPL</h4>
<pre><code class="lang-lisp">
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">
</code></pre>
<h3 id="extra-credit-exercise-11212">Extra Credit Exercise 1.12.12</h3>
<p><strong>ASDF and Systems</strong></p>
<p>ASDF is, essentially, a build-system for Common Lisp---but that doesn&apos;t really do it justice. For our purposes, it is a necessary tool for defining libraries for the Quicklisp package manager.</p>
<p>ASDF focuses on the concept of a <em>System</em>. A System is a collection of Lisp source-code files, which may or may not include a number of packages, wrapped up with some meta-data.</p>
<p>ASDF Systems are typically defined in their own <code>*.asd</code> files, instead of <code>*.lisp</code>. However, they are still written using Common Lisp syntax.</p>
<h4 id="in-a-new-file">In a New File</h4>
<p>In your <code>~/quicklisp/local-projects/</code> directory, create a subdirectory called <code>my-new-project/</code>, and in it, create a new file called <code>my-new-project.asd</code>. The contents of this file should look like the following, with the appropriate personalizations completed:</p>
<pre><code class="lang-lisp">(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:cl-user</span>)
(<span class="hljs-name">defpackage</span> my-new-project-asd
(<span class="hljs-symbol">:use</span> <span class="hljs-symbol">:cl</span> <span class="hljs-symbol">:cl-user</span> <span class="hljs-symbol">:asdf</span>))
(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:my-new-project-asd</span>)
(<span class="hljs-name">defsystem</span> my-new-project
<span class="hljs-symbol">:version</span> <span class="hljs-string">&quot;1.0.0&quot;</span>
<span class="hljs-symbol">:license</span> <span class="hljs-string">&quot;MIT&quot;</span>
<span class="hljs-symbol">:author</span> <span class="hljs-string">&quot;A.B. &lt;a.b@example.org&gt;&quot;</span>)
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">
</code></pre>
<h3 id="extra-credit-exercise-11213">Extra Credit Exercise 1.12.13</h3>
<p><strong>ASDF Systems: Dependencies</strong></p>
<p>ASDF allows you to specify library dependencies in your system definition; the project will then not compile unless these dependencies are available. The Quicklisp package manager uses this information to automatically resolve all dependencies when quickloading a library.</p>
<p>If you <em>use</em> a library in any package within your project, you must include it in the system&apos;s <code>:depends-on</code> form, to ensure that it is available to your project wherever it is run or installed.</p>
<h4 id="in-the-file">In the File</h4>
<p>Revise <code>my-new-project.asd</code> to look like the following:</p>
<pre><code class="lang-lisp">(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:cl-user</span>)
(<span class="hljs-name">defpackage</span> my-new-project-asd
(<span class="hljs-symbol">:use</span> <span class="hljs-symbol">:cl</span> <span class="hljs-symbol">:cl-user</span> <span class="hljs-symbol">:asdf</span>))
(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:my-new-project-asd</span>)
(<span class="hljs-name">defsystem</span> my-new-project
<span class="hljs-symbol">:version</span> <span class="hljs-string">&quot;1.0.0&quot;</span>
<span class="hljs-symbol">:license</span> <span class="hljs-string">&quot;MIT&quot;</span>
<span class="hljs-symbol">:author</span> <span class="hljs-string">&quot;A.B. &lt;a.b@example.org&gt;&quot;</span>
<span class="hljs-symbol">:depends-on</span> (<span class="hljs-symbol">:ironclad</span>))
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">
</code></pre>
<h3 id="extra-credit-exercise-11214">Extra Credit Exercise 1.12.14</h3>
<p><strong>ASDF Systems: Files</strong></p>
<p>In the same directory as <code>my-new-project.asd</code>, create empty files for <code>packages.lisp</code> and <code>my-new-project.lisp</code>.</p>
<p>You don&apos;t have to specify that the files you&apos;re including in components are <code>*.lisp</code> files, you only have to specify their names, without the file extension.</p>
<h4 id="in-the-file">In the File</h4>
<p>Revise <code>my-new-project.asd</code> to look like the following:</p>
<pre><code class="lang-lisp">(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:cl-user</span>)
(<span class="hljs-name">defpackage</span> my-new-project-asd
(<span class="hljs-symbol">:use</span> <span class="hljs-symbol">:cl</span> <span class="hljs-symbol">:cl-user</span> <span class="hljs-symbol">:asdf</span>))
(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:my-new-project-asd</span>)
(<span class="hljs-name">defsystem</span> my-new-project
<span class="hljs-symbol">:version</span> <span class="hljs-string">&quot;1.0.0&quot;</span>
<span class="hljs-symbol">:license</span> <span class="hljs-string">&quot;MIT&quot;</span>
<span class="hljs-symbol">:author</span> <span class="hljs-string">&quot;A.B. &lt;a.b@example.org&gt;&quot;</span>
<span class="hljs-symbol">:depends-on</span> (<span class="hljs-symbol">:ironclad</span>)
<span class="hljs-symbol">:serial</span> <span class="hljs-literal">t</span>
<span class="hljs-symbol">:components</span> ((<span class="hljs-symbol">:file</span> <span class="hljs-string">&quot;packages&quot;</span>)
(<span class="hljs-symbol">:file</span> <span class="hljs-string">&quot;my-new-project&quot;</span>)))
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">
</code></pre>
<h3 id="extra-credit-exercise-11215">Extra Credit Exercise 1.12.15</h3>
<p><strong>ASDF Systems: Modules</strong></p>
<p><em>Modules</em> in ASDF system definitions correspond to subdirectories of your project folder. Your modules may correspond to their own packages---and this is a common design pattern---but they don&apos;t have to. This is fairly different from other programming languages, where project directory structure corresponds rigidly to namespacing. Lisp is more flexible---you can span your entire project with a single package namespace, you can give each and every file its own package, or anywhere in-between.</p>
<h4 id="in-the-file">In the File</h4>
<p>Revise <code>my-new-project.asd</code> to look like the following:</p>
<pre><code class="lang-lisp">(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:cl-user</span>)
(<span class="hljs-name">defpackage</span> my-new-project-asd
(<span class="hljs-symbol">:use</span> <span class="hljs-symbol">:cl</span> <span class="hljs-symbol">:cl-user</span> <span class="hljs-symbol">:asdf</span>))
(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:my-new-project-asd</span>)
(<span class="hljs-name">defsystem</span> my-new-project
<span class="hljs-symbol">:version</span> <span class="hljs-string">&quot;1.0.0&quot;</span>
<span class="hljs-symbol">:license</span> <span class="hljs-string">&quot;MIT&quot;</span>
<span class="hljs-symbol">:author</span> <span class="hljs-string">&quot;A.B. &lt;a.b@example.org&gt;&quot;</span>
<span class="hljs-symbol">:depends-on</span> (<span class="hljs-symbol">:ironclad</span>)
<span class="hljs-symbol">:serial</span> <span class="hljs-literal">t</span>
<span class="hljs-symbol">:components</span> ((<span class="hljs-symbol">:file</span> <span class="hljs-string">&quot;packages&quot;</span>)
(<span class="hljs-symbol">:module</span> <span class="hljs-string">&quot;src&quot;</span>
<span class="hljs-symbol">:serial</span> <span class="hljs-literal">t</span>
<span class="hljs-symbol">:components</span> ((<span class="hljs-symbol">:file</span> <span class="hljs-string">&quot;utils&quot;</span>)
(<span class="hljs-symbol">:file</span> <span class="hljs-string">&quot;api&quot;</span>)))
(<span class="hljs-symbol">:file</span> <span class="hljs-string">&quot;my-new-project&quot;</span>)))
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">
</code></pre>
<p>The important thing to remember about ASDF is that it has no bearing on your project&apos;s namespace and organization. It exists to collect metadata and compilation instructions---and it doesn&apos;t care about what packages you use, or how many. It only needs to know where to find your source-code to compile it, in the right order.</p>
<h3 id="extra-credit-exercise-11216">Extra Credit Exercise 1.12.16</h3>
<p><strong>ASDF Systems: Serial mode vs. Dependency Tree</strong></p>
<p>You may have noticed in the preceding exercises a casual inclusion of the keyword <code>:serial</code> in the <code>defsystem</code> forms, set to <code>t</code>. This is actually quite significant, as it forces each sequential file and module in the system definition to be compiled <em>in the order that they appear</em>.</p>
<p>You do not <em>have</em> to serialize the compilation, however; and in some cases, you will not want to. For such cases, you can explicitly define a dependency tree for each file and module, so that ASDF can work its magic in optimizing the compilation process.</p>
<h4 id="in-the-file">In the File</h4>
<pre><code class="lang-lisp">
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">
</code></pre>
<h3 id="extra-credit-exercise-11217">Extra Credit Exercise 1.12.17</h3>
<p><strong>The Quicklisp Package Manager</strong></p>
<p>Now that you have your new project configured for ASDF, you can automatically load it and all its dependencies into your current Lisp image through the Quicklisp package manager.</p>
<p>Quicklisp runs <em>inside</em> your Lisp image, not from the command-line.</p>
<h4 id="from-the-repl">From the REPL</h4>
<pre><code class="lang-lisp">(<span class="hljs-name">ql</span><span class="hljs-symbol">:quickload</span> <span class="hljs-symbol">:my-new-project</span>)
(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:my-new-project</span>)
</code></pre>
<h4 id="what-you-should-see">What You Should See</h4>
<pre><code class="lang-lisp">(ql:quickload :my-new-project)
To load &quot;my-new-project&quot;:
Load 1 ASDF system:
my-new-project
; Loading &quot;my-new-project&quot;
..................................................
[package base58]..................................
[package cl-base64]...............................
[package nibbles].................................
[package ironclad]................................
..................................................
..................................................
..................................................
[package my-new-project]..
(:MY-NEW-PROJECT)
(in-package :my-new-project)
#&lt;PACKAGE &quot;MY-NEW-PROJECT&quot;&gt;
</code></pre>
<p>It is considered best practice to not call Quicklisp inside your Lisp source-code, but to rely on ASDF and portable Common Lisp; for deploying executables, tools such as <code>BUILDAPP</code> will use Quicklisp automatically to retrieve dependencies and include them in your binary, without adding the overhead of the Quicklisp system to your binary. The Lisp API to Quicklisp, however, is very useful for testing, exploring, and interactively programming with Lisp libraries from the REPL. For example, you can now automatically load the library you&apos;ve created in this chapter into a new Lisp image, and start playing with it right away.</p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="1-11-0-text-adventure.html" class="navigation navigation-prev " aria-label="Previous page: Extra Credit: A Simple Text Adventure">
<i class="fa fa-angle-left"></i>
</a>
<a href="1-13-0-simple-web-app.html" class="navigation navigation-next " aria-label="Next page: Extra Credit: A Simple Web Application">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Namespaces, Symbols, Packages, and Systems","level":"3.1.12","depth":2,"next":{"title":"Extra Credit: A Simple Web Application","level":"3.1.13","depth":2,"path":"1-13-0-simple-web-app.md","ref":"./1-13-0-simple-web-app.md","articles":[]},"previous":{"title":"Extra Credit: A Simple Text Adventure","level":"3.1.11","depth":2,"path":"1-11-0-text-adventure.md","ref":"./1-11-0-text-adventure.md","articles":[]},"dir":"ltr"},"config":{"plugins":["hints","folding-chapters"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"hints":{"danger":"fa fa-exclamation-circle","info":"fa fa-info-circle","tip":"fa fa-mortar-board","working":"fa fa-wrench"},"folding-chapters":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"\"the Phoeron\" Colin J.E. Lupton","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"index.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Learn Lisp The Hard Way","gitbook":"*"},"file":{"path":"1-12-0-namespaces.md","mtime":"2022-08-22T22:47:49.767Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-08-24T16:15:01.957Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-folding-chapters/folding-chapters.js"></script>
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>