<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Common Lisp tips, by Zach Beane.

Got a tip to share? Email it to lisptips@xach.com.</description><title>Common Lisp Tips</title><generator>Tumblr (3.0; @lisptips)</generator><link>http://lisptips.com/</link><item><title>Formatting integers in different radixes</title><description>&lt;p&gt;&lt;a href="http://l1sp.org/cl/format" target="_blank"&gt;format&lt;/a&gt; has four directives for formatting integers with different radixes:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href="http://l1sp.org/cl/~b" target="_blank"&gt;~B&lt;/a&gt; formats as binary: &lt;code&gt;(format nil "~B" 42) =&amp;gt; "101010"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://l1sp.org/cl/~o" target="_blank"&gt;~O&lt;/a&gt; formats as octal: &lt;code&gt;(format nil "~O" 42) =&amp;gt; "52"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://l1sp.org/cl/~x" target="_blank"&gt;~X&lt;/a&gt; formats as hexadecimal: &lt;code&gt;(format nil "~X" 666) =&amp;gt; "29A"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://l1sp.org/cl/~r" target="_blank"&gt;~R&lt;/a&gt; formats with an arbitrary radix between 2 and 36: &lt;code&gt;(format nil "~36R" 18321) =&amp;gt; "E4X"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Each directive takes padding and pad-character options (as well as other options). I define these functions in my init files to quickly dump out hex and bit views of integers:&lt;/p&gt;
&lt;pre&gt;(defun :hex (value &amp;amp;optional (size 4))
  (format t "~v,'0X" size value))

(defun :bits (value &amp;amp;optional (size 8))
  (format t "~v,'0B" size value))
  
* &lt;strong&gt;(:bits 42)&lt;/strong&gt;
00101010

* &lt;strong&gt;(:hex 666)&lt;/strong&gt;
029A
&lt;/pre&gt;
&lt;p&gt;The lower-level &lt;a href="http://l1sp.org/cl/write" target="_blank"&gt;write&lt;/a&gt; function accepts a :base argument that specifies a radix to use when printing integers. The default value is taken from &lt;a href="http://l1sp.org/cl/*print-base*" target="_blank"&gt;*print-base*&lt;/a&gt;. For example, &lt;code&gt;(write-to-string 65535 :base 16) =&amp;gt; "FFFF"&lt;/code&gt;. Since write underlies how other standard functions produce output, binding *print-base* will affect pretty much everything.&lt;/p&gt;</description><link>http://lisptips.com/post/44509805155</link><guid>http://lisptips.com/post/44509805155</guid><pubDate>Sun, 03 Mar 2013 21:26:00 -0500</pubDate></item><item><title>Literal syntax for integers</title><description>&lt;p&gt;There are several ways to write literal integers with different radixes in Common Lisp. #b&amp;#8230; is for binary, #o&amp;#8230; is for octal, #x&amp;#8230; is for hexadecimal, and #r is for an arbitrary radix from 2 to 36. Section &lt;a href="http://l1sp.org/cl/2.4.8.10" target="_blank"&gt;2.4.8.10&lt;/a&gt; has this example:&lt;/p&gt;
&lt;pre&gt;#2r11010101  ;Another way of writing 213 decimal  
#b11010101   ;Ditto                               
#b+11010101  ;Ditto                               
#o325        ;Ditto, in octal radix               
#xD5         ;Ditto, in hexadecimal radix         
#16r+D5      ;Ditto                               
#o-300       ;Decimal -192, written in base 8     
#3r-21010    ;Same thing in base 3                
#25R-7H      ;Same thing in base 25               
#xACCEDED    ;181202413, in hexadecimal radix&lt;/pre&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;&lt;/p&gt;</description><link>http://lisptips.com/post/44370032877</link><guid>http://lisptips.com/post/44370032877</guid><pubDate>Sat, 02 Mar 2013 09:30:30 -0500</pubDate></item><item><title>How do I convert an integer to a list of bits?</title><description>&lt;p&gt;Novices sometimes ask how to get a convert an integer to a list of bits, often for the purpose of iterating over the bits somehow. Common Lisp has a rich set of functions for directly accessing the bits of an integer, so constructing an intermediate list of bits is rarely necessary.&lt;/p&gt;
&lt;p&gt;Here are a few examples of accessing the bits of an integer.&lt;/p&gt;
&lt;p&gt;With &lt;a href="http://l1sp.org/cl/integer-length" target="_blank"&gt;integer-length&lt;/a&gt; and &lt;a href="http://l1sp.org/cl/logbitp" target="_blank"&gt;logbitp&lt;/a&gt; to test the bit at a particular index:&lt;/p&gt;
&lt;pre&gt;(defun list-of-bits (integer)
  (loop for index below (integer-length integer)
        collect (if (logbitp index integer) 1 0)))
