<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Jorge Tavares weblog &#187; Programming</title>
	<atom:link href="http://jorgetavares.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://jorgetavares.com</link>
	<description></description>
	<lastBuildDate>Sun, 20 May 2012 16:27:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jorgetavares.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/dd7ededad4bb476ae4089ad9d3094443?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Jorge Tavares weblog &#187; Programming</title>
		<link>http://jorgetavares.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jorgetavares.com/osd.xml" title="Jorge Tavares weblog" />
	<atom:link rel='hub' href='http://jorgetavares.com/?pushpress=hub'/>
		<item>
		<title>zsort: portable sorting algorithms in Common Lisp</title>
		<link>http://jorgetavares.com/2012/04/22/zsort-portable-sorting-algorithms-in-common-lisp/</link>
		<comments>http://jorgetavares.com/2012/04/22/zsort-portable-sorting-algorithms-in-common-lisp/#comments</comments>
		<pubDate>Sun, 22 Apr 2012 20:15:40 +0000</pubDate>
		<dc:creator>Jorge Tavares</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Common Lisp]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Sorting Algorithms]]></category>
		<category><![CDATA[zsort]]></category>

		<guid isPermaLink="false">http://jorgetavares.com/?p=1225</guid>
		<description><![CDATA[zsort is a library that I started working on as a simple hobby project. More or less around the same time I decided to check which algorithms the different Common Lisp implementations use. It is now part of Quicklisp so it can be easily used (thanks Zack!). The main goal of zsort is to be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jorgetavares.com&#038;blog=3353190&#038;post=1225&#038;subd=jorgetavares&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://jorgetavares.github.com/zsort/">zsort</a> is a library that I started working on as a simple hobby project. More or less around the same time I decided to check <a href="http://jorgetavares.com/2012/02/02/sorting-algorithms-used-in-the-cl-implementations/">which algorithms the different Common Lisp implementations use</a>. It is now part of <a href="http://www.quicklisp.org/">Quicklisp</a> so it can be easily used (thanks <a href="http://xach.livejournal.com/">Zack</a>!). </p>
<p>The main goal of <a href="http://jorgetavares.github.com/zsort/">zsort</a> is to be a collection of portable sorting algorithms. If they can be fast, even better. Common lisp provides the <a href="http://www.lispworks.com/documentation/lw50/CLHS/Body/f_sort_.htm">sort</a> and <a href="http://www.lispworks.com/documentation/lw50/CLHS/Body/f_sort_.htm">stable-sort</a> functions but these can have different algorithms implemented according to each implementation, which can make an application unportable if you rely on a specific type of sorting. Also, the standard functions might not be the best for a certain situation and as such you might need a specialized sort. Even if for most situations the standard functions are more than enough, the <a href="http://jorgetavares.github.com/zsort/">zsort</a> library could be a useful complement.</p>
<p>Right now the implemented algorithms are: <a href="http://en.wikipedia.org/wiki/Insertion_sort">insertion sort</a>, <a href="http://en.wikipedia.org/wiki/Quicksort">quicksort</a>, <a href="http://en.wikipedia.org/wiki/Quicksort#Randomized_quicksort_expected_complexity">randomized quicksort</a>, <a href="http://en.wikipedia.org/wiki/Merge_sort">merge sort</a>, <a href="http://en.wikipedia.org/wiki/Heapsort">heapsort</a> and <a href="http://en.wikipedia.org/wiki/Counting_sort">counting sort</a>. The plan is to add more algorithms, for example, <a href="http://en.wikipedia.org/wiki/Bucket_sort">bucket sort</a> and <a href="http://en.wikipedia.org/wiki/Timsort">timsort</a>. However, the main thing on the todo list is adding the possibility of <a href="http://en.wikipedia.org/wiki/External_sorting">external sorting</a> (to handle large amounts of data) and parallel versions of some sorting algorithms. I am considering using <a href="http://lparallel.com/">lparallel</a> for this but I am still undecided.</p>
<p>There is still a lot of work to be done, but I think the library as it is can already be a little useful. And of course, all kind of suggestions and improvements are welcome!</p>
<br />Filed under: <a href='http://jorgetavares.com/category/programming/'>Programming</a> Tagged: <a href='http://jorgetavares.com/tag/common-lisp/'>Common Lisp</a>, <a href='http://jorgetavares.com/tag/library/'>library</a>, <a href='http://jorgetavares.com/tag/lisp/'>Lisp</a>, <a href='http://jorgetavares.com/tag/sorting-algorithms/'>Sorting Algorithms</a>, <a href='http://jorgetavares.com/tag/zsort/'>zsort</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jorgetavares.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jorgetavares.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jorgetavares.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jorgetavares.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jorgetavares.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jorgetavares.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jorgetavares.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jorgetavares.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jorgetavares.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jorgetavares.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jorgetavares.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jorgetavares.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jorgetavares.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jorgetavares.wordpress.com/1225/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jorgetavares.com&#038;blog=3353190&#038;post=1225&#038;subd=jorgetavares&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jorgetavares.com/2012/04/22/zsort-portable-sorting-algorithms-in-common-lisp/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/378de7e75b3ac59ec9cab927683c0904?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jast</media:title>
		</media:content>
	</item>
		<item>
		<title>Book review: The Tangled Web</title>
		<link>http://jorgetavares.com/2012/02/16/book-review-the-tangled-web/</link>
		<comments>http://jorgetavares.com/2012/02/16/book-review-the-tangled-web/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 17:43:44 +0000</pubDate>
		<dc:creator>Jorge Tavares</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Book review]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://jorgetavares.com/?p=1097</guid>
		<description><![CDATA[For the past couple of months I&#8217;ve been reading The Tangled Web: A Guide to Securing Modern Web Applications by Michael Zalewski. The book was published by no starch press and they were kind enough to provide me a copy for review. I enjoy reading about topics outside my main field and interests. It&#8217;s a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jorgetavares.com&#038;blog=3353190&#038;post=1097&#038;subd=jorgetavares&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://jorgetavares.files.wordpress.com/2012/02/tangledweb_cvr-webquality.png"><img src="http://jorgetavares.files.wordpress.com/2012/02/tangledweb_cvr-webquality.png?w=700" alt="" title="tangledWeb_cvr-webquality"   class="alignnone size-full wp-image-1102" /></a></p>
<p>For the past couple of months I&#8217;ve been reading <a href="http://lcamtuf.coredump.cx/tangled/">The Tangled Web: A Guide to Securing Modern Web Applications</a> by <a href="http://lcamtuf.coredump.cx/">Michael Zalewski</a>. The book was published by <a href="http://nostarch.com/">no starch press</a> and they were kind enough to provide me a copy for review. I enjoy reading about topics outside my main field and interests. It&#8217;s a way to keep myself informed and learn new things and this book fits exactly into this category. </p>
<p>The Tangled Web is mostly about web technologies and how insecure they are by nature. The book is a very engaging narrative, full of details and impressive war stories. It focuses on the practical issues of web technologies and not on the theory of security. The book can be very useful for web developers and those interested in security. For example, at the end of each chapter we can find a &#8220;Security Engineering Cheat Sheet&#8221;, which presents us a summary of things to consider/do. These sheets alone make the book worthwhile having. The book is organized in three main parts. In the first one, the author tells us the story of the inception of the web until today and discusses all the important technologies, protocols, etc. The second part focuses on the browser security and the third part on &#8220;the things to come&#8221;. Although the book is not very thick (around 300 pages) it addresses too many important issues to completely absorb them in a single reading. </p>
<p>To conclude, the Tangled Web is a solid book, full of interesting and useful information. For web developers and security experts it should be a must read book. For the rest of us it is an enjoyable reading. </p>
<br />Filed under: <a href='http://jorgetavares.com/category/programming/'>Programming</a> Tagged: <a href='http://jorgetavares.com/tag/book-review/'>Book review</a>, <a href='http://jorgetavares.com/tag/security/'>Security</a>, <a href='http://jorgetavares.com/tag/web/'>Web</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jorgetavares.wordpress.com/1097/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jorgetavares.wordpress.com/1097/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jorgetavares.wordpress.com/1097/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jorgetavares.wordpress.com/1097/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jorgetavares.wordpress.com/1097/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jorgetavares.wordpress.com/1097/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jorgetavares.wordpress.com/1097/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jorgetavares.wordpress.com/1097/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jorgetavares.wordpress.com/1097/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jorgetavares.wordpress.com/1097/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jorgetavares.wordpress.com/1097/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jorgetavares.wordpress.com/1097/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jorgetavares.wordpress.com/1097/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jorgetavares.wordpress.com/1097/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jorgetavares.com&#038;blog=3353190&#038;post=1097&#038;subd=jorgetavares&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jorgetavares.com/2012/02/16/book-review-the-tangled-web/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/378de7e75b3ac59ec9cab927683c0904?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jast</media:title>
		</media:content>

		<media:content url="http://jorgetavares.files.wordpress.com/2012/02/tangledweb_cvr-webquality.png" medium="image">
			<media:title type="html">tangledWeb_cvr-webquality</media:title>
		</media:content>
	</item>
		<item>
		<title>Macros Design Patterns</title>
		<link>http://jorgetavares.com/2012/02/13/macros-design-patterns/</link>
		<comments>http://jorgetavares.com/2012/02/13/macros-design-patterns/#comments</comments>
		<pubDate>Mon, 13 Feb 2012 11:20:25 +0000</pubDate>
		<dc:creator>Jorge Tavares</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Common Lisp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Macros]]></category>

		<guid isPermaLink="false">http://jorgetavares.com/?p=956</guid>
		<description><![CDATA[I was reading the thread What are some fun or useful macros? on reddit and it reminded me of another thread that appeared in the Pro mailing list. These kind of threads are always enjoyable because each time you learn something new and see really interesting things. While reading them, it crossed my mind that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jorgetavares.com&#038;blog=3353190&#038;post=956&#038;subd=jorgetavares&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was reading the thread <a href="http://www.reddit.com/r/lisp/comments/pg2yx/what_are_some_fun_or_useful_macros/">What are some fun or useful macros?</a> on reddit and it reminded me of <a href="http://lists.common-lisp.net/pipermail/pro/2010-September/000006.html">another thread</a> that appeared in the Pro mailing list. These kind of threads are always enjoyable because each time you learn something new and see really interesting things. While reading them, it crossed my mind that another variant of this question would be <em>what are some fun or useful macros design patterns</em>. Instead of examples of specific code macros (general or not) it would be nice to see common programming practices using macros. So, for a lack of a better expression name, let&#8217;s call them <em>macros design patterns</em>. </p>
<p>My favorite one is to configure an algorithm, especially when we want to use the correct types. Essentially, we write an algorithm using a macro that takes types or configuration arguments and then we expand it to the appropriate desired configurations. For example, if you have an algorithm that operates on different types of sequences, instead of writing several duplicate functions with the same algorithm but with the associated type declarations, just apply this pattern. A simple but contrived example: we want to compute the mean of a vector but use its proper type. In addition, we might also want the possibility of using a key to access the vector elements. We can write the following macro:</p>
<pre><code class="commonlisp">(defmacro mean-body (vector vector-type vector-ref key)
  (let ((size (gensym)) (i (gensym)))
    `(locally 
	 (declare (type ,vector-type ,vector))
       (let ((,size (length ,vector)))
	 (/ (loop for ,i from 0 below ,size
		  sum ,(if key 
			   `(funcall ,key (,vector-ref ,vector ,i))
			   `(,vector-ref ,vector ,i)))
	    ,size)))))
</pre>
<p></code></p>
<p>The macro contains the algorithm (in this simple case the mean) and the arguments allow us to configure the multiple versions we need. If we want a simple-vector, the macro will expand to use the correct type declaration and svref. If a key function is needed it will also include it. Then, we can call the macro with the several configurations value inside the main function:</p>
<pre><code class="commonlisp">(defun mean (vector &amp;optional key)
  (typecase vector
    (simple-vector 
     (if key 
	 (mean-body vector simple-vector svref key)
	 (mean-body vector simple-vector svref nil)))
    (vector 
     (if key 
	 (mean-body vector vector aref key)
	 (mean-body vector vector aref nil)))
    (otherwise 
      (if key 
	 (mean-body vector sequence elt key)
	 (mean-body vector sequence elt nil)))))
</pre>
<p></code></p>
<p>This can be very useful in situations where we want to optimize code since it becomes easy to add the proper type declarations to the input arguments of an algorithm. Moreover, we keep the algorithm in a single place, making it easier to maintain. Depending on the situation, we can also define a function for each configuration. In the example we could have a mean-simple-vector and mean-vector.</p>
<p>I don't know if it has already a specific name but I like to call it <em>the configurable algorithm pattern</em>. I find it very useful. And thinking back to the reddit <a href="http://www.reddit.com/r/lisp/comments/pg2yx/what_are_some_fun_or_useful_macros/">thread</a>, what are your favorite macros design patterns? Which ones do you find useful and use them regularly? If you want to share, feel free to drop a line. I am interested in seing and learning other patterns!  </p>
<br />Filed under: <a href='http://jorgetavares.com/category/programming/'>Programming</a> Tagged: <a href='http://jorgetavares.com/tag/common-lisp/'>Common Lisp</a>, <a href='http://jorgetavares.com/tag/design-patterns/'>Design Patterns</a>, <a href='http://jorgetavares.com/tag/lisp/'>Lisp</a>, <a href='http://jorgetavares.com/tag/macros/'>Macros</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jorgetavares.wordpress.com/956/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jorgetavares.wordpress.com/956/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jorgetavares.wordpress.com/956/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jorgetavares.wordpress.com/956/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jorgetavares.wordpress.com/956/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jorgetavares.wordpress.com/956/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jorgetavares.wordpress.com/956/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jorgetavares.wordpress.com/956/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jorgetavares.wordpress.com/956/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jorgetavares.wordpress.com/956/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jorgetavares.wordpress.com/956/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jorgetavares.wordpress.com/956/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jorgetavares.wordpress.com/956/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jorgetavares.wordpress.com/956/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jorgetavares.com&#038;blog=3353190&#038;post=956&#038;subd=jorgetavares&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jorgetavares.com/2012/02/13/macros-design-patterns/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/378de7e75b3ac59ec9cab927683c0904?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jast</media:title>
		</media:content>
	</item>
		<item>
		<title>Sorting algorithms used in the CL implementations</title>
		<link>http://jorgetavares.com/2012/02/02/sorting-algorithms-used-in-the-cl-implementations/</link>
		<comments>http://jorgetavares.com/2012/02/02/sorting-algorithms-used-in-the-cl-implementations/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 09:45:29 +0000</pubDate>
		<dc:creator>Jorge Tavares</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Common Lisp]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Sorting Algorithms]]></category>
		<category><![CDATA[Survey]]></category>

		<guid isPermaLink="false">http://jorgetavares.com/?p=823</guid>
		<description><![CDATA[Which sorting algorithm should one implement when developing a program? The best answer is probably none. Use the sort provided by your system/library/etc. Unless you know your input data has some special properties that you can take advantage of, the provided sort should be enough for your needs and probably is more efficiently implemented. However, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jorgetavares.com&#038;blog=3353190&#038;post=823&#038;subd=jorgetavares&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Which <a href="http://en.wikipedia.org/wiki/Sorting_algorithm">sorting algorithm</a> should one implement when developing a program? The best answer is probably <em>none</em>. Use the sort provided by your system/library/etc. Unless you know your input data has some special properties that you can take advantage of, the provided sort should be enough for your needs and probably is more efficiently implemented. </p>
<p>However, I think it is important to know what sorting algorithm is implemented. If one knows the properties of the data, it is possible to understand if the provided sort can or will pose a problem. In the same way a programmer shouldn&#8217;t implement a sorting algorithm every time it needs to sort something, the programmer should also be aware of the limitations/advantages of the system sort. That way one can decide if a special sort is needed or not.</p>
<p>Common Lisp provides the functions <a href="http://www.lispworks.com/documentation/lw50/CLHS/Body/f_sort_.htm">sort</a> and <a href="http://www.lispworks.com/documentation/lw50/CLHS/Body/f_sort_.htm">stable-sort</a>. The <a href="http://www.lispworks.com/documentation/lw50/CLHS/Front/index.htm">HyperSpec</a> describes their operation well but it does not define the sorting algorithm. That decision is left free to the implementations. In addition, both functions don&#8217;t necessarily share the same algorithm. The difference between the two is that the second function sorts in a way that guarantees <a href="http://en.wikipedia.org/wiki/Sorting_algorithm#Stability">stability</a>, i.e., two elements that are equal remain in the same position after sorting is completed. The use of sort and stable-sort requires some care (see the section <a href="http://www.aiai.ed.ac.uk/~jeff/lisp/cl-pitfalls">sort pitfalls</a>) but lets focus on the algorithms and not on its usage.</p>
<p>What sorting algorithms do the major open source <a href="http://common-lisp.net/~dlw/LispSurvey.html">CL implementations</a> actually implement? I was curious about it and went to check the source for <a href="http://common-lisp.net/project/armedbear/">ABCL</a>, <a href="http://ccl.clozure.com/">CCL</a>, <a href="http://clisp.org/">CLISP</a>, <a href="http://www.cons.org/cmucl/">CMUCL</a>, <a href="http://ecls.sourceforge.net/">ECL</a> and <a href="http://www.sbcl.org/">SBCL</a>. Not surprising, we find some differences between the implementations. What it was more unexpected to discover is that some implementations also use different sorting algorithms according to the sequence type. A quick survey of the findings is summarized in the following table (if anythings is incorrect, please tell me). The links for the source code are in the implementation name (careful, in CCL and SBCL there are two links).</p>
<table style="text-align:center;" width="690" border="1">
<tbody>
<tr>
<th>Implementation</th>
<th>sort</th>
<th>stable-sort</th>
</tr>
<tr>
<td><a href="http://svn.common-lisp.net/armedbear/trunk/abcl/src/org/armedbear/lisp/sort.lisp">ABCL</a></td>
<td>merge sort (lists) / quicksort</td>
<td>merge sort</td>
</tr>
<tr>
<td><a href="http://trac.clozure.com/ccl/browser/trunk/source/level-1/l1-Mergesort.lisp">C</a><a href="http://trac.clozure.com/ccl/browser/trunk/source/lib/sort.lisp">CL</a></td>
<td>merge sort (lists) / quicksort</td>
<td>merge sort</td>
</tr>
<tr>
<td><a href="http://clisp.hg.sourceforge.net/hgweb/clisp/clisp/file/e789e5f15ae3/src/sort.d">CLISP</a></td>
<td>tree sort</td>
<td>tree sort</td>
</tr>
<tr>
<td><a href="http://trac.common-lisp.net/cmucl/browser/src/code/sort.lisp">CMUCL</a></td>
<td>heapsort</td>
<td>merge sort</td>
</tr>
<tr>
<td><a href="http://ecls.git.sourceforge.net/git/gitweb.cgi?p=ecls/ecl;a=blob;f=src/lsp/seqlib.lsp;h=4ad06d67fab06bb2263cc5b2ce54b8c0d6af09a6;hb=HEAD">ECL</a></td>
<td>merge sort (lists) / quicksort</td>
<td>quicksort (strings + bit vectors) / merge sort</td>
</tr>
<tr>
<td><a href="http://sbcl.git.sourceforge.net/git/gitweb.cgi?p=sbcl/sbcl.git;a=blob;f=src/code/sort.lisp;h=06cd2c6664d62c83d2ed7e2c28ae04b6b6478305;hb=refs/heads/master">SB</a><a href="http://sbcl.git.sourceforge.net/git/gitweb.cgi?p=sbcl/sbcl.git;a=blob;f=src/compiler/srctran.lisp;h=16accff9e535c3f8e03fccaf71884d8e1c3d8ae6;hb=refs/heads/master">CL</a></td>
<td>merge sort (lists) / heapsort</td>
<td>merge sort</td>
</tr>
</tbody>
</table>
<p>&nbsp;<br />
In terms of the implementation of sort, <a href="http://en.wikipedia.org/wiki/Quicksort">quicksort</a> is the most used algorithm, followed by <a href="http://en.wikipedia.org/wiki/Heapsort">heapsort</a>. The choice for these algorithms is expected. Both have an average-case performance of O(<em>n</em>lg<em>n</em>) and heapsort guarantees a worst-case performace of O(<em>n</em>lg<em>n</em>) too. Quicksort has a worst-case performance of O(<em>n<sup>2</sup></em>) but it can be optimized in several ways so that it also gives an expected worst-case performance of O(<em>n</em>lg<em>n</em>). However, it seems that the quicksort implementations are not completely optimized. In ECL (and ABCL) quicksort implements a partition scheme which deals better with duplicate elements (although is not the three-way partitioning) but it always picks as pivot the first element. CCL chooses the pivot with a median-of-3 method and always sorts the smaller partition to ensure a worst-case stack depth of O(lg<em>n</em>). </p>
<p>As for CLISP, I think it uses a <a href="http://en.wikipedia.org/wiki/Tree_sort">tree sort</a> but I am not entirely sure. The only source file I could find with a sort implementation was <a href="http://clisp.hg.sourceforge.net/hgweb/clisp/clisp/file/e789e5f15ae3/src/sort.d">sort.d</a> and it looks like it contains an implementation of tree sort with a self-balanced binary tree, which also gives this algorithm an average and worst-case performance of O(<em>n</em>lg<em>n</em>). </p>
<p>As expected, most of the implementations use <a href="http://en.wikipedia.org/wiki/Merge_sort">merge sort</a> to implement stable-sort since it is a stable sort with average and worst-case performance of O(<em>n</em>lg<em>n</em>). Apparently, all implementations are bottom-up merge sorts with the exception of CCL and ECL. Another interesting thing is that merge sort is also used for lists in sort, in most of the implementations. However, I found it surprising to find quicksort in the stable-sort column because it is not a stable algorithm. Since it is only used for strings and bit vectors, it is not really an issue. While reading the source code of the implementations, I realized that ABCL was using quicksort in stable-sort for all non-list sequences. This is a problem that exists in the current <a href="http://common-lisp.net/project/armedbear/releases/1.0.1/">1.0.1 release</a> but I&#8217;ve sent a <a href="http://trac.common-lisp.net/armedbear/ticket/196">bug report</a> <a href="http://trac.common-lisp.net/armedbear/changeset/13838">with a quick fix</a> to the maintainers. The next release should have stable-sort fixed.</p>
<p>This exploration of the sorting algorithms used in the open source implementations was very educational and interesting to me. I&#8217;ve learned what algorithms are actually used and enjoyed seing how they were implemented. Just spotting the issue in ABCL stable-sort made this review worthwhile. I think there is still room for improvement in some implementations but knowing now the strengths and weaknesses of the sorts in CL is already good enough. On a final note, I just wonder what are the algorithms used in <a href="http://www.franz.com/products/allegro-common-lisp/">ACL</a> and <a href="http://www.lispworks.com/products/lispworks.html">LW</a>.   </p>
<br />Filed under: <a href='http://jorgetavares.com/category/programming/'>Programming</a> Tagged: <a href='http://jorgetavares.com/tag/common-lisp/'>Common Lisp</a>, <a href='http://jorgetavares.com/tag/lisp/'>Lisp</a>, <a href='http://jorgetavares.com/tag/sorting-algorithms/'>Sorting Algorithms</a>, <a href='http://jorgetavares.com/tag/survey/'>Survey</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jorgetavares.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jorgetavares.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jorgetavares.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jorgetavares.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jorgetavares.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jorgetavares.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jorgetavares.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jorgetavares.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jorgetavares.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jorgetavares.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jorgetavares.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jorgetavares.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jorgetavares.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jorgetavares.wordpress.com/823/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jorgetavares.com&#038;blog=3353190&#038;post=823&#038;subd=jorgetavares&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jorgetavares.com/2012/02/02/sorting-algorithms-used-in-the-cl-implementations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/378de7e75b3ac59ec9cab927683c0904?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jast</media:title>
		</media:content>
	</item>
		<item>
		<title>Packages organization and exporting symbols</title>
		<link>http://jorgetavares.com/2011/11/08/packages-organization-and-exporting-symbols/</link>
		<comments>http://jorgetavares.com/2011/11/08/packages-organization-and-exporting-symbols/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 16:43:04 +0000</pubDate>
		<dc:creator>Jorge Tavares</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Common Lisp]]></category>
		<category><![CDATA[HyperSpec]]></category>
		<category><![CDATA[Packages]]></category>

		<guid isPermaLink="false">http://jorgetavares.com/?p=792</guid>
		<description><![CDATA[I&#8217;ve started to re-design my main library for evolutionary computation. One of the main things I did for the new version was a complete new organization of the packages (and respective files/modules). Before I had essentially two main packages, the library itself and the examples. Although simple, it became a pain to use this model [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jorgetavares.com&#038;blog=3353190&#038;post=792&#038;subd=jorgetavares&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started to re-design my main library for evolutionary computation. One of the main things I did for the new version was a complete new organization of the <a href="http://www.lispworks.com/documentation/HyperSpec/Body/11_aa.htm">packages</a> (and respective files/modules). Before I had essentially two main packages, the library itself and the examples. Although simple, it became a pain to use this model when I extended it heavily with more algorithms and related utilities. I hope I am not going now in the opposite direction (too complicated) but so far I like the new organization. </p>
<p>In short, there is a package for each main branch of algorithms (e.g., GA, GP) with everything specific that   kind, which imports from a core package with the common components. These &#8220;sub-packages&#8221; are gathered together in a single package (the main library package). This way, it is possible to use in a project everything or simply just the desired component (e.g., if you just want GP). Furthermore, an extra package for the users is also provided to allow <a href="http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a> experimentation without being on the library main package. </p>
<p>However, while implementing this scheme I realized that I wanted to have all the exported symbols from the packages that compose the library, also exported by the library main package. This way, all the symbols that compose the library are easily seen on the main package. For me this is very useful since it allows exploration of a library, especially if it has many things. Since I have never done something like this before, I went and search for a way to solve this minor problem in an easy way. </p>
<p>The answer is basically use <strong><a href="http://www.lispworks.com/documentation/HyperSpec/Body/m_do_sym.htm">do-external-symbols</a></strong>. With this macro you iterate over the exported symbols of a given package and then <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_export.htm">export</a> them again on the package you want. Do this inside an <a href="http://www.lispworks.com/documentation/HyperSpec/Body/s_eval_w.htm">eval-when</a> form and when the library is loaded and the main package will contain all the symbols. If *library-sub-packages* is a list with the packages labels that compose your library:</p>
<p><code>
<pre>
(eval-when (:compile-toplevel :compile-toplevel :execute)
  (dolist (package *library-sub-packages*)
    (do-external-symbols (symbol (find-package package))
      (export symbol (find-package *library-main-package*)))))
</pre>
<p></code></p>
<p>Making all the exported symbols of internal packages also exportable by the main package turn out to be an easy thing to do. I don&#8217;t recall seing do-external-symbols (or the <a href="http://www.lispworks.com/documentation/HyperSpec/Body/m_do_sym.htm">related macros</a>) but I&#8217;m glad such a macro is provided. As always, the <a href="http://www.lispworks.com/documentation/HyperSpec/Front/index.htm">HyperSpec</a> is your friend :-)</p>
<br />Filed under: <a href='http://jorgetavares.com/category/programming/'>Programming</a> Tagged: <a href='http://jorgetavares.com/tag/common-lisp/'>Common Lisp</a>, <a href='http://jorgetavares.com/tag/hyperspec/'>HyperSpec</a>, <a href='http://jorgetavares.com/tag/packages/'>Packages</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jorgetavares.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jorgetavares.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jorgetavares.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jorgetavares.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jorgetavares.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jorgetavares.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jorgetavares.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jorgetavares.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jorgetavares.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jorgetavares.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jorgetavares.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jorgetavares.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jorgetavares.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jorgetavares.wordpress.com/792/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jorgetavares.com&#038;blog=3353190&#038;post=792&#038;subd=jorgetavares&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jorgetavares.com/2011/11/08/packages-organization-and-exporting-symbols/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/378de7e75b3ac59ec9cab927683c0904?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jast</media:title>
		</media:content>
	</item>
	</channel>
</rss>
