<?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/"
	>

<channel>
	<title>バカな火星人 &#187; ocaml</title>
	<atom:link href="http://martian.org/marty/tag/ocaml/feed/" rel="self" type="application/rss+xml" />
	<link>http://martian.org/marty</link>
	<description>Marty was here!</description>
	<lastBuildDate>Wed, 11 Jan 2012 13:16:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Practical OCaml: larger example</title>
		<link>http://martian.org/marty/2008/03/23/practical-ocaml-larger-example/</link>
		<comments>http://martian.org/marty/2008/03/23/practical-ocaml-larger-example/#comments</comments>
		<pubDate>Sat, 22 Mar 2008 16:00:11 +0000</pubDate>
		<dc:creator>Marty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ocaml]]></category>

		<guid isPermaLink="false">http://martian.org/marty/2008/03/23/practical-ocaml-larger-example/</guid>
		<description><![CDATA[This &#34;Larger Example&#34; I&#8217;m going to write about is in chapter 18, starting on page 243. The chapter is (supposed to be) about the object oriented features of OCaml (they would be the &#34;O&#34; part) but this example is more objectionable than objective. It was intended to be an implementation of the Levenshtein distance algorithm: [...]]]></description>
			<content:encoded><![CDATA[<p>This &quot;Larger Example&quot; I&#8217;m going to write about is in chapter 18, starting on page 243.  The chapter is (supposed to be) about the object oriented features of OCaml (they would be the &quot;O&quot; part) but this example is more objectionable than objective.  It was intended to be an implementation of the Levenshtein distance algorithm: it started with an incomplete description of that algorithm and a figure of a matrix with four mistakes.  Then it got worse.</p>

<p>Smith&#8217;s plan was to create a base class containing the algorithm and then use subclasses to adapt the algorithm to different data types.  But when I looked at the <code>edit_distance</code> base class I noticed that half of the core algorithm was missing!  I found it in the <code>string_edit_distance</code> subclass, and also in the <code>list_edit_distance</code> subclass.  Martin Fowler in his great &quot;<a href="http://martinfowler.com/books.html#refactoring">Refactoring</a>&quot; book says: <q>Number one in the stink parade is duplicated code.  If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them.</q>  Following Fowler&#8217;s good advice, which Smith should have done, I moved the duplicated code to the base class.  There was only a small bit of code that was different for each subclass: the part that compares the elements of the particular data structures (strings and lists in this example) to work out if a changing cost is required.  I created a <code>cost_to_change</code> method to handle that.</p>

<p>Then I tried to simplify the code and make it more readable.</p>

<p>In the <code>update_matrix</code> method I removed three of the <i>let</i> bindings and passed the calculation results directly the <code>trimin</code> function I <a href="http://martian.org/marty/2008/03/22/practical-ocaml-not-again/">mentioned before</a>.</p>

<p>In the <code>gen_matrix</code> method I replaced the pattern matching and nested iteration functions with two separate iteration functions, which hopefully makes it clear that we are only initialising the first row and first column; most of the matrix is left untouched.</p>

<p>In the <code>calc</code> method, which I&#8217;d moved from the subclass to the base class, I again replaced the pattern matching and nested iteration functions, this time with nested <i>for</i> loops. Pattern matching and higher-order iteration functions are very powerful, and they could be used instead of loops and conditional statements in all ocaml code; but ocaml includes <i>for</i> loops and <i>if</i> statements because they are easier to use and understand than the more powerful features.  Choose the right tool for the job.</p>

<p>As a result of all these changes the size of a subclass has been reduced from 18 lines to 6, only 2 of which need to be changed for each data type (the other 4 are structure).  You can have a look at some of <a href="/marty/sample/ocaml-ch18.txt">Smith&#8217;s code</a> and <a href="/marty/sample/ocaml-my18.txt">my modified version</a> to see the differences for yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://martian.org/marty/2008/03/23/practical-ocaml-larger-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Practical OCaml: not again</title>
		<link>http://martian.org/marty/2008/03/22/practical-ocaml-not-again/</link>
		<comments>http://martian.org/marty/2008/03/22/practical-ocaml-not-again/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 16:41:47 +0000</pubDate>
		<dc:creator>Marty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ocaml]]></category>

		<guid isPermaLink="false">http://martian.org/marty/2008/03/22/practical-ocaml-not-again/</guid>
		<description><![CDATA[I was about to comment on the &#34;Larger Example&#34; in chapter 18, starting on page 243; but first I have to get rid of the trimin method it contains: method private trimin x y z = match x,y,z with m,n,o when (m > n) -> if (n < o) then n else o &#124; m,n,o [...]]]></description>
			<content:encoded><![CDATA[<p>I was about to comment on the &quot;Larger Example&quot; in chapter 18, starting on page 243; but first I have to get rid of the <code>trimin</code> method it contains:</p>

<pre>
    method private trimin x y z = match x,y,z with
        m,n,o when (m > n) -> if (n < o) then n else o
      | m,n,o when (m < n) -> if (m < o) then m else o
      | m,n,o -> if (n < o) then n else o
</pre>

<p>Let me now repeat exactly what I said in my <a href="http://martian.org/marty/2008/03/11/practical-ocaml-minimum-review/">previous post</a>: <strong>Huh?!</strong>  This method will return the smallest of its three arguments, although its ugliness made me doubt that for a while.  But even if it were improved, it shouldn't even be part of this class at all.  So I removed that method and replaced it with a simple function:</p>

<p></p></pre><pre>let trimin x y z = min x (min y z);;</pre>

<p>That done, I was then able to continue with the &quot;larger example&quot;.</p>
]]></content:encoded>
			<wfw:commentRss>http://martian.org/marty/2008/03/22/practical-ocaml-not-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Practical OCaml: minimum review</title>
		<link>http://martian.org/marty/2008/03/11/practical-ocaml-minimum-review/</link>
		<comments>http://martian.org/marty/2008/03/11/practical-ocaml-minimum-review/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 19:03:27 +0000</pubDate>
		<dc:creator>Marty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ocaml]]></category>

		<guid isPermaLink="false">http://martian.org/marty/2008/03/11/practical-ocaml-a-mimimum-review/</guid>
		<description><![CDATA[I have been reading Practical OCaml. I didn&#8217;t buy the book; I recommend that you don&#8217;t buy it too. I was given a copy and asked if I could say something good about it, so here goes: &#8220;it could be worse&#8221;. There are so many other negative reviews of this book I don&#8217;t want to [...]]]></description>
			<content:encoded><![CDATA[<p>I have been reading <a rel="nofollow" href="http://www.apress.com/book/view/159059620x">Practical OCaml</a>.  I didn&#8217;t buy the book; I recommend that you <em>don&#8217;t buy it</em> too.  I was given a copy and asked if I could say something good about it, so here goes: &#8220;it could be worse&#8221;.</p>

<p>There are so many other negative reviews of this book I don&#8217;t want to just repeat what has already been said.  Instead I&#8217;m going to criticise some parts of it in more detail in the hope that it may help.  I&#8217;m going to choose targets on a whim, with not much rhyme or reason; that should mirror the structure of the book quite well.</p>

<p>So, to start small, my first complaint is against page 172.  Smith (the book&#8217;s author) writes:</p>

<pre>
let min x y = match x with
    n when x &lt; y -&gt; x
  | _ -&gt; y;;
</pre>

<p><strong>Huh?!</strong></p>

<p>That can&#8217;t possibly be a sane way to define a function that returns the smaller of two arguments.  In addition to the needless complexity we have the unused named value <code>n</code>.  What was wrong with:</p>

<pre>
let min x y = if x &lt; y then x else y;;
</pre>

<p>That should be clear enough.  But I now have another question: why even bother?  The <code>min</code> function is built-in to OCaml; it is part of the <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html">Pervasives module</a> that provides &#8220;<code>&lt;</code>&#8221; and the other comparison operators.</p>
]]></content:encoded>
			<wfw:commentRss>http://martian.org/marty/2008/03/11/practical-ocaml-minimum-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

