-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquote-and-eval.html
163 lines (163 loc) · 11.5 KB
/
quote-and-eval.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>quote-and-eval</title>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
</style>
<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(title);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
background-color: #2a211c;
color: #bdae9d;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #bdae9d; padding-left: 4px; }
div.sourceCode
{ color: #bdae9d; background-color: #2a211c; }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ffff00; } /* Alert */
code span.an { color: #0066ff; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { } /* Attribute */
code span.bn { color: #44aa43; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #43a8ed; font-weight: bold; } /* ControlFlow */
code span.ch { color: #049b0a; } /* Char */
code span.cn { } /* Constant */
code span.co { color: #0066ff; font-weight: bold; font-style: italic; } /* Comment */
code span.do { color: #0066ff; font-style: italic; } /* Documentation */
code span.dt { text-decoration: underline; } /* DataType */
code span.dv { color: #44aa43; } /* DecVal */
code span.er { color: #ffff00; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #44aa43; } /* Float */
code span.fu { color: #ff9358; font-weight: bold; } /* Function */
code span.im { } /* Import */
code span.in { color: #0066ff; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #43a8ed; font-weight: bold; } /* Keyword */
code span.op { } /* Operator */
code span.pp { font-weight: bold; } /* Preprocessor */
code span.sc { color: #049b0a; } /* SpecialChar */
code span.ss { color: #049b0a; } /* SpecialString */
code span.st { color: #049b0a; } /* String */
code span.va { } /* Variable */
code span.vs { color: #049b0a; } /* VerbatimString */
code span.wa { color: #ffff00; font-weight: bold; } /* Warning */
</style>
<link rel="stylesheet" href="tufte.css" />
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<h1 id="quote-and-eval">Quote and Eval</h1>
<div class="sourceCode" id="cb1"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb1-1" title="1">(<span class="kw">quote</span> (<span class="op">+</span> <span class="dv">1</span> <span class="dv">2</span>))</a>
<a class="sourceLine" id="cb1-2" title="2"><span class="co">;; => (+ 1 2)</span></a>
<a class="sourceLine" id="cb1-3" title="3"></a>
<a class="sourceLine" id="cb1-4" title="4">(<span class="kw">quote</span> <span class="kw">nil</span>)</a>
<a class="sourceLine" id="cb1-5" title="5"><span class="co">;; => NIL</span></a>
<a class="sourceLine" id="cb1-6" title="6"></a>
<a class="sourceLine" id="cb1-7" title="7">(<span class="kw">quote</span> blah)</a>
<a class="sourceLine" id="cb1-8" title="8"><span class="co">;; => BLAH</span></a>
<a class="sourceLine" id="cb1-9" title="9"></a>
<a class="sourceLine" id="cb1-10" title="10">(<span class="kw">eval</span> (<span class="kw">quote</span> (<span class="op">+</span> <span class="dv">1</span> <span class="dv">2</span>)))</a>
<a class="sourceLine" id="cb1-11" title="11"><span class="co">;; => 3</span></a>
<a class="sourceLine" id="cb1-12" title="12"></a>
<a class="sourceLine" id="cb1-13" title="13">(<span class="kw">eval</span> (<span class="kw">quote</span> blah))</a>
<a class="sourceLine" id="cb1-14" title="14"><span class="co">;; ERROR!</span></a>
<a class="sourceLine" id="cb1-15" title="15"></a>
<a class="sourceLine" id="cb1-16" title="16">(<span class="kw">eval</span> <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb1-17" title="17"><span class="co">;; => 1</span></a>
<a class="sourceLine" id="cb1-18" title="18"></a>
<a class="sourceLine" id="cb1-19" title="19">(<span class="kw">eval</span> <span class="kw">nil</span>)</a>
<a class="sourceLine" id="cb1-20" title="20"><span class="co">;; => NIL</span></a>
<a class="sourceLine" id="cb1-21" title="21"></a>
<a class="sourceLine" id="cb1-22" title="22">(<span class="kw">eval</span> T)</a>
<a class="sourceLine" id="cb1-23" title="23"><span class="co">;; => T</span></a>
<a class="sourceLine" id="cb1-24" title="24"></a>
<a class="sourceLine" id="cb1-25" title="25">(<span class="kw">eval</span> :blah)</a>
<a class="sourceLine" id="cb1-26" title="26"><span class="co">;; => :BLAH</span></a>
<a class="sourceLine" id="cb1-27" title="27"></a>
<a class="sourceLine" id="cb1-28" title="28">(<span class="kw">eval</span> (<span class="kw">quote</span> T))</a>
<a class="sourceLine" id="cb1-29" title="29"><span class="co">;; => T</span></a></code></pre></div>
<p><code>quote</code> returns an expression as data. It prevents evaluation. <code>eval</code> allows us to evaluate such data as an expression.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb2-1" title="1">(<span class="kw">list</span> <span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>)</a>
<a class="sourceLine" id="cb2-2" title="2"><span class="co">;; => (1 2 3)</span></a>
<a class="sourceLine" id="cb2-3" title="3"></a>
<a class="sourceLine" id="cb2-4" title="4">(<span class="kw">quote</span> (<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>))</a>
<a class="sourceLine" id="cb2-5" title="5"><span class="co">;; => (1 2 3)</span></a>
<a class="sourceLine" id="cb2-6" title="6"></a>
<a class="sourceLine" id="cb2-7" title="7">'(<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>)</a>
<a class="sourceLine" id="cb2-8" title="8"><span class="co">;; => (1 2 3)</span></a></code></pre></div>
<p><code>quote</code> provides an alternative way to create a list. We can also use <code>'</code> character to quote. This is an idiomatic way to create lists.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb3-1" title="1">(<span class="kw">setf</span> my-list (<span class="kw">quote</span> (<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>)))</a>
<a class="sourceLine" id="cb3-2" title="2"><span class="co">;; => (1 2 3)</span></a>
<a class="sourceLine" id="cb3-3" title="3"></a>
<a class="sourceLine" id="cb3-4" title="4">(<span class="kw">car</span> my-list)</a>
<a class="sourceLine" id="cb3-5" title="5"><span class="co">;; => 1</span></a></code></pre></div>
<p>It’s just a normal list!</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb4-1" title="1">(<span class="kw">quote</span> foo)</a>
<a class="sourceLine" id="cb4-2" title="2"><span class="co">;; => FOO</span></a>
<a class="sourceLine" id="cb4-3" title="3"></a>
<a class="sourceLine" id="cb4-4" title="4">'foo</a>
<a class="sourceLine" id="cb4-5" title="5"><span class="co">;; => FOO</span></a>
<a class="sourceLine" id="cb4-6" title="6"></a>
<a class="sourceLine" id="cb4-7" title="7">'(foo bar)</a>
<a class="sourceLine" id="cb4-8" title="8"><span class="co">;; => (FOO BAR)</span></a>
<a class="sourceLine" id="cb4-9" title="9"></a>
<a class="sourceLine" id="cb4-10" title="10">'((<span class="kw">quote</span> foo) (<span class="kw">quote</span> bar))</a>
<a class="sourceLine" id="cb4-11" title="11"><span class="co">;; => ('FOO 'BAR)</span></a></code></pre></div>
<p>As an alternative to using strings it’s common to use quoted symbols.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb5-1" title="1">(<span class="kw">list</span> (<span class="kw">list</span> <span class="dv">1</span>) (<span class="kw">list</span> <span class="dv">2</span>) (<span class="kw">list</span> <span class="dv">3</span>))</a>
<a class="sourceLine" id="cb5-2" title="2"><span class="co">;; => ((1) (2) (3))</span></a>
<a class="sourceLine" id="cb5-3" title="3">'((<span class="dv">1</span>) (<span class="dv">2</span>) (<span class="dv">3</span>))</a>
<a class="sourceLine" id="cb5-4" title="4"><span class="co">;; => ((1) (2) (3))</span></a></code></pre></div>
<ul>
<li>quoted lists make it easier to read (and write) nested lists</li>
</ul>
<div class="sourceCode" id="cb6"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb6-1" title="1">(<span class="kw">list</span> (<span class="kw">list</span> (<span class="op">+</span> <span class="dv">1</span> <span class="dv">1</span>)) (<span class="kw">list</span> (<span class="op">+</span> <span class="dv">2</span> <span class="dv">2</span>)) (<span class="kw">list</span> (<span class="op">+</span> <span class="dv">3</span> <span class="dv">3</span>)))</a>
<a class="sourceLine" id="cb6-2" title="2"><span class="co">;; => ((2) (4) (6))</span></a>
<a class="sourceLine" id="cb6-3" title="3"></a>
<a class="sourceLine" id="cb6-4" title="4">'(((<span class="op">+</span> <span class="dv">1</span> <span class="dv">1</span>)) ((<span class="op">+</span> <span class="dv">2</span> <span class="dv">2</span>)) ((<span class="op">+</span> <span class="dv">3</span> <span class="dv">3</span>)))</a>
<a class="sourceLine" id="cb6-5" title="5"><span class="co">;; => (((+ 1 1)) ((+ 2 2)) ((+ 3 3)))</span></a></code></pre></div>
<ul>
<li>BUT nothing is evaluated inside a quoted list - it’s all just data!</li>
</ul>
<div class="sourceCode" id="cb7"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb7-1" title="1">`((,(<span class="op">+</span> <span class="dv">1</span> <span class="dv">1</span>)) (,(<span class="op">+</span> <span class="dv">2</span> <span class="dv">2</span>)) (,(<span class="op">+</span> <span class="dv">3</span> <span class="dv">3</span>)))</a>
<a class="sourceLine" id="cb7-2" title="2"><span class="co">;; => ((2) (4) (6))</span></a></code></pre></div>
<ul>
<li>BUT you can use a <em>quasiquoted</em> list using the `<code></code> - the <em>backtick</em> character.</li>
<li>Inside a quasiquoted list you can have evaluation toggled back on for a form by using the <code>,</code> character.</li>
<li>It’s QUASI!</li>
</ul>
</body>
</html>