&lt;/pre&gt;
&lt;p&gt;With &lt;a href="http://l1sp.org/cl/ash" target="_blank"&gt;ash&lt;/a&gt; to shift the integer to the right and &lt;a href="http://l1sp.org/cl/logand" target="_blank"&gt;logand&lt;/a&gt; to test the least-significant bit:&lt;/p&gt;
&lt;pre&gt;(defun list-of-bits (integer)
  (loop repeat (integer-length integer)
        for i = integer then (ash i -1)
        collect (logand i 1)))
&lt;/pre&gt;
&lt;p&gt;With ash to shift a one-bit mask to the left and &lt;a href="http://l1sp.org/cl/logtest" target="_blank"&gt;logtest&lt;/a&gt; to test the mask bit against the integer:&lt;/p&gt;
&lt;pre&gt;(defun list-of-bits (integer)
  (loop repeat (integer-length integer)
        for mask = 1 then (ash mask 1)
        collect (if (logtest mask integer) 1 0)))
&lt;/pre&gt;
&lt;p&gt;With &lt;a href="http://l1sp.org/cl/byte" target="_blank"&gt;byte&lt;/a&gt; to construct a byte specifier and &lt;a href="http://l1sp.org/cl/ldb" target="_blank"&gt;ldb&lt;/a&gt; to extract a field from the integer:&lt;/p&gt;
&lt;pre&gt;(defun list-of-bits (integer)
  (loop for position below (integer-length integer)
        collect (ldb (byte 1 position) integer)))
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;update&lt;/strong&gt; Unfortunately, all these examples are buggy - they collect bits in the wrong order. I used loop/collect to avoid having to reverse the results, but didn&amp;#8217;t think it through properly. Here are some updated definitions with dotimes that give the results in the intended order:&lt;/p&gt;
&lt;pre&gt;(defun list-of-bits (integer)
  (let ((bits '()))
    (dotimes (index (integer-length integer) bits)
      (push (if (logbitp index integer) 1 0) bits))))
&lt;/pre&gt;
&lt;pre&gt;(defun list-of-bits (integer)
  (let ((i integer)
        (bits '()))
    (dotimes (j (integer-length integer) bits)
      (push (logand i 1) bits)
      (setf i (ash i -1)))))
&lt;/pre&gt;
&lt;pre&gt;(defun list-of-bits (integer)
  (let ((mask 1)
        (bits '()))
    (dotimes (i (integer-length integer) bits)
      (push (if (logtest mask integer) 1 0) bits)
      (setf mask (ash mask 1)))))
&lt;/pre&gt;
&lt;pre&gt;(defun list-of-bits (integer)
  (let ((bits '()))
    (dotimes (position (integer-length integer) bits)
      (push (ldb (byte 1 position) integer) bits))))
&lt;/pre&gt;</description><link>http://lisptips.com/post/44261316742</link><guid>http://lisptips.com/post/44261316742</guid><pubDate>Thu, 28 Feb 2013 20:54:00 -0500</pubDate></item><item><title>The optional arguments of deftype</title><description>&lt;p&gt;&lt;a href="http://l1sp.org/cl/deftype" target="_blank"&gt;deftype&lt;/a&gt; can be used to create user-defined types that expand into built-in types. For example:&lt;/p&gt;
&lt;pre&gt;(deftype octet-vector (length)
  `(simple-array (unsigned-byte 8) (,length)))
&lt;/pre&gt;
&lt;p&gt;Given this deftype, in a type context, &lt;code&gt;(octet-vector 32)&lt;/code&gt; expands into &lt;code&gt;(simple-array (unsigned-byte 8) (32))&lt;/code&gt;, or a one-dimensional octet array of length 32.&lt;/p&gt;
&lt;p&gt;Lambda lists for deftype are very similar to lambda lists for defmacro, with an important difference: where optional arguments to defmacro with no initialization form default to nil, the default value in deftype is the symbol *. The deftype above could be rewritten to take advantage of this:&lt;/p&gt;
&lt;pre&gt;(deftype octet-vector (&amp;amp;optional length)
  `(simple-array (unsigned-byte 8) (,length)))
&lt;/pre&gt;
&lt;p&gt;Now, in a type context, a plain &lt;code&gt;octet-vector&lt;/code&gt; expands into &lt;code&gt;(simple-array (unsigned-byte 8) (*))&lt;/code&gt;, or a one-dimensional octet array of indeterminate length, and &lt;code&gt;(octet-vector 32)&lt;/code&gt; expands into &lt;code&gt;(simple-array (unsigned-byte 8) (32)).&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;For more information, see section &lt;a href="http://l1sp.org/cl/3.4.8" target="_blank"&gt;3.4.8, Deftype Lambda Lists&lt;/a&gt;.&lt;/p&gt;</description><link>http://lisptips.com/post/44186972221</link><guid>http://lisptips.com/post/44186972221</guid><pubDate>Wed, 27 Feb 2013 21:20:57 -0500</pubDate></item><item><title>Trying again with with-simple-restart</title><description>&lt;p&gt;I have some code that reads data from a config file. If there&amp;#8217;s a problem loading, I&amp;#8217;d like the opportunity to fix it, outside of Lisp, and retry loading. It&amp;#8217;s easy to do that with with-simple-restart and loop:&lt;/p&gt;
&lt;pre&gt;(loop 
  (with-simple-restart (try-again "Try again")
    (return 
     (progn 
       (setf *config* (load-config-file))))
&lt;/pre&gt;
&lt;p&gt;If any error occurs during load-config-file, I can either fix up the file and choose the try-again restart, or give up and use an abort restart.&lt;/p&gt;
&lt;p&gt;(Thanks to Frode Vatvedt Fjeld for the concise idiom.)&lt;/p&gt;</description><link>http://lisptips.com/post/44108369388</link><guid>http://lisptips.com/post/44108369388</guid><pubDate>Tue, 26 Feb 2013 21:13:12 -0500</pubDate></item><item><title>Semicolon style</title><description>&lt;p&gt;The standard explains common semicolon comment style in section &lt;a href="http://l1sp.org/cl/2.4.4.2" target="_blank"&gt;2.4.4.2&lt;/a&gt;. Section &lt;a href="http://l1sp.org/cl/2.4.4.2.5" target="_blank"&gt;2.4.4.2.5&lt;/a&gt; includes this short example showing typical use of one through four semicolons:&lt;/p&gt;
&lt;pre&gt;;;;; Math Utilities

;;; FIB computes the the Fibonacci function in the traditional
;;; recursive way.

(defun fib (n)
  (check-type n integer)
  ;; At this point we're sure we have an integer argument.
  ;; Now we can get down to some serious computation.
  (cond ((&amp;lt; n 0)
         ;; Hey, this is just supposed to be a simple example.
         ;; Did you really expect me to handle the general case?
         (error "FIB got ~D as an argument." n))
        ((&amp;lt; n 2) n)             ;fib[0]=0 and fib[1]=1
        ;; The cheap cases didn't work.
        ;; Nothing more to do but recurse.
        (t (+ (fib (- n 1))     ;The traditional formula
              (fib (- n 2)))))) ; is fib[n-1]+fib[n-2].
              
&lt;/pre&gt;</description><link>http://lisptips.com/post/43944770476</link><guid>http://lisptips.com/post/43944770476</guid><pubDate>Sun, 24 Feb 2013 20:32:14 -0500</pubDate></item><item><title>The tree-walkers of CL</title><description>&lt;p&gt;If you need to visit a tree in some way, there are two built-in functions that, while not explicitly designed for the purpose, can walk a tree effectively.&lt;/p&gt;
&lt;p&gt;First, &lt;a href="http://l1sp.org/cl/subst-if" target="_blank"&gt;subst-if&lt;/a&gt; takes a predicate function that is called for each cons and atom in a tree. As long as the predicate returns &lt;em&gt;nil&lt;/em&gt;, no substitution will actually take place.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s a simple way to turn it into a function:&lt;/p&gt;
&lt;pre&gt;(defun walk-tree (fun tree)
  (subst-if t
            (constantly nil)
            tree
            :key fun))

* &lt;strong&gt;(walk-tree 'print '(a b (1 2 . 10) c))&lt;/strong&gt;

(A B (1 2 . 10) C) 
A 
(B (1 2 . 10) C) 
B 
((1 2 . 10) C) 
(1 2 . 10) 
1 
(2 . 10) 
2 
10 
(C) 
C 
NIL 
&lt;em&gt;(A B (1 2 . 10) C)&lt;/em&gt;&lt;/pre&gt;
&lt;p&gt;Second, &lt;a href="http://l1sp.org/cl/tree-equal" target="_blank"&gt;tree-equal&lt;/a&gt; is normally considered for comparing two trees, but it can also be used to call a function for each atom in a single tree; just pass the &lt;em&gt;same&lt;/em&gt; tree to tree-equal, and use the test function to do something with each atom. As long as the test function returns &lt;em&gt;true&lt;/em&gt;, the walking continues.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s a function that wraps it up:&lt;/p&gt;
&lt;pre&gt;(defun walk-tree-atoms (fun tree)
  (tree-equal tree tree
              :test (lambda (element-1 element-2)
                      (declare (ignore element-2))
                      (funcall fun element-1)
                      t)))

* &lt;strong&gt;(walk-tree-atoms 'print '(a b (1 2 . 10) c))&lt;/strong&gt;

A 
B 
1 
2 
10 
C 
NIL 
&lt;em&gt;T&lt;/em&gt;&lt;/pre&gt;
&lt;p&gt;(This tip inspired by a few articles from &lt;a href="http://xach.com/rpw3/articles/search?q=tree-equal" target="_blank"&gt;Rob Warnock&lt;/a&gt;.)&lt;/p&gt;</description><link>http://lisptips.com/post/43404489000</link><guid>http://lisptips.com/post/43404489000</guid><pubDate>Mon, 18 Feb 2013 11:11:00 -0500</pubDate></item><item><title>Duplicated keyword arguments</title><description>&lt;p&gt;If a function call has two pairs of keyword arguments with the same keyword, the leftmost argument pair is used. This can be helpful if a list of keyword arguments is received and you want to selectively override some value in that argument list in application. There&amp;#8217;s no need to use mutation or produce a fresh variation of the list.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;(defun write-escaped (object &amp;amp;rest args &amp;amp;key &amp;amp;allow-other-keys)
  (apply 'write object :escape t args))

* &lt;strong&gt;(write "hello" :escape nil)&lt;/strong&gt;
hello

* &lt;strong&gt;(write-escaped "hello" :escape nil)&lt;/strong&gt;
"hello"
&lt;/pre&gt;
&lt;p&gt;See &lt;a href="http://l1sp.org/cl/3.4.1.4" target="_blank"&gt;3.4.1.4&lt;/a&gt; for details.&lt;/p&gt;</description><link>http://lisptips.com/post/37350128056</link><guid>http://lisptips.com/post/37350128056</guid><pubDate>Thu, 06 Dec 2012 15:39:00 -0500</pubDate></item><item><title>The four causes of symbol conflicts</title><description>&lt;p&gt;A symbol conflict arises when a package operation would result in two &lt;a href="http://l1sp.org/cl/glossary/distinct" target="_blank"&gt;distinct&lt;/a&gt; symbols with the same name being &lt;a href="http://l1sp.org/cl/glossary/accessible" target="_blank"&gt;accessible&lt;/a&gt; in a single package. There are exactly four ways this can happen.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Inheriting:&lt;/strong&gt; When you use (via &lt;a href="http://l1sp.org/cl/use-package" target="_blank"&gt;use-package&lt;/a&gt; or the &lt;a href="http://l1sp.org/cl/defpackage" target="_blank"&gt;defpackage&lt;/a&gt; :use clause) a package that exports a distinct symbol sharing the same name with a symbol already accessible in the using package. The conflict can arise between two inherited symbols, or between an inherited symbol and a &lt;a href="http://l1sp.org/cl/glossary/present" target="_blank"&gt;present&lt;/a&gt; symbol.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Importing:&lt;/strong&gt; When you &lt;a href="http://l1sp.org/cl/import" target="_blank"&gt;import&lt;/a&gt; a symbol and a distinct symbol with the same name is already accessible in the target package.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Exporting:&lt;/strong&gt; When you export a symbol from a package and a distinct symbol with the same name is already accessible in any package that uses that package.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Uninterning:&lt;/strong&gt; When you &lt;a href="http://l1sp.org/cl/unintern" target="_blank"&gt;unintern&lt;/a&gt; a shadowing symbol and the absence of its shadowing results in two distinct symbols with the same name being accessible via inheritance.&lt;/p&gt;
&lt;p&gt;Here are short examples of each case. They all assume an environment in which this package definition is in effect:&lt;/p&gt;
&lt;pre&gt;(defpackage #:auto
  (:documentation "An automotive package.")
  (:use)
  (:intern #:tan)
  (:export #:car))
&lt;/pre&gt;
&lt;p&gt;Inheritance conflict:&lt;/p&gt;
&lt;pre&gt;(defpackage #:inheritance-conflict
  (:use #:cl #:auto))

&lt;strong&gt;=&amp;gt;&lt;/strong&gt; USE-PACKAGE #[PACKAGE "COMMON-LISP"] causes name-conflicts in
#[PACKAGE "INHERITANCE-CONFLICT"] between the following symbols:
  AUTO:CAR, COMMON-LISP:CAR
   [Condition of type NAME-CONFLICT]
See also:
  Common Lisp Hyperspec, 11.1.1.2.5 [:section]

&lt;/pre&gt;
&lt;p&gt;Import conflict:&lt;/p&gt;
&lt;pre&gt;(defpackage #:import-conflict
  (:use #:cl))

(import '(auto:car) '#:import-conflict)

&lt;strong&gt;=&amp;gt;&lt;/strong&gt; IMPORT AUTO:CAR causes name-conflicts in
#[PACKAGE "IMPORT-CONFLICT"] between the following symbols:
  AUTO:CAR, COMMON-LISP:CAR
   [Condition of type NAME-CONFLICT]
See also:
  Common Lisp Hyperspec, 11.1.1.2.5 [:section]
&lt;/pre&gt;
&lt;p&gt;Export conflict:&lt;/p&gt;
&lt;pre&gt;(defpackage #:export-conflict
  (:use #:cl #:auto)
  ;; Avoid inheritance conflict with shadowing
  (:shadow #:car))

(export 'auto::tan '#:auto)

&lt;strong&gt;=&amp;gt;&lt;/strong&gt; EXPORT AUTO::TAN causes name-conflicts in
#[PACKAGE "EXPORT-CONFLICT"] between the following symbols:
  AUTO::TAN, COMMON-LISP:TAN
   [Condition of type NAME-CONFLICT]
See also:
  Common Lisp Hyperspec, 11.1.1.2.5 [:section]
&lt;/pre&gt;
&lt;p&gt;Unintern conflict:&lt;/p&gt;
&lt;pre&gt;(defpackage #:unintern-conflict
  (:use #:cl #:auto)
  ;; Avoid inheritance conflict with shadowing
  (:shadow #:car))

(unintern 'unintern-conflict::car '#:unintern-conflict)

&lt;strong&gt;=&amp;gt;&lt;/strong&gt; UNINTERN UNINTERN-CONFLICT::CAR causes name-conflicts in
#&amp;lt;PACKAGE "UNINTERN-CONFLICT"&amp;gt; between the following symbols:
  AUTO:CAR, COMMON-LISP:CAR
   [Condition of type NAME-CONFLICT]
See also:
  Common Lisp Hyperspec, 11.1.1.2.5 [:section]
&lt;/pre&gt;
&lt;p&gt;If you use Common Lisp, you have to interact with the package system. I think the best way to get along with the package system is not to keep it at arm&amp;#8217;s length while holding your nose, but to learn how it works, inside and out.&lt;/p&gt;</description><link>http://lisptips.com/post/34436452765</link><guid>http://lisptips.com/post/34436452765</guid><pubDate>Sat, 27 Oct 2012 16:22:00 -0400</pubDate></item><item><title>A little bit of file-position</title><description>&lt;p&gt;When used with one argument, &lt;a href="http://l1sp.org/cl/file-position" target="_blank"&gt;file-position&lt;/a&gt; returns an integer representing the stream&amp;#8217;s &lt;a href="http://l1sp.org/cl/glossary/file_position" target="_blank"&gt;file position&lt;/a&gt;:&lt;/p&gt;
&lt;pre&gt;* &lt;strong&gt;(defvar *s* (open "data.bin" :element-type '(unsigned-byte 8))&lt;/strong&gt;
=&amp;gt; *S*

* &lt;strong&gt;(read-byte *s*)&lt;/strong&gt;
=&amp;gt; 42

* &lt;strong&gt;(read-byte *s*)&lt;/strong&gt;
=&amp;gt; 107

* &lt;strong&gt;(file-position *s*)&lt;/strong&gt;
=&amp;gt; 2
&lt;/pre&gt;
&lt;p&gt;When used with two arguments, file-position &lt;em&gt;sets&lt;/em&gt; the stream position:&lt;/p&gt;
&lt;pre&gt;* &lt;strong&gt;(file-position *s* 1)&lt;/strong&gt;
=&amp;gt; T

* &lt;strong&gt;(read-byte *s*)&lt;/strong&gt;
=&amp;gt; 107
&lt;/pre&gt;
&lt;p&gt;When used this way, the position is often given as an integer, but the function actually accepts a &lt;a href="http://l1sp.org/cl/glossary/file_position_designator" target="_blank"&gt;designator&lt;/a&gt;. An integer position value represents itself, the keyword :start represents 0 (the first position in the stream), and the keyword :end represents the last position in the stream.&lt;/p&gt;
&lt;p&gt;What would life be like without :start and :end? For :start, not too difficult; you could just use 0. But without :end you would need to use some other function, such as file-length, to correctly position the stream at the end.&lt;/p&gt;</description><link>http://lisptips.com/post/32388049509</link><guid>http://lisptips.com/post/32388049509</guid><pubDate>Thu, 27 Sep 2012 06:28:00 -0400</pubDate></item><item><title>A brief history of Lisp</title><description>&lt;p&gt;For a 1500-word history of Lisp up to the point of Common Lisp standardization, see &lt;a href="http://l1sp.org/cl/1.1.2" target="_blank"&gt;section 1.1.2&lt;/a&gt; of the spec. It covers the institutions, projects, people, and influential ideas involved in the creation and evolution of Lisp over the course of several decades, from McCarthy at Dartmouth in the 1950s to the many active branches of Lisp in the 1980s.&lt;/p&gt;</description><link>http://lisptips.com/post/31635893951</link><guid>http://lisptips.com/post/31635893951</guid><pubDate>Sat, 15 Sep 2012 23:58:04 -0400</pubDate></item><item><title>Putting the R in REPL</title><description>&lt;p&gt;Try this in your REPL:&lt;/p&gt;
&lt;pre&gt;* &lt;strong&gt;(let (#'42) (+ . #'5))&lt;/strong&gt;
=&amp;gt; 47&lt;/pre&gt;
&lt;p&gt;If you find it perplexing, and you use SBCL, evaluating &lt;a href="http://www.lispworks.com/documentation/HyperSpec/Body/v_pl_plp.htm" target="_blank"&gt;+&lt;/a&gt; next in the REPL might help clear things up.&lt;/p&gt;</description><link>http://lisptips.com/post/31567007407</link><guid>http://lisptips.com/post/31567007407</guid><pubDate>Fri, 14 Sep 2012 23:58:10 -0400</pubDate></item><item><title>Using an adjustable displaced array as a cursor on another array.</title><description>&lt;p&gt;Scanning a string can be done using input functions withwith-input-from-string:&lt;/p&gt;
&lt;pre&gt;(with-input-from-string (stream "Hello World!  How do you do? ")
  (let ((c (make-string 4)))
    (loop
      :for pos = (read-sequence c stream)
      :while (= pos (length c))
      :do (print c)
      :finally (terpri))))

"Hell" 
"o Wo" 
"rld!" 
"  Ho" 
"w do" 
" you" 
" do?" 
nil
&lt;/pre&gt;
&lt;p&gt;This can also be done using a displaced adjustable array, without copying the data that is read:&lt;/p&gt;
&lt;pre&gt;(defun make-vector-cursor (vector &amp;amp;key (size 0) (offset 0))
  (make-array size
              :element-type (array-element-type vector)
              :adjustable t
              :displaced-to vector
              :displaced-index-offset offset))

(defun advance-cursor (cursor &amp;amp;key (size (length cursor)))
  (multiple-value-bind (vector offset) (array-displacement cursor)
    (adjust-array cursor size
                  :element-type (array-element-type vector)
                  :displaced-to vector
                  :displaced-index-offset (+ offset (length cursor)))))
&lt;/pre&gt;
&lt;pre&gt;(let ((c (make-vector-cursor "Hello World!  How do you do? " :size 4)))
  (loop
    :do (print c)
    :while (ignore-errors (advance-cursor c))
    :finally (terpri)))

"Hell" 
"o Wo" 
"rld!" 
"  Ho" 
"w do" 
" you" 
" do?" 
NIL
&lt;/pre&gt;
&lt;pre&gt;(let ((c (make-vector-cursor "Hello World!  How do you do? " :size 1)))
  (loop
    :for size :from 1
    :do (print c)
    :while (ignore-errors (advance-cursor c :size size))
    :finally (terpri)))

"H" 
"e" 
"ll" 
"o W" 
"orld" 
"!  Ho" 
"w do y" 
"ou do? " 
nil
&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;This post courtesy of Pascal J. Bourguignon&lt;/em&gt;&lt;/p&gt;</description><link>http://lisptips.com/post/31516446212</link><guid>http://lisptips.com/post/31516446212</guid><pubDate>Fri, 14 Sep 2012 06:29:42 -0400</pubDate></item><item><title>Binding keyword arguments</title><description>&lt;p&gt;By default, keyword argument variable bindings match the name of the keyword used to pass the value. For example:&lt;/p&gt;
&lt;pre&gt;(defun keytest (&amp;amp;key foo)
  (list foo))

* &lt;strong&gt;(keytest :foo 42)&lt;/strong&gt;
=&amp;gt; (42)
&lt;/pre&gt;
&lt;p&gt;However, the variable doesn&amp;#8217;t have to match the keyword provided. The following syntax will accept a keyword of :foo in the function call but bind a variable named bar:&lt;/p&gt;
&lt;pre&gt;(defun keytest (&amp;amp;key ((:foo bar)))
  (list bar))

* &lt;strong&gt;(keytest :foo 42)&lt;/strong&gt;
=&amp;gt; (42)
&lt;/pre&gt;
&lt;p&gt;Binding keywords this way can be helpful when binding a plist via destructuring-bind when you don&amp;#8217;t want the plist keys to name the variables you actually want to use in the scope of the destructuring bind. For example, the plist may contain &lt;code&gt;(:customer-id 42 ...)&lt;/code&gt;, but in your context it makes more sense to bind a variable named &lt;code&gt;old-customer-id.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The &amp;#8220;keyword&amp;#8221; also need not be a keyword:&lt;/p&gt;
&lt;pre&gt;(defun keytest (&amp;amp;key ((foo bar)))
  (list bar))

* &lt;strong&gt;(keytest 'foo 42)&lt;/strong&gt;
=&amp;gt; (42)&lt;/pre&gt;
&lt;p&gt;Using internal symbols as function keyword arguments is one way to indicate that they are not part of a function&amp;#8217;s public API.&lt;/p&gt;</description><link>http://lisptips.com/post/31253190116</link><guid>http://lisptips.com/post/31253190116</guid><pubDate>Sun, 09 Sep 2012 23:56:44 -0400</pubDate></item><item><title>Multiple export clauses in defpackage</title><description>&lt;p&gt;The syntax for &lt;a href="http://l1sp.org/cl/defpackage" target="_blank"&gt;defpackage&lt;/a&gt; allows multiple export clauses. I like to use this feature to visually group related symbols.&lt;/p&gt;
&lt;pre&gt;(defpackage #:myproject
  (:use #:cl)
  ;; Web stuff
  (:export #:fetch
           #:parse-url
           #:status)
  ;; File utilities
  (:export #:lines
           #:first-line
           #:touch)
  ...)
&lt;/pre&gt;
&lt;p&gt;Although it has no effect on the semantics, I find it helpful for reading.&lt;/p&gt;</description><link>http://lisptips.com/post/31040421235</link><guid>http://lisptips.com/post/31040421235</guid><pubDate>Thu, 06 Sep 2012 23:56:08 -0400</pubDate></item><item><title>A simple REPL</title><description>&lt;p&gt;Here&amp;#8217;s a very simple REPL that includes the *, **, and *** variables:&lt;/p&gt;
&lt;pre&gt;(defun repl ()
  (princ "&amp;gt; ")
  (loop 
    (shiftf *** ** * (eval (read))) 
    (format t "~a~&amp;amp;&amp;gt; " *)))
  &lt;/pre&gt;
&lt;p&gt;Provided by Stas Boukarev.&lt;/p&gt;</description><link>http://lisptips.com/post/30978863226</link><guid>http://lisptips.com/post/30978863226</guid><pubDate>Wed, 05 Sep 2012 23:54:11 -0400</pubDate></item><item><title>Printing package-qualified symbols</title><description>&lt;p&gt;When &lt;a href="http://l1sp.org/cl/*print-escape*" target="_blank"&gt;*print-escape*&lt;/a&gt; is true, symbols are normally printed with package prefixes only if the current package &lt;a href="http://l1sp.org/cl/*package*" target="_blank"&gt;*package*&lt;/a&gt; is not the symbol&amp;#8217;s home package. It&amp;#8217;s easy to make sure that a symbol is always printed with a package prefix, e.g. for debugging.&lt;/p&gt;
&lt;p&gt;When the current package is the &lt;a href="http://l1sp.org/cl/11.1.2.3" target="_blank"&gt;keyword package&lt;/a&gt;, non-keyword symbols are printed with package prefixes, and keywords are printed with their normal colon prefix. For example, in SBCL:&lt;/p&gt;
&lt;pre&gt;(in-package #:cl-user)
(let ((*package* (find-package "KEYWORD")))
  (prin1-to-string '(car stream :car quit)))

=&amp;gt; "(COMMON-LISP:CAR COMMON-LISP:STREAM :CAR SB-EXT:QUIT)"&lt;/pre&gt;
&lt;p&gt;For full details, see &lt;a href="http://l1sp.org/cl/22.1.3.3.1" target="_blank"&gt;22.1.3.3.1, Package Prefixes for Symbols&lt;/a&gt;.&lt;/p&gt;</description><link>http://lisptips.com/post/20871695085</link><guid>http://lisptips.com/post/20871695085</guid><pubDate>Tue, 10 Apr 2012 20:02:36 -0400</pubDate></item><item><title>Evaluating the last expression</title><description>&lt;p&gt;In the REPL, +, ++, and +++ have as values the three most recently evaluated expressions. A quick way to evaluate the previous expression, especially handy in a REPL without input history, is&lt;/p&gt;
&lt;pre&gt;#.+&lt;/pre&gt;
&lt;p&gt;It&amp;#8217;s equivalent to (eval +).&lt;/p&gt;
&lt;p&gt;(Thanks to Anton Kovalenko.)&lt;/p&gt;</description><link>http://lisptips.com/post/15239867131</link><guid>http://lisptips.com/post/15239867131</guid><pubDate>Tue, 03 Jan 2012 08:50:06 -0500</pubDate></item><item><title>Un-displacing an array</title><description>&lt;p&gt;Here&amp;#8217;s a function to get the underlying array on which a displaced array is based:&lt;/p&gt;
&lt;pre&gt;(defun undisplace-array (array)
  "Return the fundamental array and the start and end positions into
it of a displaced array."
  (let ((length (length array))
        (start 0))
    (loop
      (multiple-value-bind (to offset) (array-displacement array)
        (if to
            (setq array to
                  start (+ start offset))
          (return (values array start (+ start length))))))))
&lt;/pre&gt;
&lt;p&gt;From an &lt;a href="http://www.xach.com/naggum/articles/3283364284060266KL2065E@naggum.no.html" target="_blank"&gt;article by Erik Naggum&lt;/a&gt;.&lt;/p&gt;</description><link>http://lisptips.com/post/15126780102</link><guid>http://lisptips.com/post/15126780102</guid><pubDate>Sun, 01 Jan 2012 10:00:06 -0500</pubDate></item><item><title>Referring to method parameters</title><description>&lt;p&gt;In &lt;a href="http://l1sp.org/cl/defmethod" target="_blank"&gt;defmethod&lt;/a&gt; lambda lists, required parameters that aren&amp;#8217;t explicitly specialized default to specializing on the system class &lt;em&gt;t&lt;/em&gt;. But there&amp;#8217;s a difference between an implicit and explicit specialization on &lt;em&gt;t&lt;/em&gt;. The hyperspec explains:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The expansion of the defmethod macro &amp;#8220;refers to&amp;#8221; each specialized parameter (see the description of &lt;a href="http://l1sp.org/cl/ignore" target="_blank"&gt;ignore&lt;/a&gt; within the description of &lt;a href="http://l1sp.org/cl/declare" target="_blank"&gt;declare&lt;/a&gt;). This includes parameters that have an explicit parameter specializer name of t. This means that a compiler warning does not occur if the body of the method does not refer to a specialized parameter, while a warning might occur if the body of the method does not refer to an unspecialized parameter. For this reason, a parameter that specializes on t is not quite synonymous with an unspecialized parameter in this context.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This makes a difference in Clozure CL; here&amp;#8217;s what you get if you don&amp;#8217;t use an implicitly specialized required parameter:&lt;/p&gt;
&lt;pre&gt;? (defmethod foo (bar) 42)
;Compiler warnings :
;   In (FOO (T)) inside an anonymous lambda form: Unused lexical variable BAR
#&amp;lt;STANDARD-METHOD FOO (T)&amp;gt;
&lt;/pre&gt;
&lt;p&gt;There is no warning with an explicit specialization:&lt;/p&gt;
&lt;pre&gt;? (defmethod bar ((baz t)) 42)
#&amp;lt;STANDARD-METHOD BAR (T)&amp;gt;
&lt;/pre&gt;
&lt;p&gt;(Thanks to Hans Hübner.)&lt;/p&gt;</description><link>http://lisptips.com/post/15025308254</link><guid>http://lisptips.com/post/15025308254</guid><pubDate>Fri, 30 Dec 2011 08:47:00 -0500</pubDate></item></channel></rss>
