<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Cognitive Consonance</title>
	
	<link>http://tiago.org/cc</link>
	<description>Software engineering in a computational biology environment</description>
	<lastBuildDate>Thu, 03 Dec 2009 11:32:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/CognitiveConsonance" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="cognitiveconsonance" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Lisp and Clojure: prefix notation and parentheses</title>
		<link>http://tiago.org/cc/2009/12/03/lisp-and-clojure-prefix-notation-and-parentheses/</link>
		<comments>http://tiago.org/cc/2009/12/03/lisp-and-clojure-prefix-notation-and-parentheses/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 11:32:47 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[prolog]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=205</guid>
		<description><![CDATA[I would like to add just a few more arguments regarding the issue of notation.
There is a certain austerity tradition coming from the CS community (at least from the BEST part of it). Most people see syntactic sugar as a waste of time and energy. Furthermore everything is just trees and a linear representation with [...]]]></description>
			<content:encoded><![CDATA[<p>I would like to add just a few more arguments regarding the issue of notation.</p>
<p>There is a certain austerity tradition coming from the CS community (at least from the BEST part of it). Most people see syntactic sugar as a waste of time and energy. Furthermore everything is just trees and a linear representation with the root at the head is just fine to represent trees. Lisp embodies this tradition to the full. And for the most part is a great tradition.</p>
<p>This is all fine, and I would understand the defensiveness considered that we live in a world polluted with overly complex languages which just bring problems and no advantages to compensate.</p>
<p>But some level of syntactic sugar is important for humans. And while meta-programming is nice, I suppose most programming languages are still used by human programmers <img src='http://tiago.org/cc/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  . </p>
<p>It would suggest to have a look at the literature on human cognition, namely the strategies used to read text: it is messy. But I prefer to go in a different direction. I just want to expose how some subtle representation issues might have a massive impact: Have you ever tried to do basic arithmetic with roman numerals? Have a look <a href="http://mathforum.org/dr.math/faq/faq.roman.html">here</a>, namely &#8220;How can I use Roman numerals to do arithmetic problems?&#8221;. While this is a different problem, it serves to emphasize that representation matters in the most strangest ways.</p>
<p>Even if it doesn&#8217;t, humans are a conservative species, they are not ready to change everything at the same time. Having a to jump to imperative+heteroiconic+infix to functional+homoiconic+prefix is too much. Lisp derived languages loose potential to attract more people. And in this case is is really worth it? Is it really THAT IMPORTANT to be prefixed? Is that the main conceptual selling point of Lisp?</p>
<p><b>But most importantly there are solutions that are not dirty:</b>.</p>
<p>First nobody is forcing infix notation, one can have both at the same time (check Prolog  :- op &#8211; you can do BOTH +(1 2) and 1+2).</p>
<p>Second hard-coding operators is not necessary at all: again check the Prolog suggestion. You can have all infix operators that you want (including none)</p>
<p>It is easy to conceptualize a superset of Lisp with infix notation and ZERO INITIAL infix operators.</p>
<p>If I can shout something is this: Check Prolog&#8217;s :- op ! Simple, elegant. And fully coherent with the Lisp philosophy at its core.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/iwgNlfHioQQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/12/03/lisp-and-clojure-prefix-notation-and-parentheses/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Clojure: “Being a Lisp Is a Handicap”</title>
		<link>http://tiago.org/cc/2009/12/02/clojure-being-a-lisp-is-a-handicap/</link>
		<comments>http://tiago.org/cc/2009/12/02/clojure-being-a-lisp-is-a-handicap/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 15:34:44 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[prolog]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=200</guid>
		<description><![CDATA[Tim Bray presented his 11 Theses on Clojure. Thesis number 2 is &#8220;Being a Lisp Is a Handicap&#8221;. This argument has actually 3 parts: the Lisp-parenthesis syntax, which he finds bad. Homoiconicity (he finds good) and macros (again good).
I would like to discuss a bit the first (syntax) and the last (macros). The macro part [...]]]></description>
			<content:encoded><![CDATA[<p>Tim Bray presented his <a href="http://www.tbray.org/ongoing/When/200x/2009/12/01/Clojure-Theses">11 Theses on Clojure</a>. Thesis number 2 is &#8220;Being a Lisp Is a Handicap&#8221;. This argument has actually 3 parts: the Lisp-parenthesis syntax, which he finds bad. Homoiconicity (he finds good) and macros (again good).</p>
<p>I would like to discuss a bit the first (syntax) and the last (macros). The macro part discussed somewhere in the future. For now I would like to concentrate on syntax.</p>
<p>Lisp syntax is strange for most humans, we simply do not write (+ (* 2 3) (/ 4 2)). Lisp proposers are probably aware that this will not be very popular, as people are used to something completely different. It is not only different but is also very uneconomical to both read and write: 2*3 + 4/2 is arguably easier to read. One can even make some intuitive declarative things. Notice that I&#8217;ve written 2*3 (no space around *) but 2*3 + 4*2 (spaces around +). This allows to give queues to readers about the precedence of operators. You can say that these issues are just simple and ridiculous syntactic sugar. Well, it seems that <i>Homo sapiens</i> has evolved to appreciate it. And I don&#8217;t really see a reason not to accommodate this requirement.</p>
<p>Yes, I read you already: infix syntax forces hard-coding of operators and precedences (e.g. + precedes *) . Makes parsing much more complex (there goes the beautifully simple Lisp parser out of the window) and in fact forcing a certain structure with hard-coded operators.</p>
<p>Well&#8230; no.</p>
<p>Take a look at Prolog <a href="http://www.cse.unsw.edu.au/~billw/cs9414/notes/prolog/op.html">op built-in</a>. :- op allows you to define operator precedence and association rules. So the parser is really not much complex: it starts with a simple, uber-flexible, blank slate which you can fill in.</p>
<p>So, for (- 4 3 2) you can say &#8211; is an infix operator with first argument with precedence over the second (i.e. (4 &#8211; 3) &#8211; 2) and, voila, you can write 4 &#8211; 3 &#8211; 2. No need for hard-coded operators, all dynamically defined. You could, maybe for sadistic reasons, change the argument precedence so 4 &#8211; 3 &#8211; 2 would become 4 &#8211; (3 &#8211; 2). The flexibility is there. </p>
<p>You can also state that + and &#8211; have higher precedence than  * and /. So that 2*3 + 2 is interpreted as (+ (* 2 3) 2). Again, you can change the precedence rules.</p>
<p>Ah&#8230; and you can have operators that are atoms (alphanumeric symbols). In Prolog you can say &#8220;X is 3 +2&#8243;. &#8220;is&#8221; is an operator. You can redefine operator rules, define new operators.</p>
<p>Operators are nothing more than a set of syntactic sugar rules to convert a more intuitive representation to another. But they really make things much more pleasant and humane.</p>
<p>I would imagine that adding this to Clojure would be DEAD EASY, by the way. </p>
<p>Regarding the issues about macros, I will leave it for another post.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/a81loz5yvgk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/12/02/clojure-being-a-lisp-is-a-handicap/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Brain to symbol impedance</title>
		<link>http://tiago.org/cc/2009/11/19/brain-to-symbol-impedance/</link>
		<comments>http://tiago.org/cc/2009/11/19/brain-to-symbol-impedance/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 18:31:59 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=157</guid>
		<description><![CDATA[What is the importance of the names that are given to things when programming? There seem to be quite some different approaches to this issue. Some people think that calling data items/variables a, b1, c is OK. Less people are seen defending the idea that functions should be called f1, f2, f2. Some other people [...]]]></description>
			<content:encoded><![CDATA[<p>What is the importance of the names that are given to things when programming? There seem to be quite some different approaches to this issue. Some people think that calling data items/variables a, b1, c is OK. Less people are seen defending the idea that functions should be called f1, f2, f2. Some other people think variables should have names connected with their meaning, so, if you have a data item whose content is related to a person object, maybe that variable should be called something with &#8220;person&#8221; on its name, like currentPerson, or whatever.</p>
<p>So far, nothing new. I for one, am in the group of the explicit naming of everything.</p>
<p>Sometimes the name has some impedance between the average expectation and the functionality exposed. That can be a source of lots of grief. Lets have a look at a concrete example to make things clear.</p>
<p>Clojure has a function named contains? . How do you expect such a function to operate? Let me give a few examples (and if you know how it really operates in reality, try to forget for now what you know).</p>
<p>What you think would be the result of:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>contains? <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>contains? <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>contains? <span style="color: #ff0000;">&quot;bla&quot;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>contains? <span style="color: #ff0000;">&quot;bla&quot;</span> <span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>contains? '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Does [3 5] contains 3?<br />
Does [3 5] contains 1?<br />
Does &#8220;bla&#8221; contains 1?<br />
Does &#8220;bla&#8221; contains &#8220;a&#8221;?<br />
Does (1 2) contains 1?</p>
<p>Actually (contains? [3 5] 3) is false. Why? Lets have a look at the documentation:</p>
<pre>
clojure.core/contains?
([coll key])
Returns true if key is present in the given collection, otherwise
returns false.  Note that for numerically indexed collections like
vectors and Java arrays, this tests if the numeric key is within the
range of indexes. [...]
</pre>
<p>So contains? is looking at the KEYS of a collection. The vector [3 5] has two keys: 0 and 1 (the indexes with occupation of the said vector), that is why (contains? &#8220;bla&#8221; 1) is true (the string &#8220;bla&#8221; has 3 keys 0, 1 and 2.</p>
<p>By the way, the list (1 2) is not a collection, so that false is caused by a type error.</p>
<p>Note that contains? actually has some utility <img src='http://tiago.org/cc/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  : it is used to check if a map has a certain key.</p>
<p>There is an slight impedance between what most people would expect from &#8220;containing something&#8221; and contains? It so happens that slight impedances are much worse than big ones, because big ones are so damn obvious that they are easy to spot and normally end up being corrected (imagine a function drawLine, that draws a circle: it is a dead obvious problem that one will easily notice and the developer most probably correct). Slight impedance problems are more obnoxious:</p>
<ul>
<li>They will confuse newbies (like myself), whose expectations will be aligned with the general knowledge of a certain name or action.
  </li>
<li>They will increase the cognitive load of experts which will need to be aware of the dissonance between the meaning of a certain word in the programming language and the meaning in the natural language.
  </li>
<li>It will make reading and understanding a program more difficult.
  </li>
<li>They will be a source of bugs, as developers, even experienced one, will sometimes forget the local meaning of a word and unconsciously apply the general meaning.
</li>
</ul>
<p>By the way, my personal solution for contains? Well, quite simply rename it hasKey? and only apply it to maps. Note that this post is not about contains? in particular, but about this &#8220;impedance&#8221; problem in general.</p>
<p>I would imagine that some readers might think this as irrelevant and nitpicking. In my subjective and personal point of view, this issue is a potential source of hard to find bugs and a waste of mental energy. In fact this blog is called Cognitive _Consonance_ for some reason&#8230;</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/CacKTgyn8KI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/19/brain-to-symbol-impedance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading newick/nexus phylogenetic trees with BioJava</title>
		<link>http://tiago.org/cc/2009/11/17/reading-newicknexus-phylogenetic-trees-with-biojava/</link>
		<comments>http://tiago.org/cc/2009/11/17/reading-newicknexus-phylogenetic-trees-with-biojava/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 18:02:11 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[biojava]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[phylogenetics]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=183</guid>
		<description><![CDATA[BioJava has a parser for the nexus file format. While Nexus supports quite a lot of information (like DNA sequences), the most complex part to process is the phylogenetic tree descriptions based on the Newick format. Below you can find a small tutorial on how to process these.
BioJava&#8217;s parser relies on JGraphT to create a [...]]]></description>
			<content:encoded><![CDATA[<p>BioJava has a parser for the <a href="http://hydrodictyon.eeb.uconn.edu/eebedia/index.php/Phylogenetics:_NEXUS_Format">nexus file format</a>. While Nexus supports quite a lot of information (like DNA sequences), the most complex part to process is the phylogenetic tree descriptions based on the <a href="http://en.wikipedia.org/wiki/Newick_format">Newick format</a>. Below you can find a small tutorial on how to process these.</p>
<p>BioJava&#8217;s parser relies on <a href="http://jgrapht.sourceforge.net/">JGraphT</a> to create a representation of the &#8220;tree&#8221;. The tree is actually really more an acyclic graph than a tree, though some trees are rooted (and therefore trees in the proper sense). Manipulating the JGraphT weighted graph is the complicated part, not really the BioJava interface. Note that JGraphT objects can be easily rendered using the <a href="http://www.jgraph.com/jgraph.html">JGraph</a> library (yeah, it is confusing: there is one lib with graph algorithms called JGraphT and another, for vizualisation, called JGraph).</p>
<p>In this small tutorial, we will only try to write a textual representation of a tree.</p>
<p>Imagine this simple nexus file:</p>
<pre>
#NEXUS

Begin TREES;
	tree test1 = (1,2);
	tree test4 = (1:0.1,(2:0.2,3:0.3):0.4);
End;
</pre>
<p>We just want to draw this:</p>
<pre>
coder@move-on:~/development/biobug/test$ java Test test1.nex test1
Will process file test1.nex tree test1
p0
  1: 1.0
  2: 1.0
coder@move-on:~/development/biobug/test$ java Test test1.nex test4
Will process file test1.nex tree test4
p0
  1: 0.1
  p1: 0.4
    2: 0.2
    3: 0.3
</pre>
<p>So, tree 1 is composed of nodes (leaves) 1 and 2 and the inner node which was named p1. Tree 2 has distances.</p>
<p>By the way, we will also want to know which trees are in the file.</p>
<p>Lets start!.</p>
<p>So, we start by loading and parsing the file:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojavax.bio.phylo.io.nexus.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#91;</span>...<span style="color: #009900;">&#93;</span>
        <span style="color: #666666; font-style: italic;">//file is a String with the name of the file to be processed</span>
        NexusFileBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NexusFileBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        NexusFileFormat.<span style="color: #006633;">parseFile</span><span style="color: #009900;">&#40;</span>builder, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        NexusFile nexus <span style="color: #339933;">=</span> builder.<span style="color: #006633;">getNexusFile</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Nexus files have several blocks (Taxa, Data, Tree, Set). We are interested in getting the Tree block, lets do a function for that:</p>
<pre language="java">
    TreesBlock getTreeNode(NexusFile nexus) {
        Iterator it = nexus.blockIterator();
        NexusBlock block;
        while(it.hasNext()) {
                block = (NexusBlock)it.next();
                if (block.getBlockName().equals("TREES")) {
                        return (TreesBlock)block;
                }
            }
            return null;
    }
</pre>
<p>We get the nexus block iterator and go through it until we find a block whose name is TREES, and return that block.</p>
<p>No that we have the TREES block, lets print the names of all trees:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000066; font-weight: bold;">void</span> printTrees<span style="color: #009900;">&#40;</span>NexusFile nexus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            TreesBlock node <span style="color: #339933;">=</span> getTreeNode<span style="color: #009900;">&#40;</span>nexus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Map</span> trees <span style="color: #339933;">=</span> node.<span style="color: #006633;">getTrees</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Set</span> keys <span style="color: #339933;">=</span> trees.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Trees:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj <span style="color: #339933;">:</span> keys<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>We get a map, where the key is the name of the tree and the value would be the tree as, essentially, a String based (but with some annotated info) representation (not  a graph). Now, given a certain name, lets get the graph:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojava.bio.seq.io.ParseException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jgrapht.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jgrapht.graph.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#91;</span>...<span style="color: #009900;">&#93;</span>
&nbsp;
    WeightedGraph<span style="color: #339933;">&lt;</span>string , DefaultWeightedEdge<span style="color: #339933;">&gt;</span> getTree<span style="color: #009900;">&#40;</span>NexusFile nexus, <span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">ParseException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> topNode<span style="color: #339933;">;</span>
        TreesBlock node <span style="color: #339933;">=</span> getTreeNode<span style="color: #009900;">&#40;</span>nexus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        WeightedGraph<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;&lt;</span>string , DefaultWeightedEdge<span style="color: #339933;">&gt;</span> graph <span style="color: #339933;">=</span> node.<span style="color: #006633;">getTreeAsWeightedJGraphT</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        topNode <span style="color: #339933;">=</span> node.<span style="color: #006633;">getTopNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The top node is: &quot;</span> <span style="color: #339933;">+</span> topNode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> graph<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Note that getTreeAsWeightedJGraphT will do some parsing, so ParsingException can be raised. Note also that the top node name can be retrieved (in the case of tree test1, that will be named p1). Some considerations: You can change the rules to create internal nodes; if there are clashes of names inner nodes will be renamed (not leaves!).</p>
<p>Regarding the top node, we call it top node and not root node. While from a data structure perspective the tree has a root, from a phylogenetic perspective the tree might be rooted or not (in which case being root has no meaning, and it is really just a simple weighted acyclic graph). How to know if the tree is rooted? Remember the function to get all trees (getTrees)? The value of the map has a method called getRootType. So, to know if is rooted, you need to use that function. Not the best design&#8230; but at least works.</p>
<p>Ok, now we just need to print a tree&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> dump<span style="color: #009900;">&#40;</span>WeightedGraph<span style="color: #339933;">&lt;</span>string , DefaultWeightedEdge<span style="color: #339933;">&gt;</span> graph,
            <span style="color: #003399;">String</span> parent, <span style="color: #003399;">String</span> node, <span style="color: #003399;">String</span> depth<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Set<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;&lt;</span>string<span style="color: #339933;">&gt;</span> verts <span style="color: #339933;">=</span> graph.<span style="color: #006633;">vertexSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> vertex <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> candidate <span style="color: #339933;">:</span> verts<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>candidate.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>node<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                vertex <span style="color: #339933;">=</span> candidate<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span> <span style="color: #009900;">&#40;</span>depth <span style="color: #339933;">+</span> vertex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>parent <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">+</span> graph.<span style="color: #006633;">getEdgeWeight</span><span style="color: #009900;">&#40;</span>graph.<span style="color: #006633;">getEdge</span><span style="color: #009900;">&#40;</span>parent, vertex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>DefaultWeightedEdge e<span style="color: #339933;">:</span> graph.<span style="color: #006633;">edgesOf</span><span style="color: #009900;">&#40;</span>vertex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>graph.<span style="color: #006633;">getEdgeSource</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>node<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                dump<span style="color: #009900;">&#40;</span>graph, vertex, graph.<span style="color: #006633;">getEdgeTarget</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;  &quot;</span><span style="color: #339933;">+</span> depth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> 
<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Ok, this is the complicated part. Note the following:</p>
<ul>
<li>The complexity has to do with processing graphs</li>
<li>dump is a recursive function</li>
<li>Node is synonym with Vertex</li>
<li>Notice the <strong>important bit</strong>, if you know that there is a node called &#8220;bla&#8221;, it is not enough to do graph.containsVertex(&#8220;bla&#8221;). The answer will probably be false. Remember that one thing is reference (which we have here, i.e. ==) and not content equality (.equals). See below, a remainder</li>
<li>Finally we go through all edges referencing the current vertex and choose the ones that start on the current one. Again, if the tree is unrooted, the notion of direction does not apply, but it is still good to do a &#8220;tree&#8221; traversal</li>
</ul>
<p>We end here.</p>
<p>Regarding the &#8220;equal&#8221; issue remember that:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Returns false, true. By this order. This is important when traversing the graph. If you know that the reference is equal (and it is when we getEdgeTarget) than one could use it. If you don&#8217;t know (like you pass a String that you have constructed yourself or got from some other place), then one needs to go through the vertex/node list and do a .equals to get the correct vertex.</p>
<p>A small example with all the above, <a href="http://tiago.org/cc/wp-content/uploads/2009/11/Test.java">is here</a>, ready to use.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/kLEOBIXNQJg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/17/reading-newicknexus-phylogenetic-trees-with-biojava/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A phylogenetic tree viewer in clojure</title>
		<link>http://tiago.org/cc/2009/11/16/a-phylogenetic-tree-viewer-in-clojure/</link>
		<comments>http://tiago.org/cc/2009/11/16/a-phylogenetic-tree-viewer-in-clojure/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 19:44:11 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[phylogenetics]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=160</guid>
		<description><![CDATA[Here is my first clojure application: A phylogenetic tree viewer (PhyloXML format). The obligatory screenshot:
Preamble:

This is newbie code: Handle with care! My main objective is not to make a tree viewer but a tree comparer. So this is no more than a learning step.
You can test it yourself as it is a Java WebStart application, [...]]]></description>
			<content:encoded><![CDATA[<p>Here is my first clojure application: A phylogenetic tree viewer (PhyloXML format). The obligatory screenshot:</p>
<div id="attachment_172" class="wp-caption alignnone" style="width: 461px"><img src="http://tiago.org/cc/wp-content/uploads/2009/11/Screenshot-Simple.png" alt="Simple Phylogenetic viewer" title="Simple" width="451" height="480" class="size-full wp-image-172" /><p class="wp-caption-text">Simple Phylogenetic viewer</p></div>
<p>Preamble:</p>
<ol>
<li>This is newbie code: Handle with care! My main objective is not to make a tree viewer but a tree comparer. So this is no more than a learning step.</li>
<li>You can test it yourself as it is a Java WebStart application, just click <a href="http://popgen.eu/soft/tV/launch.jnlp">here</a>. You don&#8217;t need to have a phylogenetic tree file yourself. I supply an example inside.
         </li>
<li>This makes use of JGraph and Archaeopteryx (the PhyloXML parser)
</li>
</ol>
<p>I do maintain this code on github. I have <a href="http://github.com/tiagoantao/treeHugger">one project for the viewer</a> and <a href="http://github.com/tiagoantao/cljUtils">another for general utilities</a>. All the code is still very crude, but you might be interesting in stealing some of the <a href="http://github.com/tiagoantao/cljUtils/blob/master/src/org/tiago/swing.clj">swing code</a>, either as a crude example of how to interact with swing or taking my <a href="http://tiago.org/cc/2009/10/27/swing-menus-in-clojure/">micro-DSL for menus</a>. If you want to interact with JGraph, <a href="http://github.com/tiagoantao/cljUtils/blob/master/src/org/tiago/graph_vis.clj">this</a> might be a starting point. I don&#8217;t want, in any way, suggest that this code is any good. </p>
<p>Some lessons that I&#8217;ve learned and that I would like to share:</p>
<ol>
<li>Some of the clojure.contrib code is a bit green. I tried to use the graph library, but it is very small and specific. I ended up starting doing my own. Mine is even smaller and specific, no claims of generality.</li>
<li>I don&#8217;t appreciate some of the core functions of Clojure (I&#8217;ve written on this before and will write more in the near future). The great thing about Clojure is that you can import only what you want from the core and extend it yourself. I intend to do just that for my personal use. This is a PLUS point for Clojure: the flexibility that is made available to change many of the decisions of the language implementor (in the great tradition of declarative and homoiconic languages)
  </li>
<li>While I can change the core for my uses, I think defnk should really be core for everybody! I fact I wander if defn should not become defnk&#8230;</li>
<li>I am pretty sure that when *warn-on-reflection* is activated and action taken to correct the warnings, lots of code will increase in performance. With the more important side effect of annotating the code with type info.</li>
<li>I have quite a lot of recursive code that doesn&#8217;t use recur. Something to learn and master&#8230;</li>
<li>JGraph layout algorithms are not fantastic. I&#8217;ve tried with much bigger trees and the result was far from perfect (I also noticed performance problems in my own code).</li>
</ol>
<p>The biggest hurdle that I&#8217;ve found was the construction of user interfaces and how verbose Clojure Java interop can become. Of course one can create functions (and that was done) to create buttons, frames, menus, etc. But the creation of Java container structures (think frame contains menubar which contains menu with menus inside and so on) would benefit from a dialect where, when a certain (container) object was created it&#8217;s (Java) namespace would become easily available.</p>
<p>Imagine constructing a Structure like this:</p>
<pre>
MenuBar[
    Menu(File) [
        Menu(New)
        Menu(Close)
        Separator
        Menu
    ]
    Menu(Edit) [
        Menu(Cut)
        Menu(Paste)
    ]
]
</pre>
<p>it would be nice to be able to write something like:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>new JMenuBar<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;File&quot;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;New&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;Close&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>addSeparator<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;Edit&quot;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;Cut&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;Paste&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>&#8220;add&#8221; and &#8220;addSeparator&#8221; are Java methods. All this would be dynamic against the Java object hierarchy (not a hand-written library!). Note that there is no doto special form (or variants) and, most importantly, note that, given a list (a b c d), if a is a Java object b c d are evaluated as methods of a. If b is (i (y x s)), x and s would be evaluated as methods of y, if they failed then as methods of a, if this fails interpreted as normal Clojure. Something like this (rough sketch).<br />
This would be useful, e.g., to construct Swing hierarchies by hand in a expedient way (not suggesting anything more, especially not to do big programs with outside scope).<br />
I am going to try to write some code that does this in the next few days.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/Ks2VOeaWiYc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/16/a-phylogenetic-tree-viewer-in-clojure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The 99 Prolog problems in Clojure</title>
		<link>http://tiago.org/cc/2009/11/11/the-99-prolog-problems-in-clojure/</link>
		<comments>http://tiago.org/cc/2009/11/11/the-99-prolog-problems-in-clojure/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 12:33:45 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[prolog]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=167</guid>
		<description><![CDATA[Here can be found an interesting effort to implement the 99 Prolog problems in Clojure.
It is not clear to me that the exercise is conducted in the same way as the original one [Update: the author actually says this in the preamble]. Let me explain:
The original Prolog problems are solved without the help of the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://m.3wa.com/?p=426">Here</a> can be found an interesting effort to implement the <a href="https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/">99 Prolog problems</a> in Clojure.</p>
<p>It is not clear to me that the exercise is conducted in the same way as the original one [Update: the author actually says this in the preamble]. Let me explain:</p>
<p>The original Prolog problems are solved without the help of the (existing) Prolog libraries, just using the basic language mechanisms. They are a good at illustrating the underlying declarative power of Prolog.</p>
<p>For instance, problem 1, finding the last element of a list is solved with this in Prolog:</p>

<div class="wp_syntax"><div class="code"><pre class="prolog" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">% P01 (*): Find the last element of a list</span>
&nbsp;
<span style="color: #666666; font-style: italic;">% my_last(X,L) :- X is the last element of the list L</span>
<span style="color: #666666; font-style: italic;">%    (element,list) (?,?)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">% Note: last(?Elem, ?List) is predefined</span>
&nbsp;
my<span style="color: #008080;">_last</span><span style="color: #009900;">&#40;</span><span style="color: #008080;">X</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #008080;">X</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
my<span style="color: #008080;">_last</span><span style="color: #009900;">&#40;</span><span style="color: #008080;">X</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #008080;">_</span><span style="color: #339933;">|</span><span style="color: #008080;">L</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:-</span> my<span style="color: #008080;">_last</span><span style="color: #009900;">&#40;</span><span style="color: #008080;">X</span><span style="color: #339933;">,</span><span style="color: #008080;">L</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span></pre></div></div>

<p>Notice the in-code comment that &#8220;last is predefined&#8221;. In fact, using the Prolog library this could be done with a one liner:</p>

<div class="wp_syntax"><div class="code"><pre class="prolog" style="font-family:monospace;"><span style="color: #339933;">?-</span> last<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #800080;">1</span><span style="color: #339933;">,</span><span style="color: #800080;">2</span><span style="color: #339933;">,</span><span style="color: #800080;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #008080;">E</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
<span style="color: #008080;">E</span> <span style="color: #339933;">=</span> <span style="color: #800080;">3.</span></pre></div></div>

<p>The offered solution in Clojure is also a one liner:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">last</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">8</span></pre></div></div>

<p>Given a sufficiently large and clever library (and Clojure has a very nice library) all problems on the list could be solved with a one-liner.</p>
<p>In my opinion, an apples-to-apples comparison with the original solutions would not use the core library.<br />
It would probably be like this for the same problem:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn mylast <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">l</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>mynext <span style="color: #66cc66;">&#40;</span>next <span style="color: #b1b100;">l</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil</span>? mynext<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>first <span style="color: #b1b100;">l</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>mylast mynext<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Yep, next is in the core library also, but being a call to clojure.lang.RT, I think it is fair game to use it.</p>
<p>Ok, better yet, with recur, as it is on core (this is essentially a copy of the core version):</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn mylast <span style="color: #66cc66;">&#91;</span>s<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>next s<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>recur <span style="color: #66cc66;">&#40;</span>next s<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>first s<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The Prolog exercise exposes the declarativeness and expressive power of Prolog. The Clojure example exposes mostly the cleverness of the core library.</p>
<p>Both are interesting points of view (I am not criticizing the Clojure solution), but they cannot be used for comparison purposes.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/VtxyZ06DXMs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/11/the-99-prolog-problems-in-clojure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using E220 and E620 data cards with Ubuntu 9.10</title>
		<link>http://tiago.org/cc/2009/11/10/using-e220-and-e620-data-cards-with-ubuntu-9-10/</link>
		<comments>http://tiago.org/cc/2009/11/10/using-e220-and-e620-data-cards-with-ubuntu-9-10/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 19:20:38 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=161</guid>
		<description><![CDATA[If you have mobile Internet via E220 and E620 Huawei dongles (Vodafone in Europe uses a lot of E620s and Three in the UK seems to have E220s at least), the new Ubuntu 9.10 (Karmic Koala) is bound to have some problems. The modem in the dongle won&#8217;t work (it will instead load the driver [...]]]></description>
			<content:encoded><![CDATA[<p>If you have mobile Internet via E220 and E620 Huawei dongles (Vodafone in Europe uses a lot of E620s and Three in the UK seems to have E220s at least), the new Ubuntu 9.10 (Karmic Koala) is bound to have some problems. The modem in the dongle won&#8217;t work (it will instead load the driver for the small memory stick disk that is attached). Note that this is strictly not an Ubuntu problem, it is a Linux kernel problem and probably other distributions will be affected as long as they have recent kernels.</p>
<p>How to solve this? Two options:</p>
<ol>
<li>Install the kernel offered <a href="https://bugs.launchpad.net/ubuntu/+source/linux/+bug/446146/comments/15">here</a>. This will load the driver for the memory stick AND the modem one. Good enough. Some computers might have a problem with this. For instance, a version previously offered was not able to recognize my touchpad. It this works for you, it is probably the best solution.</li>
<li>Do <a href="https://bugs.launchpad.net/ubuntu/+source/linux/+bug/446146/comments/47">this</a>. The problem with this one is that it is very tricky, especially is Ubuntu tries to automount the disk (which it does). Do like this:
<ol>
<li>Do the rmmod before installing the dongle. It should either remove the driver or, most probably say it is not loaded &#8211; both are good. If it is loaded this means that is is in use. If you cannot find what is making it being loaded then you are stuffed.</li>
<li>Now the tricky (and stinky) part: Connect the dongle and start issuing, on the command line lots of rmmod. The module will load, you are trying to unload it before the automount kicks in. You will notice that most rmmods will say that the module is not loaded, but then one will say that the module was removed. For some reason the module is loaded twice, so you have to see the removal twice (most of the times).</li>
<li>If the automount kicks in before you do rmmod then remove the dongle do rmmod and repeat the steps above.</li>
<li>Do the modprobe</li>
</ol>
</li>
</ol>
<p>A less stinky solution (this one stinks as hell) might be to disable automount, but then legitimate uses for the automount (memory sticks and such) would be problematic. If you know of a better one, feel free to comment here or give a pointer&#8230;</p>
<p>OK, now were are done.</p>
<p>Well, not really. When establishing a connection sometimes Ubuntu (this is probably just a Karmic problem, not related with the kernel) does not pick up the DNS info. So you connect, but cannot use the connection. Just disconnect and reconnect. You might need to do this more than once, but it will eventually pick the DNS info.</p>
<p>Hope this helps.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/7h-akOp4wG4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/10/using-e220-and-e620-data-cards-with-ubuntu-9-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tweaking clojure</title>
		<link>http://tiago.org/cc/2009/11/10/tweaking-clojure/</link>
		<comments>http://tiago.org/cc/2009/11/10/tweaking-clojure/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 00:19:26 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=150</guid>
		<description><![CDATA[There is, in my view, an inconsistency with the clojure core API with regards to type checking.
Consider the two functions contains? and even?
contains? returns true if a certain collection has a certain key:

=&#62; &#40;contains? &#123;'a 1&#125; 'a&#41;
true
=&#62; &#40;contains? &#123;'a 1&#125; 'b&#41;
false

If you pass an object which is not a collection, contains? silently returns false.

=&#62; &#40;contains? [...]]]></description>
			<content:encoded><![CDATA[<p>There is, in my view, an inconsistency with the clojure core API with regards to type checking.</p>
<p>Consider the two functions contains? and even?</p>
<p>contains? returns true if a certain collection has a certain key:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>contains? <span style="color: #66cc66;">&#123;</span>'a <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#125;</span> 'a<span style="color: #66cc66;">&#41;</span>
true
<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>contains? <span style="color: #66cc66;">&#123;</span>'a <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#125;</span> 'b<span style="color: #66cc66;">&#41;</span>
false</pre></div></div>

<p>If you pass an object which is not a collection, contains? silently returns false.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>contains? <span style="color: #cc66cc;">1</span> 'a<span style="color: #66cc66;">&#41;</span>
false</pre></div></div>

<p>I.e., a type error is not distinguishable from a collection which does not contain a certain element.</p>
<p>even? , the function to check if a certain number is, well, even, behaves in a completely different fashion:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>even? 'a<span style="color: #66cc66;">&#41;</span>
java<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>ClassCastException<span style="color: #66cc66;">:</span> <span style="color: #555;">clojure</span><span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span><span style="color: #b1b100;">Symbol</span> cannot be cast to java<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>Number <span style="color: #66cc66;">&#40;</span>NO_SOURCE_FILE<span style="color: #66cc66;">:</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>A type error on the parameter raises an exception with even?.</p>
<p>From a design philosophy I really do not like this inconsistent behavior: For the same type of error (a typical error pattern) the API behaves in a clearly different way.</p>
<p>The 2 points that are important (but, alas, are not the fundamental issue of this post) are:</p>
<ul>
<li>Core functions should have a coherent and consistent way of dealing with type errors. This is the most important point.
</li>
<li>If they have a consistent way of dealing with type errors, my preference would be for a behavior like even? (ie, throw) and not like contains?.
</li>
</ul>
<p>Yes, I do understand that other reasons might have taken precedence (like performance). I still don&#8217;t like it.</p>
<p>But the beauty of clojure is that one can redefine these &#8220;core&#8221; functions. For instance, I prefer to have</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn contains? <span style="color: #66cc66;">&#91;</span>coll key<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>coll? coll<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>clojure<span style="color: #66cc66;">.</span>core/contains? coll key<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">throw</span> <span style="color: #66cc66;">&#40;</span>new ClassCastException
                     <span style="color: #66cc66;">&#40;</span>str <span style="color: #66cc66;">&#40;</span>type coll<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot; cannot be cast as collection&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>[Newbie alert: there might be better ways to design a function like this (suggestions welcome).]</p>
<p>Now, one as to be careful not to import the original contains? into a namespace. Easy done in clojure:</p>
<pre lang="">
(ns myuser (:refer-clojure :exclude [contains?]))
</pre>
<p>This has to be done before the definition of the new contains? (note that, when calling the old contains? it includes the full namespace).</p>
<p>Of course, redefining core functions (even if inside namespaces not seen outside) is a bit like laying a mine field and probably has to be done with care. Irrespective of that, it is good to be able to have a language which allows one to express itself with the syntax and semantics that one desires, and not to be constrained to the whims of the original developer.</p>
<p>So far, no big problems found with Clojure (at least until now).</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/-v_IFw8rTkY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/10/tweaking-clojure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More clojure N00b tips</title>
		<link>http://tiago.org/cc/2009/11/04/more-clojure-n00b-tips/</link>
		<comments>http://tiago.org/cc/2009/11/04/more-clojure-n00b-tips/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 16:16:20 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=139</guid>
		<description><![CDATA[Having read Tim Bray&#8217;s tips for Clojure newbies from a newbie, I decided to write a version of my own.
First, I generally agree with Tim&#8217;s observations, so this post is more like minor extension. In fact I recommend reading Tim&#8217;s version first.
My background so that you know my point of view: Added to Java, C [...]]]></description>
			<content:encoded><![CDATA[<p>Having read Tim Bray&#8217;s <a href="http://www.tbray.org/ongoing/When/200x/2009/11/03/Clojure-N00b-Advice">tips for Clojure newbies from a newbie</a>, I decided to write a version of my own.</p>
<p>First, I generally agree with Tim&#8217;s observations, so this post is more like minor extension. In fact I recommend reading Tim&#8217;s version first.</p>
<p>My background so that you know my point of view: Added to Java, C and Python experience I have lots of exposure to Prolog (which is, like Clojure, homoiconic) and some experience with OCaml (a functional language). I have had some practical contact with other &#8220;modern languages over JVM&#8221;: Scala and Groovy. No Lisp experience (other than basic elisp).</p>
<p>Reading suggestions: Other than Tim&#8217;s comments I would add: The clojure site information is really not very useful as a starting point, the reference part assumes that you already know quite a bit and it is though on newbies, the only thing that I tend to use is the <a href="http://clojure.org/api">API section</a>. Like Tim, I also use Mark Volkmann&#8217;s <a href="http://java.ociweb.com/mark/clojure/article.html">introduction</a> (It is my main documentation, and I would be a bit more positive than Tim about it. For an intro article it is great. I strongly recommend it as a starting point and as the reading anchor during the first weeks). Next week I plan to order Stuart Halloway&#8217;s book, so I still cannot comment on that.</p>
<p>On a more general note, while learning Clojure, I&#8217;ve found Paul Graham&#8217;s <a href="http://www.paulgraham.com/onlisp.html">On Lisp</a> (available for free), a gem and I would strongly recommend it. It is not an easy read, it probably takes months to digest the content. But it is really a great book.</p>
<p>Clojure is a fast moving target. Documentation of many modules and functions might not be up to date with the current code. I have noticed that sometimes the best &#8220;documentation&#8221; ends up being reading the code (clojure is hosted on github and so are many satellite projects &#8211; In fact getting familiar with github is probably another recommendation).</p>
<p>Some of the contrib stuff is a bit too green, and I would recommend inspection of some of the modules before using them. Just because it is accepted on clojure.contrib it does not mean it is production quality, has even bare functionality or the exposed API is stable. As an example the <a href="http://richhickey.github.com/clojure-contrib/graph-api.html">graph API</a> is minimal and I question if the graph structure directed-graph is enough to represent a general directed graph (future changes to it will probably break existing code built on top of it). This is not a criticism, a new product is bound to be fast changing, and agile methods of development will entail lots of instability at the beginning. But a caveat should be added to the stability and completeness of parts of clojure-contrib.</p>
<p>Regarding editors and IDEs, I would probably recommend for you to stick with what you feel more comfortable with (vi, emacs, netbeans, eclipse, &#8230;). Note that Clojure, being a Lisp derivative has a big share of its user base on emacs. I mainly use Netbeans using the wonderful <a href="http://www.enclojure.org/">enclojure</a> plug-in. I can only say that encloure is stable enough for usage and I recommend it if you are a Netbeans type of person.</p>
<p>I also second Tim&#8217;s comments on namespace hell. Uses/imports/requires can be quite confusing. Hell is too harsh of a word, but purgatory seems an accurate description <img src='http://tiago.org/cc/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  . Also, as Tim says, the clojure mailing list and IRC channels are very, very helpful.</p>
<p>One of the most annoying kinds of bugs come from typing problems, things like this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn hiddenBug <span style="color: #66cc66;">&#91;</span>a b<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>println a<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;lets do a println for debug purposes</span>
  <span style="color: #66cc66;">&#40;</span>println b<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;lets do a println for debug purposes</span>
  <span style="color: #66cc66;">&#40;</span>if-<span style="color: #b1b100;">not</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> a b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;they are different!&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now lets call this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>hiddenBug 'x <span style="color: #ff0000;">&quot;x&quot;</span><span style="color: #66cc66;">&#41;</span>
x
x
they are different<span style="color: #66cc66;">!</span></pre></div></div>

<p>Notice that, when you are debugging a and b will <i>seem</i> equal on a println, but they are not (one is a symbol, another a string)!</p>
<p>The biggest gotcha that I have been getting is the expectation (Java based) that stupid things raise exceptions, but sometimes they don&#8217;t. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>contains? '<span style="color: #66cc66;">&#40;</span>a b<span style="color: #66cc66;">&#41;</span> 'a<span style="color: #66cc66;">&#41;</span>
false
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>contains? 'blab 'a<span style="color: #66cc66;">&#41;</span>
false
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>contains? <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'a 'b<span style="color: #66cc66;">&#41;</span> 'a<span style="color: #66cc66;">&#41;</span>
false
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>contains? <span style="color: #66cc66;">&#91;</span>'a 'b<span style="color: #66cc66;">&#93;</span> 'a<span style="color: #66cc66;">&#41;</span>
false
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>contains? '<span style="color: #66cc66;">&#40;</span>a b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">a</span><span style="color: #66cc66;">&#41;</span>
false
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>contains? '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">a</span> b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">a</span><span style="color: #66cc66;">&#41;</span>
false</pre></div></div>

<p>In all these cases, the Java-expecting gnome inside me was hoping something of a throw (as the first argument is not of the type required by contains? ) . It is not clear to me if this is a design issue with contains? only, or it is something that is standard along the whole API. But I notice this from the API reference:</p>
<pre>
(even? n)
Returns true if n is even, throws an exception if n is not an integer
</pre>
<p>So it seems that the design is not homogeneous throughout the API as some functions throw exceptions. I would probably prefer a throw when the type of arguments is wrong, but people with lots of Lisp experience might have a different view on the issue. Anyway I would like to understand if the lack of homogeneity is a feature or a bug.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/BDkQvAadKp8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/04/more-clojure-n00b-tips/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Concurrency and multicore CPU/GPUs are overhyped</title>
		<link>http://tiago.org/cc/2009/11/02/concurrency-and-multicore-cpugpus-are-overhyped/</link>
		<comments>http://tiago.org/cc/2009/11/02/concurrency-and-multicore-cpugpus-are-overhyped/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 14:07:47 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[concurrrent programming]]></category>
		<category><![CDATA[multicore]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=130</guid>
		<description><![CDATA[More than 10 years ago I participated in the development of an University IT system (the front- and backend to maintain grades and that sort of stuff). The system was based on a DB/2 backend (a very nice database system) with the business code stored on a Prolog interpreter (Prolog interpreter which was in-house developed) [...]]]></description>
			<content:encoded><![CDATA[<p>More than 10 years ago I participated in the development of an University IT system (the front- and backend to maintain grades and that sort of stuff). The system was based on a DB/2 backend (a very nice database system) with the business code stored on a Prolog interpreter (Prolog interpreter which was in-house developed) and the web backend being a Java servlet engine (the old JServ, the thingy pre-Tomcat from Apache). Prolog is famed to be slow, and Java (at that point in time) was very slow. Surprise, surprise&#8230; the bottleneck was on the DB/2 server. Eventually, as the system grow (and the database hardware was beefed up) the bottleneck come forward to the business and web tiers, but the problem was sorted by just adding more machines: The contention was on a bunch of parallel independent process, they could be run on separate machines.</p>
<p>The example above illustrates why the concurrency problem posed by multiple core CPUs and GPUs, might not be that much important:</p>
<ol>
<li>Many problems are not CPU bound anyway, and even if they are, the bottleneck might be elsewhere. Another example: I am the proud owner of 3 cheap, slow laptops (one being a netbook). For my use case I really don&#8217;t need faster applications, I wonder how many users really need more than they already have?</li>
<li>Even if more CPU/GPU power is needed, a loosely coupled model (without much interprocess communication and contention issues) might be enough. This is typically the case of many web apps, which can scale by just adding more computers which run independent processes.</li>
</ol>
<p>Concurrency, even with modern abstractions, is hard. It should be avoided if possible and it can be avoided in many applications. If it cannot be avoided, maybe a loosely coupled model is enough&#8230; Guido van Rossum <a href="http://mail.python.org/pipermail/python-3000/2007-May/007414.html">has a nice take on this issue</a>.</p>
<p>This is important as concurrency is being touted as an important criteria to evaluate languages. Modern functional languages (think Scala and Clojure) are being touted as a better option precisely because they are better to do concurrency (both because of functional &#8211; &#8220;no changing state&#8221; &#8211; programming and the availability of libraries implementing nice concurrency paradigms like actors).</p>
<p>When addressing this importance of this issue, I would propose, that people would ask themselves this: &#8220;Am I developing computationally intensive software?&#8221; and &#8220;If I am developing computationally intensive software, can I live with loosely coupled models of computation, preferably processes with no shared memory?&#8221;</p>
<p>This is not to say that there are not some cases where tightly coupled computing is a good idea. It is just that, this complex solution might be an overkill for many problems.</p>
<p>I would just like to add that I am not defending my cause, in fact it is quite the opposite. There is actually some content produced here, in the past, on how to tackle concurrent programming:</p>
<ol>
<li><a href="http://popgen.eu/soft/lositan">LOSITAN</a> &#8211; A multicore-aware Jython-based (Python for the JVM) Web Start application to do selection detection.</li>
<li>An introductory tutorial on concurrent computing targeting computational biologists &#8211; Part <a href="http://tiago.org/ps/2007/07/06/bioinformatics-multi-core-cpus-and-grid-computing-introduction-14/">1</a>, <a href="http://tiago.org/ps/2007/07/17/bioinformatics-multi-core-cpus-and-grid-computing-user-perspective-24/">2</a> and <a href="http://tiago.org/ps/2007/07/31/bioinformatics-multi-core-cpus-and-grid-computing-developer-perspective-34/">3</a></li>
</ol>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/jw7SiP8LY-w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/02/concurrency-and-multicore-cpugpus-are-overhyped/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
