You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<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>
9
9
<h3id="defining-a-function">Defining a function:</h3>
<p>Note that <code>DEFCLASS</code> accessor functions for slots can be <code>SETF</code>’d and serve as both getters and setters for the slot.</p>
20
23
<p><code>:INITARG</code> is the keyword used in <code>MAKE-INSTANCE</code> to denote the value of the initial argument (see below).</p>
21
24
<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>""</code>, or <code>(error "You must set slot <slotname> to a value")</code> as the usual initform set.</p>
22
25
<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>
<spanclass="co">;; implicit progn where variable[12] are valid</span>
57
+
;; implicit progn where variable[12] are valid</span>
50
58
)</code></pre>
59
+
51
60
<h3id="loop">LOOP</h3>
52
61
<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>
<p>See <ahref="http://www.lispworks.com/documentation/HyperSpec/Body/s_unwind.htm">UNWIND-PROTECT</a> for details on this very useful form.</p>
88
97
<h3id="writing-a-text-file">Writing a text file</h3>
89
98
<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>
<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’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 “keywords”.</p>
16
16
<p>The second form in the code defines a global that describes what an URL looks like. Of course, it’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>
"Hacked up regex suitable for demo purposes.")</code></pre>
19
19
<p>Next, a predicate for HTTP. We really don’t care about FTP, HTTPS, GOPHER, GIT, etc protocols. It returns nil if it can’t find http:// in the URL.</p>
<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>
(FLEXI-STREAMS:EXTERNAL-FORMAT-ENCODING-ERROR (se) (format t "~a threw ~a~%" url se)))))</code></pre>
61
61
<p>The initial form is <code>WHEN</code> - a one-branch conditional. When we have a http link and it’s not a binary URL, then…</p>
62
62
<p><code>HANDLER-CASE</code> wraps some code. <code>HANDLER-CASE</code> is part of the Common Lisp condition system; it serves as the “try” block in this situation. The list of errors below form the “catch” blocks. The interested reader is referred to <ahref="../quicklinks.html">the quicklinks section</a> for more resources. Note that the errors are typical network errors- timeouts, stream errors, encoding errors.</p>
63
63
<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’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>
0 commit comments