Skip to content

Commit 373d2f1

Browse files
committed
fix code snippets for highlight-lisp
fixes #11
1 parent 56825f2 commit 373d2f1

File tree

5 files changed

+112
-103
lines changed

5 files changed

+112
-103
lines changed

examples/snippets.html

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,107 +7,116 @@ <h1 class="title">Snippets</h1>
77
</header>
88
<p>One of the great resources we can have as developers is snippets and example code to understand how the idiom of a language is spoken and written.</p>
99
<h3 id="defining-a-function">Defining a function:</h3>
10-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defun</span><span class="fu"> function-name </span>(param1 param2 &amp;key (keyword1 default-value))
11-
<span class="co">;; keywords are optional; optional positional parameters are also available. </span>
12-
<span class="co">;; implicit progn</span>
13-
)</code></pre>
10+
<pre class="sourceCode lisp"><code class="sourceCode lisp">(defun function-name (param1 param2 &amp;key (keyword1 default-value))
11+
;; keywords are optional; optional positional parameters are also available.
12+
;; implicit progn
13+
)
14+
</code></pre>
15+
1416
<h3 id="defining-a-method-on-a-type">Defining a method on a type:</h3>
15-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defmethod</span><span class="fu"> method-name </span>((param1 <span class="kw">class-name</span>))
16-
<span class="co">;; implicit progn</span>
17+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmethod method-name ((param1 class-name))
18+
;; implicit progn
1719
)</code></pre>
20+
1821
<h3 id="defining-a-class">Defining a class</h3>
1922
<p>Note that <code>DEFCLASS</code> accessor functions for slots can be <code>SETF</code>&#8217;d and serve as both getters and setters for the slot.</p>
2023
<p><code>:INITARG</code> is the keyword used in <code>MAKE-INSTANCE</code> to denote the value of the initial argument (see below).</p>
2124
<p><code>:INITFORM</code> is the form used to initialize the slot. Without this, it defaults to <code>nil</code>. I favor using <code>nil</code>, <code>0</code>, <code>&quot;&quot;</code>, or <code>(error &quot;You must set slot &lt;slotname&gt; to a value&quot;)</code> as the usual initform set.</p>
2225
<p>Note that <code>(:documentation ...)</code> is the standard documentation mechanism, which can be viewed in the running image with <code>DESCRIBE</code> (at least in SBCL).</p>
23-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
24-
<span class="co">;; no superclass, no slots.</span>
25-
(<span class="kw">defclass</span><span class="fu"> superclass-1 </span><span class="kw">nil</span> <span class="kw">nil</span>)
26+
<pre class="sourceCode commonlisp"><code class="sourceCode lisp">
27+
;; no superclass, no slots.
28+
(defclass superclass-1 nil nil)
2629

