Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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">(<spanclass="kw">defmethod</span><spanclass="fu">method-name </span>((param1 <spanclass="kw">class-name</span>))
16-
<spanclass="co">;; implicit progn</span>
17+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmethodmethod-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-
<spanclass="co">;; no superclass, no slots.</span>
25-
(<spanclass="kw">defclass</span><spanclass="fu">superclass-1 </span><spanclass="kw">nil</span><spanclass="kw">nil</span>)
26+
<pre class="sourceCode commonlisp"><code class="sourceCode lisp">
27+
;; no superclass, no slots.
28+
(defclasssuperclass-1 nilnil)
2629

27-
(<spanclass="kw">defclass</span><spanclass="fu">my-class </span>(superclass<spanclass="dv">-1</span>)
30+
(defclassmy-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">(<spanclass="kw">let</span> ((variable1 value-form)
55+
<pre class="sourceCode lisp"><code class="sourceCode lisp">(let ((variable1 value-form)
4856
(variable2 value-again))
49-
<spanclass="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-
(<spanclass="kw">loop</span> for i from <spanclass="dv">0</span> upto <spanclass="dv">10</span>
63+
(loop for i from 0 upto 10
5564
collect i)
5665

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

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

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

68-
(<spanclass="kw">loop</span> for ele across <spanclass="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-
(<spanclass="kw">mapcar</span>
76-
#&#39;(<spanclass="kw">lambda</span> (x)
77-
(<spanclass="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">(<spanclass="kw">defmacro</span><spanclass="fu">with-resource </span>((resource) &amp;body body)
90+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmacrowith-resource ((resource) &amp;body body)
8291
(allocate-resource ,resource)
83-
(<spanclass="kw">unwind-protect</span>
84-
(<spanclass="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">(<spanclass="kw">defun</span><spanclass="fu">write-file </span>(filename data)
91-
(<spanclass="kw">with-open-file</span> (<spanclass="kw">stream</span>
99+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defunwrite-file (filename data)
100+
(with-open-file (stream
92101
filename
93-
<spanclass="kw">:direction</span><spanclass="kw">:output</span>
94-
:if-exists <spanclass="kw">:supersede</span>
95-
:if-does-not-exist <spanclass="kw">:create</span>)
96-
(<spanclass="kw">write-sequence</span>
102+
:direction:output
103+
:if-exists :supersede
104+
:if-does-not-exist :create)
105+
(write-sequence
97106
data
98-
<spanclass="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-
(<spanclass="kw">defun</span><spanclass="fu">read-file </span>(filename)
102-
<spanclass="st">&quot;Reads `filename` as a sequence of unsigned 8-bit bytes, no</span>
103-
<spanclass="st">encoding&quot;</span>
104-
(<spanclass="kw">with-open-file</span> (fin filename
105-
<spanclass="kw">:direction</span><spanclass="kw">:input</span>
106-
:if-does-not-exist <spanclass="kw">:error</span>)
107-
(<spanclass="kw">let</span> ((seq (<spanclass="kw">make-array</span> (<spanclass="kw">file-length</span> fin)
108-
:fill-pointer <spanclass="kw">t</span>)))
109-
(<spanclass="kw">setf</span> (<spanclass="kw">fill-pointer</span> seq)
110-
(<spanclass="kw">read-sequence</span> seq fin))
110+
(defunread-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">(<spanclass="kw">defparameter</span><spanclass="fu">*url-regex* </span><spanclass="st">&quot;(</span>\\<spanclass="st">w+://[-</span>\\<spanclass="st">w</span>\\<spanclass="st">./_?=%&amp;]+)&quot;</span>
18-
<spanclass="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">(<spanclass="kw">defun</span><spanclass="fu">http-p </span>(url)
21-
(cl-ppcre:all-matches <spanclass="st">&quot;</span>\\<spanclass="st">bhttp://&quot;</span> url))</code></pre>
20+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defunhttp-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">(<spanclass="kw">defun</span><spanclass="fu">ascii-p </span>(data)
24-
<spanclass="st">&quot;A not terribly great way to determine if the data is ASCII&quot;</span>
25-
(<spanclass="kw">unless</span> (<spanclass="kw">stringp</span> data)
26-
(<spanclass="kw">loop</span> for elem across data <spanclass="kw">do</span>
27-
(<spanclass="kw">when</span> (<spanclass="kw">&gt;</span> elem <spanclass="dv">127</span>)
28-
(<spanclass="kw">return-from</span> ascii-p <spanclass="kw">nil</span>))))
29-
<spanclass="kw">t</span>)</code></pre>
23+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defunascii-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">(<spanclass="kw">defun</span><spanclass="fu">known-binary-p </span>(url)
32-
<spanclass="st">&quot;Is this url a binary file?&quot;</span>
33-
(<spanclass="kw">let</span> ((binaries
34-
&#39;(<spanclass="st">&quot;.png&quot;</span><spanclass="st">&quot;.bmp&quot;</span><spanclass="st">&quot;.jpg&quot;</span><spanclass="st">&quot;.exe&quot;</span><spanclass="st">&quot;.dmg&quot;</span><spanclass="st">&quot;.package&quot;</span><spanclass="st">&quot;.css&quot;</span>
35-
<spanclass="st">&quot;.ico&quot;</span><spanclass="st">&quot;.gif&quot;</span><spanclass="st">&quot;.dtd&quot;</span><spanclass="st">&quot;.pdf&quot;</span><spanclass="st">&quot;.xml&quot;</span><spanclass="st">&quot;.tgz&quot;</span>)))
36-
(<spanclass="kw">dolist</span> (b binaries NIL)
37-
(<spanclass="kw">when</span> (<spanclass="kw">search</span> b url)
38-
(<spanclass="kw">return</span> T)))))</code></pre>
31+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defunknown-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-
(<spanclass="kw">defun</span><spanclass="fu">find-links </span>(url)
43-
<spanclass="st">&quot;Scrapes links from a URL. Prints to STDOUT if an error is caught&quot;</span>
44-
(<spanclass="kw">when</span> (<spanclass="kw">and</span> (http-p url)
45-
(<spanclass="kw">not</span> (known-binary-p url)))
46-
(<spanclass="kw">handler-case</span>
47-
(<spanclass="kw">let</span> ((page (drakma:http-request url)))
48-
(<spanclass="kw">when</span> page
49-
(<spanclass="kw">when</span> (ascii-p page)
42+
(defunfind-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-
(<spanclass="kw">if</span> (<spanclass="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) (<spanclass="kw">format</span><spanclass="kw">t</span><spanclass="st">&quot;Whoops, ~a didn&#39;t work. ~a~%&quot;</span> url se))
57-
(DRAKMA::DRAKMA-SIMPLE-ERROR (se) (<spanclass="kw">format</span><spanclass="kw">t</span><spanclass="st">&quot;Error? ~a threw ~a~%&quot;</span> url se))
58-
(USOCKET:TIMEOUT-ERROR (se) (<spanclass="kw">format</span><spanclass="kw">t</span><spanclass="st">&quot;timeout error ~a threw ~a~%&quot;</span> url se))
59-
(USOCKET:NS-HOST-NOT-FOUND-ERROR (se) (<spanclass="kw">format</span><spanclass="kw">t</span><spanclass="st">&quot;host-not-found error ~a threw ~a~%&quot;</span> url se))
60-
(FLEXI-STREAMS:EXTERNAL-FORMAT-ENCODING-ERROR (se) (<spanclass="kw">format</span><spanclass="kw">t</span><spanclass="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-
(<spanclass="kw">require</span> :linedit)
24-
(<spanclass="kw">funcall</span> (<spanclass="kw">intern</span><spanclass="st">&quot;INSTALL-REPL&quot;</span> :linedit) :wrap-current <spanclass="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)

AltStyle によって変換されたページ (->オリジナル) /