27-
(<span class="kw">defclass</span><span class="fu"> my-class </span>(superclass<span class="dv">-1</span>)
30+
(defclass my-class (superclass-1)
2831
((variable
2932
:accessor accessor-function
3033
:initarg :variable
3134
:initform form-for-initializition.)
3235
another-variable)
33-
(:documentation <span class="st">&quot;a class snippet!&quot;</span>))</code></pre>
36+
(:documentation &quot;a class snippet!&quot;))</code></pre>
37+
3438
<h3 id="creating-an-instance">Creating an instance</h3>
35-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">make-instance</span> &#39;my-class :variable value)</code></pre>
39+
<pre class="sourceCode commonlisp"><code class="sourceCode lisp">(make-instance &#39;my-class :variable value)</code></pre>
40+
3641
<h3 id="adding-a-constructor">Adding a constructor</h3>
3742
<p>This function runs after the instance is initialized.</p>
38-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defmethod</span><span class="fu"> initialize-instance </span>:after ((obj my-class) &amp;key)
39-
(<span class="kw">setf</span> (accessor-function obj) <span class="dv">10</span>))</code></pre>
43+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmethod initialize-instance :after ((obj my-class) &amp;key)
44+
(setf (accessor-function obj) 10))</code></pre>
45+
4046
<h3 id="defining-a-constant">Defining a constant:</h3>
4147
<p>Note that the convention is that constants are surrounded with +</p>
42-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defconstant</span><span class="fu"> +name+ </span>value <span class="st">&quot;docstring&quot;</span>)</code></pre>
48+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defconstant +name+ value &quot;docstring&quot;)</code></pre>
49+
4350
<h3 id="defining-a-global-variable">Defining a global variable:</h3>
4451
<p>Note that the convention is that globals are surrounded with *</p>
45-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defparameter</span><span class="fu"> *name* </span>value <span class="st">&quot;docstring&quot;</span>)</code></pre>
52+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defparameter *name* value "docstring")</code></pre>
53+
4654
<h3 id="creating-local-variables.">Creating local variables.</h3>
47-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">let</span> ((variable1 value-form)
55+
<pre class="sourceCode lisp"><code class="sourceCode lisp">(let ((variable1 value-form)
4856
(variable2 value-again))
49-
<span class="co">;; implicit progn where variable[12] are valid</span>
57+
;; implicit progn where variable[12] are valid</span>
5058
)</code></pre>
59+
5160
<h3 id="loop">LOOP</h3>
5261
<p>LOOP is a contentious form in Common Lisp: some people love its imperative style, others hate it. Regardless, its really handy! Here are my favorite LOOPs</p>
5362
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
54-
(<span class="kw">loop</span> for i from <span class="dv">0</span> upto <span class="dv">10</span>
63+
(loop for i from 0 upto 10
5564
collect i)
5665

57-
(<span class="kw">loop</span> for i from <span class="dv">0</span> below <span class="dv">10</span>
66+
(loop for i from 0 below 10
5867
collect i)
5968

60-
(<span class="kw">loop</span> for i from <span class="dv">0</span> upto <span class="dv">10</span>
61-
<span class="kw">do</span>
69+
(loop for i from 0 upto 10
70+
do
6271
(side-effect i))
6372

64-
(<span class="kw">loop</span> for ele in <span class="kw">list</span>
73+
(loop for ele in list
6574
collect
6675
(operate-on ele))
6776

68-
(<span class="kw">loop</span> for ele across <span class="kw">array</span>
77+
(loop for ele across array
6978
collect
7079
(operate-on ele))</code></pre>
7180
<h3 id="lambda-functions">Lambda functions</h3>
7281
<p>The lambda functions is an anonymous function, i.e., unnamed.</p>
7382
<p>Here we map over <code>numeric-list</code> and increment each element in it by 1 with <code>1+</code>, returning the incremented list.</p>
7483
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
75-
(<span class="kw">mapcar</span>
76-
#&#39;(<span class="kw">lambda</span> (x)
77-
(<span class="kw">1+</span> x))
84+
(mapcar
85+
#&#39;(lambda (x)
86+
(1+ x))
7887
numeric-list)</code></pre>
7988
<h3 id="macro">Macro</h3>
8089
<p>Note that Lisp macros should be used with care: they read source code and emit source code.</p>
81-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defmacro</span><span class="fu"> with-resource </span>((resource) &amp;body body)
90+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmacro with-resource ((resource) &amp;body body)
8291
(allocate-resource ,resource)
83-
(<span class="kw">unwind-protect</span>
84-
(<span class="kw">progn</span>
92+
(unwind-protect
93+
(progn
8594
,@body)
8695
(deallocate-resource ,resource)))</code></pre>
8796
<p>See <a href="http://www.lispworks.com/documentation/HyperSpec/Body/s_unwind.htm">UNWIND-PROTECT</a> for details on this very useful form.</p>
8897
<h3 id="writing-a-text-file">Writing a text file</h3>
8998
<p>More complete reading and writing of text files can be done by using the ALEXANDRIA library; these routines are great for starting to customize your own routine.</p>
90-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defun</span><span class="fu"> write-file </span>(filename data)
91-
(<span class="kw">with-open-file</span> (<span class="kw">stream</span>
99+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defun write-file (filename data)
100+
(with-open-file (stream
92101
filename
93-
<span class="kw">:direction</span> <span class="kw">:output</span>
94-
:if-exists <span class="kw">:supersede</span>
95-
:if-does-not-exist <span class="kw">:create</span>)
96-
(<span class="kw">write-sequence</span>
102+
:direction :output
103+
:if-exists :supersede
104+
:if-does-not-exist :create)
105+
(write-sequence
97106
data
98-
<span class="kw">stream</span>)))</code></pre>
107+
stream)))</code></pre>
99108
<h3 id="reading-a-text-file">Reading a text file</h3>
100109
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
101-
(<span class="kw">defun</span><span class="fu"> read-file </span>(filename)
102-
<span class="st">&quot;Reads `filename` as a sequence of unsigned 8-bit bytes, no</span>
103-
<span class="st">encoding&quot;</span>
104-
(<span class="kw">with-open-file</span> (fin filename
105-
<span class="kw">:direction</span> <span class="kw">:input</span>
106-
:if-does-not-exist <span class="kw">:error</span>)
107-
(<span class="kw">let</span> ((seq (<span class="kw">make-array</span> (<span class="kw">file-length</span> fin)
108-
:fill-pointer <span class="kw">t</span>)))
109-
(<span class="kw">setf</span> (<span class="kw">fill-pointer</span> seq)
110-
(<span class="kw">read-sequence</span> seq fin))
110+
(defun read-file (filename)
111+
&quot;Reads `filename` as a sequence of unsigned 8-bit bytes, no
112+
encoding&quot;
113+
(with-open-file (fin filename
114+
:direction :input
115+
:if-does-not-exist :error)
116+
(let ((seq (make-array (file-length fin)
117+
:fill-pointer t)))
118+
(setf (fill-pointer seq)
119+
(read-sequence seq fin))
111120
seq)))</code></pre>
112121
<p>Please feel free to contribute your examples or library information to this page! Send in those pull requests and file those bugs!</p>
113122
<hr/>

examples/trotter.html

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,50 @@ <h1 class="title">Trotter Walkthrough</h1>
1414
:babel))</code></pre>
1515
<p><code>DRAKMA</code> is the standard HTTP(S) client library in Lisp, <code>SPLIT-SEQUENCE</code> does what you might think, <code>CL-PPCRE</code> is a reimplementation of the PPCRE library in native Lisp (It&#8217;s considered one of the best modern CL libraries, go check the source out sometime). <code>BABEL</code> is a encoding/decoding tool for strings. Note that they are referred to here as &#8220;keywords&#8221;.</p>
1616
<p>The second form in the code defines a global that describes what an URL looks like. Of course, it&#8217;s not very precise, but it suffices for our purpose. The key thing here is that <code>\</code> are doubled for the special forms. This is part of the CL-PPCRE regex definition.</p>
17-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defparameter</span><span class="fu"> *url-regex* </span><span class="st">&quot;(</span>\\<span class="st">w+://[-</span>\\<span class="st">w</span>\\<span class="st">./_?=%&amp;]+)&quot;</span>
18-
<span class="st">&quot;Hacked up regex suitable for demo purposes.&quot;</span>)</code></pre>
17+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defparameter *url-regex* &quot;(\\w+://[-\\w\\./_?=%&amp;]+)&quot;
18+
&quot;Hacked up regex suitable for demo purposes.&quot;)</code></pre>
1919
<p>Next, a predicate for HTTP. We really don&#8217;t care about FTP, HTTPS, GOPHER, GIT, etc protocols. It returns nil if it can&#8217;t find http:// in the URL.</p>
20-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defun</span><span class="fu"> http-p </span>(url)
21-
(cl-ppcre:all-matches <span class="st">&quot;</span>\\<span class="st">bhttp://&quot;</span> url))</code></pre>
20+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defun http-p (url)
21+
(cl-ppcre:all-matches &quot;\\bhttp://&quot; url))</code></pre>
2222
<p>Next, a predicate to check to see if our data is ASCII. While Common Lisp can handle Unicode data, this is a sample program, and the complexities of Unicode can be deferred.</p>
23-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defun</span><span class="fu"> ascii-p </span>(data)
24-
<span class="st">&quot;A not terribly great way to determine if the data is ASCII&quot;</span>
25-
(<span class="kw">unless</span> (<span class="kw">stringp</span> data)
26-
(<span class="kw">loop</span> for elem across data <span class="kw">do</span>
27-
(<span class="kw">when</span> (<span class="kw">&gt;</span> elem <span class="dv">127</span>)
28-
(<span class="kw">return-from</span> ascii-p <span class="kw">nil</span>))))
29-
<span class="kw">t</span>)</code></pre>
23+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defun ascii-p (data)
24+
&quot;A not terribly great way to determine if the data is ASCII&quot;
25+
(unless (stringp data)
26+
(loop for elem across data do
27+
(when (&gt; elem 127)
28+
(return-from ascii-p nil))))
29+
t)</code></pre>
3030
<p>Next, we have a quick (and dirty) way to see if a URL is going to point to a binary. It just looks for a binary file extension in the string.</p>
31-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defun</span><span class="fu"> known-binary-p </span>(url)
32-
<span class="st">&quot;Is this url a binary file?&quot;</span>
33-
(<span class="kw">let</span> ((binaries
34-
&#39;(<span class="st">&quot;.png&quot;</span> <span class="st">&quot;.bmp&quot;</span> <span class="st">&quot;.jpg&quot;</span> <span class="st">&quot;.exe&quot;</span> <span class="st">&quot;.dmg&quot;</span> <span class="st">&quot;.package&quot;</span> <span class="st">&quot;.css&quot;</span>
35-
<span class="st">&quot;.ico&quot;</span> <span class="st">&quot;.gif&quot;</span> <span class="st">&quot;.dtd&quot;</span> <span class="st">&quot;.pdf&quot;</span> <span class="st">&quot;.xml&quot;</span> <span class="st">&quot;.tgz&quot;</span>)))
36-
(<span class="kw">dolist</span> (b binaries NIL)
37-
(<span class="kw">when</span> (<span class="kw">search</span> b url)
38-
(<span class="kw">return</span> T)))))</code></pre>
31+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defun known-binary-p (url)
32+
&quot;Is this url a binary file?&quot;
33+
(let ((binaries
34+
&#39;(&quot;.png&quot; &quot;.bmp&quot; &quot;.jpg&quot; &quot;.exe&quot; &quot;.dmg&quot; &quot;.package&quot; &quot;.css&quot;
35+
&quot;.ico&quot; &quot;.gif&quot; &quot;.dtd&quot; &quot;.pdf&quot; &quot;.xml&quot; &quot;.tgz&quot;)))
36+
(dolist (b binaries NIL)
37+
(when (search b url)
38+
(return T)))))</code></pre>
3939
<p>The next form is the most complex: <code>find-links</code>.</p>
4040
<p><code>find-links</code> attempts to find all the links in a given url.</p>
4141
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
42-
(<span class="kw">defun</span><span class="fu"> find-links </span>(url)
43-
<span class="st">&quot;Scrapes links from a URL. Prints to STDOUT if an error is caught&quot;</span>
44-
(<span class="kw">when</span> (<span class="kw">and</span> (http-p url)
45-
(<span class="kw">not</span> (known-binary-p url)))
46-
(<span class="kw">handler-case</span>
47-
(<span class="kw">let</span> ((page (drakma:http-request url)))
48-
(<span class="kw">when</span> page
49-
(<span class="kw">when</span> (ascii-p page)
42+
(defun find-links (url)
43+
&quot;Scrapes links from a URL. Prints to STDOUT if an error is caught&quot;
44+
(when (and (http-p url)
45+
(not (known-binary-p url)))
46+
(handler-case
47+
(let ((page (drakma:http-request url)))
48+
(when page
49+
(when (ascii-p page)
5050
(cl-ppcre:all-matches-as-strings
5151
*url-regex*
52-
(<span class="kw">if</span> (<span class="kw">stringp</span> page)
52+
(if (stringp page)
5353
page
5454
(babel:octets-to-string page))))))
5555

56-
#+sbcl(sb-int:simple-stream-error (se) (<span class="kw">format</span> <span class="kw">t</span> <span class="st">&quot;Whoops, ~a didn&#39;t work. ~a~%&quot;</span> url se))
57-
(DRAKMA::DRAKMA-SIMPLE-ERROR (se) (<span class="kw">format</span> <span class="kw">t</span> <span class="st">&quot;Error? ~a threw ~a~%&quot;</span> url se))
58-
(USOCKET:TIMEOUT-ERROR (se) (<span class="kw">format</span> <span class="kw">t</span> <span class="st">&quot;timeout error ~a threw ~a~%&quot;</span> url se))
59-
(USOCKET:NS-HOST-NOT-FOUND-ERROR (se) (<span class="kw">format</span> <span class="kw">t</span> <span class="st">&quot;host-not-found error ~a threw ~a~%&quot;</span> url se))
60-
(FLEXI-STREAMS:EXTERNAL-FORMAT-ENCODING-ERROR (se) (<span class="kw">format</span> <span class="kw">t</span> <span class="st">&quot;~a threw ~a~%&quot;</span> url se)))))</code></pre>
56+
#+sbcl(sb-int:simple-stream-error (se) (format t &quot;Whoops, ~a didn&#39;t work. ~a~%&quot; url se))
57+
(DRAKMA::DRAKMA-SIMPLE-ERROR (se) (format t &quot;Error? ~a threw ~a~%&quot; url se))
58+
(USOCKET:TIMEOUT-ERROR (se) (format t &quot;timeout error ~a threw ~a~%&quot; url se))
59+
(USOCKET:NS-HOST-NOT-FOUND-ERROR (se) (format t &quot;host-not-found error ~a threw ~a~%&quot; url se))
60+
(FLEXI-STREAMS:EXTERNAL-FORMAT-ENCODING-ERROR (se) (format t &quot;~a threw ~a~%&quot; url se)))))</code></pre>
6161
<p>The initial form is <code>WHEN</code> - a one-branch conditional. When we have a http link and it&#8217;s not a binary URL, then&#8230;</p>
6262
<p><code>HANDLER-CASE</code> wraps some code. <code>HANDLER-CASE</code> is part of the Common Lisp condition system; it serves as the &#8220;try&#8221; block in this situation. The list of errors below form the &#8220;catch&#8221; blocks. The interested reader is referred to <a href="../quicklinks.html">the quicklinks section</a> for more resources. Note that the errors are typical network errors- timeouts, stream errors, encoding errors.</p>
6363
<p>The first form in <code>HANDLER-CASE</code> requests in the url and assigns it to page. Supposing we got something, we make sure it&#8217;s an ascii page; if so, we then find all the url-ish looking things, using the previously defined global. Supposing that the page is, indeed, a string, we return it, otherwise we convert the octets to a string and return that. N.b.: Common Lisp makes a difference between strings and vectors of bytes. Of course, if an error occurred, the <code>HANDLER-CASE</code> will route to the known conditions.</p>

implementations/ccl-setup.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ <h2 id="installing">Installing</h2>
2020
<h2 id="linedit">Linedit</h2>
2121
<p>A CLI tool called linedit is useful.</p>
2222
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(ql:quickload :linedit)
23-
(<span class="kw">require</span> :linedit)
24-
(<span class="kw">funcall</span> (<span class="kw">intern</span> <span class="st">&quot;INSTALL-REPL&quot;</span> :linedit) :wrap-current <span class="kw">t</span>)</code></pre>
23+
(require :linedit)
24+
(funcall (intern &quot;INSTALL-REPL&quot; :linedit) :wrap-current t)</code></pre>
2525
<h2 id="notes">Notes</h2>
2626
<p>CCL, in general, is very fast to load and has a good reputation for performance.</p>
2727
<hr/>

0 commit comments

Comments
 (0)